UCommon
object.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
30 #ifndef _UCOMMON_OBJECT_H_
31 #define _UCOMMON_OBJECT_H_
32 
33 #ifndef _UCOMMON_CPR_H_
34 #include <ucommon/cpr.h>
35 #endif
36 
37 #ifndef _UCOMMON_GENERICS_H_
38 #include <ucommon/generics.h>
39 #endif
40 
41 #ifndef _UCOMMON_PROTOCOLS_H_
42 #include <ucommon/protocols.h>
43 #endif
44 
45 #include <stdlib.h>
46 
47 namespace ucommon {
48 
56 class __EXPORT CountedObject : public __PROTOCOL ObjectProtocol
57 {
58 private:
59  volatile unsigned count;
60 
61 protected:
65  CountedObject();
66 
73  CountedObject(const ObjectProtocol &ref);
74 
80  virtual void dealloc(void);
81 
85  inline void reset(void) {
86  count = 0;
87  }
88 
89 public:
95  inline bool is_copied(void) const {
96  return count > 1;
97  }
98 
103  inline bool is_retained(void) const {
104  return count > 0;
105  }
106 
111  inline unsigned copied(void) const {
112  return count;
113  }
114 
118  void retain(void) __OVERRIDE;
119 
124  void release(void) __OVERRIDE;
125 };
126 
137 class __EXPORT AutoObject
138 {
139 protected:
140  ObjectProtocol *object;
141 
142  AutoObject();
143 
148  AutoObject(ObjectProtocol *object);
149 
155  AutoObject(const AutoObject &pointer);
156 
162  ~AutoObject();
163 
170  void set(ObjectProtocol *object);
171 
172 public:
177  void release(void);
178 
183  bool operator!() const;
184 
189  operator bool() const;
190 
191 };
192 
204 class __EXPORT SparseObjects
205 {
206 private:
207  ObjectProtocol **vector;
208  unsigned max;
209 
210  __DELETE_DEFAULTS(SparseObjects);
211 
212 protected:
218  virtual ObjectProtocol *create(void) = 0;
219 
223  void purge(void);
224 
225  virtual ObjectProtocol *invalid(void) const;
226 
232  ObjectProtocol *get(unsigned offset);
233 
239  SparseObjects(unsigned size);
240 
244  virtual ~SparseObjects();
245 
246 public:
251  unsigned count(void);
252 };
253 
263 template <class T>
264 class sarray : public SparseObjects
265 {
266 private:
267  __DELETE_DEFAULTS(sarray);
268 
269 public:
274  inline sarray(unsigned size) : SparseObjects(size) {}
275 
282  inline T *get(unsigned offset) {
283  return static_cast<T*>(SparseObjects::get(offset));
284  }
285 
292  inline T& operator[](unsigned offset) {
293  return reference_cast<T>(get(offset));
294  }
295 
296  inline T& at(unsigned offset) {
297  return reference_cast<T>(SparseObjects::get(offset));
298  }
299 
300  inline const T* operator()(unsigned offset) const {
301  return get(offset);
302  }
303 
304  inline void operator()(unsigned offset, T value) {
305  T& ref = at(offset);
306  ref = value;
307  }
308 
309 private:
310  __LOCAL ObjectProtocol *create(void) __FINAL {
311  return new T;
312  }
313 };
314 
327 template <class T>
329 {
330 public:
334  inline object_pointer() : AutoObject() {}
335 
340  inline object_pointer(T* object) : AutoObject(object) {}
341 
342  inline object_pointer(const object_pointer& copy) : AutoObject(copy) {}
343 
348  inline T* operator*() const {
349  return protocol_cast<T*>(object);
350  }
351 
356  inline T& operator()() const {
357  return reference_cast<T>(object);
358  }
359 
364  inline T* operator->() const {
365  return protocol_cast<T*>(object);
366  }
367 
372  inline T* get(void) const {
373  return protocol_cast<T*>(object);
374  }
375 
380  inline object_pointer& operator=(T *typed) {
381  AutoObject::set(polypointer_cast<ObjectProtocol*>(typed));
382  return *this;
383  }
384 
385  inline object_pointer& operator=(const object_pointer& from) {
386  AutoObject::set(polypointer_cast<ObjectProtocol*>(from.object));
387  return *this;
388  }
389 
393  inline operator bool() const {
394  return object != NULL;
395  }
396 
400  inline bool operator!() const {
401  return object == NULL;
402  }
403 };
404 
405 } // namespace ucommon
406 
407 #endif
A common base class for all managed objects.
Definition: protocols.h:173
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
Definition: generics.h:445
T & operator()() const
Reference object we are pointing to through function reference.
Definition: object.h:356
ObjectProtocol * get(unsigned offset)
Get (reference) an object at a specified offset in the array.
bool is_retained(void) const
Test if the object has been referenced (retained) by anyone yet.
Definition: object.h:103
bool operator!() const
See if pointer is not set.
Definition: object.h:400
A sparse array of managed objects.
Definition: object.h:204
T * operator->() const
Reference member of object we are pointing to.
Definition: object.h:364
Typed smart pointer class.
Definition: object.h:328
object_pointer()
Create a pointer with no reference.
Definition: object.h:334
A general purpose smart pointer helper class.
Definition: object.h:137
T & operator[](unsigned offset)
Array operation to access member object.
Definition: object.h:292
bool is_copied(void) const
Test if the object has copied references.
Definition: object.h:95
T * get(void) const
Get pointer to object.
Definition: object.h:372
object_pointer & operator=(T *typed)
Perform assignment operator to existing object.
Definition: object.h:380
Common namespace for all ucommon objects.
Definition: access.h:47
T * get(unsigned offset)
Get typed member of array.
Definition: object.h:282
sarray(unsigned size)
Generate a sparse typed array of specified size.
Definition: object.h:274
void reset(void)
Force reset of count.
Definition: object.h:85
T copy(const T &src)
Convenience function to copy objects.
Definition: generics.h:395
Abstract interfaces and support.
unsigned copied(void) const
Return the number of active references (retentions) to our object.
Definition: object.h:111
T * operator *() const
Reference object we are pointing to through pointer indirection.
Definition: object.h:348
object_pointer(T *object)
Create a pointer with a reference to a heap object.
Definition: object.h:340
void set(ObjectProtocol *object)
Set our pointer to a specific object.
Generate a typed sparse managed object array.
Definition: object.h:264
A base class for reference counted objects.
Definition: object.h:56
Generic templates for C++.
Runtime functions.
Generic smart pointer class.
Definition: generics.h:54