From 80854a2c39e81cbab26a473e2c67081a41403eea Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 9 May 2007 19:48:33 +0200 Subject: [PATCH] remove ptr::Remove, make ptr::Peek share the same signature as ptr::Get --- samples/main-ptr.cc | 3 ++- src/core/ptr.cc | 13 ++++++----- src/core/ptr.h | 53 ++++++++++++++++----------------------------- 3 files changed, 28 insertions(+), 41 deletions(-) diff --git a/samples/main-ptr.cc b/samples/main-ptr.cc index 3dc2fb246..fc6f97551 100644 --- a/samples/main-ptr.cc +++ b/samples/main-ptr.cc @@ -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; } diff --git a/src/core/ptr.cc b/src/core/ptr.cc index a0d37633c..610f535b2 100644 --- a/src/core/ptr.cc +++ b/src/core/ptr.cc @@ -241,7 +241,8 @@ PtrTest::RunTests (void) { Ptr 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 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; } diff --git a/src/core/ptr.h b/src/core/ptr.h index 7af3440bd..7ebe43af6 100644 --- a/src/core/ptr.h +++ b/src/core/ptr.h @@ -72,7 +72,23 @@ public: Ptr (Ptr const &o); ~Ptr () ; Ptr &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 const_pointer_cast (Ptr 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::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 @@ -172,10 +174,10 @@ Ptr::operator = (Ptr const& o) } template -T const& +T * Ptr::Peek () const { - return *m_ptr; + return m_ptr; } template @@ -218,23 +220,6 @@ Ptr::operator Tester * () const return &test; } -template -T * -Ptr::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 bool