A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
callback.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 * Stefano Avallone <stavallo@unina.it>
19 */
20
21#ifndef CALLBACK_H
22#define CALLBACK_H
23
24#include "attribute-helper.h"
25#include "attribute.h"
26#include "fatal-error.h"
27#include "ptr.h"
28#include "simple-ref-count.h"
29
30#include <functional>
31#include <memory>
32#include <typeinfo>
33#include <utility>
34#include <vector>
35
42namespace ns3
43{
44
45// Define the doxygen subgroups all at once,
46// since the implementations are interleaved.
47
80class CallbackImplBase : public SimpleRefCount<CallbackImplBase>
81{
82 public:
85 {
86 }
87
94 virtual bool IsEqual(Ptr<const CallbackImplBase> other) const = 0;
99 virtual std::string GetTypeid() const = 0;
100
101 protected:
106 static std::string Demangle(const std::string& mangled);
107
114 template <typename T>
115 static std::string GetCppTypeid()
116 {
117 std::string typeName;
118 try
119 {
120 typeName = typeid(T).name();
121 typeName = Demangle(typeName);
122 }
123 catch (const std::bad_typeid& e)
124 {
125 typeName = e.what();
126 }
127 return typeName;
128 }
129};
130
137{
138 public:
141 {
142 }
143
150 virtual bool IsEqual(std::shared_ptr<const CallbackComponentBase> other) const = 0;
151};
152
162template <typename T, bool isComparable = true>
164{
165 public:
172 : m_comp(t)
173 {
174 }
175
182 bool IsEqual(std::shared_ptr<const CallbackComponentBase> other) const override
183 {
184 auto p = std::dynamic_pointer_cast<const CallbackComponent<T>>(other);
185
186 // other must have the same type and value as ours
187 return !(p == nullptr || p->m_comp != m_comp);
188 }
189
190 private:
192};
193
205template <typename T>
207{
208 public:
215 {
216 }
217
224 bool IsEqual(std::shared_ptr<const CallbackComponentBase> other) const override
225 {
226 return false;
227 }
228};
229
231typedef std::vector<std::shared_ptr<CallbackComponentBase>> CallbackComponentVector;
232
240template <typename R, typename... UArgs>
242{
243 public:
250 CallbackImpl(std::function<R(UArgs...)> func, const CallbackComponentVector& components)
251 : m_func(func),
252 m_components(components)
253 {
254 }
255
260 const std::function<R(UArgs...)>& GetFunction() const
261 {
262 return m_func;
263 }
264
270 {
271 return m_components;
272 }
273
280 R operator()(UArgs... uargs) const
281 {
282 return m_func(uargs...);
283 }
284
285 bool IsEqual(Ptr<const CallbackImplBase> other) const override
286 {
287 const CallbackImpl<R, UArgs...>* otherDerived =
288 dynamic_cast<const CallbackImpl<R, UArgs...>*>(PeekPointer(other));
289
290 if (otherDerived == nullptr)
291 {
292 return false;
293 }
294
295 // if the two callback implementations are made of a distinct number of
296 // components, they are different
297 if (m_components.size() != otherDerived->GetComponents().size())
298 {
299 return false;
300 }
301
302 // the two functions are equal if they compare equal or the shared pointers
303 // point to the same locations
304 if (!m_components.at(0)->IsEqual(otherDerived->GetComponents().at(0)) &&
305 m_components.at(0) != otherDerived->GetComponents().at(0))
306 {
307 return false;
308 }
309
310 // check if the remaining components are equal one by one
311 for (std::size_t i = 1; i < m_components.size(); i++)
312 {
313 if (!m_components.at(i)->IsEqual(otherDerived->GetComponents().at(i)))
314 {
315 return false;
316 }
317 }
318
319 return true;
320 }
321
322 std::string GetTypeid() const override
323 {
324 return DoGetTypeid();
325 }
326
328 static std::string DoGetTypeid()
329 {
330 static std::vector<std::string> vec = {GetCppTypeid<R>(), GetCppTypeid<UArgs>()...};
331
332 static std::string id("CallbackImpl<");
333 for (auto& s : vec)
334 {
335 id.append(s + ",");
336 }
337 if (id.back() == ',')
338 {
339 id.pop_back();
340 }
341 id.push_back('>');
342
343 return id;
344 }
345
346 private:
348 std::function<R(UArgs...)> m_func;
349
351 std::vector<std::shared_ptr<CallbackComponentBase>> m_components;
352};
353
360{
361 public:
363 : m_impl()
364 {
365 }
366
369 {
370 return m_impl;
371 }
372
373 protected:
379 : m_impl(impl)
380 {
381 }
382
384};
385
436template <typename R, typename... UArgs>
437class Callback : public CallbackBase
438{
439 template <typename ROther, typename... UArgsOther>
440 friend class Callback;
441
442 public:
444 {
445 }
446
453 : CallbackBase(impl)
454 {
455 }
456
464 template <typename... BArgs>
465 Callback(const Callback<R, BArgs..., UArgs...>& cb, BArgs... bargs)
466 {
467 auto f = cb.DoPeekImpl()->GetFunction();
468
469 CallbackComponentVector components(cb.DoPeekImpl()->GetComponents());
470 components.insert(components.end(), {std::make_shared<CallbackComponent<BArgs>>(bargs)...});
471
472 m_impl = Create<CallbackImpl<R, UArgs...>>(
473 [f, bargs...](auto&&... uargs) -> R {
474 return f(bargs..., std::forward<decltype(uargs)>(uargs)...);
475 },
476 components);
477 }
478
491 template <typename T,
492 std::enable_if_t<!std::is_base_of_v<CallbackBase, T>, int> = 0,
493 typename... BArgs>
494 Callback(T func, BArgs... bargs)
495 {
496 // store the function in a std::function object
497 std::function<R(BArgs..., UArgs...)> f(func);
498
499 // The original function is comparable if it is a function pointer or
500 // a pointer to a member function or a pointer to a member data.
501 constexpr bool isComp =
502 std::is_function_v<std::remove_pointer_t<T>> || std::is_member_pointer_v<T>;
503
504 CallbackComponentVector components({std::make_shared<CallbackComponent<T, isComp>>(func),
505 std::make_shared<CallbackComponent<BArgs>>(bargs)...});
506
507 m_impl = Create<CallbackImpl<R, UArgs...>>(
508 [f, bargs...](auto&&... uargs) -> R {
509 return f(bargs..., std::forward<decltype(uargs)>(uargs)...);
510 },
511 components);
512 }
513
514 private:
526 template <std::size_t... INDEX, typename... BoundArgs>
527 auto BindImpl(std::index_sequence<INDEX...> seq, BoundArgs&&... bargs)
528 {
529 Callback<R, std::tuple_element_t<sizeof...(bargs) + INDEX, std::tuple<UArgs...>>...> cb;
530
531 const auto f = DoPeekImpl()->GetFunction();
532
533 CallbackComponentVector components(DoPeekImpl()->GetComponents());
534 components.insert(components.end(),
535 {std::make_shared<CallbackComponent<BoundArgs>>(bargs)...});
536
537 cb.m_impl = Create<std::remove_pointer_t<decltype(cb.DoPeekImpl())>>(
538 [f, bargs...](auto&&... uargs) mutable {
539 return f(bargs..., std::forward<decltype(uargs)>(uargs)...);
540 },
541 components);
542
543 return cb;
544 }
545
546 public:
554 template <typename... BoundArgs>
555 auto Bind(BoundArgs&&... bargs)
556 {
557 static_assert(sizeof...(UArgs) > 0);
558 return BindImpl(std::make_index_sequence<sizeof...(UArgs) - sizeof...(BoundArgs)>{},
559 std::forward<BoundArgs>(bargs)...);
560 }
561
567 bool IsNull() const
568 {
569 return (DoPeekImpl() == nullptr);
570 }
571
573 void Nullify()
574 {
575 m_impl = nullptr;
576 }
577
584 R operator()(UArgs... uargs) const
585 {
586 return (*(DoPeekImpl()))(uargs...);
587 }
588
595 bool IsEqual(const CallbackBase& other) const
596 {
597 return m_impl->IsEqual(other.GetImpl());
598 }
599
606 bool CheckType(const CallbackBase& other) const
607 {
608 return DoCheckType(other.GetImpl());
609 }
610
617 bool Assign(const CallbackBase& other)
618 {
619 auto otherImpl = other.GetImpl();
620 if (!DoCheckType(otherImpl))
621 {
622 std::string othTid = otherImpl->GetTypeid();
623 std::string myTid = CallbackImpl<R, UArgs...>::DoGetTypeid();
624 NS_FATAL_ERROR_CONT("Incompatible types. (feed to \"c++filt -t\" if needed)"
625 << std::endl
626 << "got=" << othTid << std::endl
627 << "expected=" << myTid);
628 return false;
629 }
630 m_impl = const_cast<CallbackImplBase*>(PeekPointer(otherImpl));
631 return true;
632 }
633
634 private:
636 CallbackImpl<R, UArgs...>* DoPeekImpl() const
637 {
638 return static_cast<CallbackImpl<R, UArgs...>*>(PeekPointer(m_impl));
639 }
640
648 {
649 if (other && dynamic_cast<const CallbackImpl<R, UArgs...>*>(PeekPointer(other)) != nullptr)
650 {
651 return true;
652 }
653 else if (!other)
654 {
655 return true;
656 }
657 else
658 {
659 return false;
660 }
661 }
662};
663
674template <typename R, typename... Args>
675bool
677{
678 return !a.IsEqual(b);
679}
680
700template <typename T, typename OBJ, typename R, typename... Args>
701Callback<R, Args...>
702MakeCallback(R (T::*memPtr)(Args...), OBJ objPtr)
703{
704 return Callback<R, Args...>(memPtr, objPtr);
705}
706
707template <typename T, typename OBJ, typename R, typename... Args>
708Callback<R, Args...>
709MakeCallback(R (T::*memPtr)(Args...) const, OBJ objPtr)
710{
711 return Callback<R, Args...>(memPtr, objPtr);
712}
713
726template <typename R, typename... Args>
727Callback<R, Args...>
728MakeCallback(R (*fnPtr)(Args...))
729{
730 return Callback<R, Args...>(fnPtr);
731}
732
743template <typename R, typename... Args>
744Callback<R, Args...>
746{
747 return Callback<R, Args...>();
748}
749
761template <typename R, typename... Args, typename... BArgs>
762auto
763MakeBoundCallback(R (*fnPtr)(Args...), BArgs&&... bargs)
764{
765 return Callback<R, Args...>(fnPtr).Bind(std::forward<BArgs>(bargs)...);
766}
767
782template <typename T, typename OBJ, typename R, typename... Args, typename... BArgs>
783auto
784MakeCallback(R (T::*memPtr)(Args...), OBJ objPtr, BArgs... bargs)
785{
786 return Callback<R, Args...>(memPtr, objPtr).Bind(bargs...);
787}
788
789template <typename T, typename OBJ, typename R, typename... Args, typename... BArgs>
790auto
791MakeCallback(R (T::*memPtr)(Args...) const, OBJ objPtr, BArgs... bargs)
792{
793 return Callback<R, Args...>(memPtr, objPtr).Bind(bargs...);
794}
795
798} // namespace ns3
799
800namespace ns3
801{
802
804{
805 public:
812 CallbackValue(const CallbackBase& base);
814 ~CallbackValue() override;
816 void Set(CallbackBase base);
817 /* Documented by print-introspected-doxygen.cc */
818 template <typename T>
819 bool GetAccessor(T& value) const;
821 Ptr<AttributeValue> Copy() const override;
827 std::string SerializeToString(Ptr<const AttributeChecker> checker) const override;
835 bool DeserializeFromString(std::string value, Ptr<const AttributeChecker> checker) override;
836
837 private:
838 /* Documented by print-introspected-doxygen.cc */
840};
841
844
845} // namespace ns3
846
847namespace ns3
848{
849
850template <typename T>
851bool
853{
854 if (value.CheckType(m_value))
855 {
856 if (!value.Assign(m_value))
857 {
859 }
860 return true;
861 }
862 return false;
863}
864
865} // namespace ns3
866
867#endif /* CALLBACK_H */
double f(double x, void *params)
Definition: 80211b.c:70
Attribute helper (ATTRIBUTE_ )macros definition.
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Hold a value for an Attribute.
Definition: attribute.h:70
Base class for Callback class.
Definition: callback.h:360
CallbackBase(Ptr< CallbackImplBase > impl)
Construct from a pimpl.
Definition: callback.h:378
Ptr< CallbackImplBase > m_impl
the pimpl
Definition: callback.h:383
Ptr< CallbackImplBase > GetImpl() const
Definition: callback.h:368
bool IsEqual(std::shared_ptr< const CallbackComponentBase > other) const override
Equality test between functions.
Definition: callback.h:224
CallbackComponent(const T &t)
Constructor.
Definition: callback.h:214
Abstract base class for CallbackComponent.
Definition: callback.h:137
virtual bool IsEqual(std::shared_ptr< const CallbackComponentBase > other) const =0
Equality test.
virtual ~CallbackComponentBase()
Virtual destructor.
Definition: callback.h:140
Stores a component of a callback, i.e., the callable object or a bound argument.
Definition: callback.h:164
CallbackComponent(const T &t)
Constructor.
Definition: callback.h:171
T m_comp
the value of the callback component
Definition: callback.h:191
bool IsEqual(std::shared_ptr< const CallbackComponentBase > other) const override
Equality test between the values of two components.
Definition: callback.h:182
Callback template class.
Definition: callback.h:438
bool IsEqual(const CallbackBase &other) const
Equality test.
Definition: callback.h:595
bool DoCheckType(Ptr< const CallbackImplBase > other) const
Check for compatible types.
Definition: callback.h:647
void Nullify()
Discard the implementation, set it to null.
Definition: callback.h:573
Callback(const Ptr< CallbackImpl< R, UArgs... > > &impl)
Construct from a CallbackImpl pointer.
Definition: callback.h:452
bool IsNull() const
Check for null implementation.
Definition: callback.h:567
CallbackImpl< R, UArgs... > * DoPeekImpl() const
Definition: callback.h:636
R operator()(UArgs... uargs) const
Functor with varying numbers of arguments.
Definition: callback.h:584
auto Bind(BoundArgs &&... bargs)
Bind a variable number of arguments.
Definition: callback.h:555
auto BindImpl(std::index_sequence< INDEX... > seq, BoundArgs &&... bargs)
Implementation of the Bind method.
Definition: callback.h:527
Callback(T func, BArgs... bargs)
Construct from a function and bind some arguments (if any)
Definition: callback.h:494
Callback(const Callback< R, BArgs..., UArgs... > &cb, BArgs... bargs)
Construct from another callback and bind some arguments (if any)
Definition: callback.h:465
bool CheckType(const CallbackBase &other) const
Check for compatible types.
Definition: callback.h:606
bool Assign(const CallbackBase &other)
Adopt the other's implementation, if type compatible.
Definition: callback.h:617
Abstract base class for CallbackImpl Provides reference counting and equality test.
Definition: callback.h:81
virtual bool IsEqual(Ptr< const CallbackImplBase > other) const =0
Equality test.
static std::string GetCppTypeid()
Helper to get the C++ typeid as a string.
Definition: callback.h:115
virtual ~CallbackImplBase()
Virtual destructor.
Definition: callback.h:84
static std::string Demangle(const std::string &mangled)
Definition: callback.cc:142
virtual std::string GetTypeid() const =0
Get the name of this object type.
CallbackImpl class with varying numbers of argument types.
Definition: callback.h:242
std::vector< std::shared_ptr< CallbackComponentBase > > m_components
Stores the original callable object and the bound arguments, if any.
Definition: callback.h:351
std::function< R(UArgs...)> m_func
Stores the callable object associated with this callback (as a lambda)
Definition: callback.h:348
R operator()(UArgs... uargs) const
Function call operator.
Definition: callback.h:280
const std::function< R(UArgs...)> & GetFunction() const
Get the stored function.
Definition: callback.h:260
static std::string DoGetTypeid()
Get the name of this object type.
Definition: callback.h:328
CallbackImpl(std::function< R(UArgs...)> func, const CallbackComponentVector &components)
Constructor.
Definition: callback.h:250
const CallbackComponentVector & GetComponents() const
Get the vector of callback components.
Definition: callback.h:269
std::string GetTypeid() const override
Get the name of this object type.
Definition: callback.h:322
bool IsEqual(Ptr< const CallbackImplBase > other) const override
Equality test.
Definition: callback.h:285
AttributeValue implementation for Callback.
Definition: callback.h:804
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker) override
Deserialize from string (not implemented)
Definition: callback.cc:76
std::string SerializeToString(Ptr< const AttributeChecker > checker) const override
Serialize to string.
Definition: callback.cc:67
void Set(CallbackBase base)
Definition: callback.cc:52
CallbackValue()
Constructor.
Definition: callback.cc:35
bool GetAccessor(T &value) const
Access the Callback value as type T.
Definition: callback.h:852
Ptr< AttributeValue > Copy() const override
Definition: callback.cc:60
~CallbackValue() override
Destructor.
Definition: callback.cc:46
CallbackBase m_value
The stored Callback instance.
Definition: callback.h:839
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
A template-based reference counting class.
NS_FATAL_x macro definitions.
#define ATTRIBUTE_ACCESSOR_DEFINE(type)
Define the attribute accessor functions MakeTypeAccessor for class type.
#define ATTRIBUTE_CHECKER_DEFINE(type)
Declare the AttributeChecker class typeChecker and the MaketypeChecker function for class type.
Callback< R, Args... > MakeNullCallback()
Definition: callback.h:745
#define NS_FATAL_ERROR_CONT(msg)
Report a fatal error with a message, deferring termination.
Definition: fatal-error.h:195
#define NS_FATAL_ERROR_NO_MSG()
Report a fatal error and terminate.
Definition: fatal-error.h:142
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition: callback.h:763
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition: ptr.h:481
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:488
bool operator!=(Callback< R, Args... > a, Callback< R, Args... > b)
Inequality test.
Definition: callback.h:676
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:702
std::vector< std::shared_ptr< CallbackComponentBase > > CallbackComponentVector
Vector of callback components.
Definition: callback.h:231
ns3::Ptr smart pointer declaration and implementation.
ns3::SimpleRefCount declaration and template implementation.