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  ("inofficial" and yet experimental doxygen-generated source code documentation)  

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 
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 
138 {
139 protected:
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 
205 {
206 private:
208  unsigned max;
209 
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:
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:
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 
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
generics.h
ucommon::object_pointer::object_pointer
object_pointer()
Definition: object.h:334
ucommon::sarray::sarray
sarray(unsigned size)
Definition: object.h:274
ucommon::sarray::operator()
void operator()(unsigned offset, T value)
Definition: object.h:304
ucommon::CountedObject::is_copied
bool is_copied(void) const
Definition: object.h:95
ucommon
Definition: access.cpp:23
ucommon::sarray::__DELETE_DEFAULTS
__DELETE_DEFAULTS(sarray)
ucommon::AutoObject::object
ObjectProtocol * object
Definition: object.h:140
__LOCAL
#define __LOCAL
Definition: platform.h:298
protocols.h
__OVERRIDE
#define __OVERRIDE
Definition: platform.h:158
ucommon::object_pointer::operator->
T * operator->() const
Definition: object.h:364
ucommon::SparseObjects::max
unsigned max
Definition: object.h:208
ucommon::sarray::at
T & at(unsigned offset)
Definition: object.h:296
ucommon::SparseObjects::get
ObjectProtocol * get(unsigned offset)
Definition: object.cpp:152
ucommon::ObjectProtocol
Definition: protocols.h:173
ucommon::copy
T copy(const T &src)
Definition: generics.h:395
ucommon::sarray
Definition: object.h:264
ucommon::CountedObject::is_retained
bool is_retained(void) const
Definition: object.h:103
ucommon::object_pointer::operator()
T & operator()() const
Definition: object.h:356
ucommon::object_pointer::operator=
object_pointer & operator=(T *typed)
Definition: object.h:380
ucommon::CountedObject::reset
void reset(void)
Definition: object.h:85
ucommon::sarray::get
T * get(unsigned offset)
Definition: object.h:282
ucommon::sarray::create
__LOCAL ObjectProtocol * create(void) __FINAL
Definition: object.h:310
ucommon::object_pointer::operator=
object_pointer & operator=(const object_pointer &from)
Definition: object.h:385
ucommon::SparseObjects::vector
ObjectProtocol ** vector
Definition: object.h:207
ucommon::SparseObjects
Definition: object.h:204
ucommon::CountedObject::copied
unsigned copied(void) const
Definition: object.h:111
__EXPORT
#define __EXPORT
Definition: config.h:49
ucommon::sarray::operator[]
T & operator[](unsigned offset)
Definition: object.h:292
ucommon::object_pointer::get
T * get(void) const
Definition: object.h:372
ucommon::object_pointer::object_pointer
object_pointer(T *object)
Definition: object.h:340
ucommon::AutoObject
Definition: object.h:137
ucommon::pointer
Definition: generics.h:54
cpr.h
ucommon::sarray::operator()
const T * operator()(unsigned offset) const
Definition: object.h:300
__DELETE_DEFAULTS
#define __DELETE_DEFAULTS(x)
Definition: platform.h:162
ucommon::__FINAL
Definition: typeref.cpp:698
ucommon::object_pointer
Definition: object.h:328
ucommon::CountedObject::count
volatile unsigned count
Definition: object.h:59
__PROTOCOL
#define __PROTOCOL
Definition: platform.h:112
ucommon::AutoObject::set
void set(ObjectProtocol *object)
Definition: object.cpp:99
ucommon::object_pointer::operator!
bool operator!() const
Definition: object.h:400
ucommon::object_pointer::operator*
T * operator*() const
Definition: object.h:348
ucommon::object_pointer::object_pointer
object_pointer(const object_pointer &copy)
Definition: object.h:342
ucommon::CountedObject
Definition: object.h:56