rename Ptr::Get and Ptr::Peek to GetPointer and PeekPointer

Mathieu Lacage 2007-05-11 09:46:01 +02:00
parent bc15f7157e
commit 24d19e29c0
10 changed files with 163 additions and 132 deletions

View File

@ -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> a = new A ();
Ptr<A> a = MakeNewObject<A> ();
a->Method ();
Ptr<A> 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> a = new A ();
Ptr<A> a = MakeNewObject<A> ();
Ptr<A> 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 ();
}

View File

@ -214,7 +214,7 @@ NsUnknown::DoQueryInterface (Iid iid) const
void
NsUnknown::AddInterface (Ptr<NsUnknown> 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<NsUnknown> interface)
void
NsUnknown::AddSelfInterface (Iid iid, Ptr<NsUnknown> interface)
{
m_impl->AddSelfInterface (iid, interface.Peek ());
m_impl->AddSelfInterface (iid, PeekPointer (interface));
}

View File

@ -117,7 +117,7 @@ NsUnknown::QueryInterface (Iid iid) const
Ptr<NsUnknown> found = DoQueryInterface (iid);
if (found != 0)
{
return Ptr<T> (dynamic_cast<T *> (found.Peek ()));
return Ptr<T> (dynamic_cast<T *> (PeekPointer (found)));
}
return 0;
}

View File

@ -241,7 +241,7 @@ PtrTest::RunTests (void)
{
Ptr<NoCount const> 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<NoCount> p = MakeNewObject<NoCount> (cb);
const NoCount *v1 = p.Peek();
NoCount *v2 = p.Peek();
const NoCount *v1 = PeekPointer (p);
NoCount *v2 = PeekPointer (p);
v1->Nothing ();
v2->Nothing ();
}

View File

