106 static std::string
Demangle(
const std::string& mangled);
114 template <
typename T>
117 std::string typeName;
120 typeName =
typeid(T).name();
123 catch (
const std::bad_typeid& e)
150 virtual bool IsEqual(std::shared_ptr<const CallbackComponentBase> other)
const = 0;
162template <
typename T,
bool isComparable = true>
182 bool IsEqual(std::shared_ptr<const CallbackComponentBase> other)
const override
184 auto p = std::dynamic_pointer_cast<const CallbackComponent<T>>(other);
187 return !(p ==
nullptr || p->m_comp !=
m_comp);
224 bool IsEqual(std::shared_ptr<const CallbackComponentBase> other)
const override
240template <
typename R,
typename... UArgs>
290 if (otherDerived ==
nullptr)
297 if (
m_components.size() != otherDerived->GetComponents().size())
304 if (!
m_components.at(0)->IsEqual(otherDerived->GetComponents().at(0)) &&
305 m_components.at(0) != otherDerived->GetComponents().at(0))
313 if (!
m_components.at(i)->IsEqual(otherDerived->GetComponents().at(i)))
330 static std::vector<std::string> vec = {GetCppTypeid<R>(), GetCppTypeid<UArgs>()...};
332 static std::string id(
"CallbackImpl<");
337 if (
id.back() ==
',')
436template <
typename R,
typename... UArgs>
439 template <
typename ROther,
typename... UArgsOther>
464 template <
typename... BArgs>
470 components.insert(components.end(), {std::make_shared<CallbackComponent<BArgs>>(bargs)...});
473 [
f, bargs...](
auto&&... uargs) -> R {
474 return f(bargs..., std::forward<
decltype(uargs)>(uargs)...);
491 template <
typename T,
492 std::enable_if_t<!std::is_base_of_v<CallbackBase, T>,
int> = 0,
497 std::function<R(BArgs..., UArgs...)>
f(func);
501 constexpr bool isComp =
502 std::is_function_v<std::remove_pointer_t<T>> || std::is_member_pointer_v<T>;
508 [
f, bargs...](
auto&&... uargs) -> R {
509 return f(bargs..., std::forward<
decltype(uargs)>(uargs)...);
526 template <std::size_t... INDEX,
typename... BoundArgs>
527 auto BindImpl(std::index_sequence<INDEX...> seq, BoundArgs&&... bargs)
529 Callback<R, std::tuple_element_t<
sizeof...(bargs) + INDEX, std::tuple<UArgs...>>...> cb;
534 components.insert(components.end(),
535 {std::make_shared<CallbackComponent<BoundArgs>>(bargs)...});
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)...);
554 template <
typename... BoundArgs>
555 auto Bind(BoundArgs&&... bargs)
557 static_assert(
sizeof...(UArgs) > 0);
558 return BindImpl(std::make_index_sequence<
sizeof...(UArgs) -
sizeof...(BoundArgs)>{},
559 std::forward<BoundArgs>(bargs)...);
619 auto otherImpl = other.
GetImpl();
622 std::string othTid = otherImpl->GetTypeid();
626 <<
"got=" << othTid << std::endl
627 <<
"expected=" << myTid);
674template <
typename R,
typename... Args>
700template <
typename T,
typename OBJ,
typename R,
typename... Args>
704 return Callback<R, Args...>(memPtr, objPtr);
707template <
typename T,
typename OBJ,
typename R,
typename... Args>
711 return Callback<R, Args...>(memPtr, objPtr);
726template <
typename R,
typename... Args>
743template <
typename R,
typename... Args>
761template <
typename R,
typename... Args,
typename... BArgs>
765 return Callback<R, Args...>(fnPtr).Bind(std::forward<BArgs>(bargs)...);
782template <
typename T,
typename OBJ,
typename R,
typename... Args,
typename... BArgs>
786 return Callback<R, Args...>(memPtr, objPtr).Bind(bargs...);
789template <
typename T,
typename OBJ,
typename R,
typename... Args,
typename... BArgs>
791MakeCallback(R (T::*memPtr)(Args...)
const, OBJ objPtr, BArgs... bargs)
793 return Callback<R, Args...>(memPtr, objPtr).Bind(bargs...);
818 template <
typename T>
double f(double x, void *params)
Attribute helper (ATTRIBUTE_ )macros definition.
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Hold a value for an Attribute.
Base class for Callback class.
CallbackBase(Ptr< CallbackImplBase > impl)
Construct from a pimpl.
Ptr< CallbackImplBase > m_impl
the pimpl
Ptr< CallbackImplBase > GetImpl() const
bool IsEqual(std::shared_ptr< const CallbackComponentBase > other) const override
Equality test between functions.
CallbackComponent(const T &t)
Constructor.
Abstract base class for CallbackComponent.
virtual bool IsEqual(std::shared_ptr< const CallbackComponentBase > other) const =0
Equality test.
virtual ~CallbackComponentBase()
Virtual destructor.
Stores a component of a callback, i.e., the callable object or a bound argument.
CallbackComponent(const T &t)
Constructor.
T m_comp
the value of the callback component
bool IsEqual(std::shared_ptr< const CallbackComponentBase > other) const override
Equality test between the values of two components.
bool IsEqual(const CallbackBase &other) const
Equality test.
bool DoCheckType(Ptr< const CallbackImplBase > other) const
Check for compatible types.
void Nullify()
Discard the implementation, set it to null.
Callback(const Ptr< CallbackImpl< R, UArgs... > > &impl)
Construct from a CallbackImpl pointer.
bool IsNull() const
Check for null implementation.
CallbackImpl< R, UArgs... > * DoPeekImpl() const
R operator()(UArgs... uargs) const
Functor with varying numbers of arguments.
auto Bind(BoundArgs &&... bargs)
Bind a variable number of arguments.
auto BindImpl(std::index_sequence< INDEX... > seq, BoundArgs &&... bargs)
Implementation of the Bind method.
Callback(T func, BArgs... bargs)
Construct from a function and bind some arguments (if any)
Callback(const Callback< R, BArgs..., UArgs... > &cb, BArgs... bargs)
Construct from another callback and bind some arguments (if any)
bool CheckType(const CallbackBase &other) const
Check for compatible types.
bool Assign(const CallbackBase &other)
Adopt the other's implementation, if type compatible.
Abstract base class for CallbackImpl Provides reference counting and equality test.
virtual bool IsEqual(Ptr< const CallbackImplBase > other) const =0
Equality test.
static std::string GetCppTypeid()
Helper to get the C++ typeid as a string.
virtual ~CallbackImplBase()
Virtual destructor.
static std::string Demangle(const std::string &mangled)
virtual std::string GetTypeid() const =0
Get the name of this object type.
CallbackImpl class with varying numbers of argument types.
std::vector< std::shared_ptr< CallbackComponentBase > > m_components
Stores the original callable object and the bound arguments, if any.
std::function< R(UArgs...)> m_func
Stores the callable object associated with this callback (as a lambda)
R operator()(UArgs... uargs) const
Function call operator.
const std::function< R(UArgs...)> & GetFunction() const
Get the stored function.
static std::string DoGetTypeid()
Get the name of this object type.
CallbackImpl(std::function< R(UArgs...)> func, const CallbackComponentVector &components)
Constructor.
const CallbackComponentVector & GetComponents() const
Get the vector of callback components.
std::string GetTypeid() const override
Get the name of this object type.
bool IsEqual(Ptr< const CallbackImplBase > other) const override
Equality test.
AttributeValue implementation for Callback.
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker) override
Deserialize from string (not implemented)
std::string SerializeToString(Ptr< const AttributeChecker > checker) const override
Serialize to string.
void Set(CallbackBase base)
CallbackValue()
Constructor.
bool GetAccessor(T &value) const
Access the Callback value as type T.
Ptr< AttributeValue > Copy() const override
~CallbackValue() override
Destructor.
CallbackBase m_value
The stored Callback instance.
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()
#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.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
bool operator!=(Callback< R, Args... > a, Callback< R, Args... > b)
Inequality test.
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...
std::vector< std::shared_ptr< CallbackComponentBase > > CallbackComponentVector
Vector of callback components.
ns3::Ptr smart pointer declaration and implementation.
ns3::SimpleRefCount declaration and template implementation.