RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
cobject.h
Go to the documentation of this file.
1 //--------------------------------------------------------------- @License begins
2 // RioEngine: The late night Coke -without whores- debugging sessions
3 // 2012-2015 Leopoldo Lomas Flores. Torreon, Coahuila. MEXICO
4 // leopoldolomas [at] gmail
5 // www.rioengine.com
6 // www.leopoldolomas.info
7 // "You have a problem?, you face it like a man."
8 //
9 // This is free and unencumbered software released into the public domain.
10 //
11 // Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
12 // software, either in source code form or as a compiled binary, for any purpose,
13 // commercial or non-commercial, and by any means.
14 //
15 // In jurisdictions that recognize copyright laws, the author or authors of this
16 // software dedicate any and all copyright interest in the software to the public
17 // domain. We make this dedication for the benefit of the public at large and to
18 // the detriment of our heirs and successors. We intend this dedication to be
19 // an overt act of relinquishment in perpetuity of all present and future
20 // rights to this software under copyright law.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS
24 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
26 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //--------------------------------------------------------------- @License ends
29 
30 #ifndef RIOENGINE_CENGINE_COBJECT_H_
31 #define RIOENGINE_CENGINE_COBJECT_H_
32 
33 #include "macro.h"
34 
35 #define SAFE_RETAIN(x) CObject::safeRetain(x)
36 #define SAFE_RELEASE(x) CObject::safeRelease(x)
37 #define SAFE_RELEASE_ARRAY(x) CObject::safeReleaseArray(x)
38 
46 class CObject : public QObject {
47  public:
51  CObject();
52 
56  explicit CObject(const CObject& object);
57 
58  CObject& operator=(const CObject& object);
59 
60  // ---------- Reference counting methods
61 
65  CObject* retain();
66 
70  unsigned int release() const;
71 
77 
82  unsigned int retainCount() const;
83 
87  bool isValidCObject() const;
88 
93  virtual ~CObject();
94 
98  static void safeRetain(void* pointer) {
99  if (!pointer) {
100  return;
101  }
102 
103  CObject* ref_as_cobject = reinterpret_cast<CObject*>(pointer);
104 
105  if (ref_as_cobject->isValidCObject()) {
106  ref_as_cobject->retain();
107  }
108  }
109 
114  template<class T>
115  static void safeRelease(T* pointer)
116  {
117  if (pointer) {
118  CObject* ref_as_cobject = reinterpret_cast<CObject*>(pointer);
119  if (ref_as_cobject->isValidCObject()) {
120  ref_as_cobject->release();
121  }
122  else {
123  delete pointer;
124  }
125  pointer = NULL;
126  }
127  }
128 
129  template<class T>
130  static void safeReleaseArray(T* array)
131  {
132  if (array) {
133  delete[] array;
134  array = NULL;
135  }
136  }
137 
138  static void safeRelease(const char* pointer) {
139  safeReleaseArray(pointer);
140  }
141 
142  protected:
146  virtual void copyValuesFromObject(const CObject& object);
147 
148  private:
154  mutable unsigned int __refCount;
155 
159  void* __selfPointer;
160 };
161 Q_DECLARE_METATYPE(CObject)
162 QDataStream &operator<<(QDataStream &ds, const CObject &obj);
163 QDataStream &operator>>(QDataStream &ds, CObject &obj);
164 
165 #endif // RIOENGINE_CENGINE_COBJECT_H_
static void safeRelease(T *pointer)
Definition: cobject.h:115
static void safeRelease(const char *pointer)
Definition: cobject.h:138
virtual void copyValuesFromObject(const CObject &object)
Definition: cobject.cpp:60
bool isValidCObject() const
Definition: cobject.cpp:100
unsigned int retainCount() const
Definition: cobject.cpp:94
static void safeRetain(void *pointer)
Definition: cobject.h:98
unsigned int release() const
Definition: cobject.cpp:74
static void safeReleaseArray(T *array)
Definition: cobject.h:130
virtual ~CObject()
Definition: cobject.cpp:134
CObject * autorelease()
Definition: cobject.cpp:87
CObject()
Definition: cobject.cpp:37
CObject & operator=(const CObject &object)
Definition: cobject.cpp:51
CObject * retain()
Definition: cobject.cpp:66