ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
object.cpp
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
19#include <ucommon-config.h>
20#include <ucommon/export.h>
21#include <ucommon/protocols.h>
22#include <ucommon/object.h>
23#include <stdlib.h>
24#include <string.h>
25
26namespace ucommon {
27
29{
30 count = 0;
31}
32
34{
35 count = 0;
36}
37
39{
40 delete this;
41}
42
44{
45 ++count;
46}
47
49{
50 if(count > 1) {
51 --count;
52 return;
53 }
54 dealloc();
55}
56
58{
59 if(o)
60 o->retain();
61
62 object = o;
63}
64
66{
67 object = 0;
68}
69
71{
72 if(object)
73 object->release();
74 object = 0;
75}
76
78{
79 release();
80}
81
83{
84 object = from.object;
85 if(object)
86 object->retain();
87}
88
90{
91 return (object == 0);
92}
93
94AutoObject::operator bool() const
95{
96 return (object != 0);
97}
98
100{
101 if(object == o)
102 return;
103
104 if(o)
105 o->retain();
106 if(object)
107 object->release();
108 object = o;
109}
110
112{
113 assert(m > 0);
114 max = m;
115 vector = new ObjectProtocol *[m];
116 memset(vector, 0, sizeof(ObjectProtocol *) * m);
117}
118
120{
121 purge();
122}
123
125{
126 if(!vector)
127 return;
128
129 for(unsigned pos = 0; pos < max; ++ pos) {
130 if(vector[pos])
131 vector[pos]->release();
132 }
133 delete[] vector;
134 vector = NULL;
135}
136
138{
139 unsigned c = 0;
140 for(unsigned pos = 0; pos < max; ++pos) {
141 if(vector[pos])
142 ++c;
143 }
144 return c;
145}
146
148{
149 return NULL;
150}
151
153{
154 ObjectProtocol *obj;
155
156 if(pos >= max)
157 return invalid();
158
159 if(!vector[pos]) {
160 obj = create();
161 if(!obj)
162 return invalid();
163 obj->retain();
164 vector[pos] = obj;
165 }
166 return vector[pos];
167}
168
169} // namespace ucommon
170
A general purpose smart pointer helper class.
Definition: object.h:138
void release(void)
Manually release the pointer.
Definition: object.cpp:70
ObjectProtocol * object
Definition: object.h:140
bool operator!() const
Test if the pointer is not set.
Definition: object.cpp:89
~AutoObject()
Delete auto pointer.
Definition: object.cpp:77
void set(ObjectProtocol *object)
Set our pointer to a specific object.
Definition: object.cpp:99
void release(void) __OVERRIDE
Decrease reference count when released.
Definition: object.cpp:48
virtual void dealloc(void)
Dealloc object no longer referenced.
Definition: object.cpp:38
volatile unsigned count
Definition: object.h:59
CountedObject()
Construct a counted object, mark initially as unreferenced.
Definition: object.cpp:28
void retain(void) __OVERRIDE
Increase reference count when retained.
Definition: object.cpp:43
A common base class for all managed objects.
Definition: protocols.h:174
virtual void release(void)=0
Method to release (or decrease retention) of an object.
virtual void retain(void)=0
Method to retain (or increase retention) of an object.
SparseObjects(unsigned size)
Create a sparse array of known size.
Definition: object.cpp:111
ObjectProtocol ** vector
Definition: object.h:207
virtual ~SparseObjects()
Destroy sparse array and delete all generated objects.
Definition: object.cpp:119
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.
Definition: object.cpp:124
ObjectProtocol * get(unsigned offset)
Get (reference) an object at a specified offset in the array.
Definition: object.cpp:152
virtual ObjectProtocol * invalid(void) const
Definition: object.cpp:147
unsigned count(void)
Get count of array elements.
Definition: object.cpp:137
Common namespace for all ucommon objects.
Definition: access.cpp:23
Abstract interfaces and support.
Export interfaces for library interfaces.
A common object base class with auto-pointer support.