Ice 3.7 C++11 API Reference
Handle.h
Go to the documentation of this file.
1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef ICE_UTIL_HANDLE_H
6 #define ICE_UTIL_HANDLE_H
7 
8 #include <IceUtil/Exception.h>
9 #include <algorithm>
10 
11 //
12 // "Handle" or "smart pointer" class for classes derived from
13 // IceUtil::Shared, IceUtil::SimpleShared, or IceInternal::GCShared.
14 //
15 namespace IceUtil
16 {
17 
18 template<typename T>
20 {
21 public:
22 
23  typedef T element_type;
24 
25  T* get() const
26  {
27  return _ptr;
28  }
29 
30  T* operator->() const
31  {
32  if(!_ptr)
33  {
34  //
35  // We don't throw directly NullHandleException here to
36  // keep the code size of this method to a minimun (the
37  // assembly code for throwing an exception is much bigger
38  // than just a function call). This maximises the chances
39  // of inlining by compiler optimization.
40  //
41  throwNullHandleException(__FILE__, __LINE__);
42  }
43 
44  return _ptr;
45  }
46 
47  T& operator*() const
48  {
49  if(!_ptr)
50  {
51  //
52  // We don't throw directly NullHandleException here to
53  // keep the code size of this method to a minimun (the
54  // assembly code for throwing an exception is much bigger
55  // than just a function call). This maximises the chances
56  // of inlining by compiler optimization.
57  //
58  throwNullHandleException(__FILE__, __LINE__);
59  }
60 
61  return *_ptr;
62  }
63 
64  operator bool() const
65  {
66  return _ptr ? true : false;
67  }
68 
69  void swap(HandleBase& other)
70  {
71  std::swap(_ptr, other._ptr);
72  }
73 
74  T* _ptr;
75 
76 private:
77 
78  void throwNullHandleException(const char *, int) const;
79 };
80 
81 template<typename T> inline void
82 HandleBase<T>::throwNullHandleException(const char* file, int line) const
83 {
84  throw NullHandleException(file, line);
85 }
86 
87 template<typename T, typename U>
88 inline bool operator==(const HandleBase<T>& lhs, const HandleBase<U>& rhs)
89 {
90  T* l = lhs.get();
91  U* r = rhs.get();
92  if(l && r)
93  {
94  return *l == *r;
95  }
96 
97  // Note: don't use if { } else { }. This causes lots warnings when
98  // compiling with GCC and optimization enabled. See bug 2330.
99  return !l && !r;
100 }
101 
102 template<typename T, typename U>
103 inline bool operator!=(const HandleBase<T>& lhs, const HandleBase<U>& rhs)
104 {
105  return !operator==(lhs, rhs);
106 }
107 
108 template<typename T, typename U>
109 inline bool operator<(const HandleBase<T>& lhs, const HandleBase<U>& rhs)
110 {
111  T* l = lhs.get();
112  U* r = rhs.get();
113  if(l && r)
114  {
115  return *l < *r;
116  }
117 
118  // Note: don't use if { } else { }. This causes lots warnings when
119  // compiling with GCC and optimization enabled. See bug 2330.
120  return !l && r;
121 }
122 
123 template<typename T, typename U>
124 inline bool operator<=(const HandleBase<T>& lhs, const HandleBase<U>& rhs)
125 {
126  return lhs < rhs || lhs == rhs;
127 }
128 
129 template<typename T, typename U>
130 inline bool operator>(const HandleBase<T>& lhs, const HandleBase<U>& rhs)
131 {
132  return !(lhs < rhs || lhs == rhs);
133 }
134 
135 template<typename T, typename U>
136 inline bool operator>=(const HandleBase<T>& lhs, const HandleBase<U>& rhs)
137 {
138  return !(lhs < rhs);
139 }
140 
141 template<typename T>
142 class Handle : public HandleBase<T>
143 {
144 public:
145 
146  Handle(T* p = 0)
147  {
148  this->_ptr = p;
149 
150  if(this->_ptr)
151  {
152  this->_ptr->__incRef();
153  }
154  }
155 
156  template<typename Y>
157  Handle(const Handle<Y>& r)
158  {
159  this->_ptr = r._ptr;
160 
161  if(this->_ptr)
162  {
163  this->_ptr->__incRef();
164  }
165  }
166 
167  Handle(const Handle& r)
168  {
169  this->_ptr = r._ptr;
170 
171  if(this->_ptr)
172  {
173  this->_ptr->__incRef();
174  }
175  }
176 
178  {
179  if(this->_ptr)
180  {
181  this->_ptr->__decRef();
182  }
183  }
184 
186  {
187  if(this->_ptr != p)
188  {
189  if(p)
190  {
191  p->__incRef();
192  }
193 
194  T* ptr = this->_ptr;
195  this->_ptr = p;
196 
197  if(ptr)
198  {
199  ptr->__decRef();
200  }
201  }
202  return *this;
203  }
204 
205  template<typename Y>
207  {
208  if(this->_ptr != r._ptr)
209  {
210  if(r._ptr)
211  {
212  r._ptr->__incRef();
213  }
214 
215  T* ptr = this->_ptr;
216  this->_ptr = r._ptr;
217 
218  if(ptr)
219  {
220  ptr->__decRef();
221  }
222  }
223  return *this;
224  }
225 
227  {
228  if(this->_ptr != r._ptr)
229  {
230  if(r._ptr)
231  {
232  r._ptr->__incRef();
233  }
234 
235  T* ptr = this->_ptr;
236  this->_ptr = r._ptr;
237 
238  if(ptr)
239  {
240  ptr->__decRef();
241  }
242  }
243  return *this;
244  }
245 
246  template<class Y>
248  {
249  return Handle(dynamic_cast<T*>(r._ptr));
250  }
251 
252  template<class Y>
253  static Handle dynamicCast(Y* p)
254  {
255  return Handle(dynamic_cast<T*>(p));
256  }
257 };
258 
259 }
260 
261 #endif
IceUtil::operator==
bool operator==(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition: Handle.h:88
IceUtil::operator>
bool operator>(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition: Handle.h:130
IceUtil::Handle::dynamicCast
static Handle dynamicCast(const HandleBase< Y > &r)
Definition: Handle.h:247
IceUtil::operator<
bool operator<(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition: Handle.h:109
IceUtil::Handle::operator=
Handle & operator=(T *p)
Definition: Handle.h:185
IceUtil
Definition: Optional.h:1095
IceUtil::Handle::Handle
Handle(const Handle &r)
Definition: Handle.h:167
IceUtil::NullHandleException
This exception indicates an attempt to dereference a null IceUtil::Handle or IceInternal::Handle.
Definition: Exception.h:186
IceUtil::Handle::dynamicCast
static Handle dynamicCast(Y *p)
Definition: Handle.h:253
IceUtil::operator>=
bool operator>=(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition: Handle.h:136
IceUtil::HandleBase::swap
void swap(HandleBase &other)
Definition: Handle.h:69
IceUtil::Handle::~Handle
~Handle()
Definition: Handle.h:177
IceUtil::operator<=
bool operator<=(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition: Handle.h:124
Exception.h
IceUtil::Handle::Handle
Handle(T *p=0)
Definition: Handle.h:146
IceUtil::HandleBase::_ptr
T * _ptr
Definition: Handle.h:74
IceUtil::Handle::Handle
Handle(const Handle< Y > &r)
Definition: Handle.h:157
IceUtil::HandleBase
Definition: Handle.h:20
IceUtil::HandleBase::element_type
T element_type
Definition: Handle.h:23
IceUtil::HandleBase::get
T * get() const
Definition: Handle.h:25
IceUtil::Handle
Definition: Handle.h:143
IceUtil::HandleBase::operator*
T & operator*() const
Definition: Handle.h:47
IceUtil::Handle::operator=
Handle & operator=(const Handle &r)
Definition: Handle.h:226
IceUtil::operator!=
bool operator!=(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition: Handle.h:103
IceUtil::Handle::operator=
Handle & operator=(const Handle< Y > &r)
Definition: Handle.h:206
IceUtil::HandleBase::operator->
T * operator->() const
Definition: Handle.h:30