ucommon
commoncpp/object.h
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
00002 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
00003 //
00004 // This program is free software; you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation; either version 2 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free software
00018 // library without restriction.  Specifically, if other files instantiate
00019 // templates or use macros or inline functions from this file, or you compile
00020 // this file and link it with other files to produce an executable, this
00021 // file does not by itself cause the resulting executable to be covered by
00022 // the GNU General Public License.  This exception does not however
00023 // invalidate any other reasons why the executable file might be covered by
00024 // the GNU General Public License.
00025 //
00026 // This exception applies only to the code released under the name GNU
00027 // Common C++.  If you copy code from other releases into a copy of GNU
00028 // Common C++, as the General Public License permits, the exception does
00029 // not apply to the code that you add in this way.  To avoid misleading
00030 // anyone as to the status of such modified files, you must delete
00031 // this exception notice from them.
00032 //
00033 // If you write modifications of your own for GNU Common C++, it is your choice
00034 // whether to permit this exception to apply to your modifications.
00035 // If you do not wish that, delete this exception notice.
00036 //
00037 
00044 #ifndef COMMONCPP_OBJECT_H_
00045 #define COMMONCPP_OBJECT_H_
00046 
00047 #ifndef COMMONCPP_CONFIG_H_
00048 #include <commoncpp/config.h>
00049 #endif
00050 
00051 namespace ost {
00052 
00053 class MapObject;
00054 class MapIndex;
00055 
00063 class __EXPORT RefObject
00064 {
00065 protected:
00066     friend class RefPointer;
00067 
00068     unsigned refCount;
00069 
00073     inline RefObject()
00074         {refCount = 0;}
00075 
00080     virtual ~RefObject();
00081 
00082 public:
00091     virtual void *getObject(void) = 0;
00092 };
00093 
00102 class __EXPORT RefPointer
00103 {
00104 protected:
00105     RefObject *ref;
00106 
00110     void detach(void);
00111 
00116     virtual void enterLock(void);
00117 
00122     virtual void leaveLock(void);
00123 
00124 public:
00128     inline RefPointer()
00129         {ref = NULL;}
00130 
00136     RefPointer(RefObject *obj);
00137 
00143     RefPointer(const RefPointer &ptr);
00144 
00145     virtual ~RefPointer();
00146 
00147     RefPointer& operator=(const RefObject &ref);
00148 
00149     inline void *operator*() const
00150         {return getObject();}
00151 
00152     inline void *operator->() const
00153         {return getObject();}
00154 
00155     void *getObject(void) const;
00156 
00157     bool operator!() const;
00158 };
00159 
00167 class __EXPORT LinkedSingle
00168 {
00169 protected:
00170     LinkedSingle *nextObject;
00171 
00172     inline LinkedSingle()
00173         {nextObject = NULL;}
00174 
00175     virtual ~LinkedSingle();
00176 
00177 public:
00187     virtual LinkedSingle *getFirst(void);
00188 
00196     virtual LinkedSingle *getLast(void);
00197 
00204     inline LinkedSingle *getNext(void)
00205         {return nextObject;}
00206 
00214     virtual void insert(LinkedSingle& obj);
00215 
00216     LinkedSingle &operator+=(LinkedSingle &obj);
00217 };
00218 
00226 class __EXPORT LinkedDouble
00227 {
00228 protected:
00229     LinkedDouble *nextObject, *prevObject;
00230 
00231     inline LinkedDouble()
00232         {nextObject = prevObject = NULL;}
00233 
00234     virtual ~LinkedDouble();
00235 
00236     virtual void enterLock(void);
00237 
00238     virtual void leaveLock(void);
00239 
00240     virtual LinkedDouble *firstObject();
00241 
00242     virtual LinkedDouble *lastObject();
00243 
00244 public:
00245 
00250   enum InsertMode
00251   {
00252     modeAtFirst,  
00253     modeAtLast,   
00254     modeBefore,   
00255     modeAfter     
00256   };
00257 
00265     virtual LinkedDouble *getFirst(void);
00266 
00274     virtual LinkedDouble *getLast(void);
00275 
00283     virtual LinkedDouble *getInsert(void);
00284 
00291     inline LinkedDouble *getNext(void)
00292         {return nextObject;}
00293 
00299     inline LinkedDouble *getPrev(void)
00300         {return prevObject;}
00301 
00310   virtual void insert(LinkedDouble& obj, InsertMode position = modeAtLast);
00311 
00315     virtual void detach(void);
00316 
00317     LinkedDouble &operator+=(LinkedDouble &obj);
00318 
00319     LinkedDouble &operator--();
00320 };
00321 
00332 class __EXPORT MapTable : public Mutex
00333 {
00334 protected:
00335     friend class MapObject;
00336     friend class MapIndex;
00337     unsigned range;
00338     unsigned count;
00339     MapObject **map;
00340 
00341     void cleanup(void);
00342 
00343 public:
00349     MapTable(unsigned size);
00350 
00354     virtual ~MapTable();
00355 
00364     virtual unsigned getIndex(const char *id);
00365 
00371     inline unsigned getRange(void)
00372         {return range;}
00373 
00379     inline unsigned getSize(void)
00380         {return count;}
00381 
00389     void *getObject(const char *id);
00390 
00397     void addObject(MapObject &obj);
00404     void *getFirst();
00405 
00412     void *getLast();
00413 
00420     void *getEnd()
00421         {return NULL;}
00422 
00432     void *getFree(void);
00433 
00440     void addFree(MapObject *obj);
00441 
00448     MapTable &operator+=(MapObject &obj);
00449 
00457     virtual MapTable &operator-=(MapObject &obj);
00458 };
00459 
00469 class __EXPORT MapIndex
00470 {
00471     MapObject*  thisObject;
00472 
00473 public :
00474 
00478     MapIndex() : thisObject(NULL) {}
00479 
00485     MapIndex(MapObject* theObject) : thisObject(theObject) {}
00486 
00492     MapIndex(const MapIndex& theIndex) : thisObject(theIndex.thisObject) {}
00493 
00500     void* operator*() const
00501         {return (void*)thisObject;}
00502 
00508     MapIndex& operator=(MapObject *theObject);
00509 
00515     MapIndex& operator++();           // prefix
00516 
00522     MapIndex  operator++(int)     // postfix
00523         {return this->operator++();}
00524 
00530     bool operator==(const MapIndex& theIndex) const
00531         {return thisObject == theIndex.thisObject;}
00532 
00533     bool operator!=(const MapIndex& theIndex) const
00534         {return !(*this == theIndex);}
00535 
00542      bool operator==(const MapObject* theObject) const
00543         {return thisObject == theObject;}
00544 
00545     bool operator!=(const MapObject* theObject) const
00546         {return !(*this == theObject);}
00547 };
00548 
00557 class __EXPORT MapObject
00558 {
00559 protected:
00560     friend class MapTable;
00561     friend class MapIndex;
00562     MapObject *nextObject;
00563     const char *idObject;
00564     MapTable *table;
00565 
00566 public:
00567 
00571     void detach(void);
00572 
00578     MapObject(const char *id);
00579 };
00580 
00581 } // namespace ost
00582 
00583 #endif