introduce ObjectBase
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed, 30 Jan 2008 17:25:06 +0100
changeset 2370 5f7ad186b798
parent 2255 ac534291636f
child 2371 2118204a86e6
introduce ObjectBase
src/common/packet.h
src/core/callback.h
src/core/object-base.cc
src/core/object-base.h
src/core/object.h
src/core/ptr.cc
src/core/trace-resolver.h
src/core/wscript
src/devices/wifi/wifi-phy.cc
src/simulator/event-impl.h
src/simulator/simulator.cc
--- a/src/common/packet.h	Wed Jan 30 17:20:55 2008 +0100
+++ b/src/common/packet.h	Wed Jan 30 17:25:06 2008 +0100
@@ -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 @@
  * 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;
--- a/src/core/callback.h	Wed Jan 30 17:20:55 2008 +0100
+++ b/src/core/callback.h	Wed Jan 30 17:25:06 2008 +0100
@@ -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 @@
   }
 };
 
-class CallbackImplBase {
+class CallbackImplBase : public ObjectBase 
+{
 public:
   CallbackImplBase ()
     : m_count (1) {}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/core/object-base.cc	Wed Jan 30 17:25:06 2008 +0100
@@ -0,0 +1,3 @@
+#include "object-base.h"
+
+ns3::ObjectBase::~ObjectBase () {}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/core/object-base.h	Wed Jan 30 17:25:06 2008 +0100
@@ -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 */
--- a/src/core/object.h	Wed Jan 30 17:20:55 2008 +0100
+++ b/src/core/object.h	Wed Jan 30 17:25:06 2008 +0100
@@ -132,7 +132,7 @@
  * BonoboObject in Bonobo: it provides three main methods: Ref, Unref and
  * QueryInterface.
  */
-class Object
+class Object : public ObjectBase
 {
 public:
   static TypeId GetTypeId (void);
--- a/src/core/ptr.cc	Wed Jan 30 17:20:55 2008 +0100
+++ b/src/core/ptr.cc	Wed Jan 30 17:25:06 2008 +0100
@@ -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);
+  void DestroyNotify (void);
 private:
-  void DestroyNotify (void);
   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 @@
 {
   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 @@
   m_nDestroyed = 0;
   {
     Ptr<NoCount> p;
-    p = CreateObject<NoCount> (cb);
+    p = Create<NoCount> (this);
     p = p;
   }
   if (m_nDestroyed != 1)
@@ -119,7 +150,7 @@
   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 @@
   m_nDestroyed = 0;
   {
     Ptr<NoCount> p1;
-    p1 = CreateObject<NoCount> (cb);
+    p1 = Create<NoCount> (this);
     Ptr<NoCount> p2;
     p2 = p1;
   }
@@ -142,8 +173,8 @@
   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 @@
   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 @@
   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 @@
     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 @@
     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 @@
   {
     NoCount *raw;
     {
-      Ptr<NoCount> p = CreateObject<NoCount> (cb);
+      Ptr<NoCount> p = Create<NoCount> (this);
       {
         Ptr<NoCount const> p1 = p;
       }
@@ -256,10 +287,9 @@
     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 @@
     }
 
   {
-    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 @@
         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.
--- a/src/core/trace-resolver.h	Wed Jan 30 17:20:55 2008 +0100
+++ b/src/core/trace-resolver.h	Wed Jan 30 17:25:06 2008 +0100
@@ -25,6 +25,7 @@
 #include <list>
 #include "trace-context.h"
 #include "trace-doc.h"
+#include "object-base.h"
 
 namespace ns3 {
 
@@ -39,7 +40,7 @@
  * subclasses, doing so is complicated so, it is recommended to use
  * the default implementation ns3::CompositeTraceResolver instead.
  */
-class TraceResolver
+class TraceResolver : public ObjectBase
 {
 public:
 
--- a/src/core/wscript	Wed Jan 30 17:20:55 2008 +0100
+++ b/src/core/wscript	Wed Jan 30 17:25:06 2008 +0100
@@ -31,6 +31,7 @@
         'callback-test.cc',
         'log.cc',
         'breakpoint.cc',
+        'object-base.cc',
         'ptr.cc',
         'object.cc',
         'test.cc',
@@ -68,6 +69,7 @@
         'system-wall-clock-ms.h',
         'empty.h',
         'callback.h',
+        'object-base.h',
         'ptr.h',
         'object.h',
         'log.h',
--- a/src/devices/wifi/wifi-phy.cc	Wed Jan 30 17:20:55 2008 +0100
+++ b/src/devices/wifi/wifi-phy.cc	Wed Jan 30 17:25:06 2008 +0100
@@ -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 @@
  *       Phy event class
  ****************************************************************/
 
-class RxEvent {
+class RxEvent : public ObjectBase 
+{
 public:
   RxEvent (uint32_t size, WifiMode payloadMode, 
            enum WifiPreamble preamble,
--- a/src/simulator/event-impl.h	Wed Jan 30 17:20:55 2008 +0100
+++ b/src/simulator/event-impl.h	Wed Jan 30 17:25:06 2008 +0100
@@ -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;
--- a/src/simulator/simulator.cc	Wed Jan 30 17:20:55 2008 +0100
+++ b/src/simulator/simulator.cc	Wed Jan 30 17:25:06 2008 +0100
@@ -568,7 +568,7 @@
 {}
   
 
-class SimulatorTests : public Test {
+class SimulatorTests : public Test, public ObjectBase {
 public:
   SimulatorTests ();
   // only here for testing of Ptr<>