Make the CallbackBase class more useful than it was.
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed, 02 Jan 2008 13:39:56 +0100
changeset 2206 481d653a1a1e
parent 2205 57710f066013
child 2207 d841cca00990
child 2236 a3a52c5ca2b4
Make the CallbackBase class more useful than it was.
src/common/chunk-registry.cc
src/common/chunk-registry.h
src/common/packet-printer.h
src/core/callback.h
--- a/src/common/chunk-registry.cc	Sun Dec 30 19:36:44 2007 -0800
+++ b/src/common/chunk-registry.cc	Wed Jan 02 13:39:56 2008 +0100
@@ -105,7 +105,7 @@
 void 
 ChunkRegistry::InvokePrintCallback (uint32_t uid, uint8_t *instance, std::ostream &os,
 				    uint32_t packetUid, uint32_t size, 
-				    Ptr<CallbackImplBase> callback)
+                                    CallbackBase callback)
 {
   InfoVector *vec = GetInfoVector ();
   NS_ASSERT (uid >= 1 && uid <= vec->size ());
--- a/src/common/chunk-registry.h	Sun Dec 30 19:36:44 2007 -0800
+++ b/src/common/chunk-registry.h	Wed Jan 02 13:39:56 2008 +0100
@@ -54,7 +54,7 @@
   static bool IsTrailer (uint32_t uid);
   static void InvokePrintCallback (uint32_t uid, uint8_t *instance, std::ostream &os,
 				   uint32_t packetUid, uint32_t size, 
-				   Ptr<CallbackImplBase> callback);
+				   CallbackBase callback);
 private:
   typedef uint8_t *(*GetStaticInstanceCb) (void);
   typedef uint32_t (*DeserializeCb) (uint8_t *, Buffer::Iterator);
@@ -62,7 +62,7 @@
   typedef std::string (*GetNameCb) (uint8_t *);
   typedef void (*InvokePrintCallbackCb) (uint8_t *instance, std::ostream &os,
 					 uint32_t packetUid, uint32_t size, 
-					 Ptr<CallbackImplBase> callback);
+					 CallbackBase callback);
   struct Info {
     std::string uidString;
     bool isHeader;
@@ -85,7 +85,7 @@
   template <typename T>
   static void DoInvokePrintCallback (uint8_t *instance, std::ostream &os,
 				     uint32_t packetUid, uint32_t size, 
-				     Ptr<CallbackImplBase> callback);
+                                     CallbackBase callback);
   template <typename T>
   static uint32_t GetUid (bool isHeader, std::string uidString);
 
