RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
cobject.cpp
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 #include <QMetaProperty>
31 #include <QDataStream>
33 #include "cengine/cobject.h"
34 
35 //-----------------------------------------------------------------------------
36 
37 CObject::CObject() : QObject() {
38  __refCount = 1;
39  __selfPointer = this;
40 }
41 
42 //-----------------------------------------------------------------------------
43 
45  __refCount = 1;
46  __selfPointer = this;
47 }
48 
49 //-----------------------------------------------------------------------------
50 
52  __refCount = 1;
53  __selfPointer = this;
54 
55  return *this;
56 }
57 
58 //-----------------------------------------------------------------------------
59 
61  RE_ASSERT(false); // copyValuesFromObject() must be overridden
62 }
63 
64 //-----------------------------------------------------------------------------
65 
67  __refCount++;
68 
69  return this;
70 }
71 
72 //-----------------------------------------------------------------------------
73 
74 unsigned int CObject::release() const {
75  RE_ASSERT(__refCount != UINT_MAX); // object already deallocated?
76  unsigned int retain_count = --__refCount;
77 
78  if (retain_count == 0) {
79  __refCount = UINT_MAX; // flag it as deallocated
80  delete this;
81  }
82  return retain_count;
83 }
84 
85 //-----------------------------------------------------------------------------
86 
88  CAUTORELEASEPOOL->addObject(this);
89  return this;
90 }
91 
92 //-----------------------------------------------------------------------------
93 
94 unsigned int CObject::retainCount() const {
95  return __refCount;
96 }
97 
98 //-----------------------------------------------------------------------------
99 
101  return ( __selfPointer == this );
102 }
103 
104 //-----------------------------------------------------------------------------
105 
106 QDataStream &operator<<(QDataStream &ds, const CObject &obj) {
107  for (int i=0; i<obj.metaObject()->propertyCount(); ++i) {
108  if (obj.metaObject()->property(i).isStored(&obj)) {
109  const char* prop_name = obj.metaObject()->property(i).name();
110  LOG("(%s) - Serializing property: %s", obj.metaObject()->className(), prop_name);
111  ds << obj.metaObject()->property(i).read(&obj);
112  }
113  }
114  return ds;
115 }
116 
117 //-----------------------------------------------------------------------------
118 
119 QDataStream &operator>>(QDataStream &ds, CObject &obj) {
120  QVariant var;
121  for (int i=0; i<obj.metaObject()->propertyCount(); ++i) {
122  if (obj.metaObject()->property(i).isStored(&obj)) {
123  const char* prop_name = obj.metaObject()->property(i).name();
124  LOG("(%s) - Deserializing property: %s", obj.metaObject()->className(), prop_name);
125  ds >> var;
126  obj.metaObject()->property(i).write(&obj, var);
127  }
128  }
129  return ds;
130 }
131 
132 //-----------------------------------------------------------------------------
133 
135 }
#define LOG
Definition: macro.h:43
virtual void copyValuesFromObject(const CObject &object)
Definition: cobject.cpp:60
QDataStream & operator<<(QDataStream &ds, const CObject &obj)
Definition: cobject.cpp:106
bool isValidCObject() const
Definition: cobject.cpp:100
QDataStream & operator>>(QDataStream &ds, CObject &obj)
Definition: cobject.cpp:119
unsigned int retainCount() const
Definition: cobject.cpp:94
unsigned int release() const
Definition: cobject.cpp:74
virtual ~CObject()
Definition: cobject.cpp:134
CObject * autorelease()
Definition: cobject.cpp:87
CObject()
Definition: cobject.cpp:37
#define RE_ASSERT
Definition: macro.h:57
#define CAUTORELEASEPOOL
CObject & operator=(const CObject &object)
Definition: cobject.cpp:51
CObject * retain()
Definition: cobject.cpp:66