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
53namespace ost {
54#endif
55
58
67{
68protected:
69 friend class RefPointer;
70
71 unsigned refCount;
72
76 inline RefObject()
77 {refCount = 0;};
78
83 virtual ~RefObject();
84
85public:
94 virtual void *getObject(void) = 0;
95};
96
106{
107protected:
109
113 void detach(void);
114
119 virtual void enterLock(void);
120
125 virtual void leaveLock(void);
126
127public:
131 inline RefPointer()
132 {ref = NULL;};
133
140
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{
172protected:
174
176 {nextObject = NULL;};
177
178 virtual ~LinkedSingle();
179
180public:
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{
231protected:
233
235 {nextObject = prevObject = NULL;};
236
237 virtual ~LinkedDouble();
238
239 virtual void enterLock(void);
240
241 virtual void leaveLock(void);
242
244
246
247public:
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
335class __EXPORT MapTable : public Mutex
336{
337protected:
338 friend class MapObject;
339 friend class MapIndex;
340 unsigned range;
341 unsigned count;
343
344 void cleanup(void);
345
346public:
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
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
461};
462
473{
474 MapObject* thisObject;
475
476public :
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{
565protected:
566 friend class MapTable;
567 friend class MapIndex;
569 const char *idObject;
571
572public:
573
577 void detach(void);
578
584 MapObject(const char *id);
585};
586
587#ifdef CCXX_NAMESPACES
588}
589#endif
590
591#endif
592
Self managed double linked list object chain.
Definition object.h:230
virtual ~LinkedDouble()
virtual LinkedDouble * getInsert(void)
Virtual to get the insert point to use when adding new members.
LinkedDouble & operator--()
virtual LinkedDouble * firstObject()
InsertMode
Requested in overloaded insert() method to indicate how to insert data into list.
Definition object.h:254
@ modeAtLast
insert at last position in list pointed by current object
Definition object.h:256
@ modeBefore
insert in list before current object
Definition object.h:257
@ modeAtFirst
insert at first position in list pointed by current object
Definition object.h:255
LinkedDouble()
Definition object.h:234
virtual LinkedDouble * getFirst(void)
Get first linked object in list.
LinkedDouble * nextObject
Definition object.h:232
virtual void leaveLock(void)
LinkedDouble * getNext(void)
Get next object, for convenience.
Definition object.h:294
virtual void insert(LinkedDouble &obj, InsertMode position=modeAtLast)
Insert object into chain at given position, as indicated by InsertMode; If no position is given,...
virtual LinkedDouble * getLast(void)
Gets the last object in the list.
LinkedDouble & operator+=(LinkedDouble &obj)
virtual void detach(void)
Remove object from chain.
virtual void enterLock(void)
virtual LinkedDouble * lastObject()
LinkedDouble * getPrev(void)
Get prev object in the list.
Definition object.h:302
Self managed single linked list object chain.
Definition object.h:171
virtual void insert(LinkedSingle &obj)
Insert object into chain.
virtual LinkedSingle * getFirst(void)
Get first linked object in list.
virtual ~LinkedSingle()
LinkedSingle()
Definition object.h:175
LinkedSingle * getNext(void)
Get next object, for convenience.
Definition object.h:207
LinkedSingle * nextObject
Definition object.h:173
LinkedSingle & operator+=(LinkedSingle &obj)
virtual LinkedSingle * getLast(void)
Gets the last object in the list.
The MapIndex allows linear access into a MapTable, that otherwise could have its elements being retri...
Definition object.h:473
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
MapIndex(const MapIndex &theIndex)
Creates a copy of a given map index.
Definition object.h:497
bool operator!=(const MapObject *theObject) const
Definition object.h:551
MapIndex & operator++()
Prefix increment operator, to be used in loops and such.
bool operator!=(const MapIndex &theIndex) const
Definition object.h:539
MapIndex()
Creates an empty map index (pointing to nothing).
Definition object.h:481
bool operator==(const MapIndex &theIndex) const
Comparison operator, between two MapIndex's.
Definition object.h:536
MapIndex & operator=(MapObject *theObject)
Assignment operator to avoid implicit cast.
MapIndex(MapObject *theObject)
Creates a map index pointing to a specific map object.
Definition object.h:489
void * operator*() const
Dereference operator: the pointed object it is returned as void * for easy re-cast.
Definition object.h:506
MapIndex operator++(int)
Postfix increment operator, to be used in loops and such.
Definition object.h:528
The MapObject is a base class which can be used to make a derived class operate on a MapTable.
Definition object.h:564
MapObject * nextObject
Definition object.h:568
void detach(void)
Remove the object from it's current table.
MapTable * table
Definition object.h:570
MapObject(const char *id)
Save id, mark as not using any table.
const char * idObject
Definition object.h:569
A map table allows for entities to be mapped (hash index) onto it.
Definition object.h:336
virtual unsigned getIndex(const char *id)
Get index value from id string.
void * getEnd()
Get table's end, useful for cycle control; it is returned as void * for easy re-cast.
Definition object.h:423
void addObject(MapObject &obj)
Map an object to our table.
virtual ~MapTable()
Destroy the table, calls cleanup.
void cleanup(void)
MapTable & operator+=(MapObject &obj)
An operator to map an object to the table.
MapTable(unsigned size)
Create a map table with a specified number of slots.
void * getObject(const char *id)
Lookup an object by id key.
unsigned getSize(void)
Return the number of object stored in this table.
Definition object.h:382
unsigned count
Definition object.h:341
MapObject ** map
Definition object.h:342
unsigned getRange(void)
Return range of this table.
Definition object.h:374
void * getLast()
Get the last element into table, it is returned as void * for easy re-cast.
virtual MapTable & operator-=(MapObject &obj)
This operator is virtual in case it must also add the object to a managed free list.
void * getFirst()
Get the first element into table, it is returned as void * for easy re-cast.
void addFree(MapObject *obj)
Add an object to the managed free list.
void * getFree(void)
Get next object from managed free list.
unsigned range
Definition object.h:340
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
A reference countable object.
Definition object.h:67
unsigned refCount
Definition object.h:71
virtual ~RefObject()
The destructor is called when the reference count returns to zero.
RefObject()
The constructor simply initializes the count.
Definition object.h:76
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...
Pointer to reference counted objects.
Definition object.h:106
bool operator!() const
RefObject * ref
Definition object.h:108
virtual void enterLock(void)
Patch point for mutex in derived class.
void * operator->() const
Definition object.h:155
virtual ~RefPointer()
RefPointer()
Create an unattached pointer.
Definition object.h:131
RefPointer(RefObject *obj)
Create a pointer attached to a reference counted object.
RefPointer(const RefPointer &ptr)
A copy constructor.
void * getObject(void) const
virtual void leaveLock(void)
Patch point for a mutex in derived class.
void detach(void)
Detach current object, for example, when changing pointer.
RefPointer & operator=(const RefObject &ref)
void * operator*() const
Definition object.h:152
#define __EXPORT
Definition config.h:1045
substitute functions which may be missing in target platform libc.
Definition address.h:64