@@ -167,7 +167,7 @@
 void 
 ChunkRegistry::DoInvokePrintCallback (uint8_t *instance, std::ostream &os,
 				      uint32_t packetUid, uint32_t size, 
-				      Ptr<CallbackImplBase> callback)
+				      CallbackBase callback)
 {
   T *obj = reinterpret_cast<T *> (instance);
   Callback<void,std::ostream&,uint32_t,uint32_t,const T*> cb;
--- a/src/common/packet-printer.h	Sun Dec 30 19:36:44 2007 -0800
+++ b/src/common/packet-printer.h	Wed Jan 02 13:39:56 2008 +0100
@@ -134,7 +134,7 @@
   struct Printer
   {
     uint32_t m_chunkUid;
-    Ptr<CallbackImplBase> m_printer;
+    CallbackBase m_printer;
     Callback<void,std::ostream &,uint32_t,uint32_t,std::string &,
              struct PacketPrinter::FragmentInformation> m_fragmentPrinter;
   };
@@ -158,7 +158,7 @@
 {
   Printer p;
   p.m_chunkUid = T::GetUid ();
-  p.m_printer = printer.GetImpl ();
+  p.m_printer = printer;
   p.m_fragmentPrinter = fragmentPrinter;
   m_printerList.push_back (p);
 }
@@ -170,7 +170,7 @@
 {
   Printer p;
   p.m_chunkUid = T::GetUid ();
-  p.m_printer = printer.GetImpl ();
+  p.m_printer = printer;
   p.m_fragmentPrinter = fragmentPrinter;
   m_printerList.push_back (p);
 }
--- a/src/core/callback.h	Sun Dec 30 19:36:44 2007 -0800
+++ b/src/core/callback.h	Wed Jan 02 13:39:56 2008 +0100
@@ -75,18 +75,18 @@
   CallbackImplBase ()
     : m_count (1) {}
   virtual ~CallbackImplBase () {}
-  void Ref (void) {
+  void Ref (void) const {
     m_count++;
   }
-  void Unref (void) {
+  void Unref (void) const {
     m_count--;
     if (m_count == 0) {
       delete this;
     }
   }
-  virtual bool IsEqual (CallbackImplBase const *other) const = 0;
+  virtual bool IsEqual (Ptr<const CallbackImplBase> other) const = 0;
 private:
-  uint32_t m_count;
+  mutable uint32_t m_count;
 };
 
 // declare the CallbackImpl class
@@ -171,9 +171,9 @@
   R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6) {
     return m_functor (a1,a2,a3,a4,a5,a6);
   }
-  virtual bool IsEqual (CallbackImplBase const *other) const {
+  virtual bool IsEqual (Ptr<const CallbackImplBase> other) const {
     FunctorCallbackImpl<T,R,T1,T2,T3,T4,T5,T6> const *otherDerived = 
-      dynamic_cast<FunctorCallbackImpl<T,R,T1,T2,T3,T4,T5,T6> const *> (other);
+      dynamic_cast<FunctorCallbackImpl<T,R,T1,T2,T3,T4,T5,T6> const *> (PeekPointer(other));
     if (otherDerived == 0)
       {
         return false;
@@ -216,9 +216,9 @@
   R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6) {
     return ((CallbackTraits<OBJ_PTR>::GetReference (m_objPtr)).*m_memPtr) (a1, a2, a3, a4, a5, a6);
   }
-  virtual bool IsEqual (CallbackImplBase const *other) const {
+  virtual bool IsEqual (Ptr<const CallbackImplBase> other) const {
     MemPtrCallbackImpl<OBJ_PTR,MEM_PTR,R,T1,T2,T3,T4,T5,T6> const *otherDerived = 
-      dynamic_cast<MemPtrCallbackImpl<OBJ_PTR,MEM_PTR,R,T1,T2,T3,T4,T5,T6> const *> (other);
+      dynamic_cast<MemPtrCallbackImpl<OBJ_PTR,MEM_PTR,R,T1,T2,T3,T4,T5,T6> const *> (PeekPointer (other));
     if (otherDerived == 0)
       {
         return false;
@@ -237,9 +237,11 @@
 
 class CallbackBase {
 public:
-  virtual ~CallbackBase () {}
-  virtual CallbackImplBase *PeekImpl (void) const = 0;
-  virtual Ptr<CallbackImplBase> GetImpl (void) const = 0;
+  CallbackBase () : m_impl () {}
+  Ptr<CallbackImplBase> GetImpl (void) const {return m_impl;}
+protected:
+  CallbackBase (Ptr<CallbackImplBase> impl) : m_impl (impl) {}
+  Ptr<CallbackImplBase> m_impl;
 };
 
 /**
@@ -277,59 +279,69 @@
    typename T5 = empty, typename T6 = empty>
 class Callback : public CallbackBase {
 public:
+  Callback () {}
+
   // There are two dummy args below to ensure that this constructor is
   // always properly disambiguited by the c++ compiler
   template <typename FUNCTOR>
   Callback (FUNCTOR const &functor, bool, bool) 
-    : m_impl (Create<FunctorCallbackImpl<FUNCTOR,R,T1,T2,T3,T4,T5,T6> > (functor))
+    : CallbackBase (Create<FunctorCallbackImpl<FUNCTOR,R,T1,T2,T3,T4,T5,T6> > (functor))
   {}
 
   template <typename OBJ_PTR, typename MEM_PTR>
   Callback (OBJ_PTR const &objPtr, MEM_PTR mem_ptr)
-    : m_impl (Create<MemPtrCallbackImpl<OBJ_PTR,MEM_PTR,R,T1,T2,T3,T4,T5,T6> > (objPtr, mem_ptr))
+    : CallbackBase (Create<MemPtrCallbackImpl<OBJ_PTR,MEM_PTR,R,T1,T2,T3,T4,T5,T6> > (objPtr, mem_ptr))
   {}
 
   Callback (Ptr<CallbackImpl<R,T1,T2,T3,T4,T5,T6> > const &impl)
-      : m_impl (impl)
+    : CallbackBase (impl)
   {}
 
   bool IsNull (void) const {
-    return (PeekImpl () == 0)?true:false;
+    return (DoPeekImpl () == 0)?true:false;
   }
   void Nullify (void) {
     m_impl = 0;
   }
 
-  Callback () : m_impl () {}
   R operator() (void) const {
-    return (*(PeekImpl ())) ();
+    return (*(DoPeekImpl ())) ();
   }
   R operator() (T1 a1) const {
-    return (*(PeekImpl ())) (a1);
+    return (*(DoPeekImpl ())) (a1);
   }
   R operator() (T1 a1, T2 a2) const {
-    return (*(PeekImpl ())) (a1,a2);
+    return (*(DoPeekImpl ())) (a1,a2);
   }
   R operator() (T1 a1, T2 a2, T3 a3) const {
-    return (*(PeekImpl ())) (a1,a2,a3);
+    return (*(DoPeekImpl ())) (a1,a2,a3);
   }
   R operator() (T1 a1, T2 a2, T3 a3, T4 a4) const {
-    return (*(PeekImpl ())) (a1,a2,a3,a4);
+    return (*(DoPeekImpl ())) (a1,a2,a3,a4);
   }
   R operator() (T1 a1, T2 a2, T3 a3, T4 a4,T5 a5) const {
-    return (*(PeekImpl ())) (a1,a2,a3,a4,a5);
+    return (*(DoPeekImpl ())) (a1,a2,a3,a4,a5);
   }
   R operator() (T1 a1, T2 a2, T3 a3, T4 a4,T5 a5,T6 a6) const {
-    return (*(PeekImpl ())) (a1,a2,a3,a4,a5,a6);
+    return (*(DoPeekImpl ())) (a1,a2,a3,a4,a5,a6);
+  }
+
+  bool IsEqual (const CallbackBase &other) const {
+    return m_impl->IsEqual (other.GetImpl ());
   }
 
-  bool IsEqual (CallbackBase const &other) const {
-    return PeekImpl ()->IsEqual (other.PeekImpl ());
+  bool CheckType (const CallbackBase & other) const {
+    return DoCheckType (other.GetImpl ());
+  }
+  void Assign (const CallbackBase &other) {
+    DoAssign (other.GetImpl ());
   }
-
-  bool CheckType (CallbackBase const& other) const {
-    CallbackImplBase *otherBase = other.PeekImpl ();
-    if (dynamic_cast<CallbackImpl<R,T1,T2,T3,T4,T5,T6> *> (otherBase) != 0)
+private:
+  CallbackImpl<R,T1,T2,T3,T4,T5,T6> *DoPeekImpl (void) const {
+    return static_cast<CallbackImpl<R,T1,T2,T3,T4,T5,T6> *> (PeekPointer (m_impl));
+  }
+  bool DoCheckType (Ptr<const CallbackImplBase> other) const {
+    if (dynamic_cast<const CallbackImpl<R,T1,T2,T3,T4,T5,T6> *> (PeekPointer (other)) != 0)
       {
         return true;
       }
@@ -338,34 +350,15 @@
         return false;
       }
   }
-  void Assign (CallbackBase const &other) {
-    if (!CheckType (other))
+  void DoAssign (Ptr<const CallbackImplBase> other) {
+    if (!DoCheckType (other))
       {
         NS_FATAL_ERROR ("Incompatible types. (feed to \"c++filt -t\")"
                         " got=" << typeid (other).name () << 
                         ", expected=" << typeid (*this).name ());
       }
-    const Callback<R, T1,T2,T3,T4,T5,T6> *goodType = static_cast<const Callback<R,T1,T2,T3,T4,T5,T6> *> (&other);
-    *this = *goodType;
+    m_impl = const_cast<CallbackImplBase *> (PeekPointer (other));
   }
-  void Assign (Ptr<CallbackImplBase> other) {
-    CallbackImpl<R,T1,T2,T3,T4,T5,T6> *impl = dynamic_cast<CallbackImpl<R,T1,T2,T3,T4,T5,T6> *> (PeekPointer (other));
-    if (other == 0)
-      {
-        NS_FATAL_ERROR ("Incompatible types. (feed to \"c++filt -t\")"
-                        " got=" << typeid (other).name () << 
-                        ", expected=" << typeid (*impl).name ());
-      }
-    *this = Callback<R,T1,T2,T3,T4,T5,T6> (impl);
-  }
-  virtual Ptr<CallbackImplBase>GetImpl (void) const {
-    return m_impl;
-  }
-private:
-  virtual CallbackImpl<R,T1,T2,T3,T4,T5,T6> *PeekImpl (void) const {
-    return PeekPointer (m_impl);
-  }
-  Ptr<CallbackImpl<R,T1,T2,T3,T4,T5,T6> > m_impl;
 };
 
 /**
@@ -675,9 +668,9 @@
   R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5) {
       return m_functor (m_a,a1,a2,a3,a4,a5);
   }
-  virtual bool IsEqual (CallbackImplBase const *other) const {
+  virtual bool IsEqual (Ptr<const CallbackImplBase> other) const {
     BoundFunctorCallbackImpl<T,R,TX,T1,T2,T3,T4,T5> const *otherDerived = 
-      dynamic_cast<BoundFunctorCallbackImpl<T,R,TX,T1,T2,T3,T4,T5> const *> (other);
+      dynamic_cast<BoundFunctorCallbackImpl<T,R,TX,T1,T2,T3,T4,T5> const *> (PeekPointer (other));
     if (otherDerived == 0)
       {
         return false;