diff options
Diffstat (limited to 'cpp/include/Ice/ProxyHandle.h')
-rw-r--r-- | cpp/include/Ice/ProxyHandle.h | 150 |
1 files changed, 136 insertions, 14 deletions
diff --git a/cpp/include/Ice/ProxyHandle.h b/cpp/include/Ice/ProxyHandle.h index f9bf8d61c7c..0ed202ade8a 100644 --- a/cpp/include/Ice/ProxyHandle.h +++ b/cpp/include/Ice/ProxyHandle.h @@ -11,40 +11,162 @@ #ifndef ICE_PROXY_HANDLE_H #define ICE_PROXY_HANDLE_H -#include <Ice/Proxy.h> +#include <Ice/Config.h> +#include <algorithm> namespace __Ice { +// +// Like Handle<>, but specifically for proxies, with support for +// checkedCast() and uncheckedCast(). +// template<typename T> -class ProxyHandle : public Handle<T> +class ProxyHandle { public: - ProxyHandle(T* p = 0) : Handle<T>(p) { } - ProxyHandle(const ProxyHandle& r) : Handle<T>(r) { } + typedef T element_type; + +// ProxyHandle() : ptr_(0) { } - ProxyHandle& operator=(const ProxyHandle& r) + ProxyHandle(T* p = 0) + : ptr_(p) + { + if(ptr_) + incRef(ptr_); + } + + template<typename Y> + ProxyHandle(const ProxyHandle<Y>& r) + : ptr_(r.ptr_) + { + if(ptr_) + incRef(ptr_); + } + +#ifdef WIN32 // COMPILERBUG: Is VC++ or GNU C++ right here??? + template<> + ProxyHandle(const ProxyHandle<T>& r) +#else + ProxyHandle(const ProxyHandle& r) +#endif + : ptr_(r.ptr_) { - Handle<T>::operator=(r); + if(ptr_) + incRef(ptr_); + } + + ~ProxyHandle() + { + if(ptr_) + decRef(ptr_); + } + + ProxyHandle& operator=(T* p) + { + if(ptr_ != p) + { + if(p) + incRef(p); + + if(ptr_) + decRef(ptr_); + + ptr_ = p; + } return *this; } + + template<typename Y> + ProxyHandle& operator=(const ProxyHandle<Y>& r) + { + if(ptr_ != r.ptr_) + { + if(r.ptr_) + incRef(r.ptr_); - static ProxyHandle<T> checkedCast(::__IceProxy::Ice::Object* from) + if(ptr_) + decRef(ptr_); + + ptr_ = r.ptr_; + } + return *this; + } + +#ifdef WIN32 // COMPILERBUG: Is VC++ or GNU C++ right here??? + template<> + ProxyHandle& operator=(const ProxyHandle<T>& r) +#else + ProxyHandle& operator=(const ProxyHandle& r) +#endif + { + if(ptr_ != r.ptr_) + { + if(r.ptr_) + incRef(r.ptr_); + + if(ptr_) + decRef(ptr_); + + ptr_ = r.ptr_; + } + return *this; + } + + template<class Y> + static ProxyHandle checkedCast(const ProxyHandle<Y>& r) { - T* to; - ::__Ice::checkedCast(from, to); - return ProxyHandle<T>(to); + T* p; + ::__Ice::checkedCast(r.ptr_, p); + return ProxyHandle(p); } - static ProxyHandle<T> uncheckedCast(::__IceProxy::Ice::Object* from) + template<class Y> + static ProxyHandle uncheckedCast(const ProxyHandle<Y>& r) { - T* to; - ::__Ice::uncheckedCast(from, to); - return ProxyHandle<T>(to); + T* p; + ::__Ice::checkedCast(r.ptr_, p); + return ProxyHandle(p); } + + T* get() const { return ptr_; } + + T& operator*() const { return *ptr_; } + T* operator->() const { return ptr_; } + operator bool() const { return ptr_ ? true : false; } + + void swap(ProxyHandle& other) { std::swap(ptr_, other.ptr_); } + +#ifndef WIN32 // COMPILERBUG: VC++ 6.0 doesn't understand this + + template<typename Y> friend class ProxyHandle; + +protected: + +#endif + + T* ptr_; }; +template<typename T, typename U> +inline bool operator==(const ProxyHandle<T>& a, const ProxyHandle<U>& b) +{ + return *a.get() == *b.get(); +} + +template<typename T, typename U> +inline bool operator!=(const ProxyHandle<T>& a, const ProxyHandle<U>& b) +{ + return *a.get() != *b.get(); +} + +template<typename T, typename U> +inline bool operator<(const ProxyHandle<T>& a, const ProxyHandle<U>& b) +{ + return *a.get() < *b.get(); +} + } #endif |