// ********************************************************************** // // Copyright (c) 2003 - 2004 // ZeroC, Inc. // North Palm Beach, FL, USA // // All Rights Reserved. // // This copy of Ice is licensed to you under the terms described in the // ICE_LICENSE file included in this distribution. // // ********************************************************************** #ifndef ICE_HANDLE_H #define ICE_HANDLE_H #include #include // // "Handle" or "smart pointer" class for classes derived from // IceUtil::GCShared, IceUtil::Shared, or IceUtil::SimpleShared. // // In constrast to IceUtil::Handle, IceInternal::Handle requires the // declaration of the two global operations IceInternal::incRef(T*) // and IceInternal::decRef(T*). The use of global operations allows // this template to be used for types which are declared but not // defined, provided that the two above mentioned operations are // declared. // namespace IceInternal { template class Handle : public ::IceUtil::HandleBase { public: Handle(T* p = 0) { this->_ptr = p; if(this->_ptr) { incRef(this->_ptr); } } template Handle(const Handle& r) { this->_ptr = r._ptr; if(this->_ptr) { incRef(this->_ptr); } } template Handle(const ::IceUtil::Handle& r) { this->_ptr = r._ptr; if(this->_ptr) { incRef(this->_ptr); } } #ifdef _WIN32 // COMPILERBUG: Is VC++ or GNU C++ right here??? template<> Handle(const Handle& r) #else Handle(const Handle& r) #endif { this->_ptr = r._ptr; if(this->_ptr) { incRef(this->_ptr); } } ~Handle() { if(this->_ptr) { decRef(this->_ptr); } } Handle& operator=(T* p) { if(this->_ptr != p) { if(p) { incRef(p); } T* ptr = this->_ptr; this->_ptr = p; if(ptr) { decRef(ptr); } } return *this; } template Handle& operator=(const Handle& r) { if(this->_ptr != r._ptr) { if(r._ptr) { incRef(r._ptr); } T* ptr = this->_ptr; this->_ptr = r._ptr; if(ptr) { decRef(ptr); } } return *this; } template Handle& operator=(const ::IceUtil::Handle& r) { if(this->_ptr != r._ptr) { if(r._ptr) { incRef(r._ptr); } T* ptr = this->_ptr; this->_ptr = r._ptr; if(ptr) { decRef(ptr); } } return *this; } #ifdef _WIN32 // COMPILERBUG: Is VC++ or GNU C++ right here??? template<> Handle& operator=(const Handle& r) #else Handle& operator=(const Handle& r) #endif { if(this->_ptr != r._ptr) { if(r._ptr) { incRef(r._ptr); } T* ptr = this->_ptr; this->_ptr = r._ptr; if(ptr) { decRef(ptr); } } return *this; } template static Handle dynamicCast(const ::IceUtil::HandleBase& r) { return Handle(dynamic_cast(r._ptr)); } template static Handle dynamicCast(Y* p) { return Handle(dynamic_cast(p)); } void __clearHandleUnsafe() { this->_ptr = 0; } }; } #endif