From 24d19e29c0ec0e141cbd942b76c08037b97b9acb Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 11 May 2007 09:46:01 +0200 Subject: [PATCH] rename Ptr::Get and Ptr::Peek to GetPointer and PeekPointer --- samples/main-ptr.cc | 12 +- src/core/ns-unknown.cc | 4 +- src/core/ns-unknown.h | 2 +- src/core/ptr.cc | 6 +- src/core/ptr.h | 261 +++++++++++++----------- src/internet-node/arp-ipv4-interface.cc | 2 +- src/internet-node/internet-node.cc | 2 +- src/internet-node/ipv4-l4-demux.cc | 2 +- src/internet-node/l3-demux.cc | 2 +- src/node/node-list.cc | 2 +- 10 files changed, 163 insertions(+), 132 deletions(-) diff --git a/samples/main-ptr.cc b/samples/main-ptr.cc index fc6f97551..67f20df04 100644 --- a/samples/main-ptr.cc +++ b/samples/main-ptr.cc @@ -49,7 +49,7 @@ int main (int argc, char *argv[]) { // Create a new object of type A, store it in global // variable g_a - Ptr a = new A (); + Ptr a = MakeNewObject (); a->Method (); Ptr prev = StoreA (a); NS_ASSERT (prev == 0); @@ -58,19 +58,17 @@ int main (int argc, char *argv[]) { // Create a new object of type A, store it in global // variable g_a, get a hold on the previous A object. - Ptr a = new A (); + Ptr a = MakeNewObject (); Ptr prev = StoreA (a); // call method on object prev->Method (); // Clear the currently-stored object ClearA (); - // remove the raw pointer from its smart pointer. - // we can do this because the refcount is exactly one - // here - A *raw = prev.Get (); + // get the raw pointer and release it. + A *raw = GetPointer (prev); prev = 0; raw->Method (); - delete raw; + raw->Unref (); } diff --git a/src/core/ns-unknown.cc b/src/core/ns-unknown.cc index 15f433c1b..a7fef9a28 100644 --- a/src/core/ns-unknown.cc +++ b/src/core/ns-unknown.cc @@ -214,7 +214,7 @@ NsUnknown::DoQueryInterface (Iid iid) const void NsUnknown::AddInterface (Ptr interface) { - NsUnknown *p = interface.Peek (); + NsUnknown *p = PeekPointer (interface); m_impl->AddInterface (p); m_impl->RefAll (p->m_impl); p->m_impl->UnrefAll (); @@ -224,7 +224,7 @@ NsUnknown::AddInterface (Ptr interface) void NsUnknown::AddSelfInterface (Iid iid, Ptr interface) { - m_impl->AddSelfInterface (iid, interface.Peek ()); + m_impl->AddSelfInterface (iid, PeekPointer (interface)); } diff --git a/src/core/ns-unknown.h b/src/core/ns-unknown.h index 5637d0101..e22107675 100644 --- a/src/core/ns-unknown.h +++ b/src/core/ns-unknown.h @@ -117,7 +117,7 @@ NsUnknown::QueryInterface (Iid iid) const Ptr found = DoQueryInterface (iid); if (found != 0) { - return Ptr (dynamic_cast (found.Peek ())); + return Ptr (dynamic_cast (PeekPointer (found))); } return 0; } diff --git a/src/core/ptr.cc b/src/core/ptr.cc index ff94eaeca..77b19fcd9 100644 --- a/src/core/ptr.cc +++ b/src/core/ptr.cc @@ -241,7 +241,7 @@ PtrTest::RunTests (void) { Ptr p1 = p; } - raw = p.Get (); + raw = GetPointer (p); p = 0; } if (m_nDestroyed != 0) @@ -255,8 +255,8 @@ PtrTest::RunTests (void) m_nDestroyed = 0; { Ptr p = MakeNewObject (cb); - const NoCount *v1 = p.Peek(); - NoCount *v2 = p.Peek(); + const NoCount *v1 = PeekPointer (p); + NoCount *v2 = PeekPointer (p); v1->Nothing (); v2->Nothing (); } diff --git a/src/core/ptr.h b/src/core/ptr.h index ffbc1e7fd..60d94a18a 100644 --- a/src/core/ptr.h +++ b/src/core/ptr.h @@ -28,18 +28,26 @@ namespace ns3 { /** - * \brief smart pointer class similar to boost::shared_ptr + * \brief smart pointer class similar to boost::intrusive_ptr + * + * This smart-pointer class assumes that the underlying + * type provides a pair of Ref and Unref methods which are + * expected to increment and decrement the internal refcount + * of the object instance. * - * This smart-pointer class is supposed to be used to manage - * heap-allocated objects: when it decides it does not need - * the object it references, it invokes operator delete on it. * This implementation allows you to manipulate the smart pointer * as if it was a normal pointer: you can compare it with zero, - * compare it against other pointers, etc. However, the only - * operation we are careful to avoid is the conversion back to - * raw pointers: if you need to convert back, you need to invoke - * the Ptr::Remove method which returns a raw pointer and - * makes the smart pointer forget about the raw pointer. + * compare it against other pointers, assign zero to it, etc. + * + * It is possible to extract the raw pointer from this + * smart pointer with the GetPointer and PeekPointer methods. + * + * If you want to store a newed object into a smart pointer, + * we recommend you to use the MakeNewObject template functions + * to create the object and store it in a smart pointer to avoid + * memory leaks. These functions are really small conveniance + * functions and their goal is just is save you a small + * bit of typing. */ template class Ptr @@ -51,6 +59,11 @@ private: void operator delete (void *); }; friend class Ptr; + template + friend U *GetPointer (const Ptr &p); + template + friend U *PeekPointer (const Ptr &p); + void Acquire (void) const; public: /** @@ -74,53 +87,12 @@ public: ~Ptr () ; Ptr &operator = (Ptr const& o); - /** - * \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 -> (); // allow if (!sp) bool operator! (); // allow if (sp) operator Tester * () const; - // allow if (sp == 0) - template - inline friend bool operator == (Ptr const &lhs, T2 const *rhs); - // allow if (0 == sp) - template - inline friend bool operator == (T1 const *lhs, Ptr &rhs); - // allow if (sp != 0) - template - inline friend bool operator != (Ptr const &lhs, T2 const *rhs); - // allow if (0 != sp) - template - inline friend bool operator != (T1 const *lhs, Ptr &rhs); - - // allow if (sp0 == sp1) - template - inline friend bool operator == (Ptr const &lhs, Ptr const &rhs); - // allow if (sp0 != sp1) - template - inline friend bool operator != (Ptr const &lhs, Ptr const &rhs); - - template - inline friend Ptr const_pointer_cast (Ptr const&p); - - }; template @@ -147,6 +119,62 @@ Ptr MakeNewObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6); template Ptr MakeNewObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7); +/** + * \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. + */ +template +T * PeekPointer (const Ptr &p); + +/** + * \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. + */ +template +T * GetPointer (const Ptr &p); + + +// allow if (sp == 0) +template +bool operator == (Ptr const &lhs, T2 const *rhs); + +// allow if (0 == sp) +template +bool operator == (T1 const *lhs, Ptr &rhs); + +// allow if (sp != 0) +template +bool operator != (Ptr const &lhs, T2 const *rhs); + +// allow if (0 != sp) +template +bool operator != (T1 const *lhs, Ptr &rhs); + +// allow if (sp0 == sp1) +template +bool operator == (Ptr const &lhs, Ptr const &rhs); + +// allow if (sp0 != sp1) +template +bool operator != (Ptr const &lhs, Ptr const &rhs); + +template +Ptr const_pointer_cast (Ptr const&p); + +} // namespace ns3 + + +namespace ns3 { + + /************************************************* + * friend non-member function implementations + ************************************************/ template Ptr MakeNewObject (void) @@ -220,6 +248,73 @@ Ptr MakeNewObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7) return p; } +template +T * PeekPointer (const Ptr &p) +{ + return p.m_ptr; +} + +template +T * GetPointer (const Ptr &p) +{ + p.Acquire (); + return p.m_ptr; +} + +template +bool +operator == (Ptr const &lhs, T2 const *rhs) +{ + return PeekPointer (lhs) == rhs; +} + +template +bool +operator == (T1 const *lhs, Ptr &rhs) +{ + return lhs == PeekPointer (rhs); +} + +template +bool +operator != (Ptr const &lhs, T2 const *rhs) +{ + return PeekPointer (lhs) != rhs; +} + +template +bool +operator != (T1 const *lhs, Ptr &rhs) +{ + return lhs != PeekPointer (rhs); +} + +template +bool +operator == (Ptr const &lhs, Ptr const &rhs) +{ + return PeekPointer (lhs) == PeekPointer (rhs); +} + +template +bool +operator != (Ptr const &lhs, Ptr const &rhs) +{ + return PeekPointer (lhs) != PeekPointer (rhs); +} + + +template +Ptr +const_pointer_cast (Ptr const&p) +{ + return Ptr (const_cast (PeekPointer (p))); +} + + +/**************************************************** + * Member method implementations. + ***************************************************/ template void @@ -245,14 +340,14 @@ Ptr::Ptr (T *ptr) template Ptr::Ptr (Ptr const&o) - : m_ptr (o.Peek ()) + : m_ptr (PeekPointer (o)) { Acquire (); } template template Ptr::Ptr (Ptr const &o) - : m_ptr (o.Peek ()) + : m_ptr (PeekPointer (o)) { Acquire (); } @@ -283,21 +378,6 @@ Ptr::operator = (Ptr const& o) return *this; } -template -T * -Ptr::Peek () const -{ - return m_ptr; -} - -template -T * -Ptr::Get () const -{ - Acquire (); - return m_ptr; -} - template T * Ptr::operator -> () @@ -330,53 +410,6 @@ Ptr::operator Tester * () const return &test; } -// non-member friend functions. -template -bool -operator == (Ptr const &lhs, T2 const *rhs) -{ - return lhs.m_ptr == rhs; -} -template -bool -operator == (T1 const *lhs, Ptr &rhs) -{ - return lhs == rhs.m_ptr; -} -template -bool -operator != (Ptr const &lhs, T2 const *rhs) -{ - return lhs.m_ptr != rhs; -} -template -bool -operator != (T1 const *lhs, Ptr &rhs) -{ - return lhs != rhs.m_ptr; -} - -template -bool -operator == (Ptr const &lhs, Ptr const &rhs) -{ - return lhs.Peek () == rhs.Peek (); -} -template -bool -operator != (Ptr const &lhs, Ptr const &rhs) -{ - return lhs.Peek () != rhs.Peek (); -} - - -template -Ptr -const_pointer_cast (Ptr const&p) -{ - return Ptr (const_cast (p.m_ptr)); -} - }; // namespace ns3 diff --git a/src/internet-node/arp-ipv4-interface.cc b/src/internet-node/arp-ipv4-interface.cc index 509bd241f..a4742d04c 100644 --- a/src/internet-node/arp-ipv4-interface.cc +++ b/src/internet-node/arp-ipv4-interface.cc @@ -45,7 +45,7 @@ ArpIpv4Interface::DoCreateTraceResolver (TraceContext const &context) if (GetDevice () != 0) { resolver->Add ("netdevice", - MakeCallback (&NetDevice::CreateTraceResolver, GetDevice ().Peek ()), + MakeCallback (&NetDevice::CreateTraceResolver, PeekPointer (GetDevice ())), ArpIpv4Interface::NETDEVICE); } diff --git a/src/internet-node/internet-node.cc b/src/internet-node/internet-node.cc index d2e8d2ae4..376797828 100644 --- a/src/internet-node/internet-node.cc +++ b/src/internet-node/internet-node.cc @@ -82,7 +82,7 @@ InternetNode::CreateTraceResolver (TraceContext const &context) CompositeTraceResolver *resolver = new CompositeTraceResolver (context); Ptr ipv4 = QueryInterface (IIpv4Private::iid); resolver->Add ("ipv4", - MakeCallback (&IIpv4Private::CreateTraceResolver, ipv4.Peek ()), + MakeCallback (&IIpv4Private::CreateTraceResolver, PeekPointer (ipv4)), InternetNode::IPV4); return resolver; diff --git a/src/internet-node/ipv4-l4-demux.cc b/src/internet-node/ipv4-l4-demux.cc index 36549a1ee..5b26983f1 100644 --- a/src/internet-node/ipv4-l4-demux.cc +++ b/src/internet-node/ipv4-l4-demux.cc @@ -65,7 +65,7 @@ Ipv4L4Demux::CreateTraceResolver (TraceContext const &context) oss << (*i)->GetProtocolNumber (); Ipv4L4ProtocolTraceType protocolNumber = (*i)->GetProtocolNumber (); resolver->Add (protValue, - MakeCallback (&Ipv4L4Protocol::CreateTraceResolver, protocol.Peek ()), + MakeCallback (&Ipv4L4Protocol::CreateTraceResolver, PeekPointer (protocol)), protocolNumber); } return resolver; diff --git a/src/internet-node/l3-demux.cc b/src/internet-node/l3-demux.cc index 1388ebb9b..9bf3dffc6 100644 --- a/src/internet-node/l3-demux.cc +++ b/src/internet-node/l3-demux.cc @@ -63,7 +63,7 @@ L3Demux::CreateTraceResolver (TraceContext const &context) const oss << i->second->GetProtocolNumber (); ProtocolTraceType context = i->second->GetProtocolNumber (); resolver->Add (protValue, - MakeCallback (&L3Protocol::CreateTraceResolver, i->second.Peek ()), + MakeCallback (&L3Protocol::CreateTraceResolver, PeekPointer (i->second)), context); } return resolver; diff --git a/src/node/node-list.cc b/src/node/node-list.cc index c70cd3d20..4fed7eb5e 100644 --- a/src/node/node-list.cc +++ b/src/node/node-list.cc @@ -102,7 +102,7 @@ NodeListPriv::GetNNodes (void) Node * NodeListPriv::PeekNode (uint32_t n) { - return m_nodes[n].Peek (); + return PeekPointer (m_nodes[n]); } Ptr