merge from trunnk

This commit is contained in:
Mathieu Lacage
2007-02-06 15:50:20 +01:00
4 changed files with 649 additions and 0 deletions

View File

@@ -18,6 +18,7 @@ ns3.add(core)
core.add_sources([
'reference-list-test.cc',
'callback-test.cc',
'ptr.cc',
'test.cc'
])
env = Environment()
@@ -37,6 +38,7 @@ core.add_inst_headers([
'system-wall-clock-ms.h',
'reference-list.h',
'callback.h',
'ptr.h',
'test.h'
])
@@ -171,6 +173,12 @@ ns3.add(sample_callback)
sample_callback.add_dep('core')
sample_callback.add_source('main-callback.cc')
sample_ptr = build.Ns3Module('sample-ptr', 'samples')
sample_ptr.set_executable()
ns3.add(sample_ptr)
sample_ptr.add_dep('core')
sample_ptr.add_source('main-ptr.cc')
sample_trace = build.Ns3Module('sample-trace', 'samples')
ns3.add(sample_trace)
sample_trace.add_dep('common')

76
samples/main-ptr.cc Normal file
View File

@@ -0,0 +1,76 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
#include "ns3/ptr.h"
#include <iostream>
using namespace ns3;
class A
{
public:
A ();
~A ();
void Method (void);
};
A::A ()
{
std::cout << "A constructor" << std::endl;
}
A::~A()
{
std::cout << "A destructor" << std::endl;
}
void
A::Method (void)
{
std::cout << "A method" << std::endl;
}
static Ptr<A> g_a = 0;
static Ptr<A>
StoreA (Ptr<A> a)
{
Ptr<A> prev = g_a;
g_a = a;
return prev;
}
static void
ClearA (void)
{
g_a = 0;
}
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 ();
a->Method ();
Ptr<A> prev = StoreA (a);
assert (prev == 0);
}
{
// 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> 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.Remove ();
raw->Method ();
delete raw;
}
return 0;
}

274
src/core/ptr.cc Normal file
View File

@@ -0,0 +1,274 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2005,2006 INRIA
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
#include "ptr.h"
#ifdef RUN_SELF_TESTS
#include "test.h"
#include "callback.h"
namespace ns3 {
class NoCount
{
public:
NoCount (Callback<void> cb);
~NoCount ();
void Nothing () const;
private:
Callback<void> m_cb;
};
NoCount::NoCount (Callback<void> cb)
: m_cb (cb)
{}
NoCount::~NoCount ()
{
m_cb ();
}
void
NoCount::Nothing () const
{}
class PtrTest : Test
{
public:
PtrTest ();
virtual ~PtrTest ();
virtual bool RunTests (void);
private:
void DestroyNotify (void);
Ptr<NoCount> CallTest (Ptr<NoCount> p);
Ptr<NoCount> const CallTestConst (Ptr<NoCount> const p);
uint32_t m_nDestroyed;
};
PtrTest::PtrTest ()
: Test ("Ptr")
{}
PtrTest::~PtrTest ()
{}
void
PtrTest::DestroyNotify (void)
{
m_nDestroyed++;
}
Ptr<NoCount>
PtrTest::CallTest (Ptr<NoCount> p)
{
return p;
}
Ptr<NoCount> const
PtrTest::CallTestConst (Ptr<NoCount> const p)
{
return p;
}
bool
PtrTest::RunTests (void)
{
bool ok = true;
Callback<void> cb = MakeCallback (&PtrTest::DestroyNotify, this);
m_nDestroyed = false;
{
Ptr<NoCount> p = new NoCount (cb);
}
if (m_nDestroyed != 1)
{
ok = false;
}
m_nDestroyed = 0;
{
Ptr<NoCount> p;
p = new NoCount (cb);
p = p;
}
if (m_nDestroyed != 1)
{
ok = false;
}
m_nDestroyed = 0;
{
Ptr<NoCount> p1;
p1 = new NoCount (cb);
Ptr<NoCount> p2 = p1;
}
if (m_nDestroyed != 1)
{
ok = false;
}
m_nDestroyed = 0;
{
Ptr<NoCount> p1;
p1 = new NoCount (cb);
Ptr<NoCount> p2;
p2 = p1;
}
if (m_nDestroyed != 1)
{
ok = false;
}
m_nDestroyed = 0;
{
Ptr<NoCount> p1;
p1 = new NoCount (cb);
Ptr<NoCount> p2 = new NoCount (cb);
p2 = p1;
}
if (m_nDestroyed != 2)
{
ok = false;
}
m_nDestroyed = 0;
{
Ptr<NoCount> p1;
p1 = new NoCount (cb);
Ptr<NoCount> p2;
p2 = new NoCount (cb);
p2 = p1;
}
if (m_nDestroyed != 2)
{
ok = false;
}
m_nDestroyed = 0;
{
Ptr<NoCount> p1;
p1 = new NoCount (cb);
p1 = new NoCount (cb);
}
if (m_nDestroyed != 2)
{
ok = false;
}
m_nDestroyed = 0;
{
Ptr<NoCount> p1;
{
Ptr<NoCount> p2;
p1 = new NoCount (cb);
p2 = new NoCount (cb);
p2 = p1;
}
if (m_nDestroyed != 1)
{
ok = false;
}
}
if (m_nDestroyed != 2)
{
ok = false;
}
m_nDestroyed = 0;
{
Ptr<NoCount> p1;
{
Ptr<NoCount> p2;
p1 = new NoCount (cb);
p2 = new NoCount (cb);
p2 = CallTest (p1);
}
if (m_nDestroyed != 1)
{
ok = false;
}
}
if (m_nDestroyed != 2)
{
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;
}
m_nDestroyed = 0;
{
Ptr<NoCount> p = new NoCount (cb);
NoCount const&v1 = *p;
NoCount v2 = *p;
v1.Nothing ();
v2.Nothing ();
}
if (m_nDestroyed != 2)
{
ok = false;
}
return ok;
}
PtrTest g_ptr_test;
}; // namespace ns3
#endif /* RUN_SELF_TESTS */

