CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Handle.h
Go to the documentation of this file.
1 #ifndef INCLUDE_ORA_HANDLE_H
2 #define INCLUDE_ORA_HANDLE_H
3 
5 //
6 #include <memory>
7 #include <boost/shared_ptr.hpp>
8 
9 
10 namespace ora {
11 
12  template <typename T> class Holder {
13  public:
14  Holder();
15  Holder( T* p );
16  ~Holder();
17  std::auto_ptr<T> ptr;
18  };
19 
20  template <typename T> class Handle {
21  public:
22  Handle();
23 
24  explicit Handle(T* ptr);
25 
26  Handle(const Handle<T>& rhs);
27 
28  ~Handle();
29 
30  Handle& operator=(const Handle<T>& rhs);
31 
32  void reset( T* ptr );
33 
34  // dereference operator
35  T* operator->() const;
36 
37  // dereference operator
38  T& operator*() const;
39 
40  // implicit bool conversion
41  operator bool () const;
42 
43  // not operator
44  bool operator!() const;
45 
46  // return the real pointer
47  T* get() const;
48 
49  void clear();
50  private:
51  void validate() const;
52 
53  private:
54  boost::shared_ptr< Holder<T> > m_holder;
55  };
56 }
57 
58 template <typename T>
59 inline
61  ptr(){
62 }
63 
64 template <typename T>
65 inline
67  ptr(p){
68 }
69 
70 template <typename T>
71 inline
73 }
74 
75 template <typename T>
76 inline
78  m_holder( new Holder<T>() ){
79 }
80 
81 template <typename T>
82 inline
84  m_holder( new Holder<T>( ptr ) ){
85 }
86 
87 template <typename T>
88 inline
90  m_holder( rhs.m_holder ){
91 }
92 
93 template <typename T>
94 inline
96 }
97 
98 template <typename T>
99 inline
101  m_holder = rhs.m_holder;
102  return *this;
103 }
104 
105 template <typename T>
106 inline
108  m_holder.reset( new Holder<T>( ptr ) );
109 }
110 
111 template <typename T>
112 inline
114  if(!m_holder->ptr.get()) {
115  throwException( "Handle is not valid.",typeid(Handle<T>),"validate");
116  }
117 }
118 
119 template <typename T>
120 inline
122  validate();
123  return m_holder->ptr.get();
124 }
125 
126 template <typename T>
127 inline
129  validate();
130  return *m_holder->ptr.get();
131 }
132 
133 template <typename T>
134 inline
136  return m_holder->ptr.get()!=0;
137 }
138 
139 template <typename T>
140 inline
142  return m_holder->ptr.get()==0;
143 }
144 
145 template <typename T>
146 inline
148  return m_holder->ptr.get();
149 }
150 
151 template <typename T>
152 inline
154  if(m_holder.get()) m_holder->ptr.reset();
155 }
156 
157 #endif
158 
159 
void validate() const
Definition: Handle.h:113
Holder()
Definition: Handle.h:60
void reset(T *ptr)
Definition: Handle.h:107
Handle()
Definition: Handle.h:77
T & operator*() const
Definition: Handle.h:128
~Handle()
Definition: Handle.h:95
void clear()
Definition: Handle.h:153
bool operator!() const
Definition: Handle.h:141
~Holder()
Definition: Handle.h:72
T * get() const
Definition: Handle.h:147
void throwException(const std::string &message, const std::string &methodName) __attribute__((noreturn))
Definition: Exception.cc:10
boost::shared_ptr< Holder< T > > m_holder
Definition: Handle.h:54
long double T
std::auto_ptr< T > ptr
Definition: Handle.h:17
T * operator->() const
Definition: Handle.h:121
Handle & operator=(const Handle< T > &rhs)
Definition: Handle.h:100