ucommon
object.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
29 #ifndef _UCOMMON_OBJECT_H_
30 #define _UCOMMON_OBJECT_H_
31 
32 #ifndef _UCOMMON_CPR_H_
33 #include <ucommon/cpr.h>
34 #endif
35 
36 #ifndef _UCOMMON_GENERICS_H_
37 #include <ucommon/generics.h>
38 #endif
39 
40 #ifndef _UCOMMON_PROTOCOLS_H_
41 #include <ucommon/protocols.h>
42 #endif
43 
44 #include <stdlib.h>
45 
46 NAMESPACE_UCOMMON
47 
55 class __EXPORT CountedObject : public ObjectProtocol
56 {
57 private:
58  volatile unsigned count;
59 
60 protected:
64  CountedObject();
65 
72  CountedObject(const ObjectProtocol &ref);
73 
79  virtual void dealloc(void);
80 
84  inline void reset(void)
85  {count = 0;}
86 
87 public:
93  inline bool is_copied(void)
94  {return count > 1;};
95 
100  inline bool is_retained(void)
101  {return count > 0;};
102 
107  inline unsigned copied(void)
108  {return count;};
109 
113  void retain(void);
114 
119  void release(void);
120 };
121 
132 class __EXPORT auto_object
133 {
134 protected:
135  ObjectProtocol *object;
136 
137  auto_object();
138 
139 public:
144  auto_object(ObjectProtocol *object);
145 
152 
158  ~auto_object();
159 
164  void release(void);
165 
170  bool operator!() const;
171 
176  operator bool() const;
177 
183  bool operator==(ObjectProtocol *object) const;
184 
190  bool operator!=(ObjectProtocol *object) const;
191 
198  void operator=(ObjectProtocol *object);
199 };
200 
212 class __EXPORT SparseObjects
213 {
214 private:
215  ObjectProtocol **vector;
216  unsigned max;
217 
218 protected:
224  virtual ObjectProtocol *create(void) = 0;
225 
229  void purge(void);
230 
231  virtual ObjectProtocol *invalid(void) const;
232 
238  ObjectProtocol *get(unsigned offset);
239 
245  SparseObjects(unsigned size);
246 
250  virtual ~SparseObjects();
251 
252 public:
257  unsigned count(void);
258 };
259 
269 template <class T>
270 class sarray : public SparseObjects
271 {
272 public:
277  inline sarray(unsigned size) : SparseObjects(size) {};
278 
285  inline T *get(unsigned offset)
286  {return static_cast<T*>(SparseObjects::get(offset));}
287 
294  inline T& operator[](unsigned offset)
295  {return get(offset);};
296 
297  inline const T* at(unsigned offset)
298  {return static_cast<const T&>(SparseObjects::get(offset));}
299 
300 private:
301  __LOCAL ObjectProtocol *create(void)
302  {return new T;};
303 };
304 
314 template <typename T, class O = CountedObject>
315 class object_value : public O
316 {
317 protected:
322  inline void set(const T& object)
323  {value = object;};
324 
325 public:
326  T value;
331  inline object_value() : O() {};
332 
337  inline object_value(T& existing) : O()
338  {value = existing;};
339 
344  inline T& operator*()
345  {return value;};
346 
351  inline void operator=(const T& data)
352  {value = data;};
353 
358  inline operator T&()
359  {return value;};
360 
361  inline T& operator()()
362  {return value;};
363 
368  inline void operator()(T& data)
369  {value = data;};
370 };
371 
384 template <class T, class P = auto_object>
385 class object_pointer : public P
386 {
387 public:
391  inline object_pointer() : P() {};
392 
397  inline object_pointer(T* object) : P(object) {};
398 
403  inline T* operator*() const
404  {return static_cast<T*>(P::object);};
405 
410  inline T& operator()() const
411  {return *(static_cast<T*>(P::object));};
412 
417  inline T* operator->() const
418  {return static_cast<T*>(P::object);};
419 
424  inline T* get(void) const
425  {return static_cast<T*>(P::object);};
426 
431  inline T* operator++()
432  {P::operator++(); return get();};
433 
438  inline void operator--()
439  {P::operator--(); return get();};
440 
445  inline void operator=(T *typed)
446  {P::operator=((ObjectProtocol *)typed);};
447 
451  inline operator bool()
452  {return P::object != NULL;};
453 
457  inline bool operator!()
458  {return P::object == NULL;};
459 };
460 
465 inline void retain(ObjectProtocol *object)
466  {object->retain();}
467 
472 inline void release(ObjectProtocol *object)
473  {object->release();}
474 
480  {return object->copy();}
481 
482 END_NAMESPACE
483 
484 #endif