more tests, fix bugs uncovered by tests
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 18 Dec 2006 14:25:33 +0100
changeset 225 dad23ccd9e6c
parent 224 47985883d0f4
child 226 1f8c4e56bc86
more tests, fix bugs uncovered by tests
src/core/ptr.cc
src/core/ptr.h
--- a/src/core/ptr.cc	Mon Dec 18 13:28:29 2006 +0100
+++ b/src/core/ptr.cc	Mon Dec 18 14:25:33 2006 +0100
@@ -52,6 +52,7 @@
 private:
   void DestroyNotify (void);
   Ptr<NoCount> CallTest (Ptr<NoCount> p);
+  Ptr<NoCount> const CallTestConst (Ptr<NoCount> const p);
   uint32_t m_nDestroyed;
 };
 
@@ -73,6 +74,12 @@
   return p;
 }
 
+Ptr<NoCount> const 
+PtrTest::CallTestConst (Ptr<NoCount> const p)
+{
+  return p;
+}
+
 bool
 PtrTest::RunTests (void)
 {
@@ -92,6 +99,7 @@
   {
     Ptr<NoCount> p;
     p = new NoCount (cb);
+    p = p;
   }
   if (m_nDestroyed != 1)
     {
@@ -194,6 +202,48 @@
     {
       ok = false;
     }
+
+  {
+    Ptr<NoCount> p1;
+    Ptr<NoCount> const p2 = CallTest (p1);
+    Ptr<NoCount> const p3 = CallTestConst (p1);
+    Ptr<NoCount> p4 = CallTestConst (p1);
+    Ptr<NoCount const> p5 = p4;
+    //p4 = p5; You cannot make a const pointer be a non-const pointer.
+    // but if you use const_pointer_cast, you can.
+    p4 = const_pointer_cast<NoCount> (p5);
+    p5 = p1;
+    Ptr<NoCount> p;
+    if (p == 0)
+      {}
+    if (p != 0)
+      {}
+    if (0 == p)
+      {}
+    if (0 != p)
+      {}
+    if (p)
+      {}
+    if (!p)
+      {}
+  }
+
+  m_nDestroyed = 0;
+  {
+    NoCount *raw;
+    {
+      Ptr<NoCount> p = new NoCount (cb);
+      {
+        Ptr<NoCount const> p1 = p;
+      }
+      raw = p.Remove ();
+    }
+    if (m_nDestroyed != 0)
+      {
+        ok = false;
+      }
+    delete raw;
+  }
   
 
   return ok;
--- a/src/core/ptr.h	Mon Dec 18 13:28:29 2006 +0100
+++ b/src/core/ptr.h	Mon Dec 18 14:25:33 2006 +0100
@@ -53,6 +53,7 @@
   };
   static uint32_t *AllocCount (void);
   static void DeallocCount (uint32_t *count);
+  friend class Ptr<const T>;
 public:
   /**
    * Create an empty smart pointer
@@ -93,6 +94,10 @@
   template <typename T1, typename T2>
   inline friend bool operator != (T1 const *lhs, Ptr<T2> &rhs);
 
+  template <typename T1, typename T2>
+  inline friend Ptr<T1> const_pointer_cast (Ptr<T2> const&p);
+
+
   /**
    * \returns raw pointer
    *
@@ -233,7 +238,7 @@
 T *
 Ptr<T>::Remove (void) 
 {
-  assert (m_ptr.m_count == 1);
+  assert ((*m_count) == 1);
   T *retval = m_ptr;
   m_ptr = 0;
   return retval;
@@ -265,6 +270,14 @@
   return lhs != rhs.m_ptr;
 }
 
+template <typename T1, typename T2>
+Ptr<T1>
+const_pointer_cast (Ptr<T2> const&p)
+{
+  return Ptr<T1> (const_cast<T1 *> (p.m_ptr));
+}
+
+
 }; // namespace ns3
 
 #endif /* PTR_H */