ucommon
object.h
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
44 #ifndef COMMONCPP_OBJECT_H_
45 #define COMMONCPP_OBJECT_H_
46 
47 #ifndef COMMONCPP_CONFIG_H_
48 #include <commoncpp/config.h>
49 #endif
50 
51 NAMESPACE_COMMONCPP
52 
53 class MapObject;
54 class MapIndex;
55 
63 class __EXPORT RefObject
64 {
65 protected:
66  friend class RefPointer;
67 
68  unsigned refCount;
69 
73  inline RefObject()
74  {refCount = 0;};
75 
80  virtual ~RefObject();
81 
82 public:
91  virtual void *getObject(void) = 0;
92 };
93 
102 class __EXPORT RefPointer
103 {
104 protected:
105  RefObject *ref;
106 
110  void detach(void);
111 
116  virtual void enterLock(void);
117 
122  virtual void leaveLock(void);
123 
124 public:
128  inline RefPointer()
129  {ref = NULL;};
130 
136  RefPointer(RefObject *obj);
137 
143  RefPointer(const RefPointer &ptr);
144 
145  virtual ~RefPointer();
146 
147  RefPointer& operator=(const RefObject &ref);
148 
149  inline void *operator*() const
150  {return getObject();};
151 
152  inline void *operator->() const
153  {return getObject();};
154 
155  void *getObject(void) const;
156 
157  bool operator!() const;
158 };
159 
167 class __EXPORT LinkedSingle
168 {
169 protected:
170  LinkedSingle *nextObject;
171 
172  inline LinkedSingle()
173  {nextObject = NULL;};
174 
175  virtual ~LinkedSingle();
176 
177 public:
187  virtual LinkedSingle *getFirst(void);
188 
196  virtual LinkedSingle *getLast(void);
197 
204  inline LinkedSingle *getNext(void)
205  {return nextObject;};
206 
214  virtual void insert(LinkedSingle& obj);
215 
216  LinkedSingle &operator+=(LinkedSingle &obj);
217 };
218 
226 class __EXPORT LinkedDouble
227 {
228 protected:
229  LinkedDouble *nextObject, *prevObject;
230 
231  inline LinkedDouble()
232  {nextObject = prevObject = NULL;};
233 
234  virtual ~LinkedDouble();
235 
236  virtual void enterLock(void);
237 
238  virtual void leaveLock(void);
239 
240  virtual LinkedDouble *firstObject();
241 
242  virtual LinkedDouble *lastObject();
243 
244 public:
245 
251  {
255  modeAfter
256  };
257 
265  virtual LinkedDouble *getFirst(void);
266 
274  virtual LinkedDouble *getLast(void);
275 
283  virtual LinkedDouble *getInsert(void);
284 
291  inline LinkedDouble *getNext(void)
292  {return nextObject;};
293 
299  inline LinkedDouble *getPrev(void)
300  {return prevObject;};
301 
310  virtual void insert(LinkedDouble& obj, InsertMode position = modeAtLast);
311 
315  virtual void detach(void);
316 
317  LinkedDouble &operator+=(LinkedDouble &obj);
318 
319  LinkedDouble &operator--();
320 };
321 
332 class __EXPORT MapTable : public Mutex
333 {
334 protected:
335  friend class MapObject;
336  friend class MapIndex;
337  unsigned range;
338  unsigned count;
339  MapObject **map;
340 
341  void cleanup(void);
342 
343 public:
349  MapTable(unsigned size);
350 
354  virtual ~MapTable();
355 
364  virtual unsigned getIndex(const char *id);
365 
371  inline unsigned getRange(void)
372  {return range;};
373 
379  inline unsigned getSize(void)
380  {return count;};
381 
389  void *getObject(const char *id);
390 
397  void addObject(MapObject &obj);
404  void *getFirst();
405 
412  void *getLast();
413 
420  void *getEnd()
421  { return NULL; };
422 
432  void *getFree(void);
433 
440  void addFree(MapObject *obj);
441 
448  MapTable &operator+=(MapObject &obj);
449 
457  virtual MapTable &operator-=(MapObject &obj);
458 };
459 
469 class __EXPORT MapIndex
470 {
471  MapObject* thisObject;
472 
473 public :
474 
478  MapIndex() : thisObject(NULL)
479  {};
480 
486  MapIndex(MapObject* theObject) : thisObject(theObject)
487  {};
488 
494  MapIndex(const MapIndex& theIndex) : thisObject(theIndex.thisObject)
495  {};
496 
503  void* operator*() const
504  { return (void*)thisObject; }
505 
511  MapIndex& operator=(MapObject *theObject);
512 
518  MapIndex& operator++(); // prefix
519 
525  MapIndex operator++(int) // postfix
526  { return this->operator++(); }
527 
533  bool operator==(const MapIndex& theIndex) const
534  { return thisObject == theIndex.thisObject; };
535 
536  bool operator!=(const MapIndex& theIndex) const
537  { return !(*this == theIndex); };
538 
545  bool operator==(const MapObject* theObject) const
546  { return thisObject == theObject; };
547 
548  bool operator!=(const MapObject* theObject) const
549  { return !(*this == theObject); };
550 };
551 
560 class __EXPORT MapObject
561 {
562 protected:
563  friend class MapTable;
564  friend class MapIndex;
565  MapObject *nextObject;
566  const char *idObject;
567  MapTable *table;
568 
569 public:
570 
574  void detach(void);
575 
581  MapObject(const char *id);
582 };
583 
584 END_NAMESPACE
585 
586 #endif
insert at last position in list pointed by current object
Definition: object.h:253
The MapIndex allows linear access into a MapTable, that otherwise could have its elements being retri...
Definition: object.h:469
MapIndex()
Creates an empty map index (pointing to nothing).
Definition: object.h:478
A map table allows for entities to be mapped (hash index) onto it.
Definition: object.h:332
void * getEnd()
Get table's end, useful for cycle control; it is returned as void * for easy re-cast.
Definition: object.h:420
InsertMode
Requested in overloaded insert() method to indicate how to insert data into list. ...
Definition: object.h:250
unsigned getRange(void)
Return range of this table.
Definition: object.h:371
A reference countable object.
Definition: object.h:63
MapIndex(MapObject *theObject)
Creates a map index pointing to a specific map object.
Definition: object.h:486
unsigned getSize(void)
Return the number of object stored in this table.
Definition: object.h:379
bool operator==(const MapIndex &theIndex) const
Comparison operator, between two MapIndex's.
Definition: object.h:533
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:545
LinkedSingle * getNext(void)
Get next object, for convenience.
Definition: object.h:204
LinkedDouble * getNext(void)
Get next object, for convenience.
Definition: object.h:291
void * operator*() const
Dereference operator: the pointed object it is returned as void * for easy re-cast.
Definition: object.h:503
LinkedDouble * getPrev(void)
Get prev object in the list.
Definition: object.h:299
Self managed single linked list object chain.
Definition: object.h:167
insert in list before current object
Definition: object.h:254
RefObject()
The constructor simply initializes the count.
Definition: object.h:73
The MapObject is a base class which can be used to make a derived class operate on a MapTable...
Definition: object.h:560
RefPointer()
Create an unattached pointer.
Definition: object.h:128
Self managed double linked list object chain.
Definition: object.h:226
MapIndex(const MapIndex &theIndex)
Creates a copy of a given map index.
Definition: object.h:494
insert at first position in list pointed by current object
Definition: object.h:252
MapIndex operator++(int)
Postfix increment operator, to be used in loops and such.
Definition: object.h:525
Pointer to reference counted objects.
Definition: object.h:102