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