diff options
author | Marc Laukien <marc@zeroc.com> | 2001-06-17 01:04:03 +0000 |
---|---|---|
committer | Marc Laukien <marc@zeroc.com> | 2001-06-17 01:04:03 +0000 |
commit | 6de01ee326dc82808ccbfbf41931da342f86acfd (patch) | |
tree | 67db470c6cf6c4ce2c49289d0223a4c9583d9c54 /cpp/include/Ice/Handle.h | |
parent | fixes (diff) | |
download | ice-6de01ee326dc82808ccbfbf41931da342f86acfd.tar.bz2 ice-6de01ee326dc82808ccbfbf41931da342f86acfd.tar.xz ice-6de01ee326dc82808ccbfbf41931da342f86acfd.zip |
many handle and other fixes
Diffstat (limited to 'cpp/include/Ice/Handle.h')
-rw-r--r-- | cpp/include/Ice/Handle.h | 81 |
1 files changed, 51 insertions, 30 deletions
diff --git a/cpp/include/Ice/Handle.h b/cpp/include/Ice/Handle.h index 80373a47020..be337b38dcc 100644 --- a/cpp/include/Ice/Handle.h +++ b/cpp/include/Ice/Handle.h @@ -32,6 +32,8 @@ public: typedef T element_type; +// Handle() : ptr_(0) { } + Handle(T* p = 0) : ptr_(p) { @@ -39,7 +41,20 @@ public: incRef(ptr_); } + template<typename Y> + Handle(const Handle<Y>& r) + : ptr_(r.ptr_) + { + if(ptr_) + incRef(ptr_); + } + +#ifdef WIN32 // COMPILERBUG: Is VC++ or GNU C++ right here??? + template<> + Handle(const Handle<T>& r) +#else Handle(const Handle& r) +#endif : ptr_(r.ptr_) { if(ptr_) @@ -56,75 +71,81 @@ public: { if(ptr_ != p) { + if(p) + incRef(p); + if(ptr_) decRef(ptr_); ptr_ = p; - - if(ptr_) - incRef(ptr_); } return *this; } - Handle& operator=(const Handle& r) + template<typename Y> + Handle& operator=(const Handle<Y>& r) { if(ptr_ != r.ptr_) { + if(r.ptr_) + incRef(r.ptr_); + if(ptr_) decRef(ptr_); ptr_ = r.ptr_; - - if(ptr_) - incRef(ptr_); } return *this; } - -// -// Some compilers (like Visual C++ 6.0) do not support member -// templates :-( I therefore don't use them, otherwise Ice code could -// be non-portable. -// -/* - template<typename Y> - Handle(const Handle<Y>& r) - : ptr_(r.ptr_) - { - if(ptr_) - incRef(ptr_); - } - template<typename Y> - Handle& operator=(const Handle<Y>& r) +#ifdef WIN32 // COMPILERBUG: Is VC++ or GNU C++ right here??? + template<> + Handle& operator=(const Handle<T>& 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_; - - if(ptr_) - incRef(ptr_); } return *this; } + + template<class Y> + static Handle dynamicCast(const Handle<Y>& r) + { + return Handle(dynamic_cast<T*>(r.ptr_)); + } - template<typename Y> friend class Handle; -*/ + template<class Y> + static Handle dynamicCast(Y* p) + { + return Handle(dynamic_cast<T*>(p)); + } T* get() const { return ptr_; } T& operator*() const { return *ptr_; } T* operator->() const { return ptr_; } - operator T*() const { return ptr_; } + operator bool() const { return ptr_ ? true : false; } + + void swap(Handle& other) { std::swap(ptr_, other.ptr_); } - void swap(Handle<T>& other) { std::swap(ptr_, other.ptr_); } +#ifndef WIN32 // COMPILERBUG: VC++ 6.0 doesn't understand this + + template<typename Y> friend class Handle; protected: +#endif + T* ptr_; }; |