UCommon
object.h
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
3 // Copyright (C) 2015 Cherokees of Idaho.
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free software
19 // library without restriction. Specifically, if other files instantiate
20 // templates or use macros or inline functions from this file, or you compile
21 // this file and link it with other files to produce an executable, this
22 // file does not by itself cause the resulting executable to be covered by
23 // the GNU General Public License. This exception does not however
24 // invalidate any other reasons why the executable file might be covered by
25 // the GNU General Public License.
26 //
27 // This exception applies only to the code released under the name GNU
28 // Common C++. If you copy code from other releases into a copy of GNU
29 // Common C++, as the General Public License permits, the exception does
30 // not apply to the code that you add in this way. To avoid misleading
31 // anyone as to the status of such modified files, you must delete
32 // this exception notice from them.
33 //
34 // If you write modifications of your own for GNU Common C++, it is your choice
35 // whether to permit this exception to apply to your modifications.
36 // If you do not wish that, delete this exception notice.
37 //
38 
45 #ifndef COMMONCPP_OBJECT_H_
46 #define COMMONCPP_OBJECT_H_
47 
48 #ifndef COMMONCPP_CONFIG_H_
49 #include <commoncpp/config.h>
50 #endif
51 
52 namespace ost {
53 
54 class MapObject;
55 class MapIndex;
56 
64 class __EXPORT RefObject
65 {
66 private:
67  __DELETE_COPY(RefObject);
68 
69 protected:
70  friend class RefPointer;
71 
72  unsigned refCount;
73 
77  inline RefObject() {
78  refCount = 0;
79  }
80 
85  virtual ~RefObject();
86 
87 public:
96  virtual void *getObject(void) = 0;
97 };
98 
107 class __EXPORT RefPointer
108 {
109 protected:
110  RefObject *ref;
111 
115  void detach(void);
116 
121  virtual void enterLock(void);
122 
127  virtual void leaveLock(void);
128 
129 public:
133  inline RefPointer() {
134  ref = NULL;
135  }
136 
142  RefPointer(RefObject *obj);
143 
149  RefPointer(const RefPointer &ptr);
150 
151  virtual ~RefPointer();
152 
153  RefPointer& operator=(const RefObject &ref);
154 
155  inline void *operator*() const {
156  return getObject();
157  }
158 
159  void *getObject(void) const;
160 
161  operator bool() const;
162 
163  bool operator!() const;
164 };
165 
173 class __EXPORT LinkedSingle
174 {
175 private:
176  __DELETE_COPY(LinkedSingle);
177 
178 protected:
179  LinkedSingle *nextObject;
180 
181  inline LinkedSingle() {
182  nextObject = NULL;
183  }
184 
185  virtual ~LinkedSingle();
186 
187 public:
197  virtual LinkedSingle *getFirst(void);
198 
206  virtual LinkedSingle *getLast(void);
207 
214  inline LinkedSingle *getNext(void) {
215  return nextObject;
216  }
217 
225  virtual void insert(LinkedSingle& obj);
226 
227  LinkedSingle &operator+=(LinkedSingle &obj);
228 };
229 
237 class __EXPORT LinkedDouble
238 {
239 private:
240  __DELETE_COPY(LinkedDouble);
241 
242 protected:
243  LinkedDouble *nextObject, *prevObject;
244 
245  inline LinkedDouble() {
246  nextObject = prevObject = NULL;
247  }
248 
249  virtual ~LinkedDouble();
250 
251  virtual void enterLock(void);
252 
253  virtual void leaveLock(void);
254 
255  virtual LinkedDouble *firstObject();
256 
257  virtual LinkedDouble *lastObject();
258 
259 public:
260 
266  {
270  modeAfter
271  };
272 
280  virtual LinkedDouble *getFirst(void);
281 
289  virtual LinkedDouble *getLast(void);
290 
298  virtual LinkedDouble *getInsert(void);
299 
306  inline LinkedDouble *getNext(void) {
307  return nextObject;
308  }
309 
315  inline LinkedDouble *getPrev(void) {
316  return prevObject;
317  }
318 
327  virtual void insert(LinkedDouble& obj, InsertMode position = modeAtLast);
328 
332  virtual void detach(void);
333 
334  LinkedDouble &operator+=(LinkedDouble &obj);
335 
336  LinkedDouble &operator--();
337 };
338 
349 class __EXPORT MapTable : public Mutex
350 {
351 private:
352  __DELETE_COPY(MapTable);
353 
354 protected:
355  friend class MapObject;
356  friend class MapIndex;
357  unsigned range;
358  unsigned count;
359  MapObject **map;
360 
361  void cleanup(void);
362 
363 public:
369  MapTable(unsigned size);
370 
374  virtual ~MapTable();
375 
384  virtual unsigned getIndex(const char *id);
385 
391  inline unsigned getRange(void) {
392  return range;
393  }
394 
400  inline unsigned getSize(void) {
401  return count;
402  }
403 
411  void *getObject(const char *id);
412 
419  void addObject(MapObject &obj);
426  void *getFirst();
427 
434  void *getLast();
435 
442  void *getEnd() {
443  return NULL;
444  }
445 
455  void *getFree(void);
456 
463  void addFree(MapObject *obj);
464 
471  MapTable &operator+=(MapObject &obj);
472 
480  virtual MapTable &operator-=(MapObject &obj);
481 };
482 
492 class __EXPORT MapIndex
493 {
494  MapObject* thisObject;
495 
496 public :
497 
501  MapIndex() : thisObject(NULL) {}
502 
508  MapIndex(MapObject* theObject) : thisObject(theObject) {}
509 
515  MapIndex(const MapIndex& theIndex) : thisObject(theIndex.thisObject) {}
516 
523  void* operator*() const {
524  return (void*)thisObject;
525  }
526 
532  MapIndex& operator=(MapObject *theObject);
533 
539  MapIndex& operator++(); // prefix
540 
546  MapIndex operator++(int) { // postfix
547  return this->operator++();
548  }
549 
555  bool operator==(const MapIndex& theIndex) const {
556  return thisObject == theIndex.thisObject;
557  }
558 
559  bool operator!=(const MapIndex& theIndex) const {
560  return !(*this == theIndex);
561  }
562 
569  bool operator==(const MapObject* theObject) const {
570  return thisObject == theObject;
571  }
572 
573  bool operator!=(const MapObject* theObject) const {
574  return !(*this == theObject);
575  }
576 };
577 
586 class __EXPORT MapObject
587 {
588 private:
589  __DELETE_COPY(MapObject);
590 
591 protected:
592  friend class MapTable;
593  friend class MapIndex;
594  MapObject *nextObject;
595  const char *idObject;
596  MapTable *table;
597 
598 public:
599 
603  void detach(void);
604 
610  MapObject(const char *id);
611 };
612 
613 } // namespace ost
614 
615 #endif
A map table allows for entities to be mapped (hash index) onto it.
Definition: object.h:349
MapIndex(MapObject *theObject)
Creates a map index pointing to a specific map object.
Definition: object.h:508
Pointer to reference counted objects.
Definition: object.h:107
unsigned getSize(void)
Return the number of object stored in this table.
Definition: object.h:400
bool operator==(const MapIndex &theIndex) const
Comparison operator, between two MapIndex's.
Definition: object.h:555
MapIndex()
Creates an empty map index (pointing to nothing).
Definition: object.h:501
The MapObject is a base class which can be used to make a derived class operate on a MapTable.
Definition: object.h:586
Self managed double linked list object chain.
Definition: object.h:237
insert at last position in list pointed by current object
Definition: object.h:268
LinkedDouble * getPrev(void)
Get prev object in the list.
Definition: object.h:315
void * getEnd()
Get table's end, useful for cycle control; it is returned as void * for easy re-cast.
Definition: object.h:442
insert in list before current object
Definition: object.h:269
bool operator==(const MapObject *theObject) const
Comparison operator, between the MapIndex and a MapObject, useful to avoid casts for sake of clearnes...
Definition: object.h:569
MapIndex operator++(int)
Postfix increment operator, to be used in loops and such.
Definition: object.h:546
A reference countable object.
Definition: object.h:64
RefPointer()
Create an unattached pointer.
Definition: object.h:133
MapIndex(const MapIndex &theIndex)
Creates a copy of a given map index.
Definition: object.h:515
The MapIndex allows linear access into a MapTable, that otherwise could have its elements being retri...
Definition: object.h:492
LinkedSingle * getNext(void)
Get next object, for convenience.
Definition: object.h:214
Self managed single linked list object chain.
Definition: object.h:173
unsigned getRange(void)
Return range of this table.
Definition: object.h:391
RefObject()
The constructor simply initializes the count.
Definition: object.h:77
InsertMode
Requested in overloaded insert() method to indicate how to insert data into list.
Definition: object.h:265
insert at first position in list pointed by current object
Definition: object.h:267
LinkedDouble * getNext(void)
Get next object, for convenience.
Definition: object.h:306