CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CondCore/ORA/interface/NamedRef.h

Go to the documentation of this file.
00001 #ifndef INCLUDE_ORA_NAMEDREF_H
00002 #define INCLUDE_ORA_NAMEDREF_H
00003 
00004 #include "Exception.h"
00005 //
00006 #include <assert.h>
00007 #include <boost/shared_ptr.hpp>
00008 
00009 namespace ora {
00010   
00011   class NamedReference {
00012     public:
00013     NamedReference();
00014     explicit NamedReference( const std::string& name );
00015     NamedReference( const std::string& name, boost::shared_ptr<void> ptr );
00016     NamedReference( const NamedReference& rhs );
00017     virtual ~NamedReference();
00018     NamedReference& operator=( const NamedReference& rhs );
00019     void set( const std::string& name );
00020     const std::string& name() const;
00021     bool isPersistent() const;
00022     boost::shared_ptr<void>& ptr() const;
00023     void reset();
00024     private:
00025     std::string m_name;
00026     bool m_isPersistent;
00027     mutable boost::shared_ptr<void> m_ptr;
00028   };
00029 
00030   template <typename T> class NamedRef : public NamedReference {
00031   public:
00032     NamedRef();
00033     NamedRef( const std::string& name );
00034     NamedRef( const std::string& name, boost::shared_ptr<T>& data );
00035     template <typename C> NamedRef( const std::string& name, boost::shared_ptr<C>& data );
00036     NamedRef( const NamedRef<T>& rhs );
00037     template <typename C> NamedRef( const NamedRef<C>& rhs );
00038     virtual ~NamedRef();
00039     NamedRef<T>& operator=( const NamedRef<T>& rhs );    
00040     template <typename C> NamedRef<T>& operator=( const NamedRef<C>& rhs );
00041     T* operator->() const;
00042     T& operator*() const;
00043     operator bool () const;
00044     T* get() const;
00045     boost::shared_ptr<T> share() const;
00046     bool operator!() const;
00047     bool operator==(const NamedRef<T>& rhs) const;
00048     bool operator!=(const NamedRef<T>& rhs) const;
00049     template <class C>
00050     bool operator==(const NamedRef<C>& rhs) const;
00051     template <class C>
00052     bool operator!=(const NamedRef<C>& rhs) const;
00053   private:
00054     T* safePtr( bool throw_flag ) const;
00055 
00056   };
00057   
00058 }
00059 
00060 template <class T>
00061 inline ora::NamedRef<T>::NamedRef() :
00062   NamedReference(){}
00063 
00064 template <class T>
00065 inline ora::NamedRef<T>::NamedRef( const std::string& name ) :
00066   NamedReference( name ){}
00067 
00068 template <class T>
00069 inline ora::NamedRef<T>::NamedRef( const std::string& name, boost::shared_ptr<T>& data ) :
00070   NamedReference( name, boost::shared_ptr<void>( data ) ){}
00071 
00072 template <class T>
00073 template <class C>
00074 inline ora::NamedRef<T>::NamedRef( const std::string& name, boost::shared_ptr<C>& data ) :
00075   NamedReference( name, boost::shared_ptr<void>( data )){
00076 }
00077 
00078 template <class T>
00079 inline ora::NamedRef<T>::NamedRef(const NamedRef<T>& rhs) :
00080   NamedReference( rhs.name() ){
00081 }
00082 
00083 template <class T>
00084 template <class C>
00085 inline ora::NamedRef<T>::NamedRef(const NamedRef<C>& rhs) :
00086   NamedReference( rhs.name() ){
00087 }
00088 
00089 template <class T>
00090 inline ora::NamedRef<T>::~NamedRef() {
00091 }
00092 
00093 template <class T>
00094 inline ora::NamedRef<T>& ora::NamedRef<T>::operator=(const NamedRef<T>& rhs ){
00095   NamedReference::operator=( rhs );
00096   return *this;  
00097 }
00098 
00099 template <class T> 
00100 template <class C>
00101 inline ora::NamedRef<T>& ora::NamedRef<T>::operator=(const NamedRef<C>&  rhs ){
00102   NamedReference::operator=( rhs );
00103   return *this;  
00104 }
00105 
00106 template <class T>
00107 T* ora::NamedRef<T>::safePtr( bool throw_flag ) const {
00108   T* p = share().get();
00109   if( !p && throw_flag) throwException( "Underlying pointer is null.","NamedRef::safePtr"); 
00110   return p;
00111 }
00112 
00113 template <class T>
00114 inline T* ora::NamedRef<T>::operator->() const {
00115   return safePtr( true );
00116 }
00117 
00118 template <class T>
00119 inline T& ora::NamedRef<T>::operator*() const {
00120   return *safePtr( true );
00121 }
00122 
00123 template <class T>
00124 inline ora::NamedRef<T>::operator bool() const {
00125   return safePtr(false);
00126 }
00127 
00128 template <class T>
00129 inline T* ora::NamedRef<T>::get() const  {
00130   return safePtr(false);  
00131 }
00132 
00133 template <class T>
00134 inline boost::shared_ptr<T> ora::NamedRef<T>::share() const {
00135   return boost::static_pointer_cast<T>(ptr());
00136 }
00137 
00138 template <class T>
00139 inline bool ora::NamedRef<T>::operator!() const {
00140   return safePtr(false)==0;
00141 }
00142 
00143 template <class T>
00144 bool ora::NamedRef<T>::operator==(const NamedRef<T>& rhs) const {
00145   return share() == rhs.share();
00146 }
00147 
00148 template <class T>
00149 bool ora::NamedRef<T>::operator!=(const NamedRef<T>& rhs ) const {
00150   return !(this->operator==(rhs));     
00151 }
00152 
00153 template <class T>
00154 template <class C>
00155 bool ora::NamedRef<T>::operator==(const NamedRef<C>& rhs) const {
00156   return share() == rhs.share();
00157 }
00158 
00159 template <class T>
00160 template <class C>
00161 bool ora::NamedRef<T>::operator!=(const NamedRef<C>& rhs ) const {
00162   return !(this->operator==(rhs));     
00163 }
00164 
00165 #endif
00166   
00167     
00168