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