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_HANDLE_H
6 #define ICE_HANDLE_H
7 
8 #include <IceUtil/Handle.h>
9 #include <Ice/Config.h>
10 
11 //
12 // "Handle" or "smart pointer" template for classes derived from
13 // IceInternal::GCShared, IceUtil::Shared, or IceUtil::SimpleShared.
14 //
15 // In constrast to IceUtil::Handle, IceInternal::Handle<T> can be used
16 // for a type T that has been declared but not defined. The only
17 // requirement is a declaration of the following function:
18 //
19 // namespace IceInternal
20 // {
21 // X* upCast(T*);
22 // }
23 //
24 // Where X is (or derives from) IceUtil::Shared or IceUtil::SimpleShared.
25 //
26 
27 namespace IceInternal
28 {
29 
30 template<typename T>
31 class Handle : public ::IceUtil::HandleBase<T>
32 {
33 public:
34 
35  Handle(T* p = 0)
36  {
37  this->_ptr = p;
38 
39  if(this->_ptr)
40  {
41  upCast(this->_ptr)->__incRef();
42  }
43  }
44 
45  template<typename Y>
46  Handle(const Handle<Y>& r)
47  {
48  this->_ptr = r._ptr;
49 
50  if(this->_ptr)
51  {
52  upCast(this->_ptr)->__incRef();
53  }
54  }
55 
56  template<typename Y>
57  Handle(const ::IceUtil::Handle<Y>& r)
58  {
59  this->_ptr = r._ptr;
60 
61  if(this->_ptr)
62  {
63  upCast(this->_ptr)->__incRef();
64  }
65  }
66 
67  Handle(const Handle& r)
68  {
69  this->_ptr = r._ptr;
70 
71  if(this->_ptr)
72  {
73  upCast(this->_ptr)->__incRef();
74  }
75  }
76 
77  ~Handle()
78  {
79  if(this->_ptr)
80  {
81  upCast(this->_ptr)->__decRef();
82  }
83  }
84 
85  Handle& operator=(T* p)
86  {
87  if(this->_ptr != p)
88  {
89  if(p)
90  {
91  upCast(p)->__incRef();
92  }
93 
94  T* ptr = this->_ptr;
95  this->_ptr = p;
96 
97  if(ptr)
98  {
99  upCast(ptr)->__decRef();
100  }
101  }
102  return *this;
103  }
104 
105  template<typename Y>
106  Handle& operator=(const Handle<Y>& r)
107  {
108  if(this->_ptr != r._ptr)
109  {
110  if(r._ptr)
111  {
112  upCast(r._ptr)->__incRef();
113  }
114 
115  T* ptr = this->_ptr;
116  this->_ptr = r._ptr;
117 
118  if(ptr)
119  {
120  upCast(ptr)->__decRef();
121  }
122  }
123  return *this;
124  }
125 
126  template<typename Y>
127  Handle& operator=(const ::IceUtil::Handle<Y>& r)
128  {
129  if(this->_ptr != r._ptr)
130  {
131  if(r._ptr)
132  {
133  upCast(r._ptr)->__incRef();
134  }
135 
136  T* ptr = this->_ptr;
137  this->_ptr = r._ptr;
138 
139  if(ptr)
140  {
141  upCast(ptr)->__decRef();
142  }
143  }
144  return *this;
145  }
146 
147  Handle& operator=(const Handle& r)
148  {
149  if(this->_ptr != r._ptr)
150  {
151  if(r._ptr)
152  {
153  upCast(r._ptr)->__incRef();
154  }
155 
156  T* ptr = this->_ptr;
157  this->_ptr = r._ptr;
158 
159  if(ptr)
160  {
161  upCast(ptr)->__decRef();
162  }
163  }
164  return *this;
165  }
166 
167  template<class Y>
168  static Handle dynamicCast(const ::IceUtil::HandleBase<Y>& r)
169  {
170  return Handle(dynamic_cast<T*>(r._ptr));
171  }
172 
173  template<class Y>
174  static Handle dynamicCast(Y* p)
175  {
176  return Handle(dynamic_cast<T*>(p));
177  }
178 };
179 
180 }
181 
182 #endif
IceUtil::Shared::__decRef
virtual void __decRef()
Handle.h
IceUtil::HandleBase::_ptr
T * _ptr
Definition: Handle.h:74
IceUtil::HandleBase
Definition: Handle.h:20
Config.h
IceUtil::Shared::__incRef
virtual void __incRef()