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 #include <stdlib.h> 00037 00038 NAMESPACE_UCOMMON 00039 00047 class __EXPORT Object 00048 { 00049 public: 00053 virtual void retain(void); 00054 00058 virtual void release(void); 00059 00063 virtual ~Object(); 00064 00068 Object *copy(void); 00069 00073 inline void operator++(void) 00074 {retain();}; 00075 00079 inline void operator--(void) 00080 {release();}; 00081 }; 00082 00090 class __EXPORT CountedObject : public Object 00091 { 00092 private: 00093 volatile unsigned count; 00094 00095 protected: 00099 CountedObject(); 00100 00107 CountedObject(const Object &ref); 00108 00114 virtual void dealloc(void); 00115 00116 public: 00122 inline bool isCopied(void) 00123 {return count > 1;}; 00124 00129 inline bool isRetained(void) 00130 {return count > 0;}; 00131 00136 inline unsigned copied(void) 00137 {return count;}; 00138 00142 void retain(void); 00143 00148 void release(void); 00149 }; 00150 00159 class __EXPORT Temporary 00160 { 00161 protected: 00162 friend class auto_delete; 00163 virtual ~Temporary(); 00164 }; 00165 00174 class __EXPORT auto_delete 00175 { 00176 protected: 00177 Temporary *object; 00178 00179 public: 00180 ~auto_delete(); 00181 }; 00182 00193 class __EXPORT auto_pointer 00194 { 00195 protected: 00196 Object *object; 00197 00198 auto_pointer(); 00199 00200 public: 00205 auto_pointer(Object *object); 00206 00212 auto_pointer(const auto_pointer &pointer); 00213 00219 ~auto_pointer(); 00220 00225 void release(void); 00226 00231 bool operator!() const; 00232 00237 operator bool() const; 00238 00244 bool operator==(Object *object) const; 00245 00251 bool operator!=(Object *object) const; 00252 00259 void operator=(Object *object); 00260 }; 00261 00273 class __EXPORT sparse_array 00274 { 00275 private: 00276 Object **vector; 00277 unsigned max; 00278 00279 protected: 00285 virtual Object *create(void) = 0; 00286 00290 void purge(void); 00291 00297 Object *get(unsigned offset); 00298 00304 sparse_array(unsigned size); 00305 00306 public: 00310 virtual ~sparse_array(); 00311 00316 unsigned count(void); 00317 }; 00318 00328 template <class T> 00329 class sarray : public sparse_array 00330 { 00331 public: 00336 inline sarray(unsigned size) : sparse_array(size) {}; 00337 00344 inline T *get(unsigned offset) 00345 {static_cast<T*>(sparse_array::get(offset));}; 00346 00353 inline T& operator[](unsigned offset) 00354 {return get(offset);}; 00355 00356 private: 00357 __LOCAL Object *create(void) 00358 {return new T;}; 00359 }; 00360 00373 template <class T> 00374 class temporary : public auto_delete 00375 { 00376 public: 00380 inline temporary() 00381 {object = new T;}; 00382 00387 inline T& operator*() const 00388 {return *(static_cast<T*>(object));}; 00389 00394 inline T* operator->() const 00395 {return static_cast<T*>(object);}; 00396 }; 00397 00407 template <typename T, class O = CountedObject> 00408 class object_value : public O 00409 { 00410 protected: 00415 inline void set(const T& object) 00416 {value = object;}; 00417 00418 public: 00419 T value; 00424 inline object_value() : O() {}; 00425 00430 inline object_value(T& existing) : O() 00431 {value = existing;}; 00432 00437 inline T& operator*() 00438 {return value;}; 00439 00444 inline void operator=(const T& data) 00445 {value = data;}; 00446 00451 inline operator T&() 00452 {return value;}; 00453 00454 inline T& operator()() 00455 {return value;}; 00456 00461 inline void operator()(T& data) 00462 {value = data;}; 00463 }; 00464 00477 template <class T, class P = auto_pointer> 00478 class pointer : public P 00479 { 00480 public: 00484 inline pointer() : P() {}; 00485 00490 inline pointer(T* object) : P(object) {}; 00491 00496 inline T* operator*() const 00497 {return static_cast<T*>(P::object);}; 00498 00503 inline T& operator()() const 00504 {return *(static_cast<T*>(P::object));}; 00505 00510 inline T* operator->() const 00511 {return static_cast<T*>(P::object);}; 00512 00517 inline T* get(void) const 00518 {return static_cast<T*>(P::object);}; 00519 00524 inline T* operator++() 00525 {P::operator++(); return get();}; 00526 00531 inline void operator--() 00532 {P::operator--(); return get();}; 00533 00538 inline void operator=(T *typed) 00539 {P::operator=((Object *)typed);}; 00540 }; 00541 00546 inline void retain(Object *object) 00547 {object->retain();} 00548 00553 inline void release(Object *object) 00554 {object->release();} 00555 00560 inline Object *copy(Object *object) 00561 {return object->copy();} 00562 00568 template<class T> 00569 inline bool is(T& object) 00570 {return object.operator bool();} 00571 00572 00579 template<class T> 00580 inline bool isnull(T& object) 00581 {return (bool)(object.operator*() == NULL);} 00582 00589 template<class T> 00590 inline bool isnullp(T *object) 00591 {return (bool)(object->operator*() == NULL);} 00592 00598 template<typename T> 00599 inline void swap(T& o1, T& o2) 00600 {cpr_memswap(&o1, &o2, sizeof(T));} 00601 00608 template<typename T> 00609 inline T& (max)(T& o1, T& o2) 00610 { 00611 return o1 > o2 ? o1 : o2; 00612 } 00613 00620 template<typename T> 00621 inline T& (min)(T& o1, T& o2) 00622 { 00623 return o1 < o2 ? o1 : o2; 00624 } 00625 00626 END_NAMESPACE 00627 00628 #endif