// ********************************************************************** // // Copyright (c) 2001 // MutableRealms, Inc. // Huntsville, AL, USA // // All Rights Reserved // // ********************************************************************** #ifndef ICE_HANDLE_H #define ICE_HANDLE_H #include #include // // "Handle" or "smart pointer" class for classes derived from // 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) { _ptr = p; if (_ptr) { incRef(_ptr); } } template Handle(const Handle& r) { _ptr = r._ptr; if (_ptr) { incRef(_ptr); } } template Handle(const ::IceUtil::Handle& r) { _ptr = r._ptr; if (_ptr) { incRef(_ptr); } } #ifdef _WIN32 // COMPILERBUG: Is VC++ or GNU C++ right here??? template<> Handle(const Handle& r) #else Handle(const Handle& r) #endif { _ptr = r._ptr; if (_ptr) { incRef(_ptr); } } ~Handle() { if (_ptr) { decRef(_ptr); } } Handle& operator=(T* p) { if (_ptr != p) { if (p) { incRef(p); } if (_ptr) { decRef(_ptr); } _ptr = p; } return *this; } template Handle& operator=(const Handle& r) { if (_ptr != r._ptr) { if (r._ptr) { incRef(r._ptr); } if (_ptr) { decRef(_ptr); } _ptr = r._ptr; } return *this; } template Handle& operator=(const ::IceUtil::Handle& r) { if (_ptr != r._ptr) { if (r._ptr) { incRef(r._ptr); } if (_ptr) { decRef(_ptr); } _ptr = r._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 (_ptr != r._ptr) { if (r._ptr) { incRef(r._ptr); } if (_ptr) { decRef(_ptr); } _ptr = r._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)); } }; } #endif