Ice 3.7 C++11 API Reference
Loading...
Searching...
No Matches
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//
15namespace IceUtil
16{
17
18template<typename T>
20{
21public:
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
76private:
77
78 void throwNullHandleException(const char *, int) const;
79};
80
81template<typename T> inline void
82HandleBase<T>::throwNullHandleException(const char* file, int line) const
83{
84 throw NullHandleException(file, line);
85}
86
87template<typename T, typename U>
88inline 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
102template<typename T, typename U>
103inline bool operator!=(const HandleBase<T>& lhs, const HandleBase<U>& rhs)
104{
105 return !operator==(lhs, rhs);
106}
107
108template<typename T, typename U>
109inline 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
123template<typename T, typename U>
124inline bool operator<=(const HandleBase<T>& lhs, const HandleBase<U>& rhs)
125{
126 return lhs < rhs || lhs == rhs;
127}
128
129template<typename T, typename U>
130inline bool operator>(const HandleBase<T>& lhs, const HandleBase<U>& rhs)
131{
132 return !(lhs < rhs || lhs == rhs);
133}
134
135template<typename T, typename U>
136inline bool operator>=(const HandleBase<T>& lhs, const HandleBase<U>& rhs)
137{
138 return !(lhs < rhs);
139}
140
141template<typename T>
142class Handle : public HandleBase<T>
143{
144public:
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>
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
Definition Handle.h:20
T * operator->() const
Definition Handle.h:30
T element_type
Definition Handle.h:23
T * _ptr
Definition Handle.h:74
void swap(HandleBase &other)
Definition Handle.h:69
T & operator*() const
Definition Handle.h:47
T * get() const
Definition Handle.h:25
Handle(const Handle< Y > &r)
Definition Handle.h:157
~Handle()
Definition Handle.h:177
static Handle dynamicCast(Y *p)
Definition Handle.h:253
Handle & operator=(const Handle< Y > &r)
Definition Handle.h:206
Handle & operator=(T *p)
Definition Handle.h:185
static Handle dynamicCast(const HandleBase< Y > &r)
Definition Handle.h:247
Handle(const Handle &r)
Definition Handle.h:167
Handle & operator=(const Handle &r)
Definition Handle.h:226
Handle(T *p=0)
Definition Handle.h:146
This exception indicates an attempt to dereference a null IceUtil::Handle or IceInternal::Handle.
Definition Exception.h:186
Definition Optional.h:1095
bool operator>(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition Handle.h:130
bool operator<(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition Handle.h:109
bool operator<=(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition Handle.h:124
bool operator>=(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition Handle.h:136
bool operator!=(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition Handle.h:103
bool operator==(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition Handle.h:88