ucommon
object.h
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2010 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 General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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_COMMONCPP
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 
81  virtual ~RefObject();
82 
83 public:
92  virtual void *getObject(void) = 0;
93 };
94 
103 class __EXPORT RefPointer
104 {
105 protected:
106  RefObject *ref;
107 
111  void detach(void);
112 
117  virtual void enterLock(void);
118 
123  virtual void leaveLock(void);
124 
125 public:
129  inline RefPointer()
130  {ref = NULL;};
131 
137  RefPointer(RefObject *obj);
138 
144  RefPointer(const RefPointer &ptr);
145 
146  virtual ~RefPointer();
147 
148  RefPointer& operator=(const RefObject &ref);
149 
150  inline void *operator*() const
151  {return getObject();};
152 
153  inline void *operator->() const
154  {return getObject();};
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  virtual ~LinkedSingle();
177 
178 public:
188  virtual LinkedSingle *getFirst(void);
189 
197  virtual LinkedSingle *getLast(void);
198 
205  inline LinkedSingle *getNext(void)
206  {return nextObject;};
207 
215  virtual void insert(LinkedSingle& obj);
216 
217  LinkedSingle &operator+=(LinkedSingle &obj);
218 };
219 
227 class __EXPORT LinkedDouble
228 {
229 protected:
230  LinkedDouble *nextObject, *prevObject;
231 
232  inline LinkedDouble()
233  {nextObject = prevObject = NULL;};
234 
235  virtual ~LinkedDouble();
236 
237  virtual void enterLock(void);
238 
239  virtual void leaveLock(void);
240 
241  virtual LinkedDouble *firstObject();
242 
243  virtual LinkedDouble *lastObject();
244 
245 public:
246 
252  {
256  modeAfter
257  };
258 
266  virtual LinkedDouble *getFirst(void);
267 
275  virtual LinkedDouble *getLast(void);
276 
284  virtual LinkedDouble *getInsert(void);
285 
292  inline LinkedDouble *getNext(void)
293  {return nextObject;};
294 
300  inline LinkedDouble *getPrev(void)
301  {return prevObject;};
302 
311  virtual void insert(LinkedDouble& obj, InsertMode position = modeAtLast);
312 
316  virtual void detach(void);
317 
318  LinkedDouble &operator+=(LinkedDouble &obj);
319 
320  LinkedDouble &operator--();
321 };
322 
333 class __EXPORT MapTable : public Mutex
334 {
335 protected:
336  friend class MapObject;
337  friend class MapIndex;
338  unsigned range;
339  unsigned count;
340  MapObject **map;
341 
342  void cleanup(void);
343 
344 public:
350  MapTable(unsigned size);
351 
355  virtual ~MapTable();
356 
365  virtual unsigned getIndex(const char *id);
366 
372  inline unsigned getRange(void)
373  {return range;};
374 
380  inline unsigned getSize(void)
381  {return count;};
382 
390  void *getObject(const char *id);
391 
398  void addObject(MapObject &obj);
405  void *getFirst();
406 
413  void *getLast();
414 
421  void *getEnd()
422  { return NULL; };
423 
433  void *getFree(void);
434 
441  void addFree(MapObject *obj);
442 
449  MapTable &operator+=(MapObject &obj);
450 
458  virtual MapTable &operator-=(MapObject &obj);
459 };
460 
470 class __EXPORT MapIndex
471 {
472  MapObject* thisObject;
473 
474 public :
475 
479  MapIndex() : thisObject(NULL)
480  {};
481 
487  MapIndex(MapObject* theObject) : thisObject(theObject)
488  {};
489 
495  MapIndex(const MapIndex& theIndex) : thisObject(theIndex.thisObject)
496  {};
497 
504  void* operator*() const
505  { return (void*)thisObject; }
506 
512  MapIndex& operator=(MapObject *theObject);
513 
519  MapIndex& operator++(); // prefix
520 
526  MapIndex operator++(int) // postfix
527  { return this->operator++(); }
528 
534  bool operator==(const MapIndex& theIndex) const
535  { return thisObject == theIndex.thisObject; };
536 
537  bool operator!=(const MapIndex& theIndex) const
538  { return !(*this == theIndex); };
539 
546  bool operator==(const MapObject* theObject) const
547  { return thisObject == theObject; };
548 
549  bool operator!=(const MapObject* theObject) const
550  { return !(*this == theObject); };
551 };
552 
561 class __EXPORT MapObject
562 {
563 protected:
564  friend class MapTable;
565  friend class MapIndex;
566  MapObject *nextObject;
567  const char *idObject;
568  MapTable *table;
569 
570 public:
571 
575  void detach(void);
576 
582  MapObject(const char *id);
583 };
584 
585 END_NAMESPACE
586 
587 #endif
insert at last position in list pointed by current object
Definition: object.h:254
The MapIndex allows linear access into a MapTable, that otherwise could have its elements being retri...
Definition: object.h:470
MapIndex()
Creates an empty map index (pointing to nothing).
Definition: object.h:479
A map table allows for entities to be mapped (hash index) onto it.
Definition: object.h:333
void * getEnd()
Get table&#39;s end, useful for cycle control; it is returned as void * for easy re-cast.
Definition: object.h:421
InsertMode
Requested in overloaded insert() method to indicate how to insert data into list. ...
Definition: object.h:251
unsigned getRange(void)
Return range of this table.
Definition: object.h:372
A reference countable object.
Definition: object.h:64
MapIndex(MapObject *theObject)
Creates a map index pointing to a specific map object.
Definition: object.h:487
unsigned getSize(void)
Return the number of object stored in this table.
Definition: object.h:380
bool operator==(const MapIndex &theIndex) const
Comparison operator, between two MapIndex&#39;s.
Definition: object.h:534
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:546
LinkedSingle * getNext(void)
Get next object, for convenience.
Definition: object.h:205
LinkedDouble * getNext(void)
Get next object, for convenience.
Definition: object.h:292
void * operator*() const
Dereference operator: the pointed object it is returned as void * for easy re-cast.
Definition: object.h:504
LinkedDouble * getPrev(void)
Get prev object in the list.
Definition: object.h:300
Self managed single linked list object chain.
Definition: object.h:168
insert in list before current object
Definition: object.h:255
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:561
RefPointer()
Create an unattached pointer.
Definition: object.h:129
Self managed double linked list object chain.
Definition: object.h:227
MapIndex(const MapIndex &theIndex)
Creates a copy of a given map index.
Definition: object.h:495
insert at first position in list pointed by current object
Definition: object.h:253
MapIndex operator++(int)
Postfix increment operator, to be used in loops and such.
Definition: object.h:526
Pointer to reference counted objects.
Definition: object.h:103