@ -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<T>::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 <typename T>
class Ptr
@ -51,6 +59,11 @@ private:
void operator delete (void *);
};
friend class Ptr<const T>;
template <typename U>
friend U *GetPointer (const Ptr<U> &p);
template <typename U>
friend U *PeekPointer (const Ptr<U> &p);
void Acquire (void) const;
public:
/**
@ -74,53 +87,12 @@ public:
~Ptr () ;
Ptr<T> &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 <typename T1, typename T2>
inline friend bool operator == (Ptr<T1> const &lhs, T2 const *rhs);
// allow if (0 == sp)
template <typename T1, typename T2>
inline friend bool operator == (T1 const *lhs, Ptr<T2> &rhs);
// allow if (sp != 0)
template <typename T1, typename T2>
inline friend bool operator != (Ptr<T1> const &lhs, T2 const *rhs);
// allow if (0 != sp)
template <typename T1, typename T2>
inline friend bool operator != (T1 const *lhs, Ptr<T2> &rhs);
// allow if (sp0 == sp1)
template <typename T1, typename T2>
inline friend bool operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs);
// allow if (sp0 != sp1)
template <typename T1, typename T2>
inline friend bool operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs);
template <typename T1, typename T2>
inline friend Ptr<T1> const_pointer_cast (Ptr<T2> const&p);
};
template <typename T>
@ -147,6 +119,62 @@ Ptr<T> MakeNewObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6);
template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
Ptr<T> 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 <typename T>
T * PeekPointer (const Ptr<T> &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 <typename T>
T * GetPointer (const Ptr<T> &p);
// allow if (sp == 0)
template <typename T1, typename T2>
bool operator == (Ptr<T1> const &lhs, T2 const *rhs);
// allow if (0 == sp)
template <typename T1, typename T2>
bool operator == (T1 const *lhs, Ptr<T2> &rhs);
// allow if (sp != 0)
template <typename T1, typename T2>
bool operator != (Ptr<T1> const &lhs, T2 const *rhs);
// allow if (0 != sp)
template <typename T1, typename T2>
bool operator != (T1 const *lhs, Ptr<T2> &rhs);
// allow if (sp0 == sp1)
template <typename T1, typename T2>
bool operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs);
// allow if (sp0 != sp1)
template <typename T1, typename T2>
bool operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs);
template <typename T1, typename T2>
Ptr<T1> const_pointer_cast (Ptr<T2> const&p);
} // namespace ns3
namespace ns3 {
/*************************************************
* friend non-member function implementations
************************************************/
template <typename T>
Ptr<T> MakeNewObject (void)
@ -220,6 +248,73 @@ Ptr<T> MakeNewObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7)
return p;
}
template <typename T>
T * PeekPointer (const Ptr<T> &p)
{
return p.m_ptr;
}
template <typename T>
T * GetPointer (const Ptr<T> &p)
{
p.Acquire ();
return p.m_ptr;
}
template <typename T1, typename T2>
bool
operator == (Ptr<T1> const &lhs, T2 const *rhs)
{
return PeekPointer (lhs) == rhs;
}
template <typename T1, typename T2>
bool
operator == (T1 const *lhs, Ptr<T2> &rhs)
{
return lhs == PeekPointer (rhs);
}
template <typename T1, typename T2>
bool
operator != (Ptr<T1> const &lhs, T2 const *rhs)
{
return PeekPointer (lhs) != rhs;
}
template <typename T1, typename T2>
bool
operator != (T1 const *lhs, Ptr<T2> &rhs)
{
return lhs != PeekPointer (rhs);
}
template <typename T1, typename T2>
bool
operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
{
return PeekPointer (lhs) == PeekPointer (rhs);
}
template <typename T1, typename T2>
bool
operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
{
return PeekPointer (lhs) != PeekPointer (rhs);
}
template <typename T1, typename T2>
Ptr<T1>
const_pointer_cast (Ptr<T2> const&p)
{
return Ptr<T1> (const_cast<T1 *> (PeekPointer (p)));
}
/****************************************************
* Member method implementations.
***************************************************/
template <typename T>
void
@ -245,14 +340,14 @@ Ptr<T>::Ptr (T *ptr)
template <typename T>
Ptr<T>::Ptr (Ptr const&o)
: m_ptr (o.Peek ())
: m_ptr (PeekPointer (o))
{
Acquire ();
}
template <typename T>
template <typename U>
Ptr<T>::Ptr (Ptr<U> const &o)
: m_ptr (o.Peek ())
: m_ptr (PeekPointer (o))
{
Acquire ();
}
@ -283,21 +378,6 @@ Ptr<T>::operator = (Ptr const& o)
return *this;
}
template <typename T>
T *
Ptr<T>::Peek () const
{
return m_ptr;
}
template <typename T>
T *
Ptr<T>::Get () const
{
Acquire ();
return m_ptr;
}
template <typename T>
T *
Ptr<T>::operator -> ()
@ -330,53 +410,6 @@ Ptr<T>::operator Tester * () const
return &test;
}
// non-member friend functions.
template <typename T1, typename T2>
bool
operator == (Ptr<T1> const &lhs, T2 const *rhs)
{
return lhs.m_ptr == rhs;
}
template <typename T1, typename T2>
bool
operator == (T1 const *lhs, Ptr<T2> &rhs)
{
return lhs == rhs.m_ptr;
}
template <typename T1, typename T2>
bool
operator != (Ptr<T1> const &lhs, T2 const *rhs)
{
return lhs.m_ptr != rhs;
}
template <typename T1, typename T2>
bool
operator != (T1 const *lhs, Ptr<T2> &rhs)
{
return lhs != rhs.m_ptr;
}
template <typename T1, typename T2>
bool
operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
{
return lhs.Peek () == rhs.Peek ();
}
template <typename T1, typename T2>
bool
operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
{
return lhs.Peek () != rhs.Peek ();
}
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

View File

@ -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);
}

View File

@ -82,7 +82,7 @@ InternetNode::CreateTraceResolver (TraceContext const &context)
CompositeTraceResolver *resolver = new CompositeTraceResolver (context);
Ptr<IIpv4Private> ipv4 = QueryInterface<IIpv4Private> (IIpv4Private::iid);
resolver->Add ("ipv4",
MakeCallback (&IIpv4Private::CreateTraceResolver, ipv4.Peek ()),
MakeCallback (&IIpv4Private::CreateTraceResolver, PeekPointer (ipv4)),
InternetNode::IPV4);
return resolver;

View File

@ -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;

View File

@ -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;

View File

@ -102,7 +102,7 @@ NodeListPriv::GetNNodes (void)
Node *
NodeListPriv::PeekNode (uint32_t n)
{
return m_nodes[n].Peek ();
return PeekPointer (m_nodes[n]);
}
Ptr<Node>