• Main Page
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

ucommon/object.h

Go to the documentation of this file.
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 <class 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 
00458     inline T& operator()()
00459         {return value;};
00460 
00465     inline void operator()(T& data)
00466         {value = data;};
00467 };
00468 
00481 template <class T, class P = auto_pointer>
00482 class pointer : public P
00483 {
00484 public:
00488     inline pointer() : P() {};
00489 
00494     inline pointer(T* object) : P(object) {};
00495 
00500     inline T* operator*() const
00501         {return static_cast<T*>(P::object);};
00502 
00507     inline T& operator()() const
00508         {return *(static_cast<T*>(P::object));};
00509 
00514     inline T* operator->() const
00515         {return static_cast<T*>(P::object);};
00516 
00521     inline T* get(void) const
00522         {return static_cast<T*>(P::object);};
00523 
00528     inline T* operator++()
00529         {P::operator++(); return get();};
00530 
00535     inline void operator--()
00536         {P::operator--(); return get();};
00537 
00542     inline void operator=(T *typed)
00543         {P::operator=((Object *)typed);};
00544 };
00545 
00550 inline void retain(Object *object)
00551     {object->retain();}
00552 
00557 inline void release(Object *object)
00558     {object->release();}
00559 
00564 inline Object *copy(Object *object)
00565     {return object->copy();}
00566 
00572 template<class T>
00573 inline bool is(T& object)
00574     {return object.operator bool();}
00575 
00576 
00583 template<class T>
00584 inline bool isnull(T& object)
00585     {return (bool)(object.operator*() == NULL);}
00586 
00593 template<class T>
00594 inline bool isnullp(T *object)
00595     {return (bool)(object->operator*() == NULL);}
00596 
00602 template<class T>
00603 inline void swap(T& o1, T& o2)
00604     {cpr_memswap(&o1, &o2, sizeof(T));}
00605 
00612 template<class T>
00613 inline T& (max)(T& o1, T& o2)
00614 {
00615     return o1 > o2 ? o1 : o2;
00616 } 
00617 
00624 template<class T>
00625 inline T& (min)(T& o1, T& o2)
00626 {
00627     return o1 < o2 ? o1 : o2;
00628 } 
00629 
00630 END_NAMESPACE
00631 
00632 #endif

Generated on Sun Sep 4 2011 for UCommon by  doxygen 1.7.1