ucommon
|
00001 // Copyright (C) 2006-2014 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_CPR_H_ 00033 #include <ucommon/cpr.h> 00034 #endif 00035 00036 #ifndef _UCOMMON_GENERICS_H_ 00037 #include <ucommon/generics.h> 00038 #endif 00039 00040 #ifndef _UCOMMON_PROTOCOLS_H_ 00041 #include <ucommon/protocols.h> 00042 #endif 00043 00044 #include <stdlib.h> 00045 00046 namespace ucommon { 00047 00055 class __EXPORT CountedObject : public ObjectProtocol 00056 { 00057 private: 00058 volatile unsigned count; 00059 00060 protected: 00064 CountedObject(); 00065 00072 CountedObject(const ObjectProtocol &ref); 00073 00079 virtual void dealloc(void); 00080 00084 inline void reset(void) 00085 {count = 0;} 00086 00087 public: 00093 inline bool is_copied(void) 00094 {return count > 1;} 00095 00100 inline bool is_retained(void) 00101 {return count > 0;} 00102 00107 inline unsigned copied(void) 00108 {return count;} 00109 00113 void retain(void); 00114 00119 void release(void); 00120 }; 00121 00132 class __EXPORT auto_object 00133 { 00134 protected: 00135 ObjectProtocol *object; 00136 00137 auto_object(); 00138 00139 public: 00144 auto_object(ObjectProtocol *object); 00145 00151 auto_object(const auto_object &pointer); 00152 00158 ~auto_object(); 00159 00164 void release(void); 00165 00170 bool operator!() const; 00171 00176 operator bool() const; 00177 00183 bool operator==(ObjectProtocol *object) const; 00184 00190 bool operator!=(ObjectProtocol *object) const; 00191 00198 void operator=(ObjectProtocol *object); 00199 }; 00200 00212 class __EXPORT SparseObjects 00213 { 00214 private: 00215 ObjectProtocol **vector; 00216 unsigned max; 00217 00218 protected: 00224 virtual ObjectProtocol *create(void) = 0; 00225 00229 void purge(void); 00230 00231 virtual ObjectProtocol *invalid(void) const; 00232 00238 ObjectProtocol *get(unsigned offset); 00239 00245 SparseObjects(unsigned size); 00246 00250 virtual ~SparseObjects(); 00251 00252 public: 00257 unsigned count(void); 00258 }; 00259 00269 template <class T> 00270 class sarray : public SparseObjects 00271 { 00272 public: 00277 inline sarray(unsigned size) : SparseObjects(size) {} 00278 00285 inline T *get(unsigned offset) 00286 {return static_cast<T*>(SparseObjects::get(offset));} 00287 00294 inline T& operator[](unsigned offset) 00295 {return get(offset);} 00296 00297 inline const T* at(unsigned offset) 00298 {return static_cast<const T&>(SparseObjects::get(offset));} 00299 00300 private: 00301 __LOCAL ObjectProtocol *create(void) 00302 {return new T;} 00303 }; 00304 00314 template <typename T, class O = CountedObject> 00315 class object_value : public O 00316 { 00317 protected: 00322 inline void set(const T& object) 00323 {value = object;} 00324 00325 public: 00326 T value; 00331 inline object_value() : O() {} 00332 00337 inline object_value(T& existing) : O() 00338 {value = existing;} 00339 00344 inline T& operator*() 00345 {return value;} 00346 00351 inline void operator=(const T& data) 00352 {value = data;} 00353 00358 inline operator T&() 00359 {return value;} 00360 00361 inline T& operator()() 00362 {return value;} 00363 00368 inline void operator()(T& data) 00369 {value = data;} 00370 }; 00371 00384 template <class T, class P = auto_object> 00385 class object_pointer : public P 00386 { 00387 public: 00391 inline object_pointer() : P() {} 00392 00397 inline object_pointer(T* object) : P(object) {} 00398 00403 inline T* operator*() const 00404 {return static_cast<T*>(P::object);} 00405 00410 inline T& operator()() const 00411 {return *(static_cast<T*>(P::object));} 00412 00417 inline T* operator->() const 00418 {return static_cast<T*>(P::object);} 00419 00424 inline T* get(void) const 00425 {return static_cast<T*>(P::object);} 00426 00431 inline T* operator++() 00432 {P::operator++(); return get();} 00433 00438 inline void operator--() 00439 {P::operator--(); return get();} 00440 00445 inline void operator=(T *typed) 00446 {P::operator=((ObjectProtocol *)typed);} 00447 00451 inline operator bool() 00452 {return P::object != NULL;} 00453 00457 inline bool operator!() 00458 {return P::object == NULL;} 00459 }; 00460 00465 inline void retain(ObjectProtocol *object) 00466 {object->retain();} 00467 00472 inline void release(ObjectProtocol *object) 00473 {object->release();} 00474 00479 inline ObjectProtocol *copy(ObjectProtocol *object) 00480 {return object->copy();} 00481 00482 } // namespace ucommon 00483 00484 #endif