diff --git a/samples/main-ptr.cc b/samples/main-ptr.cc index aa350f040..3dc2fb246 100644 --- a/samples/main-ptr.cc +++ b/samples/main-ptr.cc @@ -1,10 +1,11 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ #include "ns3/ptr.h" +#include "ns3/object.h" #include using namespace ns3; -class A +class A : public Object { public: A (); diff --git a/src/core/ptr.cc b/src/core/ptr.cc index a9ba89970..28608f62b 100644 --- a/src/core/ptr.cc +++ b/src/core/ptr.cc @@ -24,10 +24,11 @@ #include "test.h" #include "callback.h" +#include "object.h" namespace ns3 { -class NoCount +class NoCount : public Object { public: NoCount (Callback cb); diff --git a/src/core/ptr.h b/src/core/ptr.h index 889e0386e..ce49f3a4b 100644 --- a/src/core/ptr.h +++ b/src/core/ptr.h @@ -46,13 +46,10 @@ class Ptr { private: T *m_ptr; - uint32_t *m_count; class Tester { private: void operator delete (void *); }; - static uint32_t *AllocCount (void); - static void DeallocCount (uint32_t *count); friend class Ptr; public: /** @@ -115,59 +112,34 @@ public: T *Remove (void); }; -template -uint32_t * -Ptr::AllocCount (void) -{ - return new uint32_t [1] (); -} -template -void -Ptr::DeallocCount (uint32_t *count) -{ - delete [] count; -} - template Ptr::Ptr () - : m_ptr (0), - m_count (0) + : m_ptr (0) {} template Ptr::Ptr (T *ptr) - : m_ptr (ptr), - m_count (0) -{ - if (m_ptr != 0) - { - m_count = Ptr::AllocCount (); - *m_count = 1; - } -} + : m_ptr (ptr) +{} template Ptr::Ptr (Ptr const&o) - : m_ptr (o.m_ptr), - m_count (0) + : m_ptr (o.m_ptr) { if (m_ptr != 0) { - m_count = o.m_count; - (*m_count)++; + m_ptr->Ref(); } } template template Ptr::Ptr (Ptr const &o) - : m_ptr (o.m_ptr), - m_count (0) + : m_ptr (o.m_ptr) { if (m_ptr != 0) { NS_ASSERT (o.m_ptr != 0); - m_count = o.m_count; - (*m_count)++; + m_ptr->Ref(); } } @@ -176,12 +148,7 @@ Ptr::~Ptr () { if (m_ptr != 0) { - (*m_count)--; - if ((*m_count) == 0) - { - delete m_ptr; - Ptr::DeallocCount (m_count); - } + m_ptr->Unref(); } } @@ -193,18 +160,12 @@ Ptr::operator = (Ptr const& o) return *this; if (m_ptr != 0) { - (*m_count)--; - if ((*m_count) == 0) - { - delete m_ptr; - Ptr::DeallocCount (m_count); - } + m_ptr->Unref(); } m_ptr = o.m_ptr; if (m_ptr != 0) { - m_count = o.m_count; - (*m_count)++; + m_ptr->Ref(); } return *this; } @@ -258,8 +219,7 @@ Ptr::Remove (void) } else { - NS_ASSERT ((*m_count) == 1); - Ptr::DeallocCount (m_count); + NS_ASSERT (m_ptr->IsSingle()); T *retval = m_ptr; m_ptr = 0; return retval;