UCommon
|
00001 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks. 00002 // Copyright (C) 2015 Cherokees of Idaho. 00003 // 00004 // This file is part of GNU uCommon C++. 00005 // 00006 // GNU uCommon C++ is free software: you can redistribute it and/or modify 00007 // it under the terms of the GNU Lesser General Public License as published 00008 // by the Free Software Foundation, either version 3 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // GNU uCommon C++ is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU Lesser General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU Lesser General Public License 00017 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. 00018 00030 #ifndef _UCOMMON_OBJECT_H_ 00031 #define _UCOMMON_OBJECT_H_ 00032 00033 #ifndef _UCOMMON_CPR_H_ 00034 #include <ucommon/cpr.h> 00035 #endif 00036 00037 #ifndef _UCOMMON_GENERICS_H_ 00038 #include <ucommon/generics.h> 00039 #endif 00040 00041 #ifndef _UCOMMON_PROTOCOLS_H_ 00042 #include <ucommon/protocols.h> 00043 #endif 00044 00045 #include <stdlib.h> 00046 00047 namespace ucommon { 00048 00056 class __EXPORT CountedObject : public ObjectProtocol 00057 { 00058 private: 00059 volatile unsigned count; 00060 00061 protected: 00065 CountedObject(); 00066 00073 CountedObject(const ObjectProtocol &ref); 00074 00080 virtual void dealloc(void); 00081 00085 inline void reset(void) 00086 {count = 0;} 00087 00088 public: 00094 inline bool is_copied(void) const 00095 {return count > 1;} 00096 00101 inline bool is_retained(void) const 00102 {return count > 0;} 00103 00108 inline unsigned copied(void) const 00109 {return count;} 00110 00114 void retain(void); 00115 00120 void release(void); 00121 }; 00122 00133 class __EXPORT auto_object 00134 { 00135 protected: 00136 ObjectProtocol *object; 00137 00138 auto_object(); 00139 00140 public: 00145 auto_object(ObjectProtocol *object); 00146 00152 auto_object(const auto_object &pointer); 00153 00159 ~auto_object(); 00160 00165 void release(void); 00166 00171 bool operator!() const; 00172 00177 operator bool() const; 00178 00184 bool operator==(ObjectProtocol *object) const; 00185 00191 bool operator!=(ObjectProtocol *object) const; 00192 00199 void operator=(ObjectProtocol *object); 00200 }; 00201 00213 class __EXPORT SparseObjects 00214 { 00215 private: 00216 ObjectProtocol **vector; 00217 unsigned max; 00218 00219 protected: 00225 virtual ObjectProtocol *create(void) = 0; 00226 00230 void purge(void); 00231 00232 virtual ObjectProtocol *invalid(void) const; 00233 00239 ObjectProtocol *get(unsigned offset); 00240 00246 SparseObjects(unsigned size); 00247 00251 virtual ~SparseObjects(); 00252 00253 public: 00258 unsigned count(void); 00259 }; 00260 00270 template <class T> 00271 class sarray : public SparseObjects 00272 { 00273 public: 00278 inline sarray(unsigned size) : SparseObjects(size) {} 00279 00286 inline T *get(unsigned offset) 00287 {return static_cast<T*>(SparseObjects::get(offset));} 00288 00295 inline T& operator[](unsigned offset) 00296 {return get(offset);} 00297 00298 inline const T* at(unsigned offset) const 00299 {return static_cast<const T&>(SparseObjects::get(offset));} 00300 00301 private: 00302 __LOCAL ObjectProtocol *create(void) 00303 {return new T;} 00304 }; 00305 00315 template <typename T, class O = CountedObject> 00316 class object_value : public O 00317 { 00318 protected: 00323 inline void set(const T& object) 00324 {value = object;} 00325 00326 public: 00327 T value; 00332 inline object_value() : O(), value() {} 00333 00338 inline object_value(T& existing) : O() 00339 {value = existing;} 00340 00345 inline T& operator*() 00346 {return value;} 00347 00352 inline void operator=(const T& data) 00353 {value = data;} 00354 00359 inline operator T&() 00360 {return value;} 00361 00362 inline T& operator()() 00363 {return value;} 00364 00369 inline void operator()(T& data) 00370 {value = data;} 00371 }; 00372 00385 template <class T, class P = auto_object> 00386 class object_pointer : public P 00387 { 00388 public: 00392 inline object_pointer() : P() {} 00393 00398 inline object_pointer(T* object) : P(object) {} 00399 00404 inline T* operator*() const 00405 {return static_cast<T*>(P::object);} 00406 00411 inline T& operator()() const 00412 {return *(static_cast<T*>(P::object));} 00413 00418 inline T* operator->() const 00419 {return static_cast<T*>(P::object);} 00420 00425 inline T* get(void) const 00426 {return static_cast<T*>(P::object);} 00427 00432 inline T* operator++() 00433 {P::operator++(); return get();} 00434 00439 inline void operator--() 00440 {P::operator--(); return get();} 00441 00446 inline void operator=(T *typed) 00447 {P::operator=((ObjectProtocol *)typed);} 00448 00452 inline operator bool() const 00453 {return P::object != NULL;} 00454 00458 inline bool operator!() const 00459 {return P::object == NULL;} 00460 }; 00461 00466 inline void retain(ObjectProtocol *object) 00467 {object->retain();} 00468 00473 inline void release(ObjectProtocol *object) 00474 {object->release();} 00475 00480 inline ObjectProtocol *copy(ObjectProtocol *object) 00481 {return object->copy();} 00482 00483 } // namespace ucommon 00484 00485 #endif