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
47namespace ucommon {
48
56class __EXPORT CountedObject : public __PROTOCOL ObjectProtocol
57{
58private:
59 volatile unsigned count;
60
61protected:
66
74
80 virtual void dealloc(void);
81
85 inline void reset(void) {
86 count = 0;
87 }
88
89public:
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
137class __EXPORT AutoObject
138{
139protected:
140 ObjectProtocol *object;
141
142 AutoObject();
143
149
156
163
170 void set(ObjectProtocol *object);
171
172public:
177 void release(void);
178
183 bool operator!() const;
184
189 operator bool() const;
190
191};
192
204class __EXPORT SparseObjects
205{
206private:
207 ObjectProtocol **vector;
208 unsigned max;
209
210 __DELETE_DEFAULTS(SparseObjects);
211
212protected:
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
246public:
251 unsigned count(void);
252};
253
263template <class T>
264class sarray : public SparseObjects
265{
266private:
267 __DELETE_DEFAULTS(sarray);
268
269public:
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
309private:
310 __LOCAL ObjectProtocol *create(void) __FINAL {
311 return new T;
312 }
313};
314
327template <class T>
329{
330public:
335
340 inline object_pointer(T* object) : AutoObject(object) {}
341
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
Runtime functions.
Abstract interfaces and support.
Generic templates for C++.
Common namespace for all ucommon objects.
Definition access.h:47
T & max(T &o1, T &o2)
Convenience function to return max of two objects.
Definition generics.h:445
T copy(const T &src)
Convenience function to copy objects.
Definition generics.h:395
Generic smart pointer class.
Definition generics.h:55
A base class for reference counted objects.
Definition object.h:57
bool is_retained(void) const
Test if the object has been referenced (retained) by anyone yet.
Definition object.h:103
unsigned copied(void) const
Return the number of active references (retentions) to our object.
Definition object.h:111
void reset(void)
Force reset of count.
Definition object.h:85
virtual void dealloc(void)
Dealloc object no longer referenced.
CountedObject(const ObjectProtocol &ref)
Construct a copy of a counted object.
void retain(void)
Increase reference count when retained.
bool is_copied(void) const
Test if the object has copied references.
Definition object.h:95
void release(void)
Decrease reference count when released.
CountedObject()
Construct a counted object, mark initially as unreferenced.
A general purpose smart pointer helper class.
Definition object.h:138
void release(void)
Manually release the pointer.
AutoObject(const AutoObject &pointer)
Construct an auto-pointer as a copy of another pointer.
AutoObject(ObjectProtocol *object)
Construct an auto-pointer referencing an existing object.
bool operator!() const
Test if the pointer is not set.
~AutoObject()
Delete auto pointer.
void set(ObjectProtocol *object)
Set our pointer to a specific object.
A sparse array of managed objects.
Definition object.h:205
SparseObjects(unsigned size)
Create a sparse array of known size.
virtual ObjectProtocol * create(void)=0
Object factory for creating members of the spare array when they are initially requested.
void purge(void)
Purge the array by deleting all created objects.
ObjectProtocol * get(unsigned offset)
Get (reference) an object at a specified offset in the array.
virtual ~SparseObjects()
Destroy sparse array and delete all generated objects.
unsigned count(void)
Get count of array elements.
Generate a typed sparse managed object array.
Definition object.h:265
sarray(unsigned size)
Generate a sparse typed array of specified size.
Definition object.h:274
T & operator[](unsigned offset)
Array operation to access member object.
Definition object.h:292
T * get(unsigned offset)
Get typed member of array.
Definition object.h:282
Typed smart pointer class.
Definition object.h:329
T * operator*() const
Reference object we are pointing to through pointer indirection.
Definition object.h:348
bool operator!() const
See if pointer is not set.
Definition object.h:400
object_pointer(T *object)
Create a pointer with a reference to a heap object.
Definition object.h:340
object_pointer & operator=(T *typed)
Perform assignment operator to existing object.
Definition object.h:380
T & operator()() const
Reference object we are pointing to through function reference.
Definition object.h:356
T * get(void) const
Get pointer to object.
Definition object.h:372
object_pointer()
Create a pointer with no reference.
Definition object.h:334
T * operator->() const
Reference member of object we are pointing to.
Definition object.h:364
A common base class for all managed objects.
Definition protocols.h:174