Skip to content

Commit

Permalink
core: Use variadic templates for traced callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Krotov authored and stavallo committed Nov 13, 2018
1 parent a6e4169 commit 79d5fb9
Showing 1 changed file with 28 additions and 276 deletions.
304 changes: 28 additions & 276 deletions src/core/model/traced-callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,19 @@ namespace ns3 {
* \ingroup tracing
* \brief Forward calls to a chain of Callback
*
* An TracedCallback has almost exactly the same API as a normal
* A TracedCallback has almost exactly the same API as a normal
* Callback but instead of forwarding calls to a single function
* (as a Callback normally does), it forwards calls to a chain
* of Callback. Connect adds a Callback at the end of the chain
* of callbacks. Disconnect removes a Callback from the chain of callbacks.
*
* This is a functor: the chain of Callbacks is invoked by
* calling one of the \c operator() forms with the appropriate
* calling the \c operator() form with the appropriate
* number of arguments.
*
* \tparam T1 \explicit Type of the first argument to the functor.
* \tparam T2 \explicit Type of the second argument to the functor.
* \tparam T3 \explicit Type of the third argument to the functor.
* \tparam T4 \explicit Type of the fourth argument to the functor.
* \tparam T5 \explicit Type of the fifth argument to the functor.
* \tparam T6 \explicit Type of the sixth argument to the functor.
* \tparam T7 \explicit Type of the seventh argument to the functor.
* \tparam T8 \explicit Type of the eighth argument to the functor.
* \tparam Ts \explicit Types of the functor arguments.
*/
template<typename T1 = empty, typename T2 = empty,
typename T3 = empty, typename T4 = empty,
typename T5 = empty, typename T6 = empty,
typename T7 = empty, typename T8 = empty>
template<typename... Ts>
class TracedCallback
{
public:
Expand Down Expand Up @@ -94,120 +84,11 @@ class TracedCallback
*/
void Disconnect (const CallbackBase & callback, std::string path);
/**
* \name Functors taking various numbers of arguments.
*
* The version selected is determined by the number of arguments
* at the point where the Callback is invoked in the class
* which fires the Callback.
*/
/**@{*/
/** Functor which invokes the chain of Callbacks. */
void operator() (void) const;
/**
* \copybrief operator()()
* \tparam T1 \deduced Type of the first argument to the functor.
* \param [in] a1 The first argument to the functor.
*/
void operator() (T1 a1) const;
/**
* \copybrief operator()()
* \tparam T1 \deduced Type of the first argument to the functor.
* \tparam T2 \deduced Type of the second argument to the functor.
* \param [in] a1 The first argument to the functor.
* \param [in] a2 The second argument to the functor.
* \brief Functor which invokes the chain of Callbacks.
* \tparam Ts \deduced Types of the functor arguments.
* \param [in] args The arguments to the functor.
*/
void operator() (T1 a1, T2 a2) const;
/**
* \copybrief operator()()
* \tparam T1 \deduced Type of the first argument to the functor.
* \tparam T2 \deduced Type of the second argument to the functor.
* \tparam T3 \deduced Type of the third argument to the functor.
* \param [in] a1 The first argument to the functor.
* \param [in] a2 The second argument to the functor.
* \param [in] a3 The third argument to the functor.
*/
void operator() (T1 a1, T2 a2, T3 a3) const;
/**
* \copybrief operator()()
* \tparam T1 \deduced Type of the first argument to the functor.
* \tparam T2 \deduced Type of the second argument to the functor.
* \tparam T3 \deduced Type of the third argument to the functor.
* \tparam T4 \deduced Type of the fourth argument to the functor.
* \param [in] a1 The first argument to the functor.
* \param [in] a2 The second argument to the functor.
* \param [in] a3 The third argument to the functor.
* \param [in] a4 The fourth argument to the functor.
*/
void operator() (T1 a1, T2 a2, T3 a3, T4 a4) const;
/**
* \copybrief operator()()
* \tparam T1 \deduced Type of the first argument to the functor.
* \tparam T2 \deduced Type of the second argument to the functor.
* \tparam T3 \deduced Type of the third argument to the functor.
* \tparam T4 \deduced Type of the fourth argument to the functor.
* \tparam T5 \deduced Type of the fifth argument to the functor.
* \param [in] a1 The first argument to the functor.
* \param [in] a2 The second argument to the functor.
* \param [in] a3 The third argument to the functor.
* \param [in] a4 The fourth argument to the functor.
* \param [in] a5 The fifth argument to the functor.
*/
void operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) const;
/**
* \copybrief operator()()
* \tparam T1 \deduced Type of the first argument to the functor.
* \tparam T2 \deduced Type of the second argument to the functor.
* \tparam T3 \deduced Type of the third argument to the functor.
* \tparam T4 \deduced Type of the fourth argument to the functor.
* \tparam T5 \deduced Type of the fifth argument to the functor.
* \tparam T6 \deduced Type of the sixth argument to the functor.
* \param [in] a1 The first argument to the functor.
* \param [in] a2 The second argument to the functor.
* \param [in] a3 The third argument to the functor.
* \param [in] a4 The fourth argument to the functor.
* \param [in] a5 The fifth argument to the functor.
* \param [in] a6 The sixth argument to the functor.
*/
void operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) const;
/**
* \copybrief operator()()
* \tparam T1 \deduced Type of the first argument to the functor.
* \tparam T2 \deduced Type of the second argument to the functor.
* \tparam T3 \deduced Type of the third argument to the functor.
* \tparam T4 \deduced Type of the fourth argument to the functor.
* \tparam T5 \deduced Type of the fifth argument to the functor.
* \tparam T6 \deduced Type of the sixth argument to the functor.
* \tparam T7 \deduced Type of the seventh argument to the functor.
* \param [in] a1 The first argument to the functor.
* \param [in] a2 The second argument to the functor.
* \param [in] a3 The third argument to the functor.
* \param [in] a4 The fourth argument to the functor.
* \param [in] a5 The fifth argument to the functor.
* \param [in] a6 The sixth argument to the functor.
* \param [in] a7 The seventh argument to the functor.
*/
void operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7) const;
/**
* \copybrief operator()()
* \tparam T1 \deduced Type of the first argument to the functor.
* \tparam T2 \deduced Type of the second argument to the functor.
* \tparam T3 \deduced Type of the third argument to the functor.
* \tparam T4 \deduced Type of the fourth argument to the functor.
* \tparam T5 \deduced Type of the fifth argument to the functor.
* \tparam T6 \deduced Type of the sixth argument to the functor.
* \tparam T7 \deduced Type of the seventh argument to the functor.
* \tparam T8 \deduced Type of the eighth argument to the functor.
* \param [in] a1 The first argument to the functor.
* \param [in] a2 The second argument to the functor.
* \param [in] a3 The third argument to the functor.
* \param [in] a4 The fourth argument to the functor.
* \param [in] a5 The fifth argument to the functor.
* \param [in] a6 The sixth argument to the functor.
* \param [in] a7 The seventh argument to the functor.
* \param [in] a8 The eighth argument to the functor.
*/
void operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7, T8 a8) const;
/**@}*/
void operator() (Ts... args) const;

