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 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);
119 
124  void release(void);
125 };
126 
137 class __EXPORT auto_object
138 {
139 protected:
140  ObjectProtocol *object;
141 
142  auto_object();
143 
144 public:
149  auto_object(ObjectProtocol *object);
150 
157 
163  ~auto_object();
164 
169  void release(void);
170 
175  bool operator!() const;
176 
181  operator bool() const;
182 
188  bool operator==(ObjectProtocol *object) const;
189 
195  bool operator!=(ObjectProtocol *object) const;
196 
203  void operator=(ObjectProtocol *object);
204 };
205 
217 class __EXPORT SparseObjects
218 {
219 private:
220  ObjectProtocol **vector;
221  unsigned max;
222 
223 protected:
229  virtual ObjectProtocol *create(void) = 0;
230 
234  void purge(void);
235 
236  virtual ObjectProtocol *invalid(void) const;
237 
243  ObjectProtocol *get(unsigned offset);
244 
250  SparseObjects(unsigned size);
251 
255  virtual ~SparseObjects();
256 
257 public:
262  unsigned count(void);
263 };
264 
274 template <class T>
275 class sarray : public SparseObjects
276 {
277 public:
282  inline sarray(unsigned size) : SparseObjects(size) {}
283 
290  inline T *get(unsigned offset) {
291  return static_cast<T*>(SparseObjects::get(offset));
292  }
293 
300  inline T& operator[](unsigned offset) {
301  return reference_cast<T>(get(offset));
302  }
303 
304  inline const T* at(unsigned offset) const {
305  return immutable_cast<T*>(SparseObjects::get(offset));
306  }
307 
308 private:
309  __LOCAL ObjectProtocol *create(void) {
310  return new T;
311  }
312 };
313 
323 template <typename T, class O = CountedObject>
324 class object_value : public O
325 {
326 protected:
331  inline void set(const T& object) {
332  value = object;
333  }
334 
335 public:
336  T value;
341  inline object_value() : O(), value() {}
342 
347  inline object_value(T& existing) : O() {
348  value = existing;
349  }
350 
355  inline T& operator*() {
356  return value;
357  }
358 
363  inline void operator=(const T& data) {
364  value = data;
365  }
366 
371  inline operator T&() {
372  return value;
373  }
374 
375  inline T& operator()() {
376  return value;
377  }
378 
383  inline void operator()(T& data) {
384  value = data;
385  }
386 };
387 
400 template <class T, class P = auto_object>
401 class object_pointer : public P
402 {
403 public:
407  inline object_pointer() : P() {}
408 
413  inline object_pointer(T* object) : P(object) {}
414 
419  inline T* operator*() const {
420  return static_cast<T*>(P::object);
421  }
422 
427  inline T& operator()() const {
428  return reference_cast<T>(P::object);
429  }
430 
435  inline T* operator->() const {
436  return static_cast<T*>(P::object);
437  }
438 
443  inline T* get(void) const {
444  return static_cast<T*>(P::object);
445  }
446 
451  inline T* operator++() {
452  P::operator++();
453  return get();
454  }
455 
460  inline void operator--() {
461  P::operator--();
462  return get();
463  }
464 
469  inline void operator=(T *typed) {
470  P::operator=(polypointer_cast<ObjectProtocol*>(typed));
471  }
472 
476  inline operator bool() const {
477  return P::object != NULL;
478  }
479 
483  inline bool operator!() const {
484  return P::object == NULL;
485  }
486 };
487 
492 inline void retain(ObjectProtocol *object)
493 {
494  object->retain();
495 }
496 
501 inline void release(ObjectProtocol *object)
502 {
503  object->release();
504 }
505 
511  return object->copy();
512 }
513 
514 } // namespace ucommon
515 
516 #endif
void reset(void)
Force reset of count.
Definition: object.h:85
T * operator*() const
Reference object we are pointing to through pointer indirection.
Definition: object.h:419
void operator()(T &data)
Set data value by expression reference.
Definition: object.h:383
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:280
A general purpose smart pointer helper class.
Definition: object.h:137
unsigned copied(void) const
Return the number of active references (retentions) to our object.
Definition: object.h:111
A base class for reference counted objects.
Definition: object.h:56
Typed smart pointer class.
Definition: object.h:401
T & operator()() const
Reference object we are pointing to through function reference.
Definition: object.h:427
bool is_retained(void) const
Test if the object has been referenced (retained) by anyone yet.
Definition: object.h:103
void set(const T &object)
Assign our value from a typed data object.
Definition: object.h:331
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:510
T * operator->() const
Reference member of object we are pointing to.
Definition: object.h:435
bool is_copied(void) const
Test if the object has copied references.
Definition: object.h:95
A common base class for all managed objects.
Definition: protocols.h:545
void operator=(T *typed)
Perform assignment operator to existing object.
Definition: object.h:469
object_value(T &existing)
Construct composite value object and assign from existing data value.
Definition: object.h:347
T & operator*()
Pointer reference to embedded data value.
Definition: object.h:355
bool operator!() const
See if pointer is not set.
Definition: object.h:483
T * operator++()
Iterate our pointer if we reference an array on the heap.
Definition: object.h:451
Generic smart pointer class.
Definition: generics.h:54
T value
Embedded data value.
Definition: object.h:336
Common namespace for all ucommon objects.
Definition: access.h:47
A sparse array of managed objects.
Definition: object.h:217
object_value()
Construct composite value object.
Definition: object.h:341
ObjectProtocol * copy(void)
Retain (increase retention of) object when copying.
object_pointer()
Create a pointer with no reference.
Definition: object.h:407
void retain(ObjectProtocol *object)
Convenience function to access object retention.
Definition: object.h:492
Generate a typed sparse managed object array.
Definition: object.h:275
void operator--()
Iterate our pointer if we reference an array on the heap.
Definition: object.h:460
object_pointer(T *object)
Create a pointer with a reference to a heap object.
Definition: object.h:413
ObjectProtocol * get(unsigned offset)
Get (reference) an object at a specified offset in the array.
Template for embedding a data structure into a reference counted object.
Definition: object.h:324
T & operator[](unsigned offset)
Array operation to access member object.
Definition: object.h:300
void operator=(const T &data)
Assign embedded data value.
Definition: object.h:363
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
Definition: generics.h:414
sarray(unsigned size)
Generate a sparse typed array of specified size.
Definition: object.h:282
Abstract interfaces and support.
Generic templates for C++.
Runtime functions.