UCommon
object.h
Go to the documentation of this file.
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
52namespace ost {
53
54class MapObject;
55class MapIndex;
56
64class __EXPORT RefObject
65{
66private:
67 __DELETE_COPY(RefObject);
68
69protected:
70 friend class RefPointer;
71
72 unsigned refCount;
73
77 inline RefObject() {
78 refCount = 0;
79 }
80
85 virtual ~RefObject();
86
87public:
96 virtual void *getObject(void) = 0;
97};
98
107class __EXPORT RefPointer
108{
109protected:
110 RefObject *ref;
111
115 void detach(void);
116
121 virtual void enterLock(void);
122
127 virtual void leaveLock(void);
128
129public:
133 inline RefPointer() {
134 ref = NULL;
135 }
136
143
150
151 virtual ~RefPointer();
152
153 RefPointer& operator=(const RefObject &ref);
154
155 inline void *operator*() const {
156 return getObject();
157 }
158
159 void *getObject(void) const;
160
161 operator bool() const;
162
163 bool operator!() const;
164};
165
173class __EXPORT LinkedSingle
174{
175private:
176 __DELETE_COPY(LinkedSingle);
177
178protected:
179 LinkedSingle *nextObject;
180
181 inline LinkedSingle() {
182 nextObject = NULL;
183 }
184
185 virtual ~LinkedSingle();
186
187public:
197 virtual LinkedSingle *getFirst(void);
198
206 virtual LinkedSingle *getLast(void);
207
214 inline LinkedSingle *getNext(void) {
215 return nextObject;
216 }
217
225 virtual void insert(LinkedSingle& obj);
226
227 LinkedSingle &operator+=(LinkedSingle &obj);
228};
229
237class __EXPORT LinkedDouble
238{
239private:
240 __DELETE_COPY(LinkedDouble);
241
242protected:
243 LinkedDouble *nextObject, *prevObject;
244
245 inline LinkedDouble() {
246 nextObject = prevObject = NULL;
247 }
248
249 virtual ~LinkedDouble();
250
251 virtual void enterLock(void);
252
253 virtual void leaveLock(void);
254
255 virtual LinkedDouble *firstObject();
256
257 virtual LinkedDouble *lastObject();
258
259public:
260
272
280 virtual LinkedDouble *getFirst(void);
281
289 virtual LinkedDouble *getLast(void);
290
298 virtual LinkedDouble *getInsert(void);
299
306 inline LinkedDouble *getNext(void) {
307 return nextObject;
308 }
309
315 inline LinkedDouble *getPrev(void) {
316 return prevObject;
317 }
318
327 virtual void insert(LinkedDouble& obj, InsertMode position = modeAtLast);
328
332 virtual void detach(void);
333
334 LinkedDouble &operator+=(LinkedDouble &obj);
335
336 LinkedDouble &operator--();
337};
338
349class __EXPORT MapTable : public Mutex
350{
351private:
352 __DELETE_COPY(MapTable);
353
354protected:
355 friend class MapObject;
356 friend class MapIndex;
357 unsigned range;
358 unsigned count;
359 MapObject **map;
360
361 void cleanup(void);
362
363public:
369 MapTable(unsigned size);
370
374 virtual ~MapTable();
375
384 virtual unsigned getIndex(const char *id);
385
391 inline unsigned getRange(void) {
392 return range;
393 }
394
400 inline unsigned getSize(void) {
401 return count;
402 }
403
411 void *getObject(const char *id);
412
426 void *getFirst();
427
434 void *getLast();
435
442 void *getEnd() {
443 return NULL;
444 }
445
455 void *getFree(void);
456
463 void addFree(MapObject *obj);
464
472
481};
482
492class __EXPORT MapIndex
493{
494 MapObject* thisObject;
495
496public :
497
501 MapIndex() : thisObject(NULL) {}
502
508 MapIndex(MapObject* theObject) : thisObject(theObject) {}
509
515 MapIndex(const MapIndex& theIndex) : thisObject(theIndex.thisObject) {}
516
523 void* operator*() const {
524 return (void*)thisObject;
525 }
526
533
539 MapIndex& operator++(); // prefix
540
546 MapIndex operator++(int) { // postfix
547 return this->operator++();
548 }
549
555 bool operator==(const MapIndex& theIndex) const {
556 return thisObject == theIndex.thisObject;
557 }
558
559 bool operator!=(const MapIndex& theIndex) const {
560 return !(*this == theIndex);
561 }
562
569 bool operator==(const MapObject* theObject) const {
570 return thisObject == theObject;
571 }
572
573 bool operator!=(const MapObject* theObject) const {
574 return !(*this == theObject);
575 }
576};
577
586class __EXPORT MapObject
587{
588private:
589 __DELETE_COPY(MapObject);
590
591protected:
592 friend class MapTable;
593 friend class MapIndex;
594 MapObject *nextObject;
595 const char *idObject;
596 MapTable *table;
597
598public:
599
603 void detach(void);
604
610 MapObject(const char *id);
611};
612
613} // namespace ost
614
615#endif
A reference countable object.
Definition object.h:65
virtual ~RefObject()
The destructor is called when the reference count returns to zero.
RefObject()
The constructor simply initializes the count.
Definition object.h:77
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:108
virtual void enterLock(void)
Patch point for mutex in derived class.
RefPointer()
Create an unattached pointer.
Definition object.h:133
RefPointer(RefObject *obj)
Create a pointer attached to a reference counted object.
RefPointer(const RefPointer &ptr)
A copy constructor.
virtual void leaveLock(void)
Patch point for a mutex in derived class.
void detach(void)
Detach current object, for example, when changing pointer.
Self managed single linked list object chain.
Definition object.h:174
virtual void insert(LinkedSingle &obj)
Insert object into chain.
virtual LinkedSingle * getFirst(void)
Get first linked object in list.
LinkedSingle * getNext(void)
Get next object, for convenience.
Definition object.h:214
virtual LinkedSingle * getLast(void)
Gets the last object in the list.
Self managed double linked list object chain.
Definition object.h:238
virtual LinkedDouble * getInsert(void)
Virtual to get the insert point to use when adding new members.
InsertMode
Requested in overloaded insert() method to indicate how to insert data into list.
Definition object.h:266
@ modeAtLast
insert at last position in list pointed by current object
Definition object.h:268
@ modeBefore
insert in list before current object
Definition object.h:269
@ modeAtFirst
insert at first position in list pointed by current object
Definition object.h:267
virtual LinkedDouble * getFirst(void)
Get first linked object in list.
LinkedDouble * getNext(void)
Get next object, for convenience.
Definition object.h:306
virtual void insert(LinkedDouble &obj, InsertMode position=modeAtLast)
Insert object into chain at given pos, as indicated by InsertMode; If no pos is given,...
virtual LinkedDouble * getLast(void)
Gets the last object in the list.
virtual void detach(void)
Remove object from chain.
LinkedDouble * getPrev(void)
Get prev object in the list.
Definition object.h:315
A map table allows for entities to be mapped (hash index) onto it.
Definition object.h:350
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:442
void addObject(MapObject &obj)
Map an object to our table.
virtual ~MapTable()
Destroy the table, calls cleanup.
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:400
unsigned getRange(void)
Return range of this table.
Definition object.h:391
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.
The MapIndex allows linear access into a MapTable, that otherwise could have its elements being retri...
Definition object.h:493
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:569
MapIndex(const MapIndex &theIndex)
Creates a copy of a given map index.
Definition object.h:515
MapIndex & operator++()
Prefix increment operator, to be used in loops and such.
MapIndex()
Creates an empty map index (pointing to nothing).
Definition object.h:501
bool operator==(const MapIndex &theIndex) const
Comparison operator, between two MapIndex's.
Definition object.h:555
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:508
void * operator*() const
Dereference operator: the pointed object it is returned as void * for easy re-cast.
Definition object.h:523
MapIndex operator++(int)
Postfix increment operator, to be used in loops and such.
Definition object.h:546
The MapObject is a base class which can be used to make a derived class operate on a MapTable.
Definition object.h:587
void detach(void)
Remove the object from it's current table.
MapObject(const char *id)
Save id, mark as not using any table.