ucommon
object.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 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) const
94  {return count > 1;}
95 
100  inline bool is_retained(void) const
101  {return count > 0;}
102 
107  inline unsigned copied(void) const
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) const
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(), value() {}
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() const
452  {return P::object != NULL;}
453 
457  inline bool operator!() const
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 } // namespace ucommon
483 
484 #endif
void reset(void)
Force reset of count.
Definition: object.h:84
A general purpose smart pointer helper class.
Definition: object.h:132
object_value(T &existing)
Construct composite value object and assign from existing data value.
Definition: object.h:337
T & operator[](unsigned offset)
Array operation to access member object.
Definition: object.h:294
unsigned copied(void) const
Return the number of active references (retentions) to our object.
Definition: object.h:107
A common base class for all managed objects.
Definition: protocols.h:544
A base class for reference counted objects.
Definition: object.h:55
void operator()(T &data)
Set data value by expression reference.
Definition: object.h:368
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:260
T value
Embedded data value.
Definition: object.h:326
T & operator()() const
Reference object we are pointing to through function reference.
Definition: object.h:410
sarray(unsigned size)
Generate a sparse typed array of specified size.
Definition: object.h:277
void operator=(T *typed)
Perform assignment operator to existing object.
Definition: object.h:445
Common namespace for all ucommon objects.
Definition: access.h:46
T * operator*() const
Reference object we are pointing to through pointer indirection.
Definition: object.h:403
T * operator->() const
Reference member of object we are pointing to.
Definition: object.h:417
void retain(ObjectProtocol *object)
Convenience function to access object retention.
Definition: object.h:465
bool is_retained(void) const
Test if the object has been referenced (retained) by anyone yet.
Definition: object.h:100
void set(const T &object)
Assign our value from a typed data object.
Definition: object.h:322
Generic smart pointer class.
Definition: generics.h:53
Runtime functions.
bool is_copied(void) const
Test if the object has copied references.
Definition: object.h:93
Template for embedding a data structure into a reference counted object.
Definition: object.h:315
Generate a typed sparse managed object array.
Definition: object.h:270
void operator--()
Iterate our pointer if we reference an array on the heap.
Definition: object.h:438
T * operator++()
Iterate our pointer if we reference an array on the heap.
Definition: object.h:431
A sparse array of managed objects.
Definition: object.h:212
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
Definition: generics.h:540
Abstract interfaces and support.
void operator=(const T &data)
Assign embedded data value.
Definition: object.h:351
object_value()
Construct composite value object.
Definition: object.h:331
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:479
ObjectProtocol * copy(void)
Retain (increase retention of) object when copying.
object_pointer()
Create a pointer with no reference.
Definition: object.h:391
object_pointer(T *object)
Create a pointer with a reference to a heap object.
Definition: object.h:397
bool operator!() const
See if pointer is not set.
Definition: object.h:457
ObjectProtocol * get(unsigned offset)
Get (reference) an object at a specified offset in the array.
Typed smart pointer class.
Definition: object.h:385
Generic templates for C++.
T & operator*()
Pointer reference to embedded data value.
Definition: object.h:344