UCommon
|
00001 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. 00002 // 00003 // This file is part of GNU uCommon C++. 00004 // 00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published 00007 // by the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // GNU uCommon C++ 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 Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. 00017 00029 #ifndef _UCOMMON_OBJECT_H_ 00030 #define _UCOMMON_OBJECT_H_ 00031 00032 #ifndef _UCOMMON_CONFIG_H_ 00033 #include <ucommon/platform.h> 00034 #endif 00035 00036 #ifndef _UCOMMON_GENERICS_H_ 00037 #include <ucommon/generics.h> 00038 #endif 00039 00040 #include <stdlib.h> 00041 00042 NAMESPACE_UCOMMON 00043 00051 class __EXPORT ObjectProtocol 00052 { 00053 public: 00057 virtual void retain(void) = 0; 00058 00062 virtual void release(void) = 0; 00063 00067 virtual ~ObjectProtocol(); 00068 00072 ObjectProtocol *copy(void); 00073 00077 inline void operator++(void) 00078 {retain();}; 00079 00083 inline void operator--(void) 00084 {release();}; 00085 }; 00086 00094 class __EXPORT CountedObject : public ObjectProtocol 00095 { 00096 private: 00097 volatile unsigned count; 00098 00099 protected: 00103 CountedObject(); 00104 00111 CountedObject(const ObjectProtocol &ref); 00112 00118 virtual void dealloc(void); 00119 00120 public: 00126 inline bool isCopied(void) 00127 {return count > 1;}; 00128 00133 inline bool isRetained(void) 00134 {return count > 0;}; 00135 00140 inline unsigned copied(void) 00141 {return count;}; 00142 00146 void retain(void); 00147 00152 void release(void); 00153 }; 00154 00165 class __EXPORT auto_object 00166 { 00167 protected: 00168 ObjectProtocol *object; 00169 00170 auto_object(); 00171 00172 public: 00177 auto_object(ObjectProtocol *object); 00178 00184 auto_object(const auto_object &pointer); 00185 00191 ~auto_object(); 00192 00197 void release(void); 00198 00203 bool operator!() const; 00204 00209 operator bool() const; 00210 00216 bool operator==(ObjectProtocol *object) const; 00217 00223 bool operator!=(ObjectProtocol *object) const; 00224 00231 void operator=(ObjectProtocol *object); 00232 }; 00233 00245 class __EXPORT sparse_array 00246 { 00247 private: 00248 ObjectProtocol **vector; 00249 unsigned max; 00250 00251 protected: 00257 virtual ObjectProtocol *create(void) = 0; 00258 00262 void purge(void); 00263 00269 ObjectProtocol *get(unsigned offset); 00270 00276 sparse_array(unsigned size); 00277 00278 public: 00282 virtual ~sparse_array(); 00283 00288 unsigned count(void); 00289 }; 00290 00300 template <class T> 00301 class sarray : public sparse_array 00302 { 00303 public: 00308 inline sarray(unsigned size) : sparse_array(size) {}; 00309 00316 inline T *get(unsigned offset) 00317 {static_cast<T*>(sparse_array::get(offset));}; 00318 00325 inline T& operator[](unsigned offset) 00326 {return get(offset);}; 00327 00328 private: 00329 __LOCAL ObjectProtocol *create(void) 00330 {return new T;}; 00331 }; 00332 00342 template <typename T, class O = CountedObject> 00343 class object_value : public O 00344 { 00345 protected: 00350 inline void set(const T& object) 00351 {value = object;}; 00352 00353 public: 00354 T value; 00359 inline object_value() : O() {}; 00360 00365 inline object_value(T& existing) : O() 00366 {value = existing;}; 00367 00372 inline T& operator*() 00373 {return value;}; 00374 00379 inline void operator=(const T& data) 00380 {value = data;}; 00381 00386 inline operator T&() 00387 {return value;}; 00388 00389 inline T& operator()() 00390 {return value;}; 00391 00396 inline void operator()(T& data) 00397 {value = data;}; 00398 }; 00399 00412 template <class T, class P = auto_object> 00413 class object_pointer : public P 00414 { 00415 public: 00419 inline object_pointer() : P() {}; 00420 00425 inline object_pointer(T* object) : P(object) {}; 00426 00431 inline T* operator*() const 00432 {return static_cast<T*>(P::object);}; 00433 00438 inline T& operator()() const 00439 {return *(static_cast<T*>(P::object));}; 00440 00445 inline T* operator->() const 00446 {return static_cast<T*>(P::object);}; 00447 00452 inline T* get(void) const 00453 {return static_cast<T*>(P::object);}; 00454 00459 inline T* operator++() 00460 {P::operator++(); return get();}; 00461 00466 inline void operator--() 00467 {P::operator--(); return get();}; 00468 00473 inline void operator=(T *typed) 00474 {P::operator=((ObjectProtocol *)typed);}; 00475 00479 inline operator bool() 00480 {return P::object != NULL;}; 00481 00485 inline bool operator!() 00486 {return P::object == NULL;}; 00487 }; 00488 00493 inline void retain(ObjectProtocol *object) 00494 {object->retain();} 00495 00500 inline void release(ObjectProtocol *object) 00501 {object->release();} 00502 00507 inline ObjectProtocol *copy(ObjectProtocol *object) 00508 {return object->copy();} 00509 00510 END_NAMESPACE 00511 00512 #endif