00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
#ifndef UOBJECT_HPP
00029
#define UOBJECT_HPP
00030
00031
#include "ufo_global.hpp"
00032
00033
#include "ucollectable.hpp"
00034
00035
#include "signals/usignal.hpp"
00036
00037
#include <string>
00038
#include <iostream>
00039
00040
#include <list>
00041
00042
00043
00044
#ifdef UFO_RTTI
00045
00046
namespace ufo {
00047
class UObject;
00048
typedef UObject *(*UObjectConstructorFn)(
void);
00049 }
00050
00051
00052
00053
00054
00055
#define UFO_DECLARE_CLASS(name) \
00056
public: \
00057
static ufo::UClassInfo sm_class##name; \
00058
virtual ufo::UClassInfo * getClassInfo() const \
00059
{ return &name::sm_class##name; }
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
#define UFO_IMPLEMENT_CLASS(name, basename) \
00080
ufo::UClassInfo name::sm_class##name( \
00081
#name, \
00082
#basename, \
00083
(unsigned int) sizeof(name), \
00084
(ufo::UObjectConstructorFn) NULL);
00085
00086
00087
00088
00089
00090
00091
#include <map>
00092
namespace ufo {
00093
00094
class UFO_EXPORT UClassInfo {
00095
public:
00096 UClassInfo(
00097
const std::string & className,
00098
const std::string & baseName,
00099
unsigned int size,
00100 UObjectConstructorFn ctor)
00101 : m_className(className)
00102 , m_baseClassName(baseName)
00103 , m_objectSize(size)
00104 , m_objectConstructor(ctor)
00105 , m_baseInfo(NULL)
00106 , m_next(sm_first)
00107 {
00108 sm_first =
this;
00109 }
00110
00111 ~UClassInfo();
00112
00113 UObject * createObject()
const {
00114
return m_objectConstructor ? (*m_objectConstructor)() : NULL;
00115 }
00116
00117
const std::string & getClassName()
const {
return m_className; }
00118
const std::string & getBaseClassName()
const {
return m_baseClassName; }
00119
const UClassInfo * getBaseClassInfo()
const {
return m_baseInfo; }
00120
unsigned int getSize()
const {
return m_objectSize; }
00121
00122 UObjectConstructorFn getConstructor()
const {
return m_objectConstructor; }
00123
static const UClassInfo * getFirst() {
return sm_first; }
00124
const UClassInfo * getNext()
const {
return m_next; }
00125
00126
static UClassInfo * findClass(
const std::string & className);
00127
00130
static void initClassInfo();
00131
00132
private:
00133 std::string m_className;
00134 std::string m_baseClassName;
00135
unsigned int m_objectSize;
00136 UObjectConstructorFn m_objectConstructor;
00137
00138
00139
const UClassInfo * m_baseInfo;
00140
00141
00142
static UClassInfo * sm_first;
00143 UClassInfo * m_next;
00144
00145
static std::map<std::string, UClassInfo*> sm_classTable;
00146
00147 UFO_DECLARE_NO_COPY_CLASS(UClassInfo)
00148 };
00149
00150 }
00151
00152
#else // UFO_RTTI
00153
00154
#define UFO_DECLARE_CLASS(name)
00155
#define UFO_IMPLEMENT_CLASS(name, basename)
00156
00157
#endif // !UFO_RTTI
00158
00159
00160
#define UFO_DECLARE_DYNAMIC_CLASS(name) UFO_DECLARE_CLASS(name)
00161
#define UFO_DECLARE_ABSTRACT_CLASS(name) UFO_DECLARE_CLASS(name)
00162
00163
#define UFO_IMPLEMENT_DYNAMIC_CLASS(name, basename) UFO_IMPLEMENT_CLASS(name, basename)
00164
#define UFO_IMPLEMENT_DEFAULT_DYNAMIC_CLASS(name, basename) UFO_IMPLEMENT_CLASS(name, basename)
00165
#define UFO_IMPLEMENT_ABSTRACT_CLASS(name, basename) UFO_IMPLEMENT_CLASS(name, basename)
00166
#define UFO_IMPLEMENT_CTOR_DYNAMIC_CLASS(name, basename) UFO_IMPLEMENT_CLASS(name, basename)
00167
00168
namespace ufo {
00169
00170
struct UObjectSlotNode;
00171
00178 class UFO_EXPORT UObject :
public virtual UCollectable {
00179 UFO_DECLARE_CLASS(UObject)
00180
friend struct UObjectSlotNode;
00181
public:
00182 UObject();
00183 UObject(
const UObject &);
00184
virtual ~UObject();
00185
00193
virtual unsigned int hashCode()
const;
00194
00197
virtual bool equals(
const UObject * obj)
const;
00198
00199
00200
00201
bool operator==(
const UObject & obj)
const;
00202
00204
virtual UObject * clone()
const;
00205
00206
00207
00208
00209
00211
virtual std::string toString()
const;
00212
00214
friend std::ostream & operator<<(std::ostream & os,
const UObject & o);
00216
friend std::ostream & operator<<(std::ostream & os,
const UObject * o);
00217
00218
00219
00220
00221
00222
const std::string & getName()
const;
00224
void setName(
const std::string & newNameA);
00225
00230
unsigned int objCount();
00231
00235
00244
UCollectable * trackPointer(
UCollectable * c);
00253
const UCollectable * trackPointer(
const UCollectable * c);
00254
00262
bool releasePointer(
UCollectable * c);
00270
bool releasePointer(
const UCollectable * c);
00271
00276
void swapPointers(
const UCollectable * oldObj,
const UCollectable * newObj);
00277
00281
void releaseAllPointers();
00282
00283
protected:
00287
virtual std::ostream & paramString(std::ostream & os)
const;
00288
private:
00293 std::string m_name;
00294
00295 std::list<const UCollectable*> m_pointers;
00296 std::list<UObjectSlotNode*> m_objectSlots;
00297
00298
public:
00303 USignal1<UObject*> & sigDestroyed();
00304
00305
private:
00306 USignal1<UObject*> m_sigDestroyed;
00307 };
00308
00309
00310
00311
00312
00313
inline bool
00314 UObject::operator==(
const UObject & obj)
const {
00315
return equals(&obj);
00316 }
00317
00318 inline std::ostream & operator<<(std::ostream & os,
const UObject & o) {
00319
return os << o.
toString();
00320 }
00321
00322 inline std::ostream & operator<<(std::ostream & os,
const UObject * o) {
00323
if (o) {
00324
return os << o->
toString();
00325 }
else {
00326
return os <<
"NULL";
00327 }
00328 }
00329
00330
inline const std::string &
00331 UObject::getName()
const {
00332
return m_name;
00333 }
00334
00335
inline void
00336 UObject::setName(
const std::string & newNameA) {
00337 m_name = newNameA;
00338 }
00339
00340
00341
inline USignal1<UObject*> &
00342 UObject::sigDestroyed() {
00343
return m_sigDestroyed;
00344 }
00345
00346 }
00347
00348
#endif // UOBJECT_HPP