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 protected:
67  friend class RefPointer;
68 
69  unsigned refCount;
70 
74  inline RefObject() {
75  refCount = 0;
76  }
77 
82  virtual ~RefObject();
83 
84 public:
93  virtual void *getObject(void) = 0;
94 };
95 
104 class __EXPORT RefPointer
105 {
106 protected:
107  RefObject *ref;
108 
112  void detach(void);
113 
118  virtual void enterLock(void);
119 
124  virtual void leaveLock(void);
125 
126 public:
130  inline RefPointer() {
131  ref = NULL;
132  }
133 
139  RefPointer(RefObject *obj);
140 
146  RefPointer(const RefPointer &ptr);
147 
148  virtual ~RefPointer();
149 
150  RefPointer& operator=(const RefObject &ref);
151 
152  inline void *operator*() const {
153  return getObject();
154  }
155 
156  void *getObject(void) const;
157 
158  bool operator!() const;
159 };
160 
168 class __EXPORT LinkedSingle
169 {
170 protected:
171  LinkedSingle *nextObject;
172 
173  inline LinkedSingle() {
174  nextObject = NULL;
175  }
176 
177  virtual ~LinkedSingle();
178 
179 public:
189  virtual LinkedSingle *getFirst(void);
190 
198  virtual LinkedSingle *getLast(void);
199 
206  inline LinkedSingle *getNext(void) {
207  return nextObject;
208  }
209 
217  virtual void insert(LinkedSingle& obj);
218 
219  LinkedSingle &operator+=(LinkedSingle &obj);
220 };
221 
229 class __EXPORT LinkedDouble
230 {
231 protected:
232  LinkedDouble *nextObject, *prevObject;
233 
234  inline LinkedDouble() {
235  nextObject = prevObject = NULL;
236  }
237 
238  virtual ~LinkedDouble();
239 
240  virtual void enterLock(void);
241 
242  virtual void leaveLock(void);
243 
244  virtual LinkedDouble *firstObject();
245 
246  virtual LinkedDouble *lastObject();
247 
248 public:
249 
255  {
259  modeAfter
260  };
261 
269  virtual LinkedDouble *getFirst(void);
270 
278  virtual LinkedDouble *getLast(void);
279 
287  virtual LinkedDouble *getInsert(void);
288 
295  inline LinkedDouble *getNext(void) {
296  return nextObject;
297  }
298 
304  inline LinkedDouble *getPrev(void) {
305  return prevObject;
306  }
307 
316  virtual void insert(LinkedDouble& obj, InsertMode position = modeAtLast);
317 
321  virtual void detach(void);
322 
323  LinkedDouble &operator+=(LinkedDouble &obj);
324 
325  LinkedDouble &operator--();
326 };
327 
338 class __EXPORT MapTable : public Mutex
339 {
340 protected:
341  friend class MapObject;
342  friend class MapIndex;
343  unsigned range;
344  unsigned count;
345  MapObject **map;
346 
347  void cleanup(void);
348 
349 public:
355  MapTable(unsigned size);
356 
360  virtual ~MapTable();
361 
370  virtual unsigned getIndex(const char *id);
371 
377  inline unsigned getRange(void) {
378  return range;
379  }
380 
386  inline unsigned getSize(void) {
387  return count;
388  }
389 
397  void *getObject(const char *id);
398 
405  void addObject(MapObject &obj);
412  void *getFirst();
413 
420  void *getLast();
421 
428  void *getEnd() {
429  return NULL;
430  }
431 
441  void *getFree(void);
442 
449  void addFree(MapObject *obj);
450 
457  MapTable &operator+=(MapObject &obj);
458 
466  virtual MapTable &operator-=(MapObject &obj);
467 };
468 
478 class __EXPORT MapIndex
479 {
480  MapObject* thisObject;
481 
482 public :
483 
487  MapIndex() : thisObject(NULL) {}
488 
494  MapIndex(MapObject* theObject) : thisObject(theObject) {}
495 
501  MapIndex(const MapIndex& theIndex) : thisObject(theIndex.thisObject) {}
502 
509  void* operator*() const {
510  return (void*)thisObject;
511  }
512 
518  MapIndex& operator=(MapObject *theObject);
519 
525  MapIndex& operator++(); // prefix
526 
532  MapIndex operator++(int) { // postfix
533  return this->operator++();
534  }
535 
541  bool operator==(const MapIndex& theIndex) const {
542  return thisObject == theIndex.thisObject;
543  }
544 
545  bool operator!=(const MapIndex& theIndex) const {
546  return !(*this == theIndex);
547  }
548 
555  bool operator==(const MapObject* theObject) const {
556  return thisObject == theObject;
557  }
558 
559  bool operator!=(const MapObject* theObject) const {
560  return !(*this == theObject);
561  }
562 };
563 
572 class __EXPORT MapObject
573 {
574 protected:
575  friend class MapTable;
576  friend class MapIndex;
577  MapObject *nextObject;
578  const char *idObject;
579  MapTable *table;
580 
581 public:
582 
586  void detach(void);
587 
593  MapObject(const char *id);
594 };
595 
596 } // namespace ost
597 
598 #endif
insert at last position in list pointed by current object
Definition: object.h:257
The MapIndex allows linear access into a MapTable, that otherwise could have its elements being retri...
Definition: object.h:478
MapIndex()
Creates an empty map index (pointing to nothing).
Definition: object.h:487
A map table allows for entities to be mapped (hash index) onto it.
Definition: object.h:338
void * getEnd()
Get table's end, useful for cycle control; it is returned as void * for easy re-cast.
Definition: object.h:428
InsertMode
Requested in overloaded insert() method to indicate how to insert data into list. ...
Definition: object.h:254
Definition: address.h:59
unsigned getRange(void)
Return range of this table.
Definition: object.h:377
A reference countable object.
Definition: object.h:64
MapIndex(MapObject *theObject)
Creates a map index pointing to a specific map object.
Definition: object.h:494
unsigned getSize(void)
Return the number of object stored in this table.
Definition: object.h:386
bool operator==(const MapIndex &theIndex) const
Comparison operator, between two MapIndex's.
Definition: object.h:541
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:555
LinkedSingle * getNext(void)
Get next object, for convenience.
Definition: object.h:206
LinkedDouble * getNext(void)
Get next object, for convenience.
Definition: object.h:295
void * operator*() const
Dereference operator: the pointed object it is returned as void * for easy re-cast.
Definition: object.h:509
LinkedDouble * getPrev(void)
Get prev object in the list.
Definition: object.h:304
Self managed single linked list object chain.
Definition: object.h:168
insert in list before current object
Definition: object.h:258
RefObject()
The constructor simply initializes the count.
Definition: object.h:74
The MapObject is a base class which can be used to make a derived class operate on a MapTable...
Definition: object.h:572
RefPointer()
Create an unattached pointer.
Definition: object.h:130
Self managed double linked list object chain.
Definition: object.h:229
MapIndex(const MapIndex &theIndex)
Creates a copy of a given map index.
Definition: object.h:501
insert at first position in list pointed by current object
Definition: object.h:256
MapIndex operator++(int)
Postfix increment operator, to be used in loops and such.
Definition: object.h:532
Pointer to reference counted objects.
Definition: object.h:104