Ptr changed to use Object's refcounts
authorRaj Bhattacharjea <raj.b@gatech.edu>
Tue, 08 May 2007 11:44:04 -0400
changeset 542 00722b9a01b3
parent 536 059cec00b41e
child 543 a730800a31d5
Ptr changed to use Object's refcounts
samples/main-ptr.cc
src/core/ptr.cc
src/core/ptr.h
--- a/samples/main-ptr.cc	Wed May 09 17:07:50 2007 +0200
+++ b/samples/main-ptr.cc	Tue May 08 11:44:04 2007 -0400
@@ -1,10 +1,11 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 #include "ns3/ptr.h"
+#include "ns3/object.h"
 #include <iostream>
 
 using namespace ns3;
 
-class A 
+class A : public Object
 {
 public:
   A ();
--- a/src/core/ptr.cc	Wed May 09 17:07:50 2007 +0200
+++ b/src/core/ptr.cc	Tue May 08 11:44:04 2007 -0400
@@ -24,10 +24,11 @@
 
 #include "test.h"
 #include "callback.h"
+#include "object.h"
 
 namespace ns3 {
 
-class NoCount
+class NoCount : public Object
 {
 public:
   NoCount (Callback<void> cb);
--- a/src/core/ptr.h	Wed May 09 17:07:50 2007 +0200
+++ b/src/core/ptr.h	Tue May 08 11:44:04 2007 -0400
@@ -46,13 +46,10 @@
 {
 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<const T>;
 public:
   /**
@@ -116,58 +113,33 @@
 };
 
 template <typename T>
-uint32_t *
-Ptr<T>::AllocCount (void)
-{
-  return new uint32_t [1] ();
-}
-template <typename T>
-void 
-Ptr<T>::DeallocCount (uint32_t *count)
-{
-  delete [] count;
-}
-
-template <typename T>
 Ptr<T>::Ptr ()
-  : m_ptr (0),
-    m_count (0)
+  : m_ptr (0)
 {}
 
 template <typename T>
 Ptr<T>::Ptr (T *ptr) 
-  : m_ptr (ptr),
-    m_count (0)
-{
-  if (m_ptr != 0)
-    {
-      m_count = Ptr::AllocCount ();
-      *m_count = 1;
-    }
-}
+  : m_ptr (ptr)
+{}
 
 template <typename T>
 Ptr<T>::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 <typename T>
 template <typename U>
 Ptr<T>::Ptr (Ptr<U> 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 @@
 {
   if (m_ptr != 0) 
     {
-      (*m_count)--;
-      if ((*m_count) == 0) 
-        {
-          delete m_ptr;
-          Ptr::DeallocCount (m_count);
-        }
+      m_ptr->Unref();
     }
 }
 
@@ -193,18 +160,12 @@
     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 @@
     }
   else
     {
-      NS_ASSERT ((*m_count) == 1);
-      Ptr::DeallocCount (m_count);
+      NS_ASSERT (m_ptr->IsSingle());
       T *retval = m_ptr;
       m_ptr = 0;
       return retval;