remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get

Mathieu Lacage 2007-05-09 19:48:33 +02:00
parent bd73c92bb5
commit 80854a2c39
3 changed files with 28 additions and 41 deletions

View File

@ -67,7 +67,8 @@ int main (int argc, char *argv[])
// remove the raw pointer from its smart pointer.
// we can do this because the refcount is exactly one
// here
A *raw = prev.Remove ();
A *raw = prev.Get ();
prev = 0;
raw->Method ();
delete raw;
}

View File

@ -241,7 +241,8 @@ PtrTest::RunTests (void)
{
Ptr<NoCount const> p1 = p;
}
raw = p.Remove ();
raw = p.Get ();
p = 0;
}
if (m_nDestroyed != 0)
{
@ -254,12 +255,12 @@ PtrTest::RunTests (void)
m_nDestroyed = 0;
{
Ptr<NoCount> p = new NoCount (cb);
NoCount const&v1 = p.Peek();
NoCount v2 = p.Peek();
v1.Nothing ();
v2.Nothing ();
const NoCount *v1 = p.Peek();
NoCount *v2 = p.Peek();
v1->Nothing ();
v2->Nothing ();
}
if (m_nDestroyed != 2)
if (m_nDestroyed != 1)
{
ok = false;
}

View File

@ -72,7 +72,23 @@ public:
Ptr (Ptr<U> const &o);
~Ptr () ;
Ptr<T> &operator = (Ptr const& o);
T const& Peek () const;
/**
* \return the pointer managed by this smart pointer.
*
* The underlying refcount is not incremented prior
* to returning to the caller so the caller is not
* responsible for calling Unref himself.
*/
T * Peek () const;
/**
* \return the pointer managed by this smart pointer.
*
* The underlying refcount is incremented prior
* to returning to the caller so the caller is
* responsible for calling Unref himself.
*/
T * Get () const;
T *operator -> () const;
T *operator -> ();
@ -97,20 +113,6 @@ public:
inline friend Ptr<T1> const_pointer_cast (Ptr<T2> const&p);
/**
* \returns raw pointer
*
* It is a programming error to invoke this method when
* the reference count of the smart pointer is not one.
* If you try to do it anyway, an assert will be triggered.
* If asserts are disabled, bad things will happen.
* Once you have successfully called Ptr<T>::Remove on
* a smart pointer, the smart pointer will forget
* about the raw pointer and will stop managing it. As such,
* you, as the caller, become responsible for invoking
* operator delete on the returned raw pointer.
*/
T *Remove (void);
};
template <typename T>
@ -172,10 +174,10 @@ Ptr<T>::operator = (Ptr const& o)
}
template <typename T>
T const&
T *
Ptr<T>::Peek () const
{
return *m_ptr;
return m_ptr;
}
template <typename T>
@ -218,23 +220,6 @@ Ptr<T>::operator Tester * () const
return &test;
}
template <typename T>
T *
Ptr<T>::Remove (void)
{
if (m_ptr == 0)
{
return (T *) 0;
}
else
{
//NS_ASSERT (m_ptr->IsSingle());
T *retval = m_ptr;
m_ptr = 0;
return retval;
}
}
// non-member friend functions.
template <typename T1, typename T2>
bool