From 71c635dedc5578179b01cd8d4b80ff300616d3eb Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 30 Jan 2008 17:25:06 +0100 Subject: [PATCH] introduce ObjectBase --- src/common/packet.h | 3 +- src/core/callback.h | 4 +- src/core/object-base.cc | 3 + src/core/object-base.h | 20 ++++++ src/core/object.h | 2 +- src/core/ptr.cc | 130 +++++++++++++++++++++-------------- src/core/trace-resolver.h | 3 +- src/core/wscript | 2 + src/devices/wifi/wifi-phy.cc | 4 +- src/simulator/event-impl.h | 4 +- src/simulator/simulator.cc | 2 +- 11 files changed, 120 insertions(+), 57 deletions(-) create mode 100644 src/core/object-base.cc create mode 100644 src/core/object-base.h diff --git a/src/common/packet.h b/src/common/packet.h index 7f4ca70bd..afd16d5c5 100644 --- a/src/common/packet.h +++ b/src/common/packet.h @@ -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; diff --git a/src/core/callback.h b/src/core/callback.h index 1fa9d9c52..a8a1fd41c 100644 --- a/src/core/callback.h +++ b/src/core/callback.h @@ -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 } }; -class CallbackImplBase { +class CallbackImplBase : public ObjectBase +{ public: CallbackImplBase () : m_count (1) {} diff --git a/src/core/object-base.cc b/src/core/object-base.cc new file mode 100644 index 000000000..98a080220 --- /dev/null +++ b/src/core/object-base.cc @@ -0,0 +1,3 @@ +#include "object-base.h" + +ns3::ObjectBase::~ObjectBase () {} diff --git a/src/core/object-base.h b/src/core/object-base.h new file mode 100644 index 000000000..36f09ffe5 --- /dev/null +++ b/src/core/object-base.h @@ -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 */ diff --git a/src/core/object.h b/src/core/object.h index 952a1f7ab..1bca906b9 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -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); diff --git a/src/core/ptr.cc b/src/core/ptr.cc index feb09ad61..39b7f9554 100644 --- a/src/core/ptr.cc +++ b/src/core/ptr.cc @@ -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 void Foo (void) {} - -class NoCount : public Object -{ -public: - NoCount (void (*fn) (void)); - NoCount (Callback cb); - ~NoCount (); - void Nothing (void) const; -private: - Callback m_cb; -}; -NoCount::NoCount (Callback 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 CallTest (Ptr p); Ptr const CallTestConst (Ptr 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 cb = MakeCallback (&PtrTest::DestroyNotify, this); m_nDestroyed = false; { - Ptr p = CreateObject (cb); + Ptr p = Create (this); } if (m_nDestroyed != 1) { @@ -108,7 +139,7 @@ PtrTest::RunTests (void) m_nDestroyed = 0; { Ptr p; - p = CreateObject (cb); + p = Create (this); p = p; } if (m_nDestroyed != 1) @@ -119,7 +150,7 @@ PtrTest::RunTests (void) m_nDestroyed = 0; { Ptr p1; - p1 = CreateObject (cb); + p1 = Create (this); Ptr p2 = p1; } if (m_nDestroyed != 1) @@ -130,7 +161,7 @@ PtrTest::RunTests (void) m_nDestroyed = 0; { Ptr p1; - p1 = CreateObject (cb); + p1 = Create (this); Ptr p2; p2 = p1; } @@ -142,8 +173,8 @@ PtrTest::RunTests (void) m_nDestroyed = 0; { Ptr p1; - p1 = CreateObject (cb); - Ptr p2 = CreateObject (cb); + p1 = Create (this); + Ptr p2 = Create (this); p2 = p1; } if (m_nDestroyed != 2) @@ -154,9 +185,9 @@ PtrTest::RunTests (void) m_nDestroyed = 0; { Ptr p1; - p1 = CreateObject (cb); + p1 = Create (this); Ptr p2; - p2 = CreateObject (cb); + p2 = Create (this); p2 = p1; } if (m_nDestroyed != 2) @@ -167,8 +198,8 @@ PtrTest::RunTests (void) m_nDestroyed = 0; { Ptr p1; - p1 = CreateObject (cb); - p1 = CreateObject (cb); + p1 = Create (this); + p1 = Create (this); } if (m_nDestroyed != 2) { @@ -180,8 +211,8 @@ PtrTest::RunTests (void) Ptr p1; { Ptr p2; - p1 = CreateObject (cb); - p2 = CreateObject (cb); + p1 = Create (this); + p2 = Create (this); p2 = p1; } if (m_nDestroyed != 1) @@ -199,8 +230,8 @@ PtrTest::RunTests (void) Ptr p1; { Ptr p2; - p1 = CreateObject (cb); - p2 = CreateObject (cb); + p1 = Create (this); + p2 = Create (this); p2 = CallTest (p1); } if (m_nDestroyed != 1) @@ -242,7 +273,7 @@ PtrTest::RunTests (void) { NoCount *raw; { - Ptr p = CreateObject (cb); + Ptr p = Create (this); { Ptr p1 = p; } @@ -256,10 +287,9 @@ PtrTest::RunTests (void) delete raw; } - m_nDestroyed = 0; { - Ptr p = CreateObject (cb); + Ptr p = Create (this); const NoCount *v1 = PeekPointer (p); NoCount *v2 = PeekPointer (p); v1->Nothing (); @@ -271,8 +301,8 @@ PtrTest::RunTests (void) } { - Ptr p0 = CreateObject (cb); - Ptr p1 = CreateObject (cb); + Ptr p0 = Create (this); + Ptr p1 = Create (this); if (p0 == p1) { ok = false; @@ -285,23 +315,23 @@ PtrTest::RunTests (void) ok = false; } } - +#if 0 { - Ptr p = CreateObject (cb); + Ptr p = Create (cb); Callback callback = MakeCallback (&NoCount::Nothing, p); callback (); } { - Ptr p = CreateObject (cb); + Ptr p = Create (cb); Callback callback = MakeCallback (&NoCount::Nothing, p); callback (); } - +#endif #if 0 // as expected, fails compilation. { - Ptr p = CreateObject (cb); + Ptr p = Create (cb); Callback callback = MakeCallback (&NoCount::Nothing, p); } // local types are not allowed as arguments to a template. diff --git a/src/core/trace-resolver.h b/src/core/trace-resolver.h index 027beb5c1..8a0bb9e1f 100644 --- a/src/core/trace-resolver.h +++ b/src/core/trace-resolver.h @@ -25,6 +25,7 @@ #include #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: diff --git a/src/core/wscript b/src/core/wscript index 1c69ee1db..088bce41b 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -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', diff --git a/src/devices/wifi/wifi-phy.cc b/src/devices/wifi/wifi-phy.cc index 285e13212..ca741d6af 100644 --- a/src/devices/wifi/wifi-phy.cc +++ b/src/devices/wifi/wifi-phy.cc @@ -30,6 +30,7 @@ #include "ns3/assert.h" #include "ns3/log.h" #include "ns3/composite-trace-resolver.h" +#include "ns3/object-base.h" #include 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, diff --git a/src/simulator/event-impl.h b/src/simulator/event-impl.h index 9fb967577..dcb63e0ff 100644 --- a/src/simulator/event-impl.h +++ b/src/simulator/event-impl.h @@ -21,10 +21,12 @@ #define EVENT_IMPL_H #include +#include "ns3/object-base.h" namespace ns3 { -class EventImpl { +class EventImpl : public ObjectBase +{ public: EventImpl (); inline void Ref (void) const; diff --git a/src/simulator/simulator.cc b/src/simulator/simulator.cc index 35b774e5e..aec7007cc 100644 --- a/src/simulator/simulator.cc +++ b/src/simulator/simulator.cc @@ -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<>