From 187ae2fe0e3c3bdb28b6a2bfdadcd963ce71db16 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 6 Sep 2007 10:04:19 +0200 Subject: [PATCH] add tests, fix the failing tests and add support for 6-arg callbacks --- src/core/callback-test.cc | 50 +++++++++++++ src/core/callback.h | 148 +++++++++++++++++++++++++++----------- 2 files changed, 155 insertions(+), 43 deletions(-) diff --git a/src/core/callback-test.cc b/src/core/callback-test.cc index 9e8cf259f..0e9811e7b 100644 --- a/src/core/callback-test.cc +++ b/src/core/callback-test.cc @@ -57,6 +57,14 @@ void *Test10 (bool *a, int const & b) return a; } +void TestFZero (void) {} +void TestFOne (int) {} +void TestFTwo (int, int) {} +void TestFThree (int, int, int) {} +void TestFFour (int, int, int, int) {} +void TestFFive (int, int, int, int, int) {} +void TestFSix (int, int, int, int, int, int) {} + class CallbackTest : public ns3::Test { private: bool m_test1; @@ -73,6 +81,22 @@ public: void Test3 (double a); int Test4 (double a, int b); void Test8 (Callback callback); + + void TestZero (void) {} + void TestOne (int) {} + void TestTwo (int, int) {} + void TestThree (int, int, int) {} + void TestFour (int, int, int, int) {} + void TestFive (int, int, int, int, int) {} + void TestSix (int, int, int, int, int, int) {} + + void TestCZero (void) const {} + void TestCOne (int) const {} + void TestCTwo (int, int) const {} + void TestCThree (int, int, int) const {} + void TestCFour (int, int, int, int) const {} + void TestCFive (int, int, int, int, int) const {} + void TestCSix (int, int, int, int, int, int) const {} }; CallbackTest::CallbackTest () @@ -110,6 +134,7 @@ CallbackTest::Test8 (Callback callback) { callback (3); } + bool CallbackTest::IsWrong (void) { @@ -216,6 +241,31 @@ CallbackTest::RunTests (void) MakeBoundCallback (&Test9, &v); MakeBoundCallback (&Test10, &v); + + MakeCallback (&CallbackTest::TestZero, this); + MakeCallback (&CallbackTest::TestOne, this); + MakeCallback (&CallbackTest::TestTwo, this); + MakeCallback (&CallbackTest::TestThree, this); + MakeCallback (&CallbackTest::TestFour, this); + MakeCallback (&CallbackTest::TestFive, this); + MakeCallback (&CallbackTest::TestSix, this); + + MakeCallback (&CallbackTest::TestCZero, this); + MakeCallback (&CallbackTest::TestCOne, this); + MakeCallback (&CallbackTest::TestCTwo, this); + MakeCallback (&CallbackTest::TestCThree, this); + MakeCallback (&CallbackTest::TestCFour, this); + MakeCallback (&CallbackTest::TestCFive, this); + MakeCallback (&CallbackTest::TestCSix, this); + + MakeCallback (&TestFZero); + MakeCallback (&TestFOne); + MakeCallback (&TestFTwo); + MakeCallback (&TestFThree); + MakeCallback (&TestFFour); + MakeCallback (&TestFFive); + MakeCallback (&TestFSix); + return ok; } diff --git a/src/core/callback.h b/src/core/callback.h index c5d3cf442..4e43a1436 100644 --- a/src/core/callback.h +++ b/src/core/callback.h @@ -25,6 +25,7 @@ #include "ptr.h" #include "fatal-error.h" #include "empty.h" +#include namespace ns3 { @@ -89,55 +90,62 @@ private: }; // declare the CallbackImpl class -template +template class CallbackImpl; // define CallbackImpl for 0 params template -class CallbackImpl : public CallbackImplBase { +class CallbackImpl : public CallbackImplBase { public: virtual ~CallbackImpl () {} virtual R operator() (void) = 0; }; // define CallbackImpl for 1 params template -class CallbackImpl : public CallbackImplBase { +class CallbackImpl : public CallbackImplBase { public: virtual ~CallbackImpl () {} virtual R operator() (T1) = 0; }; // define CallbackImpl for 2 params template -class CallbackImpl : public CallbackImplBase { +class CallbackImpl : public CallbackImplBase { public: virtual ~CallbackImpl () {} virtual R operator() (T1, T2) = 0; }; // define CallbackImpl for 3 params template -class CallbackImpl : public CallbackImplBase { +class CallbackImpl : public CallbackImplBase { public: virtual ~CallbackImpl () {} virtual R operator() (T1, T2, T3) = 0; }; // define CallbackImpl for 4 params template -class CallbackImpl : public CallbackImplBase { +class CallbackImpl : public CallbackImplBase { public: virtual ~CallbackImpl () {} virtual R operator() (T1, T2, T3, T4) = 0; }; // define CallbackImpl for 5 params template -class CallbackImpl : public CallbackImplBase { +class CallbackImpl : public CallbackImplBase { public: virtual ~CallbackImpl () {} virtual R operator() (T1, T2, T3, T4, T5) = 0; }; +// define CallbackImpl for 6 params + template +class CallbackImpl : public CallbackImplBase { +public: + virtual ~CallbackImpl () {} + virtual R operator() (T1, T2, T3, T4, T5, T6) = 0; +}; // an impl for Functors: -template -class FunctorCallbackImpl : public CallbackImpl { +template +class FunctorCallbackImpl : public CallbackImpl { public: FunctorCallbackImpl (T const &functor) : m_functor (functor) {} @@ -160,9 +168,12 @@ public: R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5) { return m_functor (a1,a2,a3,a4,a5); } + 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 { - FunctorCallbackImpl const *otherDerived = - dynamic_cast const *> (other); + FunctorCallbackImpl const *otherDerived = + dynamic_cast const *> (other); if (otherDerived == 0) { return false; @@ -178,8 +189,8 @@ private: }; // an impl for pointer to member functions -template -class MemPtrCallbackImpl : public CallbackImpl { +template +class MemPtrCallbackImpl : public CallbackImpl { public: MemPtrCallbackImpl (OBJ_PTR const&objPtr, MEM_PTR mem_ptr) : m_objPtr (objPtr), m_memPtr (mem_ptr) {} @@ -202,9 +213,12 @@ public: R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5) { return ((CallbackTraits::GetReference (m_objPtr)).*m_memPtr) (a1, a2, a3, a4, a5); } + 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 { - MemPtrCallbackImpl const *otherDerived = - dynamic_cast const *> (other); + MemPtrCallbackImpl const *otherDerived = + dynamic_cast const *> (other); if (otherDerived == 0) { return false; @@ -260,22 +274,22 @@ public: template + typename T5 = empty, typename T6 = empty> class Callback : public CallbackBase { public: // 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)) + : m_impl (Create > (functor)) {} template Callback (OBJ_PTR const &objPtr, MEM_PTR mem_ptr) - : m_impl (Create > (objPtr, mem_ptr)) + : m_impl (Create > (objPtr, mem_ptr)) {} - Callback (Ptr > const &impl) + Callback (Ptr > const &impl) : m_impl (impl) {} @@ -305,6 +319,9 @@ public: R operator() (T1 a1, T2 a2, T3 a3, T4 a4,T5 a5) const { return (*(PeekImpl ())) (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); + } bool IsEqual (CallbackBase const &other) const { return PeekImpl ()->IsEqual (other.PeekImpl ()); @@ -312,7 +329,7 @@ public: bool CheckType (CallbackBase const& other) const { CallbackImplBase *otherBase = other.PeekImpl (); - if (dynamic_cast *> (otherBase) != 0) + if (dynamic_cast *> (otherBase) != 0) { return true; } @@ -328,27 +345,27 @@ public: " got=" << typeid (other).name () << ", expected=" << typeid (*this).name ()); } - const Callback *goodType = static_cast *> (&other); + const Callback *goodType = static_cast *> (&other); *this = *goodType; } void Assign (Ptr other) { - CallbackImpl *impl = dynamic_cast *> (PeekPointer (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); + *this = Callback (impl); } virtual PtrGetImpl (void) const { return m_impl; } private: - virtual CallbackImpl *PeekImpl (void) const { + virtual CallbackImpl *PeekImpl (void) const { return PeekPointer (m_impl); } - Ptr > m_impl; + Ptr > m_impl; }; /** @@ -369,7 +386,7 @@ Callback MakeCallback (R (T::*memPtr) (void), OBJ objPtr) { return Callback (objPtr, memPtr); } template -Callback MakeCallback (R (T::*mem_ptr) () const, OBJ const objPtr) { +Callback MakeCallback (R (T::*mem_ptr) () const, OBJ objPtr) { return Callback (objPtr, mem_ptr); } /** @@ -381,11 +398,11 @@ Callback MakeCallback (R (T::*mem_ptr) () const, OBJ const objPtr) { * and potentially return a value. */ template -Callback MakeCallback (R (T::*mem_ptr) (T1), OBJ *const objPtr) { +Callback MakeCallback (R (T::*mem_ptr) (T1), OBJ objPtr) { return Callback (objPtr, mem_ptr); } template -Callback MakeCallback (R (T::*mem_ptr) (T1) const, OBJ const *const objPtr) { +Callback MakeCallback (R (T::*mem_ptr) (T1) const, OBJ objPtr) { return Callback (objPtr, mem_ptr); } /** @@ -397,11 +414,11 @@ Callback MakeCallback (R (T::*mem_ptr) (T1) const, OBJ const *const objPtr * and potentially return a value. */ template -Callback MakeCallback (R (T::*mem_ptr) (T1,T2), OBJ *const objPtr) { +Callback MakeCallback (R (T::*mem_ptr) (T1,T2), OBJ objPtr) { return Callback (objPtr, mem_ptr); } template -Callback MakeCallback (R (T::*mem_ptr) (T1,T2) const, OBJ const*const objPtr) { +Callback MakeCallback (R (T::*mem_ptr) (T1,T2) const, OBJ objPtr) { return Callback (objPtr, mem_ptr); } /** @@ -413,11 +430,11 @@ Callback MakeCallback (R (T::*mem_ptr) (T1,T2) const, OBJ const*const o * and potentially return a value. */ template -Callback MakeCallback (R (T::*mem_ptr) (T1,T2,T3), OBJ *const objPtr) { +Callback MakeCallback (R (T::*mem_ptr) (T1,T2,T3), OBJ objPtr) { return Callback (objPtr, mem_ptr); } template -Callback MakeCallback (R (T::*mem_ptr) (T1,T2,T3) const, OBJ const*const objPtr) { +Callback MakeCallback (R (T::*mem_ptr) (T1,T2,T3) const, OBJ objPtr) { return Callback (objPtr, mem_ptr); } /** @@ -429,11 +446,11 @@ Callback MakeCallback (R (T::*mem_ptr) (T1,T2,T3) const, OBJ const*c * and potentially return a value. */ template -Callback MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4), OBJ *const objPtr) { +Callback MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4), OBJ objPtr) { return Callback (objPtr, mem_ptr); } template -Callback MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4) const, OBJ const*const objPtr) { +Callback MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4) const, OBJ objPtr) { return Callback (objPtr, mem_ptr); } /** @@ -445,13 +462,29 @@ Callback MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4) const, OBJ c * and potentially return a value. */ template -Callback MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4,T5), OBJ *const objPtr) { +Callback MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4,T5), OBJ objPtr) { return Callback (objPtr, mem_ptr); } template -Callback MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4,T5) const, OBJ const*const objPtr) { +Callback MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4,T5) const, OBJ objPtr) { return Callback (objPtr, mem_ptr); } +/** + * \ingroup MakeCallback + * \param mem_ptr class method member pointer + * \param objPtr class instance + * \return a wrapper Callback + * Build Callbacks for class method members which takes five arguments + * and potentially return a value. + */ +template +Callback MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4,T5,T6), OBJ objPtr) { + return Callback (objPtr, mem_ptr); +} +template +Callback MakeCallback (R (T::*mem_ptr) (T1,T2,T3,T4,T5,T6) const, OBJ objPtr) { + return Callback (objPtr, mem_ptr); +} /** * \ingroup MakeCallback @@ -519,6 +552,17 @@ template MakeCallback (R (*fnPtr) (T1,T2,T3,T4,T5)) { return Callback (fnPtr, true, true); } +/** + * \ingroup MakeCallback + * \param fnPtr function pointer + * \return a wrapper Callback + * Build Callbacks for functions which takes five arguments + * and potentially return a value. + */ +template +Callback MakeCallback (R (*fnPtr) (T1,T2,T3,T4,T5,T6)) { + return Callback (fnPtr, true, true); +} @@ -587,6 +631,17 @@ template MakeNullCallback (void) { return Callback (); } +/** + * \ingroup MakeCallback + * \overload Callback MakeNullCallback (void) + * \return a wrapper Callback + * Build a null callback which takes five arguments + * and potentially return a value. + */ +template +Callback MakeNullCallback (void) { + return Callback (); +} /* @@ -596,9 +651,10 @@ Callback MakeNullCallback (void) { */ // an impl for Bound Functors: template -class BoundFunctorCallbackImpl : public CallbackImpl { +class BoundFunctorCallbackImpl : public CallbackImpl { public: - BoundFunctorCallbackImpl (T const &functor, TX a) + template + BoundFunctorCallbackImpl (FUNCTOR functor, ARG a) : m_functor (functor), m_a (a) {} virtual ~BoundFunctorCallbackImpl () {} R operator() (void) { @@ -640,36 +696,42 @@ private: template Callback MakeBoundCallback (R (*fnPtr) (TX), TX a) { - Ptr > impl = + Ptr > impl = Create >(fnPtr, a); return Callback (impl); } template Callback MakeBoundCallback (R (*fnPtr) (TX,T1), TX a) { - Ptr > impl = + Ptr > impl = Create > (fnPtr, a); return Callback (impl); } template Callback MakeBoundCallback (R (*fnPtr) (TX,T1,T2), TX a) { - Ptr > impl = + Ptr > impl = Create > (fnPtr, a); return Callback (impl); } template Callback MakeBoundCallback (R (*fnPtr) (TX,T1,T2,T3,T4), TX a) { - Ptr > impl = + Ptr > impl = Create > (fnPtr, a); return Callback (impl); } template Callback MakeBoundCallback (R (*fnPtr) (TX,T1,T2,T3,T4,T5), TX a) { - Ptr > impl = + Ptr > impl = Create > (fnPtr, a); return Callback (impl); } +template +Callback MakeBoundCallback (R (*fnPtr) (TX,T1,T2,T3,T4,T5,T6), TX a) { + Ptr > impl = + Create > (fnPtr, a); + return Callback (impl); +} }; // namespace ns3