/**
* TracedCallback signature for POD.
Expand All @@ -225,16 +106,9 @@ class TracedCallback
/**
* Container type for holding the chain of Callbacks.
*
* \tparam T1 \deduced Type of the first argument to the functor.
* \tparam T2 \deduced Type of the second argument to the functor.
* \tparam T3 \deduced Type of the third argument to the functor.
* \tparam T4 \deduced Type of the fourth argument to the functor.
* \tparam T5 \deduced Type of the fifth argument to the functor.
* \tparam T6 \deduced Type of the sixth argument to the functor.
* \tparam T7 \deduced Type of the seventh argument to the functor.
* \tparam T8 \deduced Type of the eighth argument to the functor.
* \tparam Ts \deduced Types of the functor arguments.
*/
typedef std::list<Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> > CallbackList;
typedef std::list<Callback<void,Ts...> > CallbackList;
/** The chain of Callbacks. */
CallbackList m_callbackList;
};
Expand All @@ -248,45 +122,33 @@ class TracedCallback

namespace ns3 {

template<typename T1, typename T2,
typename T3, typename T4,
typename T5, typename T6,
typename T7, typename T8>
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::TracedCallback ()
template<typename... Ts>
TracedCallback<Ts...>::TracedCallback ()
: m_callbackList ()
{
}
template<typename T1, typename T2,
typename T3, typename T4,
typename T5, typename T6,
typename T7, typename T8>
template<typename... Ts>
void
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::ConnectWithoutContext (const CallbackBase & callback)
TracedCallback<Ts...>::ConnectWithoutContext (const CallbackBase & callback)
{
Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> cb;
Callback<void,Ts...> cb;
if (!cb.Assign (callback))
NS_FATAL_ERROR_NO_MSG();
m_callbackList.push_back (cb);
}
template<typename T1, typename T2,
typename T3, typename T4,
typename T5, typename T6,
typename T7, typename T8>
template<typename... Ts>
void
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::Connect (const CallbackBase & callback, std::string path)
TracedCallback<Ts...>::Connect (const CallbackBase & callback, std::string path)
{
Callback<void,std::string,T1,T2,T3,T4,T5,T6,T7,T8> cb;
Callback<void,std::string,Ts...> cb;
if (!cb.Assign (callback))
NS_FATAL_ERROR ("when connecting to " << path);
Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> realCb = cb.Bind (path);
Callback<void,Ts...> realCb = cb.Bind (path);
m_callbackList.push_back (realCb);
}
template<typename T1, typename T2,
typename T3, typename T4,
typename T5, typename T6,
typename T7, typename T8>
template<typename... Ts>
void
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::DisconnectWithoutContext (const CallbackBase & callback)
TracedCallback<Ts...>::DisconnectWithoutContext (const CallbackBase & callback)
{
for (typename CallbackList::iterator i = m_callbackList.begin ();
i != m_callbackList.end (); /* empty */)
Expand All @@ -301,134 +163,24 @@ TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::DisconnectWithoutContext (const Callbac
}
}
}
template<typename T1, typename T2,
typename T3, typename T4,
typename T5, typename T6,
typename T7, typename T8>
template<typename... Ts>
void
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::Disconnect (const CallbackBase & callback, std::string path)
TracedCallback<Ts...>::Disconnect (const CallbackBase & callback, std::string path)
{
Callback<void,std::string,T1,T2,T3,T4,T5,T6,T7,T8> cb;
Callback<void,std::string,Ts...> cb;
if (!cb.Assign (callback))
NS_FATAL_ERROR ("when disconnecting from " << path);
Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> realCb = cb.Bind (path);
Callback<void,Ts...> realCb = cb.Bind (path);
DisconnectWithoutContext (realCb);
}
template<typename T1, typename T2,
typename T3, typename T4,
typename T5, typename T6,
typename T7, typename T8>
void
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (void) const
{
for (typename CallbackList::const_iterator i = m_callbackList.begin ();
i != m_callbackList.end (); i++)
{
(*i)();
}
}
template<typename T1, typename T2,
typename T3, typename T4,
typename T5, typename T6,
typename T7, typename T8>
void
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1) const
{
for (typename CallbackList::const_iterator i = m_callbackList.begin ();
i != m_callbackList.end (); i++)
{
(*i)(a1);
}
}
template<typename T1, typename T2,
typename T3, typename T4,
typename T5, typename T6,
typename T7, typename T8>
void
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2) const
{
for (typename CallbackList::const_iterator i = m_callbackList.begin ();
i != m_callbackList.end (); i++)
{
(*i)(a1, a2);
}
}
template<typename T1, typename T2,
typename T3, typename T4,
typename T5, typename T6,
typename T7, typename T8>
void
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3) const
{
for (typename CallbackList::const_iterator i = m_callbackList.begin ();
i != m_callbackList.end (); i++)
{
(*i)(a1, a2, a3);
}
}
template<typename T1, typename T2,
typename T3, typename T4,
typename T5, typename T6,
typename T7, typename T8>
void
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4) const
{
for (typename CallbackList::const_iterator i = m_callbackList.begin ();
i != m_callbackList.end (); i++)
{
(*i)(a1, a2, a3, a4);
}
}
template<typename T1, typename T2,
typename T3, typename T4,
typename T5, typename T6,
typename T7, typename T8>
void
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) const
{
for (typename CallbackList::const_iterator i = m_callbackList.begin ();
i != m_callbackList.end (); i++)
{
(*i)(a1, a2, a3, a4, a5);
}
}
template<typename T1, typename T2,
typename T3, typename T4,
typename T5, typename T6,
typename T7, typename T8>
void
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) const
{
for (typename CallbackList::const_iterator i = m_callbackList.begin ();
i != m_callbackList.end (); i++)
{
(*i)(a1, a2, a3, a4, a5, a6);
}
}
template<typename T1, typename T2,
typename T3, typename T4,
typename T5, typename T6,
typename T7, typename T8>
void
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7) const
{
for (typename CallbackList::const_iterator i = m_callbackList.begin ();
i != m_callbackList.end (); i++)
{
(*i)(a1, a2, a3, a4, a5, a6, a7);
}
}
template<typename T1, typename T2,
typename T3, typename T4,
typename T5, typename T6,
typename T7, typename T8>
template<typename... Ts>
void
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7, T8 a8) const
TracedCallback<Ts...>::operator() (Ts... args) const
{
for (typename CallbackList::const_iterator i = m_callbackList.begin ();
i != m_callbackList.end (); i++)
{
(*i)(a1, a2, a3, a4, a5, a6, a7, a8);
(*i)(args...);
}
}

Expand Down

0 comments on commit 79d5fb9

Please sign in to comment.