Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Link.h

Go to the documentation of this file.
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 
00028 #ifndef __SIMDATA_LINK_H__
00029 #define __SIMDATA_LINK_H__
00030 
00031 
00032 #if defined(_MSC_VER) && (_MSC_VER <= 1300)
00033 #pragma warning (disable : 4290)
00034 #endif
00035 
00036 
00037 #include <SimData/Export.h>
00038 #include <SimData/Path.h>
00039 #include <SimData/Object.h>
00040 
00041 #include <string>
00042 #include <cassert>
00043 
00044 
00045 
00046 NAMESPACE_SIMDATA
00047 
00048 
00049 class DataArchive;
00050 
00051 
00057 class SIMDATA_EXPORT ReferencePointer {
00058 public:
00059 
00060         // SWIG python specific comparisons
00061         bool __eq__(const ReferencePointer& other);
00062         bool __ne__(const ReferencePointer& other); 
00063         
00067         explicit ReferencePointer(): _reference(0) { }
00068 
00071         explicit ReferencePointer(Object* ptr): _reference(0) {
00072                 _assign_safe(ptr);
00073         }
00074 
00077         explicit ReferencePointer(const ReferencePointer& r): _reference(0) {
00078                 _assign_safe(r._reference);
00079         }
00080 
00084         virtual ~ReferencePointer() {
00085                 _release();
00086         }
00087 
00090         inline bool unique() const {
00091                 return (_reference ? _reference->_count()==1 : true);
00092         }
00093 
00094 #ifndef SWIG
00095 
00097         inline ReferencePointer& operator=(const ReferencePointer& r) {
00098                 _assign_safe(r._reference);
00099                 return *this;
00100         }
00101 #endif
00102 
00103 #ifndef SWIG
00104 
00106         inline void *operator=(void *p) {
00107                 assert(p==0);
00108                 _reference = 0;
00109                 return p;
00110         }
00111 #endif
00112 
00115         inline void setNull() {
00116                 _assign_fast(0);
00117         }
00118 
00119 
00122         inline bool isNull() const { 
00123                 return _reference == 0; 
00124         }
00125 
00128 #ifndef SWIG
00129         inline bool operator!() const {
00130                 return _reference == 0;
00131         }
00132 #endif
00133 
00136         inline bool valid() const {
00137                 return _reference != 0;
00138         }
00139         
00142         inline bool operator==(ReferencePointer const &p) const {
00143                 return _reference == p._reference;
00144         }
00145 
00146         virtual Object *__get__() { return _reference; }
00147 
00148 protected:
00155         inline void _assign_safe(Object* ptr) {
00156                 _release();
00157                 _update(ptr);
00158                 if (!isNull()) _reference->_ref();
00159         }
00160 
00166         inline void _assign_fast(Object *ptr) {
00167                 _release();
00168                 _reference = ptr;
00169                 if (!isNull()) _reference->_ref();
00170         }
00171 
00174         inline void _release() {
00175                 if (!isNull()) {
00176                         _reference->_deref();
00177                         _reference = 0;
00178                 }
00179         }
00180 
00186         virtual void _update(Object* p) { _reference = p; }
00187 
00190         inline Object* _get() const { 
00191                 return _reference; 
00192         }
00193 
00196         Object* _reference;
00197 };
00198 
00199 
00209 class SIMDATA_EXPORT LinkBase: public Path, public ReferencePointer {
00210         template <class T> friend class Ref;
00211 public:
00212 
00213         // SWIG python specific comparisons
00214         bool __eq__(const LinkBase& other);
00215         bool __ne__(const LinkBase& other); 
00216         
00220         explicit LinkBase(): Path((const char*)0), ReferencePointer() { }
00221 
00224         LinkBase(const char* path): Path(path), ReferencePointer() { }
00225 
00228         explicit LinkBase(const Path& path, Object* ptr): 
00229                 Path(path), ReferencePointer(ptr) { }
00230         
00233         explicit LinkBase(Object* ptr): Path(), ReferencePointer(ptr) {}
00234 
00238         virtual ~LinkBase() { }
00239 
00242         LinkBase(const LinkBase& r): Path(r.getPath()), ReferencePointer(r) {
00243         }
00244 
00245 #ifndef SWIG
00246 
00248         LinkBase& operator=(const LinkBase& r) {
00249                 ReferencePointer::operator=(r);
00250                 _path = r.getPath();
00251                 return *this;
00252         }
00253 #endif
00254 
00255 
00268         virtual void serialize(Archive&);
00269 
00272         virtual std::string asString() const;
00273 
00276         virtual std::string typeString() const { return std::string("type::LinkBase"); }
00277         
00280         bool operator==(LinkBase const &p) const {
00281                 return ReferencePointer::operator==(p);
00282         }
00283 
00284 protected:
00290         void _load(DataArchive* archive, ObjectID path=0);
00291 
00292 };
00293 
00294 
00310 template<class T> class Link: public LinkBase {
00311 public:
00312 
00315         typedef std::vector< Link<T> > vector;
00316 
00319         explicit Link(): LinkBase() {}
00320         
00323         explicit Link(const Path& path, T* ptr): LinkBase(path, ptr) {}
00324         
00327         explicit Link(const char* path): LinkBase(path) {}
00328 
00331         explicit Link(T* t): LinkBase() {
00332                 *this = t;
00333         }
00334 
00337         Link(const Link<T>& p) { 
00338                 _path = p.getPath();
00339                 _assign_fast(p._reference);
00340         }
00341         
00344         Link(const LinkBase& p) { 
00345                 LinkBase::operator=(p);
00346         }
00347         
00348         /*
00349         Link<T>& operator=(const LinkBase& p) {
00350                 LinkBase::operator=(p);
00351         }
00352         */
00353 
00356         virtual std::string typeString() const { return std::string("type::Link::") + T::_getClassName(); }
00357 
00358 #ifndef SWIG
00359 
00361         T *operator =(T *t) {
00362                 _path = 0;
00363                 _assign_fast(t);
00364                 return t;
00365         }
00366 #endif // SWIG
00367 
00370         T* operator->() { 
00371                 return (T*) _reference;
00372         }
00373         
00376         const T* operator->() const { 
00377                 return (T*) _reference;
00378         }
00379         
00382         T& operator*() {
00383                 return *((T*) _reference);
00384         }
00385         
00388         const T& operator*() const {
00389                 return *((T*) _reference);
00390         }
00391 
00394         const T* get() const {
00395                 return (T*) _reference;
00396         }
00397         
00400         T* get() {
00401                 return (T*) _reference;
00402         }
00403         
00404 protected:
00410         virtual void _update(Object* ptr) throw(ObjectTypeMismatch) {
00411                 LinkBase::_update(ptr);
00412                 T* _special = dynamic_cast<T*>(ptr);
00413                 if (ptr != 0 && _special == 0) 
00414                         throw ObjectTypeMismatch("dynamic_cast<> failed in Link<>::_update()");
00415         }
00416 };
00417 
00418 
00419 
00420 NAMESPACE_SIMDATA_END
00421 
00422 #endif //__SIMDATA_LINK_H__
00423 

SimData version pre-0.4.0. For more information on SimData, visit the SimData Homepage.

Generated on Tue Oct 14 12:06:38 2003, using Doxygen 1.2.18.

[SF.net]