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
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 #ifndef VRS_SO_H
00174 #define VRS_SO_H
00175
00176 #include <vrs/sharedobj.h>
00177 #include <cassert>
00178 #include <stdlib.h>
00179
00180 namespace VRS {
00181
00182 class SharedObj;
00183 class WeakPointerBase;
00184 template<class T> class SO;
00185 template<class T> class WO;
00186
00188 template<class T>
00189 class SO {
00190
00191 public:
00192 SO() : m_obj(NULL) { }
00193 SO(T* obj) : m_obj(obj) { ref(m_obj); }
00194 SO(const SO& so) : m_obj(so.m_obj) { ref(m_obj); }
00195 SO(const WO<T>& wo) : m_obj(static_cast<T*>(wo)) { ref(m_obj); }
00196 ~SO() { unref(m_obj); }
00204 SO& operator=(T* obj) { if(m_obj != obj) { ref(obj); unref(m_obj); m_obj = obj; } return *this; }
00205 SO& operator=(const SO& so) { return operator=(so.m_obj); }
00206 SO& operator=(const WO<T>& wo) { return operator=(static_cast<T*>(wo)); }
00207 #if !defined(_MSC_VER) || (_MSC_VER >= 1300) // all compilers except visual studio < 7.0
00208 template<class S>
00209 SO& operator=(const SO<S>& so) { return operator=(static_cast<S*>(so)); }
00210 #endif // all compilers except visual studio < 7.0
00211
00215 bool operator<(const T* obj) const { return (m_obj < obj); }
00216 bool operator<(const SO& so) const { return (m_obj < so.m_obj); }
00217 friend bool operator<(const T* obj1, const SO& so2) { return (obj1 < so2.m_obj); }
00221 operator T*() const { return m_obj; }
00222 T* operator->() const { assert(((void)"NULL pointer dereferenced", static_cast<const SharedObj*>(m_obj))); return m_obj; }
00223 T& operator*() const { assert(((void)"NULL pointer dereferenced", static_cast<const SharedObj*>(m_obj))); return *m_obj; }
00228 template<class S>
00229 operator SO<S>() { return SO<S>(m_obj); }
00233 #if !defined(_MSC_VER) || (_MSC_VER >= 1300) // all compilers except visual studio < 7.0
00234 operator WO<T>() { return WO<T>(m_obj); }
00237 #endif // all compilers except visual studio < 7.0
00238
00239
00240 typedef T Type;
00241
00242 private:
00243 void ref( const T* ptr) const { if(ptr) { reinterpret_cast<const SharedObj*>(ptr)->ref(this); } }
00244 void unref(const T* ptr) const { if(ptr) { reinterpret_cast<const SharedObj*>(ptr)->unref(this); } }
00245
00246 private:
00247 T* m_obj;
00248 };
00249
00250 class WeakPointerBase {
00251 protected:
00252 WeakPointerBase() : m_obj(NULL) { }
00253 WeakPointerBase(const SharedObj* ptr) : m_obj(ptr) { reg(m_obj); }
00254 WeakPointerBase(const WeakPointerBase& wpb) : m_obj(wpb.m_obj) { reg(m_obj); }
00255 WeakPointerBase& operator=(const SharedObj* ptr) { set(ptr); return *this; }
00256 WeakPointerBase& operator=(const WeakPointerBase& wpb) { set(wpb.m_obj); return *this; }
00257 ~WeakPointerBase() { unreg(m_obj); }
00258
00259 protected:
00260 friend class SharedObj;
00261 void setToNULL(const SharedObj* who) const { assert(checkOwner(who)); m_obj = NULL; }
00262
00263 protected:
00264 void set( const SharedObj* ptr) { if(ptr != m_obj) { reg(ptr); unreg(m_obj); m_obj = ptr; } }
00265 void reg( const SharedObj* obj) { if(obj) { obj->registerWO(this); } }
00266 void unreg(const SharedObj* obj) { if(obj) { obj->unregisterWO(this); } }
00267
00268 protected:
00269 mutable const SharedObj* m_obj;
00270
00271 private:
00272 bool checkOwner(const SharedObj* who) const {
00273 if(m_obj == who) { return true; }
00274 return false;
00275 }
00276 };
00277
00278 template<class T>
00279 class WO : public WeakPointerBase {
00280 public:
00282 public:
00283 WO() : WeakPointerBase() { }
00284 WO(T* obj) : WeakPointerBase(reinterpret_cast<const SharedObj*>(obj)) { }
00285 WO(const WO& wo) : WeakPointerBase(wo) { }
00286 WO(const SO<T>& so) : WeakPointerBase(reinterpret_cast<const SharedObj*>(static_cast<const T*>(so))) { }
00293 WO& operator=(T* obj) { WeakPointerBase::set(reinterpret_cast<const SharedObj*>(obj)); return *this; }
00294 WO& operator=(const WO& wo) { WeakPointerBase::operator=(wo); return *this; }
00295 WO& operator=(const SO<T>& so) { WeakPointerBase::set(reinterpret_cast<const SharedObj*>(static_cast<const T*>(so))); return *this; }
00296 #if !defined(_MSC_VER) || (_MSC_VER >= 1300) // all compilers except visual studio < 7.0
00297 template<class S>
00298 WO& operator=(const WO<S>& wo) { return operator=(static_cast<S*>(wo)); }
00299 #endif // all compilers except visual studio < 7.0
00300
00302 bool operator<(const T* obj) const { return (m_obj < obj); }
00303 bool operator<(const WO& wo) const { return (m_obj < wo.m_obj); }
00304 friend bool operator<(const T* obj1, const WO& wo2) { return (obj1 < wo2.m_obj); }
00307 operator T*() const { return const_cast<T*>(reinterpret_cast<const T*>(m_obj)); }
00308 T* operator->() const { assert(((void)"NULL pointer dereferenced", m_obj)); return const_cast<T*>(reinterpret_cast<const T*>(m_obj)); }
00309 T& operator*() const { assert(((void)"NULL pointer dereferenced", m_obj)); return *const_cast<T*>(reinterpret_cast<const T*>(m_obj)); }
00314 template<class S>
00315 operator WO<S>() { return WO<S>(const_cast<T*>(reinterpret_cast<const T*>(m_obj))); }
00319 #if !defined(_MSC_VER) || (_MSC_VER >= 1300) // all compilers except visual studio < 7.0
00320 operator SO<T>() { return SO<T>(const_cast<T*>(reinterpret_cast<const T*>(m_obj))); }
00323 #endif // all compilers except visual studio < 7.0
00324
00325 };
00326
00327 }
00328
00329
00330
00331
00332 template<class S, class T>
00333 inline T* VRS_CastImpl(const VRS::SO<S>& s, T* dummy) {
00334 return VRS_CastImpl(static_cast<S*>(s), dummy);
00335 }
00336
00337 template<class S, class T>
00338 inline T* VRS_GuardedCastImpl(const VRS::SO<S>& s, T* dummy) {
00339 return VRS_GuardedCastImpl(static_cast<S*>(s), dummy);
00340 }
00341
00342 template<class S, class T>
00343 inline T* VRS_CastImpl(const VRS::WO<S>& s, T* dummy) {
00344 return VRS_CastImpl(static_cast<S*>(s), dummy);
00345 }
00346
00347 template<class S, class T>
00348 inline T* VRS_GuardedCastImpl(const VRS::WO<S>& s, T* dummy) {
00349 return VRS_GuardedCastImpl(static_cast<S*>(s), dummy);
00350 }
00351
00352 namespace VRS {
00353
00354 class SerializationManager;
00355
00356 template<class T>
00357 inline void
00358 serialization(
00359 SerializationManager& manager,
00360 const std::string& attributeName,
00361 SO<T>& attributeValue
00362 ) {
00363 SharedObj* ptr = const_cast<SharedObj*>(reinterpret_cast<const SharedObj*>((T*)attributeValue));
00364 manager.serialize(attributeName, ptr, typeid(T).name());
00365 attributeValue = reinterpret_cast<T*>(ptr);
00366 }
00367
00368 template<class T>
00369 inline void
00370 serialization(
00371 SerializationManager& manager,
00372 const std::string& attributeName,
00373 WO<T>& attributeValue
00374 ) {
00375 SO<T> soValue = attributeValue;
00376 serialization(manager, attributeName, soValue);
00377 attributeValue = soValue;
00378 }
00379
00380 template<typename T>
00381 inline void
00382 serialization(
00383 SerializationManager& manager,
00384 const std::string& attributeName,
00385 SO<T>& attributeValue,
00386 void* null
00387 ) {
00388 assert(null == NULL);
00389 serialization(manager, attributeName, attributeValue, SO<T>(NULL));
00390 }
00391
00392 template<typename T>
00393 inline void
00394 serialization(
00395 SerializationManager& manager,
00396 const std::string& attributeName,
00397 WO<T>& attributeValue,
00398 void* null
00399 ) {
00400 assert(null == NULL);
00401 serialization(manager, attributeName, attributeValue, WO<T>(NULL));
00402 }
00403
00404 }
00405
00406
00407
00408
00409
00410
00411
00412
00413 #define VRS_STATIC_SO(TYPE, NAME, ARGS) \
00414 namespace { \
00415 \
00416 void NAME ##_cleanup(); \
00417 \
00418 TYPE* NAME(bool destruct = false) { \
00419 static TYPE* pointer = NULL; \
00420 if(!pointer && !destruct) { \
00421 pointer = new TYPE ARGS; \
00422 pointer->ref(); \
00423 ::atexit(NAME ##_cleanup); \
00424 } else if(pointer && destruct) { \
00425 pointer->unref(); \
00426 pointer = NULL; \
00427 } \
00428 return pointer; \
00429 } \
00430 \
00431 void NAME ##_cleanup() { \
00432 NAME(true); \
00433 } \
00434 \
00435 } // namespace
00436
00437 #endif // VRS_SO_H