291
src/core/ptr.h Normal file
View File

@@ -0,0 +1,291 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2005,2006 INRIA
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
#ifndef PTR_H
#define PTR_H
#include <stdint.h>
#include <cassert>
namespace ns3 {
/**
* \brief smart pointer class similar to boost::shared_ptr
*
* 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.
*/
template <typename T>
class Ptr
{
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:
/**
* Create an empty smart pointer
*/
Ptr ();
/**
* \param ptr raw pointer to manage
*
* Create a smart pointer which points to the
* input raw pointer. This method takes ownershipt
* of the input raw pointer. That is, the smart pointer
* becomes responsible for calling delete on the
* raw pointer when needed.
*/
Ptr (T *ptr);
Ptr (Ptr const&o);
// allow conversions from T to T const.
template <typename U>
Ptr (Ptr<U> const &o);
~Ptr () ;
Ptr<T> &operator = (Ptr const& o);
T const& operator * () 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);
template <typename T1, typename T2>
inline friend Ptr<T1> const_pointer_cast (Ptr<T2> const&p);
/**
* \returns raw pointer
*
* It is a programming error to invoke this method when
* the reference count of the smart pointer is not one.
* If you try to do it anyway, an assert will be triggered.
* If asserts are disabled, bad things will happen.
* Once you have successfully called Ptr<T>::Remove on
* a smart pointer, the smart pointer will forget
* about the raw pointer and will stop managing it. As such,
* you, as the caller, become responsible for invoking
* operator delete on the returned raw pointer.
*/
T *Remove (void);
};
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 (Ptr::AllocCount ())
{}
template <typename T>
Ptr<T>::Ptr (T *ptr)
: m_ptr (ptr),
m_count (Ptr::AllocCount ())
{
if (m_ptr != 0)
{
*m_count = 1;
}
}
template <typename T>
Ptr<T>::Ptr (Ptr const&o)
: m_ptr (o.m_ptr),
m_count (o.m_count)
{
if (m_ptr != 0)
{
(*m_count)++;
}
}
template <typename T>
template <typename U>
Ptr<T>::Ptr (Ptr<U> const &o)
: m_ptr (o.m_ptr),
m_count (o.m_count)
{
if (m_ptr != 0)
{
(*m_count)++;
}
}
template <typename T>
Ptr<T>::~Ptr ()
{
if (m_ptr != 0)
{
(*m_count)--;
if ((*m_count) == 0)
{
delete m_ptr;
Ptr::DeallocCount (m_count);
}
}
}
template <typename T>
Ptr<T> &
Ptr<T>::operator = (Ptr const& o)
{
if (o.m_ptr != 0)
{
(*(o.m_count))++;
}
if (m_ptr != 0)
{
(*m_count)--;
if ((*m_count) == 0)
{
delete m_ptr;
Ptr::DeallocCount (m_count);
}
}
m_ptr = o.m_ptr;
m_count = o.m_count;
return *this;
}
template <typename T>
T const&
Ptr<T>::operator * () const
{
return *m_ptr;
}
template <typename T>
T *
Ptr<T>::operator -> ()
{
return m_ptr;
}
template <typename T>
T *
Ptr<T>::operator -> () const
{
return m_ptr;
}
template <typename T>
bool
Ptr<T>::operator! ()
{
return m_ptr == 0;
}
template <typename T>
Ptr<T>::operator Tester * () const
{
if (m_ptr == 0)
{
return 0;
}
static Tester test;
return &test;
}
template <typename T>
T *
Ptr<T>::Remove (void)
{
assert ((*m_count) == 1);
T *retval = m_ptr;
m_ptr = 0;
return retval;
}
// 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>
Ptr<T1>
const_pointer_cast (Ptr<T2> const&p)
{
return Ptr<T1> (const_cast<T1 *> (p.m_ptr));
}
}; // namespace ns3
#endif /* PTR_H */