ICE 3.4.2
[php5-ice-freebsdport.git] / cpp / include / Ice / Handle.h
blob0f9bde63edae556035ab3499e8239493c1908ffc
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 #ifndef ICE_HANDLE_H
11 #define ICE_HANDLE_H
13 #include <IceUtil/Handle.h>
14 #include <Ice/Config.h>
17 // We include ProxyHandle.h here to make sure that the Ice::ProxyHandle
18 // template is defined before any definition of upCast().
20 // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25495 for information
21 // on why this is necessary.
23 #include <Ice/ProxyHandle.h>
26 // "Handle" or "smart pointer" template for classes derived from
27 // IceInternal::GCShared, IceUtil::Shared, or IceUtil::SimpleShared.
29 // In constrast to IceUtil::Handle, IceInternal::Handle<T> can be used
30 // for a type T that has been declared but not defined. The only
31 // requirement is a declaration of the following function:
33 // namespace IceInternal
34 // {
35 // X* upCast(T*);
36 // }
38 // Where X is (or derives from) IceUtil::Shared or IceUtil::SimpleShared.
41 namespace IceInternal
44 template<typename T>
45 class Handle : public ::IceUtil::HandleBase<T>
47 public:
49 #if defined(__BCPLUSPLUS__) && (__BCPLUSPLUS__ >= 0x0600)
51 // C++Builder 2009 does not allow setting Ptr to 0.
53 Handle(int p)
55 assert(p == 0);
56 this->_ptr = 0;
58 #endif
60 Handle(T* p = 0)
62 this->_ptr = p;
64 if(this->_ptr)
66 upCast(this->_ptr)->__incRef();
70 template<typename Y>
71 Handle(const Handle<Y>& r)
73 this->_ptr = r._ptr;
75 if(this->_ptr)
77 upCast(this->_ptr)->__incRef();
81 template<typename Y>
82 Handle(const ::IceUtil::Handle<Y>& r)
84 this->_ptr = r._ptr;
86 if(this->_ptr)
88 upCast(this->_ptr)->__incRef();
92 Handle(const Handle& r)
94 this->_ptr = r._ptr;
96 if(this->_ptr)
98 upCast(this->_ptr)->__incRef();
102 ~Handle()
104 if(this->_ptr)
106 upCast(this->_ptr)->__decRef();
110 Handle& operator=(T* p)
112 if(this->_ptr != p)
114 if(p)
116 upCast(p)->__incRef();
119 T* ptr = this->_ptr;
120 this->_ptr = p;
122 if(ptr)
124 upCast(ptr)->__decRef();
127 return *this;
130 template<typename Y>
131 Handle& operator=(const Handle<Y>& r)
133 if(this->_ptr != r._ptr)
135 if(r._ptr)
137 upCast(r._ptr)->__incRef();
140 T* ptr = this->_ptr;
141 this->_ptr = r._ptr;
143 if(ptr)
145 upCast(ptr)->__decRef();
148 return *this;
151 template<typename Y>
152 Handle& operator=(const ::IceUtil::Handle<Y>& r)
154 if(this->_ptr != r._ptr)
156 if(r._ptr)
158 upCast(r._ptr)->__incRef();
161 T* ptr = this->_ptr;
162 this->_ptr = r._ptr;
164 if(ptr)
166 upCast(ptr)->__decRef();
169 return *this;
172 Handle& operator=(const Handle& r)
174 if(this->_ptr != r._ptr)
176 if(r._ptr)
178 upCast(r._ptr)->__incRef();
181 T* ptr = this->_ptr;
182 this->_ptr = r._ptr;
184 if(ptr)
186 upCast(ptr)->__decRef();
189 return *this;
192 template<class Y>
193 static Handle dynamicCast(const ::IceUtil::HandleBase<Y>& r)
195 #ifdef __BCPLUSPLUS__
196 return Handle<T>(dynamic_cast<T*>(r._ptr));
197 #else
198 return Handle(dynamic_cast<T*>(r._ptr));
199 #endif
202 template<class Y>
203 static Handle dynamicCast(Y* p)
205 #ifdef __BCPLUSPLUS__
206 return Handle<T>(dynamic_cast<T*>(p));
207 #else
208 return Handle(dynamic_cast<T*>(p));
209 #endif
212 void __clearHandleUnsafe()
214 this->_ptr = 0;
220 #endif