cast possibility for smart pointers

SVN revision: 47973
This commit is contained in:
Andreas Volz 2010-04-12 20:50:26 +00:00
parent 28a48b3484
commit fcd0e50a22
1 changed files with 31 additions and 6 deletions

View File

@ -21,6 +21,8 @@ class CountedPtr {
long* count; // shared number of owners long* count; // shared number of owners
public: public:
template <typename S> friend class CountedPtr;
// initialize pointer with existing pointer // initialize pointer with existing pointer
// - requires that the pointer p is a return value of new // - requires that the pointer p is a return value of new
explicit CountedPtr (T* p=0) explicit CountedPtr (T* p=0)
@ -29,20 +31,43 @@ class CountedPtr {
} }
// copy pointer (one more owner) // copy pointer (one more owner)
CountedPtr (const CountedPtr<T>& p) throw() CountedPtr (const CountedPtr<T>& p)
: ptr(p.ptr), count(p.count) : ptr(p.ptr), count(p.count)
{ {
++*count; ++*count;
} }
template <class S>
CountedPtr (const CountedPtr<S>& p)
: ptr(p.ptr), count(p.count)
{
++*count;
}
template <class S>
static CountedPtr<T> cast_static (const CountedPtr<S>& p)
{
T *obj = static_cast <T*> (&(*p));
return CountedPtr<T> (obj);
}
template <class S>
static CountedPtr<T> cast_dynamic (const CountedPtr<S>& p)
{
T *obj = dynamic_cast <T*> (&(*p));
return CountedPtr<T> (obj);
}
// destructor (delete value if this was the last owner) // destructor (delete value if this was the last owner)
~CountedPtr () throw() ~CountedPtr ()
{ {
dispose(); dispose();
} }
// assignment (unshare old and share new value) // assignment (unshare old and share new value)
CountedPtr<T>& operator= (const CountedPtr<T>& p) throw() CountedPtr<T>& operator= (const CountedPtr<T>& p)
{ {
if (this != &p) { if (this != &p) {
dispose(); dispose();
@ -54,11 +79,11 @@ class CountedPtr {
} }
// access the value to which the pointer refers // access the value to which the pointer refers
T& operator*() const throw() T& operator*() const
{ {
return *ptr; return *ptr;
} }
T* operator->() const throw() T* operator->() const
{ {
return ptr; return ptr;
} }