00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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 END_NAMESPACE
00483
00484 #endif