util/ReferenceHolder.h

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 Name:      ReferenceHolder.h
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Entry Holder to use with stl containers as containers of references
00006 Version:   $Id: ReferenceHolder.h 13505 2005-07-20 16:22:14Z ruff $
00007 ------------------------------------------------------------------------------*/
00008 
00009 #ifndef _UTIL_REFERENCEHOLDER_H
00010 #define _UTIL_REFERENCEHOLDER_H
00011 
00012 #include <util/XmlBCfg.h>
00013 //
00014 
00015 namespace org { namespace xmlBlaster { namespace util {
00016 
00028 template <class T> class Dll_Export ReferenceHolder
00029 {
00030 private: 
00031    T* element_;
00032 
00033    void init() const
00034    {
00035       if (!element_) return;
00036 //      if (!element_->isShareable()) {
00037 //         element_ = new T(*element_);
00038          // throw an exception here since the class could have virtual methods.
00039 //      }
00040       element_->addReference();
00041    }
00042 
00043 
00044 public:
00045 
00046    ReferenceHolder(T* el)
00047    {
00048       element_ = el;
00049       init();
00050    }
00051 
00052    ReferenceHolder(T& el) : element_(&el)
00053    {
00054       init();
00055    }
00056 
00057    ReferenceHolder(const ReferenceHolder& refHolder)
00058       : element_(refHolder.element_)
00059    {
00060       init();
00061    }
00062 
00063    ReferenceHolder& operator =(const ReferenceHolder& refHolder)
00064    {
00065       if (element_ != refHolder.element_) {
00066          if (element_) element_->removeReference();
00067          element_ = refHolder.element_;
00068          init();
00069       }
00070       return *this;
00071    }
00072 
00073    
00074    ~ReferenceHolder()
00075    {
00076       if (element_) element_->removeReference();
00077    }
00078 
00079 
00080    T& operator *() const 
00081    {
00082       return *element_;
00083    }
00084 
00085    T* operator->() const 
00086    {
00087       return element_;
00088    }
00089 
00090    T* getElement() const
00091    {
00092       return element_;
00093    }
00094 
00095    bool isNull() const
00096    {
00097       return element_ == 0;
00098    }
00099 
00100    bool operator ==(const ReferenceHolder<T> other) const
00101    {
00102       return *element_ == *other.element_;
00103    }
00104 
00105    bool operator <(const ReferenceHolder<T> other) const
00106    {
00107       return *element_ < *other.element_;
00108    }
00109 
00110    /*
00111    bool operator >(const ReferenceHolder<T> other) const
00112    {
00113       return !(this->operator<(other)) && !(this->operator==(other));
00114    }
00115    */
00116 
00117    bool operator >(const ReferenceHolder<T> other) const
00118    {
00119       return !(*element_ < *other.element_) && !(*element_ == *other.element_);
00120    }
00121 
00122 //   friend bool operator== (const ReferenceHolder<T>& lhs, const ReferenceHolder<T>& rhs);
00123 
00124 //   friend bool operator< (const ReferenceHolder<T>& lhs, const ReferenceHolder<T>& rhs);
00125 
00126 };
00127 
00128 /*
00129 template <class T> 
00130 inline bool operator== (const ReferenceHolder<T>&  lhs, const ReferenceHolder<T>& rhs)
00131 {
00132    return *lhs.element_ == *rhs.element_;
00133 }
00134 
00135 template <class T> 
00136 inline bool operator< (const ReferenceHolder<T>&  lhs, const ReferenceHolder<T>& rhs)
00137 {
00138    return *lhs.element_ < *rhs.element_;
00139 }
00140 */
00141 
00142 }}} // namespace
00143 
00144 #endif