00001 /* SimData: Data Infrastructure for Simulations 00002 * Copyright (C) 2002 Mark Rose <tm2@stm.lbl.gov> 00003 * 00004 * This file is part of SimData. 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00019 */ 00020 00021 00029 #ifndef __SIMDATA_PATH_H__ 00030 #define __SIMDATA_PATH_H__ 00031 00032 00033 #if defined(_MSC_VER) && (_MSC_VER <= 1300) 00034 #pragma warning (disable : 4290) 00035 #endif 00036 00037 00038 #if defined(_MSC_VER) && (_MSC_VER <= 1300) 00039 #include <cassert> 00040 #endif 00041 00042 #include <string> 00043 00044 00045 #include <SimData/HashUtility.h> 00046 #include <SimData/BaseType.h> 00047 00048 00049 NAMESPACE_SIMDATA 00050 00051 00052 class DataArchive; 00053 00054 typedef hasht ObjectID; 00055 typedef hasht_eq ObjectID_eq; 00056 typedef hasht_hash ObjectID_hash; 00057 00058 00059 SIMDATA_EXCEPTION(ObjectTypeMismatch) 00060 00061 00062 00063 00073 class SIMDATA_EXPORT Path: public BaseType { 00074 protected: 00075 ObjectID _path; 00076 public: 00077 00081 explicit Path(const char* path=0) { 00082 setPath(path); 00083 } 00084 00088 explicit Path(ObjectID path): _path(path) { 00089 } 00090 00091 virtual ~Path() { 00092 } 00093 00097 inline void setPath(ObjectID path) { 00098 _path = path; 00099 } 00100 00105 void setPath(const char* path); 00106 00110 inline void setNone() { 00111 setPath((ObjectID) 0); 00112 } 00113 00117 inline const ObjectID getPath() const { 00118 return _path; 00119 } 00120 00123 virtual void serialize(Archive&); 00124 00128 inline bool isNone() const { 00129 return (_path == (ObjectID) 0); 00130 } 00131 00134 inline bool operator==(Path const &p) const { 00135 return _path == p._path; 00136 } 00137 00140 inline bool operator!=(Path const &p) const { 00141 return _path != p._path; 00142 } 00143 00147 virtual std::string asString() const; 00148 00152 virtual std::string typeString() const { return "type::Path"; } 00153 }; 00154 00155 #if 0 00156 00157 NAMESPACE_SIMDATA_END 00158 00159 #include <SimData/Object.h> 00160 00161 NAMESPACE_SIMDATA 00162 00163 00164 class SIMDATA_EXPORT ReferencePointer { 00165 public: 00166 00167 // SWIG python specific comparisons 00168 bool __eq__(const ReferencePointer& other); 00169 bool __ne__(const ReferencePointer& other); 00170 00175 explicit ReferencePointer(): _reference(0) { } 00176 00180 explicit ReferencePointer(Object* ptr): _reference(0) { 00181 _assign_safe(ptr); 00182 } 00183 00187 explicit ReferencePointer(const ReferencePointer& r): _reference(0) { 00188 _assign_safe(r._reference); 00189 } 00190 00195 virtual ~ReferencePointer() { 00196 _release(); 00197 } 00198 00202 inline bool ReferencePointer::unique() const { 00203 return (_reference ? _reference->_count()==1 : true); 00204 } 00205 00209 #ifndef SWIG 00210 inline ReferencePointer& operator=(const ReferencePointer& r) { 00211 _assign_safe(r._reference); 00212 return *this; 00213 } 00214 #endif 00215 00219 #ifndef SWIG 00220 inline void *operator=(void *p) { 00221 assert(p==0); 00222 _reference = 0; 00223 return p; 00224 } 00225 #endif 00226 00230 inline void setNull() { 00231 _assign_fast(0); 00232 } 00233 00234 00238 inline bool isNull() const { 00239 return _reference == 0; 00240 } 00241 00245 #ifndef SWIG 00246 inline bool operator!() const { 00247 return _reference == 0; 00248 } 00249 #endif 00250 00254 inline bool valid() const { 00255 return _reference != 0; 00256 } 00257 00261 inline bool operator==(ReferencePointer const &p) const { 00262 return _reference == p._reference; 00263 } 00264 00265 virtual Object *__get__() { return _reference; } 00266 00267 protected: 00275 inline void _assign_safe(Object* ptr) { 00276 _release(); 00277 _update(ptr); 00278 if (!isNull()) _reference->_ref(); 00279 } 00280 00287 inline void _assign_fast(Object *ptr) { 00288 _release(); 00289 _reference = ptr; 00290 if (!isNull()) _reference->_ref(); 00291 } 00292 00296 inline void _release() { 00297 if (!isNull()) { 00298 _reference->_deref(); 00299 _reference = 0; 00300 } 00301 } 00302 00309 virtual void _update(Object* p) { _reference = p; } 00310 00314 inline Object* _get() const { 00315 return _reference; 00316 } 00317 00321 Object* _reference; 00322 }; 00323 00324 00335 class SIMDATA_EXPORT PointerBase: public Path, public ReferencePointer { 00336 template <class T> friend class Ref; 00337 public: 00338 00339 // SWIG python specific comparisons 00340 bool __eq__(const PointerBase& other); 00341 bool __ne__(const PointerBase& other); 00342 00347 explicit PointerBase(): Path((const char*)0), ReferencePointer() { } 00348 00352 PointerBase(const char* path): Path(path), ReferencePointer() { } 00353 00357 explicit PointerBase(const Path& path, Object* ptr): 00358 Path(path), ReferencePointer(ptr) { } 00359 00363 explicit PointerBase(Object* ptr): Path(), ReferencePointer(ptr) {} 00364 00369 virtual ~PointerBase() { } 00370 00374 PointerBase(const PointerBase& r): Path(r.getPath()), ReferencePointer(r) { 00375 // *this = r; 00376 } 00377 00381 PointerBase& operator=(const PointerBase& r) { 00382 ReferencePointer::operator=(r); 00383 _path = r.getPath(); 00384 return *this; 00385 } 00386 n 00399 virtual void serialize(Archive&); 00400 00404 virtual std::string asString() const; 00405 00409 virtual std::string typeString() const { return "type::PointerBase" } 00410 00414 bool operator==(PointerBase const &p) const { 00415 return ReferencePointer::operator==(p); 00416 } 00417 00418 protected: 00425 void _load(DataArchive* archive, ObjectID path=0); 00426 00427 }; 00428 00429 00452 template<class T> class Pointer: public PointerBase { 00453 public: 00454 00455 typedef std::vector< Pointer<T> > vector; 00456 00460 explicit Pointer(): PointerBase() {} 00461 00465 explicit Pointer(const Path& path, T* ptr): PointerBase(path, ptr) {} 00466 00470 explicit Pointer(const char* path): PointerBase(path) {} 00471 00475 explicit Pointer(T* t): PointerBase() { 00476 *this = t; 00477 } 00478 00479 // fast copy 00480 Pointer(const Pointer<T>& p) { 00481 _path = p.getPath(); 00482 _assign_fast(p._reference); 00483 } 00484 00485 // safe copy 00486 Pointer(const PointerBase& p) { 00487 PointerBase::operator=(p); 00488 } 00489 00490 /* 00491 Pointer<T>& operator=(const PointerBase& p) { 00492 PointerBase::operator=(p); 00493 } 00494 */ 00495 00496 #ifndef SWIG 00497 00500 T *operator =(T *t) { 00501 _path = 0; 00502 _assign_fast(t); 00503 return t; 00504 } 00505 #endif // SWIG 00506 00510 T* operator->() { 00511 return (T*) _reference; 00512 } 00513 00517 const T* operator->() const { 00518 return (T*) _reference; 00519 } 00520 00524 T& operator*() { 00525 return *((T*) _reference); 00526 } 00527 00531 const T& operator*() const { 00532 return *((T*) _reference); 00533 } 00534 00538 const T* get() const { 00539 return (T*) _reference; 00540 } 00541 00545 T* get() { 00546 return (T*) _reference; 00547 } 00548 00549 protected: 00554 virtual void _update(Object* ptr) throw(ObjectTypeMismatch) { 00555 PointerBase::_update(ptr); 00556 T* _special = dynamic_cast<T*>(ptr); 00557 if (ptr != 0 && _special == 0) 00558 throw ObjectTypeMismatch("dynamic_cast<> failed in Pointer<>::_update()"); 00559 } 00560 }; 00561 00562 #endif // 0 00563 00564 NAMESPACE_SIMDATA_END 00565 00566 #endif //__SIMDATA_PATH_H__ 00567
|
SimData version pre-0.4.0. For more information on SimData, visit the SimData Homepage. Generated on Tue Oct 14 12:06:39 2003, using Doxygen 1.2.18. |