CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
UniqueRef.h
Go to the documentation of this file.
1 #ifndef INCLUDE_ORA_UNIQUEREF_H
2 #define INCLUDE_ORA_UNIQUEREF_H
3 
4 #include "Ptr.h"
5 
6 namespace ora {
7 
13  template <typename T> class UniqueRef {
14 
15  public:
16 
17  // default constructor
18  UniqueRef();
19 
20  // from a real pointer
21  explicit UniqueRef(T* anObject);
22 
23  // copy constructor
24  UniqueRef(const UniqueRef<T>&);
25 
26  // extended copy constructor
27  template <class C> UniqueRef(const UniqueRef<C>&);
28 
29  // destructor
30  virtual ~UniqueRef();
31 
32  // Assignment operator with real pointer
34 
35  // assignment operator
37 
38  // extended assignment operator
39  template <class C> UniqueRef<T>& operator=(const UniqueRef<C>&);
40 
41  // copy operator for up/down casting
42  template <class C> UniqueRef<T>& cast(const UniqueRef<C>&);
43 
44  // dereference operator
45  T* operator->() const;
46 
47  // dereference operator
48  T& operator*() const;
49 
50  // implicit bool conversion
51  operator bool () const;
52 
53  // return the real pointer
54  T* get() const;
55 
56  // return the shared ptr
57  boost::shared_ptr<T>& share() const;
58 
59  // return the naked pointer, without to trigger the loading.
60  void* address() const;
61 
62  // return the real ptr type
63  const std::type_info* typeInfo() const;
64 
65  // 'not' operator for consistency with pointers common use
66  bool operator!() const;
67 
68  // equality operator
69  template <class C>
70  bool operator==(const UniqueRef<C>& aPtr) const {
71  return m_ptr == static_cast<C*>(aPtr.address());
72  }
73  template <class C>
74  bool operator!=(const UniqueRef<C>& aPtr) const {
75  return !(this->operator==(aPtr));
76  }
77 
78  public:
79 
80  // clear the embedded pointer and invalidates the loader,if any.
81  void reset();
82 
83  // returns the current loader
84  boost::shared_ptr<IPtrLoader>& loader() const {
85  return m_loader;
86  }
87 
88  // returns true if the emebedded object data have been loaded.
89  bool isLoaded() const {
90  return m_isLoaded;
91  }
92 
93  private:
94 
95  // pointer with throw exception clause
96  T* ptr(bool throw_flag) const;
97 
98  private:
99 
100  // embedded object pointer
101  mutable boost::shared_ptr<T> m_ptr;
102 
103  // data loader, istalled by the storage system
104  mutable boost::shared_ptr<IPtrLoader> m_loader;
105 
106  // object loaded flag
107  mutable bool m_isLoaded;
108 
109  };
110 
111 
112 }
113 
114 template <class T> ora::UniqueRef<T>::UniqueRef() :
115  m_ptr(),m_loader(),m_isLoaded(false) {}
116 
117 template <class T> ora::UniqueRef<T>::UniqueRef(T* anObject) :
118  m_ptr(anObject),m_loader(),m_isLoaded(true) {}
119 
120 template <class T> ora::UniqueRef<T>::UniqueRef(const UniqueRef<T>& aPtr) :
121  m_ptr(aPtr.m_ptr),m_loader(aPtr.m_loader),m_isLoaded(false){
122 }
123 
124 template <class T>
125 template <class C> ora::UniqueRef<T>::UniqueRef(const UniqueRef<C>& aPtr) :
126  m_ptr(aPtr.share()),m_loader(aPtr.loader()),m_isLoaded(aPtr.isLoaded()) {
127  // compile-time type checking
128  C* c = 0; T* t(c); assert(t==0);
129 }
130 
131 template <class T> ora::UniqueRef<T>::~UniqueRef() {
132 }
133 
135  reset();
136  m_ptr.reset(aPtr);
137  m_isLoaded = true;
138  return *this;
139 }
140 
142  reset();
143  m_loader = aPtr.m_loader;
144  m_ptr = aPtr.m_ptr;
145  m_isLoaded = aPtr.m_isLoaded;
146  return *this;
147 }
148 
149 template <class T>
151  C* c = 0; T* t(c); assert(t==0);
152  reset();
153  m_loader = aPtr.loader();
154  m_ptr = aPtr.share();
155  m_isLoaded = aPtr.isLoaded();
156  return *this;
157 }
158 
159 template <class T> template <class C> ora::UniqueRef<T>& ora::UniqueRef<T>::cast(const UniqueRef<C>& aPtr){
160  reset();
161  m_loader = aPtr.loader();
162  m_ptr = boost::dynamic_pointer_cast(aPtr.share());
163  m_isLoaded = aPtr.isLoaded();
164  return *this;
165 }
166 
167 template <class T> T* ora::UniqueRef<T>::operator->() const {
168  return ptr(true);
169 }
170 
171 template <class T> T& ora::UniqueRef<T>::operator*() const {
172  return *ptr(true);
173 }
174 
175 template <class T> T* ora::UniqueRef<T>::get() const {
176  return ptr(false);
177 }
178 
179 template <class T>
180 inline boost::shared_ptr<T>& ora::UniqueRef<T>::share() const {
181  return m_ptr;
182 }
183 
184 template <class T> void* ora::UniqueRef<T>::address() const {
185  return m_ptr.get();
186 }
187 
188 template <class T> const std::type_info* ora::UniqueRef<T>::typeInfo() const {
189  const std::type_info* ret = 0;
190  if(m_ptr) ret = &typeid(*m_ptr);
191  return ret;
192 }
193 
194 template <class T> ora::UniqueRef<T>::operator bool() const {
195  return ptr(false);
196 }
197 
198 template <class T> bool ora::UniqueRef<T>::operator!() const {
199  return ptr(false)==0;
200 }
201 
202 template <class T> void ora::UniqueRef<T>::reset(){
203  m_ptr.reset();
204  m_loader.reset();
205  m_isLoaded = false;
206 }
207 
208 template <class T> T* ora::UniqueRef<T>::ptr(bool throwFlag) const {
209  if(!m_ptr.get()){
210  if(!m_loader.get()){
211  if(throwFlag) throwException("Loader is not installed.",
212  "UniqueRef::ptr()");
213  }
214  if(!m_isLoaded && m_loader.get()){
215  m_ptr.reset( static_cast<T*>(m_loader->load()));
216  m_isLoaded = true;
217  }
218  }
219  if(!m_ptr.get()){
220  if(throwFlag) throwException("Underlying pointer is null.",
221  "UniqueRef::ptr()");
222  }
223  return m_ptr.get();
224 }
225 
226 #endif
boost::shared_ptr< IPtrLoader > & loader() const
Definition: UniqueRef.h:84
const std::type_info * typeInfo() const
Definition: UniqueRef.h:188
void * address() const
Definition: UniqueRef.h:184
UniqueRef< T > & operator=(T *)
Definition: UniqueRef.h:134
virtual ~UniqueRef()
Definition: UniqueRef.h:131
T & operator*() const
Definition: UniqueRef.h:171
boost::shared_ptr< T > m_ptr
Definition: UniqueRef.h:101
T * ptr(bool throw_flag) const
Definition: UniqueRef.h:208
void reset()
Definition: UniqueRef.h:202
bool operator==(const UniqueRef< C > &aPtr) const
Definition: UniqueRef.h:70
T * operator->() const
Definition: UniqueRef.h:167
bool operator!() const
Definition: UniqueRef.h:198
bool isLoaded() const
Definition: UniqueRef.h:89
boost::shared_ptr< T > & share() const
Definition: UniqueRef.h:180
UniqueRef< T > & cast(const UniqueRef< C > &)
boost::shared_ptr< IPtrLoader > m_loader
Definition: UniqueRef.h:104
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:10
long double T
void reset(double vett[256])
Definition: TPedValues.cc:11
bool operator!=(const UniqueRef< C > &aPtr) const
Definition: UniqueRef.h:74
T * get() const
Definition: UniqueRef.h:175