From 348eb1e187fffdefe4ec2f9716ee5e8dbd67776f Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 2 Jan 2008 13:39:56 +0100 Subject: [PATCH] Make the CallbackBase class more useful than it was. --- src/common/chunk-registry.cc | 2 +- src/common/chunk-registry.h | 8 +-- src/common/packet-printer.h | 6 +-- src/core/callback.h | 97 +++++++++++++++++------------------- 4 files changed, 53 insertions(+), 60 deletions(-) diff --git a/src/common/chunk-registry.cc b/src/common/chunk-registry.cc index 7186524ab..57257a778 100644 --- a/src/common/chunk-registry.cc +++ b/src/common/chunk-registry.cc @@ -105,7 +105,7 @@ ChunkRegistry::GetName (uint32_t uid, uint8_t *instance) void ChunkRegistry::InvokePrintCallback (uint32_t uid, uint8_t *instance, std::ostream &os, uint32_t packetUid, uint32_t size, - Ptr callback) + CallbackBase callback) { InfoVector *vec = GetInfoVector (); NS_ASSERT (uid >= 1 && uid <= vec->size ()); diff --git a/src/common/chunk-registry.h b/src/common/chunk-registry.h index 34e3e42ec..7d1cce212 100644 --- a/src/common/chunk-registry.h +++ b/src/common/chunk-registry.h @@ -54,7 +54,7 @@ public: 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 callback); + CallbackBase callback); private: typedef uint8_t *(*GetStaticInstanceCb) (void); typedef uint32_t (*DeserializeCb) (uint8_t *, Buffer::Iterator); @@ -62,7 +62,7 @@ private: typedef std::string (*GetNameCb) (uint8_t *); typedef void (*InvokePrintCallbackCb) (uint8_t *instance, std::ostream &os, uint32_t packetUid, uint32_t size, - Ptr callback); + CallbackBase callback); struct Info { std::string uidString; bool isHeader; @@ -85,7 +85,7 @@ private: template static void DoInvokePrintCallback (uint8_t *instance, std::ostream &os, uint32_t packetUid, uint32_t size, - Ptr callback); + CallbackBase callback); template static uint32_t GetUid (bool isHeader, std::string uidString); @@ -167,7 +167,7 @@ template void ChunkRegistry::DoInvokePrintCallback (uint8_t *instance, std::ostream &os, uint32_t packetUid, uint32_t size, - Ptr callback) + CallbackBase callback) { T *obj = reinterpret_cast (instance); Callback cb; diff --git a/src/common/packet-printer.h b/src/common/packet-printer.h index 22aede84c..ba789a9fe 100644 --- a/src/common/packet-printer.h +++ b/src/common/packet-printer.h @@ -134,7 +134,7 @@ private: struct Printer { uint32_t m_chunkUid; - Ptr m_printer; + CallbackBase m_printer; Callback m_fragmentPrinter; }; @@ -158,7 +158,7 @@ PacketPrinter::SetHeaderPrinter (Callback other) const = 0; private: - uint32_t m_count; + mutable uint32_t m_count; }; // declare the CallbackImpl class @@ -171,9 +171,9 @@ public: 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 other) const { FunctorCallbackImpl const *otherDerived = - dynamic_cast const *> (other); + dynamic_cast const *> (PeekPointer(other)); if (otherDerived == 0) { return false; @@ -216,9 +216,9 @@ public: R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6) { return ((CallbackTraits::GetReference (m_objPtr)).*m_memPtr) (a1, a2, a3, a4, a5, a6); } - virtual bool IsEqual (CallbackImplBase const *other) const { + virtual bool IsEqual (Ptr other) const { MemPtrCallbackImpl const *otherDerived = - dynamic_cast const *> (other); + dynamic_cast const *> (PeekPointer (other)); if (otherDerived == 0) { return false; @@ -237,9 +237,11 @@ private: class CallbackBase { public: - virtual ~CallbackBase () {} - virtual CallbackImplBase *PeekImpl (void) const = 0; - virtual Ptr GetImpl (void) const = 0; + CallbackBase () : m_impl () {} + Ptr GetImpl (void) const {return m_impl;} +protected: + CallbackBase (Ptr impl) : m_impl (impl) {} + Ptr m_impl; }; /** @@ -277,59 +279,69 @@ template 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 Callback (FUNCTOR const &functor, bool, bool) - : m_impl (Create > (functor)) + : CallbackBase (Create > (functor)) {} template Callback (OBJ_PTR const &objPtr, MEM_PTR mem_ptr) - : m_impl (Create > (objPtr, mem_ptr)) + : CallbackBase (Create > (objPtr, mem_ptr)) {} Callback (Ptr > 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 (CallbackBase const &other) const { - return PeekImpl ()->IsEqual (other.PeekImpl ()); + bool IsEqual (const CallbackBase &other) const { + return m_impl->IsEqual (other.GetImpl ()); } - bool CheckType (CallbackBase const& other) const { - CallbackImplBase *otherBase = other.PeekImpl (); - if (dynamic_cast *> (otherBase) != 0) + bool CheckType (const CallbackBase & other) const { + return DoCheckType (other.GetImpl ()); + } + void Assign (const CallbackBase &other) { + DoAssign (other.GetImpl ()); + } +private: + CallbackImpl *DoPeekImpl (void) const { + return static_cast *> (PeekPointer (m_impl)); + } + bool DoCheckType (Ptr other) const { + if (dynamic_cast *> (PeekPointer (other)) != 0) { return true; } @@ -338,34 +350,15 @@ public: return false; } } - void Assign (CallbackBase const &other) { - if (!CheckType (other)) + void DoAssign (Ptr other) { + if (!DoCheckType (other)) { NS_FATAL_ERROR ("Incompatible types. (feed to \"c++filt -t\")" " got=" << typeid (other).name () << ", expected=" << typeid (*this).name ()); } - const Callback *goodType = static_cast *> (&other); - *this = *goodType; + m_impl = const_cast (PeekPointer (other)); } - void Assign (Ptr other) { - CallbackImpl *impl = dynamic_cast *> (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 (impl); - } - virtual PtrGetImpl (void) const { - return m_impl; - } -private: - virtual CallbackImpl *PeekImpl (void) const { - return PeekPointer (m_impl); - } - Ptr > m_impl; }; /** @@ -675,9 +668,9 @@ public: 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 other) const { BoundFunctorCallbackImpl const *otherDerived = - dynamic_cast const *> (other); + dynamic_cast const *> (PeekPointer (other)); if (otherDerived == 0) { return false;