introduce ObjectBase

This commit is contained in:
Mathieu Lacage
2008-01-30 17:25:06 +01:00
parent 5f5518e05b
commit 71c635dedc
11 changed files with 120 additions and 57 deletions

View File

@@ -31,6 +31,7 @@
#include "ns3/callback.h"
#include "ns3/assert.h"
#include "ns3/ptr.h"
#include "ns3/object-base.h"
namespace ns3 {
@@ -73,7 +74,7 @@ class PacketPrinter;
* The performance aspects of the Packet API are discussed in
* \ref packetperf
*/
class Packet {
class Packet : public ObjectBase {
public:
void Ref (void) const;
void Unref (void) const;

View File

@@ -26,6 +26,7 @@
#include "fatal-error.h"
#include "empty.h"
#include "type-traits.h"
#include "object-base.h"
namespace ns3 {
@@ -70,7 +71,8 @@ struct CallbackTraits<T *>
}
};
class CallbackImplBase {
class CallbackImplBase : public ObjectBase
{
public:
CallbackImplBase ()
: m_count (1) {}

3
src/core/object-base.cc Normal file
View File

@@ -0,0 +1,3 @@
#include "object-base.h"
ns3::ObjectBase::~ObjectBase () {}

20
src/core/object-base.h Normal file
View File

@@ -0,0 +1,20 @@
#ifndef OBJECT_BASE_H
#define OBJECT_BASE_H
namespace ns3 {
/**
* This base class is really used only to make sure that
* every subclass has RTTI information and that they all
* share a single base class to allow us to make type
* checks across all these types.
*/
class ObjectBase
{
public:
virtual ~ObjectBase ();
};
} // namespace ns3
#endif /* OBJECT_BASE_H */

View File

@@ -132,7 +132,7 @@ private:
* BonoboObject in Bonobo: it provides three main methods: Ref, Unref and
* QueryInterface.
*/
class Object
class Object : public ObjectBase
{
public:
static TypeId GetTypeId (void);

View File

@@ -23,49 +23,81 @@
#ifdef RUN_SELF_TESTS
#include "test.h"
#include "callback.h"
#include "object.h"
#include "object-base.h"
namespace ns3 {
class NoCount;
template <typename T>
void Foo (void) {}
class NoCount : public Object
{
public:
NoCount (void (*fn) (void));
NoCount (Callback<void> cb);
~NoCount ();
void Nothing (void) 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);
private:
Ptr<NoCount> CallTest (Ptr<NoCount> p);
Ptr<NoCount> const CallTestConst (Ptr<NoCount> const p);
uint32_t m_nDestroyed;
};
class Base : public ObjectBase
{
public:
Base ();
virtual ~Base ();
void Ref (void) const;
void Unref (void) const;
private:
mutable uint32_t m_count;
};
class NoCount : public Base
{
public:
NoCount (PtrTest *test);
~NoCount ();
void Nothing (void) const;
private:
PtrTest *m_test;
};
Base::Base ()
: m_count (1)
{}
Base::~Base ()
{}
void
Base::Ref (void) const
{
m_count++;
}
void
Base::Unref (void) const
{
m_count--;
if (m_count == 0)
{
delete this;
}
}
NoCount::NoCount (PtrTest *test)
: m_test (test)
{}
NoCount::~NoCount ()
{
m_test->DestroyNotify ();
}
void
NoCount::Nothing () const
{}
PtrTest::PtrTest ()
: Test ("Ptr")
{}
@@ -95,10 +127,9 @@ PtrTest::RunTests (void)
{
bool ok = true;
Callback<void> cb = MakeCallback (&PtrTest::DestroyNotify, this);
m_nDestroyed = false;
{
Ptr<NoCount> p = CreateObject<NoCount> (cb);
Ptr<NoCount> p = Create<NoCount> (this);
}
if (m_nDestroyed != 1)
{
@@ -108,7 +139,7 @@ PtrTest::RunTests (void)
m_nDestroyed = 0;
{
Ptr<NoCount> p;
p = CreateObject<NoCount> (cb);
p = Create<NoCount> (this);
p = p;
}
if (m_nDestroyed != 1)
@@ -119,7 +150,7 @@ PtrTest::RunTests (void)
m_nDestroyed = 0;
{
Ptr<NoCount> p1;
p1 = CreateObject<NoCount> (cb);
p1 = Create<NoCount> (this);
Ptr<NoCount> p2 = p1;
}
if (m_nDestroyed != 1)
@@ -130,7 +161,7 @@ PtrTest::RunTests (void)
m_nDestroyed = 0;
{
Ptr<NoCount> p1;
p1 = CreateObject<NoCount> (cb);
p1 = Create<NoCount> (this);
Ptr<NoCount> p2;
p2 = p1;
}
@@ -142,8 +173,8 @@ PtrTest::RunTests (void)
m_nDestroyed = 0;
{
Ptr<NoCount> p1;
p1 = CreateObject<NoCount> (cb);
Ptr<NoCount> p2 = CreateObject<NoCount> (cb);
p1 = Create<NoCount> (this);
Ptr<NoCount> p2 = Create<NoCount> (this);
p2 = p1;
}
if (m_nDestroyed != 2)
@@ -154,9 +185,9 @@ PtrTest::RunTests (void)
m_nDestroyed = 0;
{
Ptr<NoCount> p1;
p1 = CreateObject<NoCount> (cb);
p1 = Create<NoCount> (this);
Ptr<NoCount> p2;
p2 = CreateObject<NoCount> (cb);
p2 = Create<NoCount> (this);
p2 = p1;
}
if (m_nDestroyed != 2)
@@ -167,8 +198,8 @@ PtrTest::RunTests (void)
m_nDestroyed = 0;
{
Ptr<NoCount> p1;
p1 = CreateObject<NoCount> (cb);
p1 = CreateObject<NoCount> (cb);
p1 = Create<NoCount> (this);
p1 = Create<NoCount> (this);
}
if (m_nDestroyed != 2)
{
@@ -180,8 +211,8 @@ PtrTest::RunTests (void)
Ptr<NoCount> p1;
{
Ptr<NoCount> p2;
p1 = CreateObject<NoCount> (cb);
p2 = CreateObject<NoCount> (cb);
p1 = Create<NoCount> (this);
p2 = Create<NoCount> (this);
p2 = p1;
}
if (m_nDestroyed != 1)
@@ -199,8 +230,8 @@ PtrTest::RunTests (void)
Ptr<NoCount> p1;
{
Ptr<NoCount> p2;
p1 = CreateObject<NoCount> (cb);
p2 = CreateObject<NoCount> (cb);
p1 = Create<NoCount> (this);
p2 = Create<NoCount> (this);
p2 = CallTest (p1);
}
if (m_nDestroyed != 1)
@@ -242,7 +273,7 @@ PtrTest::RunTests (void)
{
NoCount *raw;
{
Ptr<NoCount> p = CreateObject<NoCount> (cb);
Ptr<NoCount> p = Create<NoCount> (this);
{
Ptr<NoCount const> p1 = p;
}
@@ -256,10 +287,9 @@ PtrTest::RunTests (void)
delete raw;
}
m_nDestroyed = 0;
{
Ptr<NoCount> p = CreateObject<NoCount> (cb);
Ptr<NoCount> p = Create<NoCount> (this);
const NoCount *v1 = PeekPointer (p);
NoCount *v2 = PeekPointer (p);
v1->Nothing ();
@@ -271,8 +301,8 @@ PtrTest::RunTests (void)
}
{
Ptr<Object> p0 = CreateObject<NoCount> (cb);
Ptr<NoCount> p1 = CreateObject<NoCount> (cb);
Ptr<Base> p0 = Create<NoCount> (this);
Ptr<NoCount> p1 = Create<NoCount> (this);
if (p0 == p1)
{
ok = false;
@@ -285,23 +315,23 @@ PtrTest::RunTests (void)
ok = false;
}
}
#if 0
{
Ptr<NoCount> p = CreateObject<NoCount> (cb);
Ptr<NoCount> p = Create<NoCount> (cb);
Callback<void> callback = MakeCallback (&NoCount::Nothing, p);
callback ();
}
{
Ptr<const NoCount> p = CreateObject<NoCount> (cb);
Ptr<const NoCount> p = Create<NoCount> (cb);
Callback<void> callback = MakeCallback (&NoCount::Nothing, p);
callback ();
}
#endif
#if 0
// as expected, fails compilation.
{
Ptr<const Object> p = CreateObject<NoCount> (cb);
Ptr<const Base> p = Create<NoCount> (cb);
Callback<void> callback = MakeCallback (&NoCount::Nothing, p);
}
// local types are not allowed as arguments to a template.

View File

@@ -25,6 +25,7 @@
#include <list>
#include "trace-context.h"
#include "trace-doc.h"
#include "object-base.h"
namespace ns3 {
@@ -39,7 +40,7 @@ class CallbackBase;
* subclasses, doing so is complicated so, it is recommended to use
* the default implementation ns3::CompositeTraceResolver instead.
*/
class TraceResolver
class TraceResolver : public ObjectBase
{
public:

View File

@@ -31,6 +31,7 @@ def build(bld):
'callback-test.cc',
'log.cc',
'breakpoint.cc',
'object-base.cc',
'ptr.cc',
'object.cc',
'test.cc',
@@ -68,6 +69,7 @@ def build(bld):
'system-wall-clock-ms.h',
'empty.h',
'callback.h',
'object-base.h',
'ptr.h',
'object.h',
'log.h',

View File

@@ -30,6 +30,7 @@
#include "ns3/assert.h"
#include "ns3/log.h"
#include "ns3/composite-trace-resolver.h"
#include "ns3/object-base.h"
#include <math.h>
NS_LOG_COMPONENT_DEFINE ("WifiPhy");
@@ -76,7 +77,8 @@ WifiPhyListener::~WifiPhyListener ()
* Phy event class
****************************************************************/
class RxEvent {
class RxEvent : public ObjectBase
{
public:
RxEvent (uint32_t size, WifiMode payloadMode,
enum WifiPreamble preamble,

View File

@@ -21,10 +21,12 @@
#define EVENT_IMPL_H
#include <stdint.h>
#include "ns3/object-base.h"
namespace ns3 {
class EventImpl {
class EventImpl : public ObjectBase
{
public:
EventImpl ();
inline void Ref (void) const;

View File

@@ -568,7 +568,7 @@ static void cber5 (const int &, const int &, const int &, const int &, const int
{}
class SimulatorTests : public Test {
class SimulatorTests : public Test, public ObjectBase {
public:
SimulatorTests ();
// only here for testing of Ptr<>