CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Ptr.h
Go to the documentation of this file.
1 #ifndef INCLUDE_ORA_PTR_H
2 #define INCLUDE_ORA_PTR_H
3 
4 #include "Exception.h"
5 //
6 #include <assert.h>
7 #include <typeinfo>
8 #include <boost/shared_ptr.hpp>
9 
10 namespace ora {
11 
18  class IPtrLoader {
19 
20  public:
21  // destructor
22  virtual ~IPtrLoader(){}
23 
24  public:
25  // triggers the data loading
26  virtual void* load() const=0;
27 
28  // notify the underlying storage system that the embedded object has been destructed.
29  // maybe not required.
30  //virtual void notify() const=0;
31 
32  // invalidates the current loader. Called by the underlying service at his destruction time.
33  virtual void invalidate()=0;
34 
35  // queries the validity of the current relation with the underlying storage system
36  virtual bool isValid() const=0;
37 
38  };
39 
47  template <typename T> class Ptr {
48 
49  public:
50 
51  // default constructor
52  Ptr();
53 
54  // from a real pointer
55  explicit Ptr(T* anObject);
56 
57  // copy constructor
58  Ptr(const Ptr<T>&);
59 
60  // extended copy constructor
61  template <class C> Ptr(const Ptr<C>&);
62 
63  // destructor
64  virtual ~Ptr();
65 
66  // Assignment operator with real pointer
67  Ptr<T>& operator=(T*);
68 
69  // assignment operator
70  Ptr<T>& operator=(const Ptr<T>&);
71 
72  // extended assignment operator
73  template <class C> Ptr<T>& operator=(const Ptr<C>&);
74 
75  // copy operator for up/down casting
76  template <class C> Ptr<T>& cast(const Ptr<C>&);
77 
78  // dereference operator
79  T* operator->() const;
80 
81  // dereference operator
82  T& operator*() const;
83 
84  // implicit bool conversion
85  operator bool () const;
86 
87  // return the real pointer
88  T* get() const;
89 
90  // return the shared ptr
91  boost::shared_ptr<T>& share() const;
92 
93  // return the naked pointer, without to trigger the loading.
94  void* address() const;
95 
96  // 'not' operator for consistency with pointers common use
97  bool operator!() const;
98 
99  // equality operator
100  template <class C>
101  bool operator==(const Ptr<C>& aPtr) const {
102  return m_ptr == static_cast<C*>(aPtr.address());
103  }
104  template <class C>
105  bool operator!=(const Ptr<C>& aPtr) const {
106  return !(this->operator==(aPtr));
107  }
108 
109  public:
110 
111  // clear the embedded pointer and invalidates the loader,if any.
112  void reset();
113 
114  // returns the current loader
115  boost::shared_ptr<IPtrLoader>& loader() const {
116  return m_loader;
117  }
118 
119  // triggers the loading if the loader is installed
120  void load() const;
121 
122  // returns true if the emebedded object data have been loaded.
123  bool isLoaded() const {
124  return m_isLoaded;
125  }
126 
127  private:
128 
129  // pointer with throw exception clause
130  T* ptr(bool throw_flag) const;
131 
132  private:
133 
134  // embedded object pointer
135  mutable boost::shared_ptr<T> m_ptr;
136 
137  // data loader, istalled by the storage system
138  mutable boost::shared_ptr<IPtrLoader> m_loader;
139 
140  // object loaded flag
141  mutable bool m_isLoaded;
142 
143  };
144 
145 }
146 
147 template <class T>
149  m_ptr(),m_loader(),m_isLoaded(false) {}
150 
151 template <class T>
152 inline ora::Ptr<T>::Ptr(T* anObject) :
153  m_ptr(anObject),m_loader(),m_isLoaded(true) {}
154 
155 template <class T>
156 inline ora::Ptr<T>::Ptr(const Ptr<T>& aPtr) :
157  m_ptr(aPtr.m_ptr),m_loader(aPtr.m_loader),m_isLoaded(false){
158 }
159 
160 template <class T>
161 template <class C>
162 inline ora::Ptr<T>::Ptr(const Ptr<C>& aPtr) :
163  m_ptr(aPtr.share()),m_loader(aPtr.loader()),m_isLoaded(aPtr.isLoaded()) {
164  // compile-time type checking
165  C* c = 0; T* t(c); assert(t==0);
166 }
167 
168 template <class T>
170 }
171 
172 template <class T>
174  m_ptr.reset(aPtr);
175  m_isLoaded = true;
176  return *this;
177 }
178 
179 template <class T>
181  m_loader = aPtr.m_loader;
182  m_isLoaded = aPtr.m_isLoaded;
183  m_ptr = aPtr.m_ptr;
184  return *this;
185 }
186 
187 template <class T>
188 template <class C>
190  C* c = 0; T* t(c); assert(t==0);
191  m_loader = aPtr.loader();
192  m_isLoaded = aPtr.isLoaded();
193  m_ptr = aPtr.share();
194  return *this;
195 }
196 
197 template <class T>
198 template <class C>
199 inline ora::Ptr<T>& ora::Ptr<T>::cast(const Ptr<C>& aPtr){
200  m_loader = aPtr.loader();
201  m_ptr = boost::dynamic_pointer_cast( aPtr.share());
202  m_isLoaded = aPtr.isLoaded();
203  return *this;
204 }
205 
206 template <class T>
207 inline T* ora::Ptr<T>::operator->() const {
208  return ptr(true);
209 }
210 
211 template <class T>
212 inline T& ora::Ptr<T>::operator*() const {
213  return *ptr(true);
214 }
215 
216 template <class T>
217 inline T* ora::Ptr<T>::get() const {
218  return ptr(false);
219 }
220 
221 template <class T>
222 inline boost::shared_ptr<T>& ora::Ptr<T>::share() const {
223  return m_ptr;
224 }
225 
226 template <class T>
227 inline void* ora::Ptr<T>::address() const {
228  return m_ptr.get();
229 }
230 
231 template <class T>
232 inline ora::Ptr<T>::operator bool() const {
233  return ptr(false);
234 }
235 
236 template <class T>
237 inline bool ora::Ptr<T>::operator!() const {
238  return ptr(false)==0;
239 }
240 
241 template <class T>
242 inline void ora::Ptr<T>::reset(){
243  m_ptr.reset();
244  m_loader.reset();
245  m_isLoaded = false;
246 }
247 
248 template <class T>
249 inline void ora::Ptr<T>::load() const {
250  ptr( false );
251 }
252 
253 template <class T>
254 inline T* ora::Ptr<T>::ptr(bool throwFlag) const {
255  if(!m_ptr.get()){
256  if(!m_loader.get()){
257  if(throwFlag) throwException("Loader is not installed.","Ptr::ptr()");
258  }
259  if(!m_isLoaded && m_loader.get()){
260  m_ptr.reset( static_cast<T*>(m_loader->load()));
261  m_isLoaded = true;
262  }
263  }
264  if(!m_ptr.get()){
265  if(throwFlag) throwException("Underlying pointer is null.","Ptr::ptr()");
266  }
267  return m_ptr.get();
268 }
269 
270 #endif
boost::shared_ptr< IPtrLoader > m_loader
Definition: Ptr.h:138
bool operator!=(const Ptr< C > &aPtr) const
Definition: Ptr.h:105
T * get() const
Definition: Ptr.h:217
boost::shared_ptr< T > & share() const
Definition: Ptr.h:222
T * operator->() const
Definition: Ptr.h:207
Ptr< T > & operator=(T *)
Definition: Ptr.h:173
Ptr()
Definition: Ptr.h:148
void reset()
Definition: Ptr.h:242
Definition: Ptr.h:47
T * ptr(bool throw_flag) const
Definition: Ptr.h:254
virtual ~IPtrLoader()
Definition: Ptr.h:22
void * address() const
Definition: Ptr.h:227
bool isLoaded() const
Definition: Ptr.h:123
virtual void invalidate()=0
virtual void * load() const =0
virtual ~Ptr()
Definition: Ptr.h:169
boost::shared_ptr< IPtrLoader > & loader() const
Definition: Ptr.h:115
bool operator!() const
Definition: Ptr.h:237
void load() const
Definition: Ptr.h:249
Ptr< T > & cast(const Ptr< C > &)
virtual bool isValid() const =0
bool m_isLoaded
Definition: Ptr.h:141
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:10
boost::shared_ptr< T > m_ptr
Definition: Ptr.h:135
long double T
T & operator*() const
Definition: Ptr.h:212
bool operator==(const Ptr< C > &aPtr) const
Definition: Ptr.h:101