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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 * Stefano Avallone <stavallo@unina.it>
8 */
9
10#ifndef CALLBACK_H
11#define CALLBACK_H
12
13#include "attribute-helper.h"
14#include "attribute.h"
15#include "demangle.h"
16#include "fatal-error.h"
17#include "ptr.h"
18#include "simple-ref-count.h"
19
20#include <functional>
21#include <memory>
22#include <typeinfo>
23#include <utility>
24#include <vector>
25
26/**
27 * @file
28 * @ingroup callback
29 * Declaration of the various callback functions.
30 */
31
32namespace ns3
33{
34
35// Define the doxygen subgroups all at once,
36// since the implementations are interleaved.
37
38/**
39 * @ingroup core
40 * @defgroup callback Callbacks
41 * @brief Wrap functions, objects, and arguments into self contained callbacks.
42 *
43 * Wrapped callbacks are at the heart of scheduling events in the
44 * simulator.
45 */
46/**
47 * @ingroup callback
48 * @defgroup callbackimpl Callback Implementation
49 * Callback implementation classes
50 */
51/**
52 * @ingroup callbackimpl
53 * @defgroup makeboundcallback MakeBoundCallback from functions bound with up to three arguments.
54 *
55 * Build bound Callbacks which take varying numbers of arguments,
56 * and potentially returning a value.
57 *
58 * @internal
59 *
60 * The following is experimental code. It works but we have
61 * not yet determined whether or not it is really useful and whether
62 * or not we really want to use it.
63 */
64
65/**
66 * @ingroup callbackimpl
67 * Abstract base class for CallbackImpl
68 * Provides reference counting and equality test.
69 */
70class CallbackImplBase : public SimpleRefCount<CallbackImplBase>
71{
72 public:
73 /** Virtual destructor */
75 {
76 }
77
78 /**
79 * Equality test
80 *
81 * @param [in] other Callback Ptr
82 * @return \c true if we are equal
83 */
84 virtual bool IsEqual(Ptr<const CallbackImplBase> other) const = 0;
85 /**
86 * Get the name of this object type.
87 * @return The object type as a string.
88 */
89 virtual std::string GetTypeid() const = 0;
90
91 protected:
92 /**
93 * Helper to get the C++ typeid as a string.
94 *
95 * @tparam T \explicit The type of the argument.
96 * @returns The result of applying typeid to the template type \pname{T}.
97 */
98 template <typename T>
99 static std::string GetCppTypeid()
100 {
101 std::string typeName;
102 try
103 {
104 typeName = typeid(T).name();
105 typeName = Demangle(typeName);
106 }
107 catch (const std::bad_typeid& e)
108 {
109 typeName = e.what();
110 }
111 return typeName;
112 }
113};
114
115/**
116 * @ingroup callbackimpl
117 * Abstract base class for CallbackComponent.
118 * Provides equality test.
119 */
121{
122 public:
123 /** Virtual destructor */
125 {
126 }
127
128 /**
129 * Equality test
130 *
131 * @param [in] other CallbackComponent Ptr
132 * @return \c true if we are equal
133 */
134 virtual bool IsEqual(std::shared_ptr<const CallbackComponentBase> other) const = 0;
135};
136
137/**
138 * @ingroup callbackimpl
139 * Stores a component of a callback, i.e., the callable object
140 * or a bound argument. The purpose of this class is to test
141 * the equality of the components of two callbacks.
142 *
143 * @tparam T The type of the callback component.
144 * @tparam isComparable whether this callback component can be compared to others of the same type
145 */
146template <typename T, bool isComparable = true>
148{
149 public:
150 /**
151 * Constructor
152 *
153 * @param [in] t The value of the callback component
154 */
156 : m_comp(t)
157 {
158 }
159
160 /**
161 * Equality test between the values of two components
162 *
163 * @param [in] other CallbackComponentBase Ptr
164 * @return \c true if we are equal
165 */
166 bool IsEqual(std::shared_ptr<const CallbackComponentBase> other) const override
167 {
168 auto p = std::dynamic_pointer_cast<const CallbackComponent<T>>(other);
169
170 // other must have the same type and value as ours
171 return !(p == nullptr || p->m_comp != m_comp);
172 }
173
174 private:
175 T m_comp; //!< the value of the callback component
176};
177
178/**
179 * @ingroup callbackimpl
180 * Partial specialization of class CallbackComponent with isComparable equal
181 * to false. This is required to handle callable objects (such as lambdas and
182 * objects returned by std::function and std::bind) that do not provide the
183 * equality operator. Given that these objects cannot be compared and the only
184 * purpose of the class CallbackComponent is to compare values, no object is
185 * stored in this specialized class.
186 *
187 * @tparam T The type of the callback component.
188 */
189template <typename T>
191{
192 public:
193 /**
194 * Constructor
195 *
196 * @param [in] t The value of the callback component
197 */
199 {
200 }
201
202 /**
203 * Equality test between functions
204 *
205 * @param [in] other CallbackParam Ptr
206 * @return \c true if we are equal
207 */
208 bool IsEqual(std::shared_ptr<const CallbackComponentBase> other) const override
209 {
210 return false;
211 }
212};
213
214/// Vector of callback components
215typedef std::vector<std::shared_ptr<CallbackComponentBase>> CallbackComponentVector;
216
217/**
218 * @ingroup callbackimpl
219 * CallbackImpl class with varying numbers of argument types
220 *
221 * @tparam R \explicit The return type of the Callback.
222 * @tparam UArgs \explicit The types of any arguments to the Callback.
223 */
224template <typename R, typename... UArgs>
226{
227 public:
228 /**
229 * Constructor.
230 *
231 * @param func the callable object
232 * @param components the callback components (callable object and bound arguments)
233 */
234 CallbackImpl(std::function<R(UArgs...)> func, const CallbackComponentVector& components)
235 : m_func(func),
236 m_components(components)
237 {
238 }
239
240 /**
241 * Get the stored function.
242 * @return A const reference to the stored function.
243 */
244 const std::function<R(UArgs...)>& GetFunction() const
245 {
246 return m_func;
247 }
248
249 /**
250 * Get the vector of callback components.
251 * @return A const reference to the vector of callback components.
252 */
254 {
255 return m_components;
256 }
257
258 /**
259 * Function call operator.
260 *
261 * @param uargs The arguments to the Callback.
262 * @return Callback value
263 */
264 R operator()(UArgs... uargs) const
265 {
266 return m_func(uargs...);
267 }
268
269 bool IsEqual(Ptr<const CallbackImplBase> other) const override
270 {
271 const auto otherDerived =
272 dynamic_cast<const CallbackImpl<R, UArgs...>*>(PeekPointer(other));
273
274 if (otherDerived == nullptr)
275 {
276 return false;
277 }
278
279 // if the two callback implementations are made of a distinct number of
280 // components, they are different
281 if (m_components.size() != otherDerived->GetComponents().size())
282 {
283 return false;
284 }
285
286 // the two functions are equal if they compare equal or the shared pointers
287 // point to the same locations
288 if (!m_components.at(0)->IsEqual(otherDerived->GetComponents().at(0)) &&
289 m_components.at(0) != otherDerived->GetComponents().at(0))
290 {
291 return false;
292 }
293
294 // check if the remaining components are equal one by one
295 for (std::size_t i = 1; i < m_components.size(); i++)
296 {
297 if (!m_components.at(i)->IsEqual(otherDerived->GetComponents().at(i)))
298 {
299 return false;
300 }
301 }
302
303 return true;
304 }
305
306 std::string GetTypeid() const override
307 {
308 return DoGetTypeid();
309 }
310
311 /** @copydoc GetTypeid() */
312 static std::string DoGetTypeid()
313 {
314 static std::vector<std::string> vec = {GetCppTypeid<R>(), GetCppTypeid<UArgs>()...};
315
316 static std::string id("CallbackImpl<");
317 for (auto& s : vec)
318 {
319 id.append(s + ",");
320 }
321 if (id.back() == ',')
322 {
323 id.pop_back();
324 }
325 id.push_back('>');
326
327 return id;
328 }
329
330 private:
331 /// Stores the callable object associated with this callback (as a lambda)
332 std::function<R(UArgs...)> m_func;
333
334 /// Stores the original callable object and the bound arguments, if any
335 std::vector<std::shared_ptr<CallbackComponentBase>> m_components;
336};
337
338/**
339 * @ingroup callbackimpl
340 * Base class for Callback class.
341 * Provides pimpl abstraction.
342 *
343 * Inheritance graph was not generated because of its size.
344 * @hideinheritancegraph
345 */
347{
348 public:
350 : m_impl()
351 {
352 }
353
354 /** @return The impl pointer */
356 {
357 return m_impl;
358 }
359
360 protected:
361 /**
362 * Construct from a pimpl
363 * @param [in] impl The CallbackImplBase Ptr
364 */
366 : m_impl(impl)
367 {
368 }
369
371};
372
373/**
374 * @ingroup callback
375 * @brief Callback template class
376 *
377 * This class template implements the Functor Design Pattern.
378 * It is used to declare the type of a Callback:
379 * - the first non-optional template argument represents
380 * the return type of the callback.
381 * - the remaining (optional) template arguments represent
382 * the type of the subsequent arguments to the callback.
383 *
384 * Callback instances are built with the \ref MakeCallback
385 * template functions. Callback instances have POD semantics:
386 * the memory they allocate is managed automatically, without
387 * user intervention which allows you to pass around Callback
388 * instances by value.
389 *
390 * Sample code which shows how to use this class template
391 * as well as the function templates \ref MakeCallback :
392 * @include src/core/examples/main-callback.cc
393 *
394 * @internal
395 * This code was originally written based on the techniques
396 * described in http://www.codeproject.com/cpp/TTLFunction.asp
397 * It was subsequently rewritten to follow the architecture
398 * outlined in "Modern C++ Design" by Andrei Alexandrescu in
399 * chapter 5, "Generalized Functors".
400 *
401 * This code uses:
402 * - default template parameters to saves users from having to
403 * specify empty parameters when the number of parameters
404 * is smaller than the maximum supported number
405 * - the pimpl idiom: the Callback class is passed around by
406 * value and delegates the crux of the work to its pimpl
407 * pointer.
408 * - a reference list implementation to implement the Callback's
409 * value semantics.
410 *
411 * This code most notably departs from the alexandrescu
412 * implementation in that it does not use type lists to specify
413 * and pass around the types of the callback arguments.
414 * Of course, it also does not use copy-destruction semantics
415 * and relies on a reference list rather than autoPtr to hold
416 * the pointer.
417 *
418 * @see attribute_Callback
419 *
420 * @tparam R \explicit The return type of the Callback.
421 * @tparam UArgs \explicit The types of any arguments to the Callback.
422 *
423 * Inheritance graph was not generated because of its size.
424 * @hideinheritancegraph
425 */
426template <typename R, typename... UArgs>
427class Callback : public CallbackBase
428{
429 template <typename ROther, typename... UArgsOther>
430 friend class Callback;
431
432 public:
434 {
435 }
436
437 /**
438 * Construct from a CallbackImpl pointer
439 *
440 * @param [in] impl The CallbackImpl Ptr
441 */
443 : CallbackBase(impl)
444 {
445 }
446
447 /**
448 * Construct from another callback and bind some arguments (if any)
449 *
450 * @tparam BArgs \deduced The types of the bound arguments
451 * @param [in] cb The existing callback
452 * @param [in] bargs The values of the bound arguments
453 */
454 template <typename... BArgs>
455 Callback(const Callback<R, BArgs..., UArgs...>& cb, BArgs... bargs)
456 {
457 auto f = cb.DoPeekImpl()->GetFunction();
458
459 CallbackComponentVector components(cb.DoPeekImpl()->GetComponents());
460 components.insert(components.end(),
461 {std::make_shared<CallbackComponent<std::decay_t<BArgs>>>(bargs)...});
462
463 m_impl = Create<CallbackImpl<R, UArgs...>>(
464 [f, bargs...](auto&&... uargs) -> R {
465 return f(bargs..., std::forward<decltype(uargs)>(uargs)...);
466 },
467 components);
468 }
469
470 /**
471 * Construct from a function and bind some arguments (if any)
472 *
473 * @tparam T \deduced The type of the function
474 * @tparam BArgs \deduced The types of the bound arguments
475 * @param [in] func The function
476 * @param [in] bargs The values of the bound arguments
477 *
478 * @internal
479 * We leverage SFINAE to have the compiler discard this constructor when the type
480 * of the first argument is a class derived from CallbackBase (i.e., a Callback).
481 */
482 template <typename T,
483 typename... BArgs,
484 std::enable_if_t<!std::is_base_of_v<CallbackBase, T> &&
485 std::is_invocable_r_v<R, T, BArgs..., UArgs...>,
486 int> = 0>
487 Callback(T func, BArgs... bargs)
488 {
489 // store the function in a std::function object
490 std::function<R(BArgs..., UArgs...)> f(func);
491
492 // The original function is comparable if it is a function pointer or
493 // a pointer to a member function or a pointer to a member data.
494 constexpr bool isComp =
495 std::is_function_v<std::remove_pointer_t<T>> || std::is_member_pointer_v<T>;
496
497 CallbackComponentVector components(
498 {std::make_shared<CallbackComponent<T, isComp>>(func),
499 std::make_shared<CallbackComponent<std::decay_t<BArgs>>>(bargs)...});
500
501 m_impl = Create<CallbackImpl<R, UArgs...>>(
502 [f, bargs...](auto&&... uargs) -> R {
503 return f(bargs..., std::forward<decltype(uargs)>(uargs)...);
504 },
505 components);
506 }
507
508 private:
509 /**
510 * Implementation of the Bind method
511 *
512 * @tparam BoundArgs The types of the arguments to bind
513 * @param [in] seq A compile-time integer sequence
514 * @param [in] bargs The values of the arguments to bind
515 * @return The bound callback
516 *
517 * @internal
518 * The integer sequence is 0..N-1, where N is the number of arguments left unbound.
519 */
520 template <std::size_t... INDEX, typename... BoundArgs>
521 auto BindImpl(std::index_sequence<INDEX...> seq, BoundArgs&&... bargs)
522 {
523 Callback<R, std::tuple_element_t<sizeof...(bargs) + INDEX, std::tuple<UArgs...>>...> cb;
524
525 const auto f = DoPeekImpl()->GetFunction();
526
527 CallbackComponentVector components(DoPeekImpl()->GetComponents());
528 components.insert(components.end(),
529 {std::make_shared<CallbackComponent<std::decay_t<BoundArgs>>>(bargs)...});
530
531 cb.m_impl = Create<std::remove_pointer_t<decltype(cb.DoPeekImpl())>>(
532 [f, bargs...](auto&&... uargs) mutable {
533 return f(bargs..., std::forward<decltype(uargs)>(uargs)...);
534 },
535 components);
536
537 return cb;
538 }
539
540 public:
541 /**
542 * Bind a variable number of arguments
543 *
544 * @tparam BoundArgs \deduced The types of the arguments to bind
545 * @param [in] bargs The values of the arguments to bind
546 * @return The bound callback
547 */
548 template <typename... BoundArgs>
549 auto Bind(BoundArgs&&... bargs)
550 {
551 static_assert(sizeof...(UArgs) > 0);
552 return BindImpl(std::make_index_sequence<sizeof...(UArgs) - sizeof...(BoundArgs)>{},
553 std::forward<BoundArgs>(bargs)...);
554 }
555
556 /**
557 * Check for null implementation
558 *
559 * @return \c true if I don't have an implementation
560 */
561 bool IsNull() const
562 {
563 return (DoPeekImpl() == nullptr);
564 }
565
566 /** Discard the implementation, set it to null */
567 void Nullify()
568 {
569 m_impl = nullptr;
570 }
571
572 /**
573 * Functor with varying numbers of arguments
574 *
575 * @param uargs The arguments to the callback
576 * @return Callback value
577 */
578 R operator()(UArgs... uargs) const
579 {
580 return (*(DoPeekImpl()))(uargs...);
581 }
582
583 /**
584 * Equality test.
585 *
586 * @param [in] other Callback
587 * @return \c true if we are equal
588 */
589 bool IsEqual(const CallbackBase& other) const
590 {
591 return m_impl->IsEqual(other.GetImpl());
592 }
593
594 /**
595 * Check for compatible types
596 *
597 * @param [in] other Callback Ptr
598 * @return \c true if other can be dynamic_cast to my type
599 */
600 bool CheckType(const CallbackBase& other) const
601 {
602 return DoCheckType(other.GetImpl());
603 }
604
605 /**
606 * Adopt the other's implementation, if type compatible
607 *
608 * @param [in] other Callback
609 * @returns \c true if \pname{other} was type-compatible and could be adopted.
610 */
611 bool Assign(const CallbackBase& other)
612 {
613 auto otherImpl = other.GetImpl();
614 if (!DoCheckType(otherImpl))
615 {
616 std::string othTid = otherImpl->GetTypeid();
617 std::string myTid = CallbackImpl<R, UArgs...>::DoGetTypeid();
618 NS_FATAL_ERROR_CONT("Incompatible types. (feed to \"c++filt -t\" if needed)"
619 << std::endl
620 << "got=" << othTid << std::endl
621 << "expected=" << myTid);
622 return false;
623 }
624 m_impl = const_cast<CallbackImplBase*>(PeekPointer(otherImpl));
625 return true;
626 }
627
628 private:
629 /** @return The pimpl pointer */
630 CallbackImpl<R, UArgs...>* DoPeekImpl() const
631 {
632 return static_cast<CallbackImpl<R, UArgs...>*>(PeekPointer(m_impl));
633 }
634
635 /**
636 * Check for compatible types
637 *
638 * @param [in] other Callback Ptr
639 * @return \c true if other can be dynamic_cast to my type
640 */
642 {
643 if (!other)
644 {
645 return true;
646 }
647
648 return (dynamic_cast<const CallbackImpl<R, UArgs...>*>(PeekPointer(other)) != nullptr);
649 }
650};
651
652/**
653 * Inequality test.
654 *
655 * @tparam R \explicit The return type of the Callbacks
656 * @tparam UArgs \explicit The types of any arguments to the Callbacks
657 * @param [in] a Callback
658 * @param [in] b Callback
659 *
660 * @return \c true if the Callbacks are not equal
661 */
662template <typename R, typename... Args>
663bool
668
669/**
670 * @ingroup callback
671 * Build Callbacks for class method members which take varying numbers
672 * of arguments and potentially returning a value.
673 * @{
674 */
675/**
676 * Build Callbacks for class method members
677 *
678 * @tparam T \deduced Type of the class having the member function.
679 * @tparam OBJ \deduced Type of the class instance.
680 * @tparam R \deduced Return type of the callback.
681 * @tparam Args \deduced Type list of any arguments to the member function.
682 *
683 * @param [in] memPtr Class method member pointer
684 * @param [in] objPtr Class instance
685 * @return A wrapper Callback
686 * @hidecaller
687 */
688template <typename T, typename OBJ, typename R, typename... Args>
689Callback<R, Args...>
690MakeCallback(R (T::*memPtr)(Args...), OBJ objPtr)
691{
692 return Callback<R, Args...>(memPtr, objPtr);
693}
694
695template <typename T, typename OBJ, typename R, typename... Args>
696Callback<R, Args...>
697MakeCallback(R (T::*memPtr)(Args...) const, OBJ objPtr)
698{
699 return Callback<R, Args...>(memPtr, objPtr);
700}
701
702/**@}*/
703
704/**
705 * @ingroup callback
706 * Build Callbacks for functions which take varying numbers of arguments
707 * and potentially returning a value.
708 *
709 * @tparam R \deduced Return type of the callback function..
710 * @tparam Args \deduced Type list of any arguments to the member function.
711 * @param [in] fnPtr Function pointer
712 * @return A wrapper Callback
713 * @hidecaller
714 */
715template <typename R, typename... Args>
716Callback<R, Args...>
717MakeCallback(R (*fnPtr)(Args...))
718{
719 return Callback<R, Args...>(fnPtr);
720}
721
722/**
723 * @ingroup callback
724 * Build null Callbacks which take no arguments,
725 * for varying number of template arguments,
726 * and potentially returning a value.
727 * @tparam R \deduced Return type of the callback function..
728 * @tparam Args \deduced Type list of any arguments to the member function.
729 * @return A wrapper Callback
730 * @hidecaller
731 */
732template <typename R, typename... Args>
733Callback<R, Args...>
735{
736 return Callback<R, Args...>();
737}
738
739/**
740 * @ingroup makeboundcallback
741 * @{
742 * Make Callbacks with varying number of bound arguments.
743 * @tparam R \deduced Return type of the callback function..
744 * @tparam Args \deduced Type list of any arguments to the member function.
745 * @tparam BArgs \deduced Type list of bound arguments.
746 * @param [in] fnPtr Function pointer
747 * @param [in] bargs Bound arguments
748 * @return A bound Callback
749 * @hidecaller
750 */
751template <typename R, typename... Args, typename... BArgs>
752auto
753MakeBoundCallback(R (*fnPtr)(Args...), BArgs&&... bargs)
754{
755 return Callback<R, Args...>(fnPtr).Bind(std::forward<BArgs>(bargs)...);
756}
757
758/**
759 * @tparam T \deduced Type of the class having the member function.
760 * @tparam OBJ \deduced Type of the class instance.
761 * @tparam R \deduced Return type of the callback.
762 * @tparam Args \deduced Type list of any arguments to the member function.
763 * @tparam BArgs \deduced Type list of bound arguments.
764 * @param [in] memPtr Class method member pointer
765 * @param [in] objPtr Class instance
766 * @param [in] bargs Bound arguments
767 * @return A wrapper Callback
768 *
769 * Build Callbacks for class method members which take varying numbers of arguments
770 * and potentially returning a value.
771 */
772template <typename T, typename OBJ, typename R, typename... Args, typename... BArgs>
773auto
774MakeCallback(R (T::*memPtr)(Args...), OBJ objPtr, BArgs... bargs)
775{
776 return Callback<R, Args...>(memPtr, objPtr).Bind(bargs...);
777}
778
779template <typename T, typename OBJ, typename R, typename... Args, typename... BArgs>
780auto
781MakeCallback(R (T::*memPtr)(Args...) const, OBJ objPtr, BArgs... bargs)
782{
783 return Callback<R, Args...>(memPtr, objPtr).Bind(bargs...);
784}
785
786/**@}*/
787
788} // namespace ns3
789
790namespace ns3
791{
792
794{
795 public:
797 CallbackValue(const CallbackBase& value);
798 ~CallbackValue() override;
799 // Documented by print-introspected-doxygen.cc
800 void Set(const CallbackBase& value);
802 template <typename T>
803 bool GetAccessor(T& value) const;
804 /** @return A copy of this CallBack */
805 Ptr<AttributeValue> Copy() const override;
806 /**
807 * Serialize to string
808 * @param [in] checker The checker to validate with
809 * @return Serialized form of this Callback.
810 */
811 std::string SerializeToString(Ptr<const AttributeChecker> checker) const override;
812 /**
813 * Deserialize from string (not implemented)
814 *
815 * @param [in] value Source string
816 * @param [in] checker Checker to validate with
817 * @return \c true if successful
818 */
819 bool DeserializeFromString(std::string value, Ptr<const AttributeChecker> checker) override;
820
821 private:
822 /* Documented by print-introspected-doxygen.cc */
824};
825
828
829} // namespace ns3
830
831namespace ns3
832{
833
834template <typename T>
835bool
837{
838 if (value.CheckType(m_value))
839 {
840 if (!value.Assign(m_value))
841 {
843 }
844 return true;
845 }
846 return false;
847}
848
849} // namespace ns3
850
851#endif /* CALLBACK_H */
Attribute helper (ATTRIBUTE_ )macros definition.
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Base class for Callback class.
Definition callback.h:347
CallbackBase(Ptr< CallbackImplBase > impl)
Construct from a pimpl.
Definition callback.h:365
Ptr< CallbackImplBase > m_impl
the pimpl
Definition callback.h:370
Ptr< CallbackImplBase > GetImpl() const
Definition callback.h:355
bool IsEqual(std::shared_ptr< const CallbackComponentBase > other) const override
Equality test between functions.
Definition callback.h:208
CallbackComponent(const T &t)
Constructor.
Definition callback.h:198
Abstract base class for CallbackComponent.
Definition callback.h:121
virtual bool IsEqual(std::shared_ptr< const CallbackComponentBase > other) const =0
Equality test.
virtual ~CallbackComponentBase()
Virtual destructor.
Definition callback.h:124
Stores a component of a callback, i.e., the callable object or a bound argument.
Definition callback.h:148
CallbackComponent(const T &t)
Constructor.
Definition callback.h:155
T m_comp
the value of the callback component
Definition callback.h:175
bool IsEqual(std::shared_ptr< const CallbackComponentBase > other) const override
Equality test between the values of two components.
Definition callback.h:166
Callback template class.
Definition callback.h:428
bool IsEqual(const CallbackBase &other) const
Equality test.
Definition callback.h:589
bool DoCheckType(Ptr< const CallbackImplBase > other) const
Check for compatible types.
Definition callback.h:641
void Nullify()
Discard the implementation, set it to null.
Definition callback.h:567
Callback(const Ptr< CallbackImpl< R, UArgs... > > &impl)
Construct from a CallbackImpl pointer.
Definition callback.h:442
bool IsNull() const
Check for null implementation.
Definition callback.h:561
CallbackImpl< R, UArgs... > * DoPeekImpl() const
Definition callback.h:630
R operator()(UArgs... uargs) const
Functor with varying numbers of arguments.
Definition callback.h:578
auto Bind(BoundArgs &&... bargs)
Bind a variable number of arguments.
Definition callback.h:549
auto BindImpl(std::index_sequence< INDEX... > seq, BoundArgs &&... bargs)
Implementation of the Bind method.
Definition callback.h:521
friend class Callback
Definition callback.h:430
Callback(T func, BArgs... bargs)
Construct from a function and bind some arguments (if any).
Definition callback.h:487
Callback(const Callback< R, BArgs..., UArgs... > &cb, BArgs... bargs)
Construct from another callback and bind some arguments (if any).
Definition callback.h:455
bool CheckType(const CallbackBase &other) const
Check for compatible types.
Definition callback.h:600
bool Assign(const CallbackBase &other)
Adopt the other's implementation, if type compatible.
Definition callback.h:611
Abstract base class for CallbackImpl Provides reference counting and equality test.
Definition callback.h:71
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:99
virtual ~CallbackImplBase()
Virtual destructor.
Definition callback.h:74
virtual std::string GetTypeid() const =0
Get the name of this object type.
CallbackImpl class with varying numbers of argument types.
Definition callback.h:226
std::vector< std::shared_ptr< CallbackComponentBase > > m_components
Stores the original callable object and the bound arguments, if any.
Definition callback.h:335
std::function< R(UArgs...)> m_func
Stores the callable object associated with this callback (as a lambda).
Definition callback.h:332
R operator()(UArgs... uargs) const
Function call operator.
Definition callback.h:264
const std::function< R(UArgs...)> & GetFunction() const
Get the stored function.
Definition callback.h:244
static std::string DoGetTypeid()
Get the name of this object type.
Definition callback.h:312
CallbackImpl(std::function< R(UArgs...)> func, const CallbackComponentVector &components)
Constructor.
Definition callback.h:234
const CallbackComponentVector & GetComponents() const
Get the vector of callback components.
Definition callback.h:253
std::string GetTypeid() const override
Get the name of this object type.
Definition callback.h:306
bool IsEqual(Ptr< const CallbackImplBase > other) const override
Equality test.
Definition callback.h:269
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker) override
Deserialize from string (not implemented).
Definition callback.cc:71
CallbackBase Get()
Definition callback.cc:48
std::string SerializeToString(Ptr< const AttributeChecker > checker) const override
Serialize to string.
Definition callback.cc:62
void Set(const CallbackBase &value)
Set the value.
Definition callback.cc:41
bool GetAccessor(T &value) const
Access the Callback value as type T.
Definition callback.h:836
CallbackBase m_value
The stored Callback instance.
Definition callback.h:823
Ptr< AttributeValue > Copy() const override
Definition callback.cc:55
~CallbackValue() override
Definition callback.cc:35
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
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... > 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:690
Callback< R, Args... > MakeNullCallback()
Build null Callbacks which take no arguments, for varying number of template arguments,...
Definition callback.h:734
#define NS_FATAL_ERROR_CONT(msg)
Report a fatal error with a message, deferring termination.
#define NS_FATAL_ERROR_NO_MSG()
Report a fatal error and terminate.
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition callback.h:753
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:454
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition ptr.h:463
bool operator!=(Callback< R, Args... > a, Callback< R, Args... > b)
Inequality test.
Definition callback.h:664
std::string Demangle(const std::string &mangled)
Definition demangle.cc:69
std::vector< std::shared_ptr< CallbackComponentBase > > CallbackComponentVector
Vector of callback components.
Definition callback.h:215
ns3::Ptr smart pointer declaration and implementation.
ns3::SimpleRefCount declaration and template implementation.