diff --git a/src/core/ptr.cc b/src/core/ptr.cc index 97e26f232..47f2829d5 100644 --- a/src/core/ptr.cc +++ b/src/core/ptr.cc @@ -52,6 +52,7 @@ public: private: void DestroyNotify (void); Ptr CallTest (Ptr p); + Ptr const CallTestConst (Ptr const p); uint32_t m_nDestroyed; }; @@ -73,6 +74,12 @@ PtrTest::CallTest (Ptr p) return p; } +Ptr const +PtrTest::CallTestConst (Ptr const p) +{ + return p; +} + bool PtrTest::RunTests (void) { @@ -92,6 +99,7 @@ PtrTest::RunTests (void) { Ptr p; p = new NoCount (cb); + p = p; } if (m_nDestroyed != 1) { @@ -194,6 +202,48 @@ PtrTest::RunTests (void) { ok = false; } + + { + Ptr p1; + Ptr const p2 = CallTest (p1); + Ptr const p3 = CallTestConst (p1); + Ptr p4 = CallTestConst (p1); + Ptr 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 (p5); + p5 = p1; + Ptr p; + if (p == 0) + {} + if (p != 0) + {} + if (0 == p) + {} + if (0 != p) + {} + if (p) + {} + if (!p) + {} + } + + m_nDestroyed = 0; + { + NoCount *raw; + { + Ptr p = new NoCount (cb); + { + Ptr p1 = p; + } + raw = p.Remove (); + } + if (m_nDestroyed != 0) + { + ok = false; + } + delete raw; + } return ok; diff --git a/src/core/ptr.h b/src/core/ptr.h index ba496bf0b..4b4c9c202 100644 --- a/src/core/ptr.h +++ b/src/core/ptr.h @@ -53,6 +53,7 @@ private: }; static uint32_t *AllocCount (void); static void DeallocCount (uint32_t *count); + friend class Ptr; public: /** * Create an empty smart pointer @@ -93,6 +94,10 @@ public: template inline friend bool operator != (T1 const *lhs, Ptr &rhs); + template + inline friend Ptr const_pointer_cast (Ptr const&p); + + /** * \returns raw pointer * @@ -233,7 +238,7 @@ template T * Ptr::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 @@ operator != (T1 const *lhs, Ptr &rhs) return lhs != rhs.m_ptr; } +template +Ptr +const_pointer_cast (Ptr const&p) +{ + return Ptr (const_cast (p.m_ptr)); +} + + }; // namespace ns3 #endif /* PTR_H */