00001
00002
00003 #if !defined LOCOBJECT_H
00004 #define LOCOBJECT_H
00005
00006 #define __USING_RTTI__
00007
00008 #include "parametermap.h"
00009
00010 #include "message.h"
00011 #include <string>
00012 #include <map>
00013
00014
00015
00016
00017
00018 class CObject {
00019 friend class CLocatorBase;
00020 private:
00021 long instcount;
00022 static long objcount;
00023 string objtype;
00024 protected:
00025 CObject(void) : instcount(0L), objtype("") {}
00026 CObject(const CObject& m) : instcount(0L), objtype("") {}
00027 virtual ~CObject(void) {}
00028 public:
00029 virtual CObject& CopyState(const CObject& m);
00030
00031 long InstCount(void) const {return instcount;}
00032 static long ObjCount(void) {return objcount;}
00033
00034 virtual CObject* Clone(void) const;
00035
00036 virtual void setObjType(string t){objtype = t;}
00037 virtual string getObjType(void){return objtype;}
00038
00039
00040 void ReportObjectPars(ostream& strm) const;
00041
00042
00043 virtual bool Read(const parameterMap& s){return false;}
00044 virtual bool Write(parameterMap& s){return false;}
00045
00046
00047
00048
00049
00050 };
00051
00052
00053
00054
00055
00056 class CLocatorBase {
00057 protected:
00058 CObject* pObj;
00059 public:
00060 CLocatorBase(void) : pObj(NULL) {}
00061 explicit CLocatorBase(CObject* _pObj) : pObj(NULL)
00062 {AttachObject(_pObj);}
00063 CLocatorBase(const CLocatorBase& lo);
00064 ~CLocatorBase(void) {Dispose();}
00065 CLocatorBase& operator=(const CLocatorBase& lo);
00066 CLocatorBase& AttachObject(CObject* _pObj);
00067 CLocatorBase& AttachClonedObject(
00068 const CLocatorBase& lo, bool copystate);
00069 CObject& operator*(void)
00070 {if (pObj == NULL) ReportError(); return *pObj;}
00071 const CObject& operator*(void) const
00072 {if (pObj == NULL) ReportError(); return *pObj;}
00073 CObject* operator->(void)
00074 {if (pObj == NULL) ReportError(); return pObj;}
00075 const CObject* operator->(void) const
00076 {if (pObj == NULL) ReportError(); return pObj;}
00077 long InstanceCount(void) const {return pObj ? pObj->instcount : 0L;}
00078 bool IsValid(void) const {return pObj != NULL;}
00079 void DetachObject(void) {AttachObject(NULL);}
00080 void Report(ostream& strm) const;
00081 private:
00082 void Dispose(void);
00083 protected:
00084 void ReportError(void) const;
00085 };
00086
00087 inline ostream& operator<<(ostream& strm, const CLocatorBase& lo)
00088 {lo.Report(strm);return strm;}
00089
00090
00091
00092
00093
00094 template <class T>
00095 class CLocator : public CLocatorBase {
00096 public:
00097 CLocator(void) : CLocatorBase() {}
00098 explicit CLocator(CObject* _pObj) : CLocatorBase(_pObj) {}
00099 CLocator(const CLocator<T>& lo) : CLocatorBase(lo) {}
00100 CLocator<T>& operator=(const CLocator<T>& lo)
00101 {
00102 *static_cast<CLocatorBase*>(this) = static_cast<const CLocatorBase&>(lo);
00103 return *this;
00104 }
00105 CLocator<T>& AttachObject(T* _pObj)
00106 {return static_cast<CLocator<T>&>(CLocatorBase::AttachObject(_pObj));}
00107 CLocator<T>& AttachClonedObject(const CLocator<T>& lo, bool copystate)
00108 {return static_cast<CLocator<T>&>(CLocatorBase::AttachClonedObject(lo, copystate));}
00109 operator T*(void)
00110 {if (pObj == NULL) ReportError();return static_cast<T*>(pObj);}
00111 operator const T*(void) const
00112 {if (pObj == NULL) ReportError();return static_cast<const T*>(pObj);}
00113 T& operator*(void)
00114 {if (pObj == NULL) ReportError();return *static_cast<T*>(pObj);}
00115 const T& operator*(void) const
00116 {if (pObj == NULL) ReportError();return *static_cast<const T*>(pObj);}
00117 T* operator->(void)
00118 {if (pObj == NULL) ReportError();return static_cast<T*>(pObj);}
00119 const T* operator->(void) const
00120 {if (pObj == NULL) ReportError();return static_cast<const T*>(pObj);}
00121 T* operator()(void)
00122 {if (pObj == NULL) ReportError();return static_cast<T*>(pObj);}
00123 const T* operator()(void) const
00124 {if (pObj == NULL) ReportError();return static_cast<const T*>(pObj);}
00125 };
00126
00127 template <class T>
00128 bool operator< (const CLocator<T>& l, const CLocator<T>& r)
00129 {
00130 const T* plObj = l;
00131 const T* prObj = r;
00132
00133 if (plObj == NULL) return true;
00134 if (prObj == NULL) return false;
00135
00136 return *plObj < *prObj;
00137 }
00138
00139 template <class T>
00140 bool operator==(const CLocator<T>& l, const CLocator<T>& r)
00141 {
00142 const T* plObj = l;
00143 const T* prObj = r;
00144
00145 if ((plObj == NULL) && (prObj == NULL)) return true;
00146 if ((plObj == NULL) || (prObj == NULL)) return false;
00147
00148 return *plObj == *prObj;
00149 }
00150
00151
00152
00153
00154
00155 class CObjectRegistry : public map<string, CLocatorBase> {
00156 public:
00157 void Register(const CLocatorBase& lo, const string& ID);
00158 bool Unregister(const string& ID);
00159 CObject* Create(const string& ID) const;
00160 void ClearRegistry(void) {clear();}
00161 void Report(ostream& strm) const;
00162 };
00163
00164 inline ostream& operator<<(ostream& strm, const CObjectRegistry& m)
00165 {
00166 m.Report(strm);
00167 return strm;
00168 }
00169
00170
00171
00172
00173
00174
00175
00176 #define REGISTER_OBJECT(reg, CObjDerived, ID) \
00177 { \
00178 string id = ID; \
00179 CLocator<CObjDerived> lo(new CObjDerived); \
00180 reg.Register(lo, id); \
00181 lo->setObjType(id); \
00182 }
00183
00184 #define CREATE_OBJECT(reg, CObjTarg, lo, ID) \
00185 CLocator<CObjTarg> lo; \
00186 { \
00187 string id = ID; \
00188 CObject* pObj = reg.Create(id); \
00189 PTR_CAST(CObjTarg*, pObjTarg, pObj); \
00190 lo.AttachObject(pObjTarg); \
00191 lo->setObjType(id); \
00192 }
00193
00194
00195
00196
00197
00198
00199
00200 #if !defined LOCOBJECT_CPP
00201 extern CObjectRegistry GlobalObjectRegistry;
00202 #endif
00203
00204 #define REGISTER_GLOBAL_OBJECT(CObj) \
00205 { \
00206 CLocator<CObj> lo(new CObj); \
00207 GlobalObjectRegistry.Register(lo, #CObj); \
00208 }
00209
00210 #define UNREGISTER_GLOBAL_OBJECT(CObj) \
00211 GlobalObjectRegistry.Unregister(#CObj);
00212
00213 #define CREATE_GLOBAL_OBJECT(CObjTarg, lo, CObj) \
00214 CLocator<CObjTarg> lo; \
00215 { \
00216 CObject* pObj = GlobalObjectRegistry.Create(#CObj); \
00217 PTR_CAST(CObjTarg*, pObjTarg, pObj); \
00218 lo.AttachObject(pObjTarg); \
00219 }
00220
00221 #endif
00222
00223
00224
00225 #define DYNAMIC_CAST(CObj,ClassName) dynamic_cast<ClassName*>(&(*CObj))
00226
00227
00228
00229
00230
00231 #if defined __USING_RTTI__
00232
00233 #define PTR_CAST(T, DestPtr, SrcPtr) \
00234 T DestPtr = dynamic_cast<T>(SrcPtr); \
00235 if (DestPtr == NULL) {\
00236 MESSAGE<<"Dynamic cast of "#SrcPtr" to type "#T" failed."<<ENDM_FATAL; \
00237 exit(-1);\
00238 }
00239
00240 #define REF_CAST(T, DestRef, SrcRef) \
00241 try { \
00242 T DestRef = dynamic_cast<T>(SrcRef); \
00243 } \
00244 catch (...) { \
00245 MESSAGE<<"Dynamic cast of "#SrcRef" to type "#T" failed."<<ENDM_FATAL; \
00246 exit(-1);\
00247 } \
00248 T DestRef = static_cast<T>(SrcRef);
00249
00250 #else
00251
00252 #define PTR_CAST(T, DestPtr, SrcPtr) \
00253 T DestPtr = static_cast<T>(SrcPtr);
00254
00255 #define REF_CAST(T, DestRef, SrcRef) \
00256 T DestRef = static_cast<T>(SrcRef);
00257
00258 #endif
00259
00260
00261
00262
00263
00264 #define CONST_CAST(Data,DataType) *const_cast<DataType*>(&Data)