GNU CommonC++
object.h
Go to the documentation of this file.
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 CCXX_OBJECT_H_
46 #define CCXX_OBJECT_H_
47 
48 #ifndef CCXX_MISSING_H_
49 #include <cc++/missing.h>
50 #endif
51 
52 #ifdef CCXX_NAMESPACES
53 namespace ost {
54 #endif
55 
56 class __EXPORT MapObject;
57 class __EXPORT MapIndex;
58 
67 {
68 protected:
69  friend class RefPointer;
70 
71  unsigned refCount;
72 
76  inline RefObject()
77  {refCount = 0;};
78 
83  virtual ~RefObject();
84 
85 public:
94  virtual void *getObject(void) = 0;
95 };
96 
106 {
107 protected:
109 
113  void detach(void);
114 
119  virtual void enterLock(void);
120 
125  virtual void leaveLock(void);
126 
127 public:
131  inline RefPointer()
132  {ref = NULL;};
133 
140 
146  RefPointer(const RefPointer &ptr);
147 
148  virtual ~RefPointer();
149 
151 
152  inline void *operator*() const
153  {return getObject();};
154 
155  inline void *operator->() const
156  {return getObject();};
157 
158  void *getObject(void) const;
159 
160  bool operator!() const;
161 };
162 
171 {
172 protected:
174 
175  inline LinkedSingle()
176  {nextObject = NULL;};
177 
178  virtual ~LinkedSingle();
179 
180 public:
190  virtual LinkedSingle *getFirst(void);
191 
199  virtual LinkedSingle *getLast(void);
200 
207  inline LinkedSingle *getNext(void)
208  {return nextObject;};
209 
217  virtual void insert(LinkedSingle& obj);
218 
220 };
221 
230 {
231 protected:
232  LinkedDouble *nextObject, *prevObject;
233 
234  inline LinkedDouble()
235  {nextObject = prevObject = NULL;};
236 
237  virtual ~LinkedDouble();
238 
239  virtual void enterLock(void);
240 
241  virtual void leaveLock(void);
242 
244 
246 
247 public:
248 
254  {
258  modeAfter
259  };
260 
268  virtual LinkedDouble *getFirst(void);
269 
277  virtual LinkedDouble *getLast(void);
278 
286  virtual LinkedDouble *getInsert(void);
287 
294  inline LinkedDouble *getNext(void)
295  {return nextObject;};
296 
302  inline LinkedDouble *getPrev(void)
303  {return prevObject;};
304 
313  virtual void insert(LinkedDouble& obj, InsertMode position = modeAtLast);
314 
318  virtual void detach(void);
319 
321 
323 };
324 
335 class __EXPORT MapTable : public Mutex
336 {
337 protected:
338  friend class MapObject;
339  friend class MapIndex;
340  unsigned range;
341  unsigned count;
343 
344  void cleanup(void);
345 
346 public:
352  MapTable(unsigned size);
353 
357  virtual ~MapTable();
358 
367  virtual unsigned getIndex(const char *id);
368 
374  inline unsigned getRange(void)
375  {return range;};
376 
382  inline unsigned getSize(void)
383  {return count;};
384 
392  void *getObject(const char *id);
393 
400  void addObject(MapObject &obj);
407  void *getFirst();
408 
415  void *getLast();
416 
423  void *getEnd()
424  { return NULL; };
425 
435  void *getFree(void);
436 
443  void addFree(MapObject *obj);
444 
452 
460  virtual MapTable &operator-=(MapObject &obj);
461 };
462 
473 {
474  MapObject* thisObject;
475 
476 public :
477 
481  MapIndex() : thisObject(NULL)
482  {};
483 
489  MapIndex(MapObject* theObject) : thisObject(theObject)
490  {};
491 
497  MapIndex(const MapIndex& theIndex) : thisObject(theIndex.thisObject)
498  {};
499 
506  void* operator*() const
507  { return (void*)thisObject; }
508 
515 
521  MapIndex& operator++(); // prefix
522 
528  MapIndex operator++(int) // postfix
529  { return this->operator++(); }
530 
536  bool operator==(const MapIndex& theIndex) const
537  { return thisObject == theIndex.thisObject; };
538 
539  bool operator!=(const MapIndex& theIndex) const
540  { return !(*this == theIndex); };
541 
548  bool operator==(const MapObject* theObject) const
549  { return thisObject == theObject; };
550 
551  bool operator!=(const MapObject* theObject) const
552  { return !(*this == theObject); };
553 };
554 
564 {
565 protected:
566  friend class MapTable;
567  friend class MapIndex;
569  const char *idObject;
571 
572 public:
573 
577  void detach(void);
578 
584  MapObject(const char *id);
585 };
586 
587 #ifdef CCXX_NAMESPACES
588 }
589 #endif
590 
591 #endif
592 
ost::MapIndex::MapIndex
MapIndex(const MapIndex &theIndex)
Creates a copy of a given map index.
Definition: object.h:497
ost::MapIndex::operator=
MapIndex & operator=(MapObject *theObject)
Assignment operator to avoid implicit cast.
ost::MapObject::MapObject
MapObject(const char *id)
Save id, mark as not using any table.
ost::LinkedDouble::detach
virtual void detach(void)
Remove object from chain.
ost::MapTable::count
unsigned count
Definition: object.h:341
ost::MapObject::detach
void detach(void)
Remove the object from it's current table.
ost::MapObject::nextObject
MapObject * nextObject
Definition: object.h:568
ost::MapIndex::operator*
void * operator*() const
Dereference operator: the pointed object it is returned as void * for easy re-cast.
Definition: object.h:506
ost::LinkedDouble::getNext
LinkedDouble * getNext(void)
Get next object, for convenience.
Definition: object.h:294
ost::LinkedDouble::getFirst
virtual LinkedDouble * getFirst(void)
Get first linked object in list.
ost::RefPointer::leaveLock
virtual void leaveLock(void)
Patch point for a mutex in derived class.
ost::LinkedSingle::getFirst
virtual LinkedSingle * getFirst(void)
Get first linked object in list.
ost::MapTable::addObject
void addObject(MapObject &obj)
Map an object to our table.
ost::MapTable::range
unsigned range
Definition: object.h:340
ost::RefPointer::detach
void detach(void)
Detach current object, for example, when changing pointer.
ost::LinkedDouble::InsertMode
InsertMode
Requested in overloaded insert() method to indicate how to insert data into list.
Definition: object.h:254
ost::Mutex
The Mutex class is used to protect a section of code so that at any given time only a single thread c...
Definition: thread.h:187
ost::MapTable::operator-=
virtual MapTable & operator-=(MapObject &obj)
This operator is virtual in case it must also add the object to a managed free list.
ost::LinkedDouble::getInsert
virtual LinkedDouble * getInsert(void)
Virtual to get the insert point to use when adding new members.
ost::MapIndex::operator++
MapIndex operator++(int)
Postfix increment operator, to be used in loops and such.
Definition: object.h:528
ost::LinkedSingle::getLast
virtual LinkedSingle * getLast(void)
Gets the last object in the list.
ost::MapObject::idObject
const char * idObject
Definition: object.h:569
ost::RefPointer::getObject
void * getObject(void) const
ost::LinkedSingle::insert
virtual void insert(LinkedSingle &obj)
Insert object into chain.
ost::MapIndex::operator==
bool operator==(const MapIndex &theIndex) const
Comparison operator, between two MapIndex's.
Definition: object.h:536
ost::LinkedDouble::LinkedDouble
LinkedDouble()
Definition: object.h:234
ost::MapIndex::operator++
MapIndex & operator++()
Prefix increment operator, to be used in loops and such.
ost::LinkedSingle::LinkedSingle
LinkedSingle()
Definition: object.h:175
ost::MapIndex::operator!=
bool operator!=(const MapObject *theObject) const
Definition: object.h:551
ost::LinkedSingle::~LinkedSingle
virtual ~LinkedSingle()
ost::MapIndex::MapIndex
MapIndex(MapObject *theObject)
Creates a map index pointing to a specific map object.
Definition: object.h:489
ost::LinkedDouble::prevObject
LinkedDouble * prevObject
Definition: object.h:232
ost::RefPointer
Pointer to reference counted objects.
Definition: object.h:106
ost::RefObject::RefObject
RefObject()
The constructor simply initializes the count.
Definition: object.h:76
ost::LinkedSingle
Self managed single linked list object chain.
Definition: object.h:171
ost::MapTable::addFree
void addFree(MapObject *obj)
Add an object to the managed free list.
ost::MapTable::~MapTable
virtual ~MapTable()
Destroy the table, calls cleanup.
ost::MapObject
The MapObject is a base class which can be used to make a derived class operate on a MapTable.
Definition: object.h:564
ost::MapTable::cleanup
void cleanup(void)
ost::RefPointer::~RefPointer
virtual ~RefPointer()
ost::LinkedDouble::firstObject
virtual LinkedDouble * firstObject()
ost::MapIndex
The MapIndex allows linear access into a MapTable, that otherwise could have its elements being retri...
Definition: object.h:473
ost::RefObject::refCount
unsigned refCount
Definition: object.h:71
__EXPORT
#define __EXPORT
Definition: config.h:979
ost::RefObject::getObject
virtual void * getObject(void)=0
The actual object being managed can be returned by this method as a void and then recast to the actua...
ost::MapTable::operator+=
MapTable & operator+=(MapObject &obj)
An operator to map an object to the table.
ost::MapTable::getEnd
void * getEnd()
Get table's end, useful for cycle control; it is returned as void * for easy re-cast.
Definition: object.h:423
ost::LinkedDouble::insert
virtual void insert(LinkedDouble &obj, InsertMode position=modeAtLast)
Insert object into chain at given position, as indicated by InsertMode; If no position is given,...
ost::MapTable::MapTable
MapTable(unsigned size)
Create a map table with a specified number of slots.
ost::MapTable::getIndex
virtual unsigned getIndex(const char *id)
Get index value from id string.
ost::LinkedDouble::enterLock
virtual void enterLock(void)
ost::RefPointer::enterLock
virtual void enterLock(void)
Patch point for mutex in derived class.
ost::MapTable::getRange
unsigned getRange(void)
Return range of this table.
Definition: object.h:374
ost::MapIndex::operator!=
bool operator!=(const MapIndex &theIndex) const
Definition: object.h:539
ost::LinkedDouble::modeAtFirst
@ modeAtFirst
insert at first position in list pointed by current object
Definition: object.h:255
ost::MapTable::getLast
void * getLast()
Get the last element into table, it is returned as void * for easy re-cast.
ost::LinkedDouble::~LinkedDouble
virtual ~LinkedDouble()
ost::LinkedSingle::operator+=
LinkedSingle & operator+=(LinkedSingle &obj)
ost::LinkedDouble::operator--
LinkedDouble & operator--()
missing.h
substitute functions which may be missing in target platform libc.
ost::MapObject::table
MapTable * table
Definition: object.h:570
ost::MapTable
A map table allows for entities to be mapped (hash index) onto it.
Definition: object.h:336
ost::RefPointer::operator!
bool operator!() const
ost
Definition: address.h:64
ost::RefPointer::operator*
void * operator*() const
Definition: object.h:152
ost::MapTable::map
MapObject ** map
Definition: object.h:342
ost::MapTable::getObject
void * getObject(const char *id)
Lookup an object by id key.
ost::RefPointer::operator->
void * operator->() const
Definition: object.h:155
ost::MapIndex::MapIndex
MapIndex()
Creates an empty map index (pointing to nothing).
Definition: object.h:481
ost::LinkedDouble::operator+=
LinkedDouble & operator+=(LinkedDouble &obj)
ost::MapIndex::operator==
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:548
ost::LinkedDouble::getLast
virtual LinkedDouble * getLast(void)
Gets the last object in the list.
ost::MapTable::getFree
void * getFree(void)
Get next object from managed free list.
ost::RefObject::~RefObject
virtual ~RefObject()
The destructor is called when the reference count returns to zero.
ost::RefPointer::RefPointer
RefPointer(RefObject *obj)
Create a pointer attached to a reference counted object.
ost::LinkedSingle::nextObject
LinkedSingle * nextObject
Definition: object.h:173
ost::LinkedDouble::leaveLock
virtual void leaveLock(void)
ost::MapTable::getSize
unsigned getSize(void)
Return the number of object stored in this table.
Definition: object.h:382
ost::MapTable::getFirst
void * getFirst()
Get the first element into table, it is returned as void * for easy re-cast.
ost::LinkedDouble::modeBefore
@ modeBefore
insert in list before current object
Definition: object.h:257
ost::LinkedDouble::getPrev
LinkedDouble * getPrev(void)
Get prev object in the list.
Definition: object.h:302
ost::RefPointer::operator=
RefPointer & operator=(const RefObject &ref)
ost::RefObject
A reference countable object.
Definition: object.h:67
ost::RefPointer::ref
RefObject * ref
Definition: object.h:108
ost::RefPointer::RefPointer
RefPointer(const RefPointer &ptr)
A copy constructor.
ost::LinkedDouble::lastObject
virtual LinkedDouble * lastObject()
ost::RefPointer::RefPointer
RefPointer()
Create an unattached pointer.
Definition: object.h:131
ost::LinkedSingle::getNext
LinkedSingle * getNext(void)
Get next object, for convenience.
Definition: object.h:207
ost::LinkedDouble
Self managed double linked list object chain.
Definition: object.h:230
ost::LinkedDouble::modeAtLast
@ modeAtLast
insert at last position in list pointed by current object
Definition: object.h:256