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 */
344{
345 public:
347 : m_impl()
348 {
349 }
350
351 /** \return The impl pointer */
353 {
354 return m_impl;
355 }
356
357 protected:
358 /**
359 * Construct from a pimpl
360 * \param [in] impl The CallbackImplBase Ptr
361 */
363 : m_impl(impl)
364 {
365 }
366
368};
369
370/**
371 * \ingroup callback
372 * \brief Callback template class
373 *
374 * This class template implements the Functor Design Pattern.
375 * It is used to declare the type of a Callback:
376 * - the first non-optional template argument represents
377 * the return type of the callback.
378 * - the remaining (optional) template arguments represent
379 * the type of the subsequent arguments to the callback.
380 *
381 * Callback instances are built with the \ref MakeCallback
382 * template functions. Callback instances have POD semantics:
383 * the memory they allocate is managed automatically, without
384 * user intervention which allows you to pass around Callback
385 * instances by value.
386 *
387 * Sample code which shows how to use this class template
388 * as well as the function templates \ref MakeCallback :
389 * \include src/core/examples/main-callback.cc
390 *
391 * \internal
392 * This code was originally written based on the techniques
393 * described in http://www.codeproject.com/cpp/TTLFunction.asp
394 * It was subsequently rewritten to follow the architecture
395 * outlined in "Modern C++ Design" by Andrei Alexandrescu in
396 * chapter 5, "Generalized Functors".
397 *
398 * This code uses:
399 * - default template parameters to saves users from having to
400 * specify empty parameters when the number of parameters
401 * is smaller than the maximum supported number
402 * - the pimpl idiom: the Callback class is passed around by
403 * value and delegates the crux of the work to its pimpl
404 * pointer.
405 * - a reference list implementation to implement the Callback's
406 * value semantics.
407 *
408 * This code most notably departs from the alexandrescu
409 * implementation in that it does not use type lists to specify
410 * and pass around the types of the callback arguments.
411 * Of course, it also does not use copy-destruction semantics
412 * and relies on a reference list rather than autoPtr to hold
413 * the pointer.
414 *
415 * \see attribute_Callback
416 *
417 * \tparam R \explicit The return type of the Callback.
418 * \tparam UArgs \explicit The types of any arguments to the Callback.
419 */
420template <typename R, typename... UArgs>
421class Callback : public CallbackBase
422{
423 template <typename ROther, typename... UArgsOther>
424 friend class Callback;
425
426 public:
428 {
429 }
430
431 /**
432 * Construct from a CallbackImpl pointer
433 *
434 * \param [in] impl The CallbackImpl Ptr
435 */
437 : CallbackBase(impl)
438 {
439 }
440
441 /**
442 * Construct from another callback and bind some arguments (if any)
443 *
444 * \tparam BArgs \deduced The types of the bound arguments
445 * \param [in] cb The existing callback
446 * \param [in] bargs The values of the bound arguments
447 */
448 template <typename... BArgs>
449 Callback(const Callback<R, BArgs..., UArgs...>& cb, BArgs... bargs)
450 {
451 auto f = cb.DoPeekImpl()->GetFunction();
452
453 CallbackComponentVector components(cb.DoPeekImpl()->GetComponents());
454 components.insert(components.end(),
455 {std::make_shared<CallbackComponent<std::decay_t<BArgs>>>(bargs)...});
456
457 m_impl = Create<CallbackImpl<R, UArgs...>>(
458 [f, bargs...](auto&&... uargs) -> R {
459 return f(bargs..., std::forward<decltype(uargs)>(uargs)...);
460 },
461 components);
462 }
463
464 /**
465 * Construct from a function and bind some arguments (if any)
466 *
467 * \tparam T \deduced The type of the function
468 * \tparam BArgs \deduced The types of the bound arguments
469 * \param [in] func The function
470 * \param [in] bargs The values of the bound arguments
471 *
472 * \internal
473 * We leverage SFINAE to have the compiler discard this constructor when the type
474 * of the first argument is a class derived from CallbackBase (i.e., a Callback).
475 */
476 template <typename T,
477 typename... BArgs,
478 std::enable_if_t<!std::is_base_of_v<CallbackBase, T> &&
479 std::is_invocable_r_v<R, T, BArgs..., UArgs...>,
480 int> = 0>
481 Callback(T func, BArgs... bargs)
482 {
483 // store the function in a std::function object
484 std::function<R(BArgs..., UArgs...)> f(func);
485
486 // The original function is comparable if it is a function pointer or
487 // a pointer to a member function or a pointer to a member data.
488 constexpr bool isComp =
489 std::is_function_v<std::remove_pointer_t<T>> || std::is_member_pointer_v<T>;
490
491 CallbackComponentVector components(
492 {std::make_shared<CallbackComponent<T, isComp>>(func),
493 std::make_shared<CallbackComponent<std::decay_t<BArgs>>>(bargs)...});
494
495 m_impl = Create<CallbackImpl<R, UArgs...>>(
496 [f, bargs...](auto&&... uargs) -> R {
497 return f(bargs..., std::forward<decltype(uargs)>(uargs)...);
498 },
499 components);
500 }
501
502 private:
503 /**
504 * Implementation of the Bind method
505 *
506 * \tparam BoundArgs The types of the arguments to bind
507 * \param [in] seq A compile-time integer sequence
508 * \param [in] bargs The values of the arguments to bind
509 * \return The bound callback
510 *
511 * \internal
512 * The integer sequence is 0..N-1, where N is the number of arguments left unbound.
513 */
514 template <std::size_t... INDEX, typename... BoundArgs>
515 auto BindImpl(std::index_sequence<INDEX...> seq, BoundArgs&&... bargs)
516 {
517 Callback<R, std::tuple_element_t<sizeof...(bargs) + INDEX, std::tuple<UArgs...>>...> cb;
518
519 const auto f = DoPeekImpl()->GetFunction();
520
521 CallbackComponentVector components(DoPeekImpl()->GetComponents());
522 components.insert(components.end(),
523 {std::make_shared<CallbackComponent<std::decay_t<BoundArgs>>>(bargs)...});
524
525 cb.m_impl = Create<std::remove_pointer_t<decltype(cb.DoPeekImpl())>>(
526 [f, bargs...](auto&&... uargs) mutable {
527 return f(bargs..., std::forward<decltype(uargs)>(uargs)...);
528 },
529 components);
530
531 return cb;
532 }
533
534 public:
535 /**
536 * Bind a variable number of arguments
537 *
538 * \tparam BoundArgs \deduced The types of the arguments to bind
539 * \param [in] bargs The values of the arguments to bind
540 * \return The bound callback
541 */
542 template <typename... BoundArgs>
543 auto Bind(BoundArgs&&... bargs)
544 {
545 static_assert(sizeof...(UArgs) > 0);
546 return BindImpl(std::make_index_sequence<sizeof...(UArgs) - sizeof...(BoundArgs)>{},
547 std::forward<BoundArgs>(bargs)...);
548 }
549
550 /**
551 * Check for null implementation
552 *
553 * \return \c true if I don't have an implementation
554 */
555 bool IsNull() const
556 {
557 return (DoPeekImpl() == nullptr);
558 }
559
560 /** Discard the implementation, set it to null */
561 void Nullify()
562 {
563 m_impl = nullptr;
564 }
565
566 /**
567 * Functor with varying numbers of arguments
568 *
569 * \param uargs The arguments to the callback
570 * \return Callback value
571 */
572 R operator()(UArgs... uargs) const
573 {
574 return (*(DoPeekImpl()))(uargs...);
575 }
576
577 /**
578 * Equality test.
579 *
580 * \param [in] other Callback
581 * \return \c true if we are equal
582 */
583 bool IsEqual(const CallbackBase& other) const
584 {
585 return m_impl->IsEqual(other.GetImpl());
586 }
587
588 /**
589 * Check for compatible types
590 *
591 * \param [in] other Callback Ptr
592 * \return \c true if other can be dynamic_cast to my type
593 */
594 bool CheckType(const CallbackBase& other) const
595 {
596 return DoCheckType(other.GetImpl());
597 }
598
599 /**
600 * Adopt the other's implementation, if type compatible
601 *
602 * \param [in] other Callback
603 * \returns \c true if \pname{other} was type-compatible and could be adopted.
604 */
605 bool Assign(const CallbackBase& other)
606 {
607 auto otherImpl = other.GetImpl();
608 if (!DoCheckType(otherImpl))
609 {
610 std::string othTid = otherImpl->GetTypeid();
611 std::string myTid = CallbackImpl<R, UArgs...>::DoGetTypeid();
612 NS_FATAL_ERROR_CONT("Incompatible types. (feed to \"c++filt -t\" if needed)"
613 << std::endl
614 << "got=" << othTid << std::endl
615 << "expected=" << myTid);
616 return false;
617 }
618 m_impl = const_cast<CallbackImplBase*>(PeekPointer(otherImpl));
619 return true;
620 }
621
622 private:
623 /** \return The pimpl pointer */
624 CallbackImpl<R, UArgs...>* DoPeekImpl() const
625 {
626 return static_cast<CallbackImpl<R, UArgs...>*>(PeekPointer(m_impl));
627 }
628
629 /**
630 * Check for compatible types
631 *
632 * \param [in] other Callback Ptr
633 * \return \c true if other can be dynamic_cast to my type
634 */
636 {
637 if (!other)
638 {
639 return true;
640 }
641
642 return (dynamic_cast<const CallbackImpl<R, UArgs...>*>(PeekPointer(other)) != nullptr);
643 }
644};
645
646/**
647 * Inequality test.
648 *
649 * \tparam R \explicit The return type of the Callbacks
650 * \tparam UArgs \explicit The types of any arguments to the Callbacks
651 * \param [in] a Callback
652 * \param [in] b Callback
653 *
654 * \return \c true if the Callbacks are not equal
655 */
656template <typename R, typename... Args>
657bool
662
663/**
664 * @{
665 */
666/**
667 * Build Callbacks for class method members which take varying numbers
668 * of arguments and potentially returning a value.
669 *
670 * \tparam T \deduced Type of the class having the member function.
671 * \tparam OBJ \deduced Type of the class instance.
672 * \tparam R \deduced Return type of the callback.
673 * \tparam Args \deduced Type list of any arguments to the member function.
674 *
675 * \param [in] memPtr Class method member pointer
676 * \param [in] objPtr Class instance
677 * \return A wrapper Callback
678 *
679 * Build Callbacks for class method members which take varying numbers of arguments
680 * and potentially returning a value.
681 */
682template <typename T, typename OBJ, typename R, typename... Args>
683Callback<R, Args...>
684MakeCallback(R (T::*memPtr)(Args...), OBJ objPtr)
685{
686 return Callback<R, Args...>(memPtr, objPtr);
687}
688
689template <typename T, typename OBJ, typename R, typename... Args>
690Callback<R, Args...>
691MakeCallback(R (T::*memPtr)(Args...) const, OBJ objPtr)
692{
693 return Callback<R, Args...>(memPtr, objPtr);
694}
695
696/**@}*/
697
698/**
699 * \ingroup callback
700 * \tparam R \deduced Return type of the callback function..
701 * \tparam Args \deduced Type list of any arguments to the member function.
702 * \param [in] fnPtr Function pointer
703 * \return A wrapper Callback
704 *
705 * Build Callbacks for functions which take varying numbers of arguments
706 * and potentially returning a value.
707 */
708template <typename R, typename... Args>
709Callback<R, Args...>
710MakeCallback(R (*fnPtr)(Args...))
711{
712 return Callback<R, Args...>(fnPtr);
713}
714
715/**
716 * \ingroup callback
717 * \tparam R \deduced Return type of the callback function..
718 * \tparam Args \deduced Type list of any arguments to the member function.
719 * \return A wrapper Callback
720 *
721 * Build null Callbacks which take no arguments,
722 * for varying number of template arguments,
723 * and potentially returning a value.
724 */
725template <typename R, typename... Args>
726Callback<R, Args...>
728{
729 return Callback<R, Args...>();
730}
731
732/**
733 * \ingroup makeboundcallback
734 * @{
735 * Make Callbacks with varying number of bound arguments.
736 * \tparam R \deduced Return type of the callback function..
737 * \tparam Args \deduced Type list of any arguments to the member function.
738 * \tparam BArgs \deduced Type list of bound arguments.
739 * \param [in] fnPtr Function pointer
740 * \param [in] bargs Bound arguments
741 * \return A bound Callback
742 */
743template <typename R, typename... Args, typename... BArgs>
744auto
745MakeBoundCallback(R (*fnPtr)(Args...), BArgs&&... bargs)
746{
747 return Callback<R, Args...>(fnPtr).Bind(std::forward<BArgs>(bargs)...);
748}
749
750/**
751 * \tparam T \deduced Type of the class having the member function.
752 * \tparam OBJ \deduced Type of the class instance.
753 * \tparam R \deduced Return type of the callback.
754 * \tparam Args \deduced Type list of any arguments to the member function.
755 * \tparam BArgs \deduced Type list of bound arguments.
756 * \param [in] memPtr Class method member pointer
757 * \param [in] objPtr Class instance
758 * \param [in] bargs Bound arguments
759 * \return A wrapper Callback
760 *
761 * Build Callbacks for class method members which take varying numbers of arguments
762 * and potentially returning a value.
763 */
764template <typename T, typename OBJ, typename R, typename... Args, typename... BArgs>
765auto
766MakeCallback(R (T::*memPtr)(Args...), OBJ objPtr, BArgs... bargs)
767{
768 return Callback<R, Args...>(memPtr, objPtr).Bind(bargs...);
769}
770
771template <typename T, typename OBJ, typename R, typename... Args, typename... BArgs>
772auto
773MakeCallback(R (T::*memPtr)(Args...) const, OBJ objPtr, BArgs... bargs)
774{
775 return Callback<R, Args...>(memPtr, objPtr).Bind(bargs...);
776}
777
778/**@}*/
779
780} // namespace ns3
781
782namespace ns3
783{
784
786{
787 public:
789 CallbackValue(const CallbackBase& value);
790 ~CallbackValue() override;
791 // Documented by print-introspected-doxygen.cc
792 void Set(const CallbackBase& value);
794 template <typename T>
795 bool GetAccessor(T& value) const;
796 /** \return A copy of this CallBack */
797 Ptr<AttributeValue> Copy() const override;
798 /**
799 * Serialize to string
800 * \param [in] checker The checker to validate with
801 * \return Serialized form of this Callback.
802 */
803 std::string SerializeToString(Ptr<const AttributeChecker> checker) const override;
804 /**
805 * Deserialize from string (not implemented)
806 *
807 * \param [in] value Source string
808 * \param [in] checker Checker to validate with
809 * \return \c true if successful
810 */
811 bool DeserializeFromString(std::string value, Ptr<const AttributeChecker> checker) override;
812
813 private:
814 /* Documented by print-introspected-doxygen.cc */
816};
817
820
821} // namespace ns3
822
823namespace ns3
824{
825
826template <typename T>
827bool
829{
830 if (value.CheckType(m_value))
831 {
832 if (!value.Assign(m_value))
833 {
835 }
836 return true;
837 }
838 return false;
839}
840
841} // namespace ns3
842
843#endif /* CALLBACK_H */
Attribute helper (ATTRIBUTE_ )macros definition.
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Hold a value for an Attribute.
Definition attribute.h:59
Base class for Callback class.
Definition callback.h:344
CallbackBase(Ptr< CallbackImplBase > impl)
Construct from a pimpl.
Definition callback.h:362
Ptr< CallbackImplBase > m_impl
the pimpl
Definition callback.h:367
Ptr< CallbackImplBase > GetImpl() const
Definition callback.h:352
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:422
bool IsEqual(const CallbackBase &other) const
Equality test.
Definition callback.h:583
bool DoCheckType(Ptr< const CallbackImplBase > other) const
Check for compatible types.
Definition callback.h:635
void Nullify()
Discard the implementation, set it to null.
Definition callback.h:561
Callback(const Ptr< CallbackImpl< R, UArgs... > > &impl)
Construct from a CallbackImpl pointer.
Definition callback.h:436
bool IsNull() const
Check for null implementation.
Definition callback.h:555
CallbackImpl< R, UArgs... > * DoPeekImpl() const
Definition callback.h:624
R operator()(UArgs... uargs) const
Functor with varying numbers of arguments.
Definition callback.h:572
auto Bind(BoundArgs &&... bargs)
Bind a variable number of arguments.
Definition callback.h:543
auto BindImpl(std::index_sequence< INDEX... > seq, BoundArgs &&... bargs)
Implementation of the Bind method.
Definition callback.h:515
Callback(T func, BArgs... bargs)
Construct from a function and bind some arguments (if any)
Definition callback.h:481
Callback(const Callback< R, BArgs..., UArgs... > &cb, BArgs... bargs)
Construct from another callback and bind some arguments (if any)
Definition callback.h:449
bool CheckType(const CallbackBase &other) const
Check for compatible types.
Definition callback.h:594
bool Assign(const CallbackBase &other)
Adopt the other's implementation, if type compatible.
Definition callback.h:605
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)
Definition callback.cc:41
bool GetAccessor(T &value) const
Definition callback.h:828
CallbackBase m_value
Definition callback.h:815
Ptr< AttributeValue > Copy() const override
Definition callback.cc:55
~CallbackValue() override
Definition callback.cc:35
Smart pointer class similar to boost::intrusive_ptr.
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:727
#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:745
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition ptr.h:443
bool operator!=(Callback< R, Args... > a, Callback< R, Args... > b)
Inequality test.
Definition callback.h:658
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:684
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.