A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
make-event.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8#ifndef MAKE_EVENT_H
9#define MAKE_EVENT_H
10
11#include "warnings.h"
12
13#include <functional>
14#include <tuple>
15#include <type_traits>
16
17/**
18 * \file
19 * \ingroup events
20 * ns3::MakeEvent function declarations and template implementation.
21 */
22
23namespace ns3
24{
25
26class EventImpl;
27
28/**
29 * \ingroup events
30 * Make an EventImpl from class method members which take
31 * varying numbers of arguments.
32 *
33 * \tparam MEM \deduced The class method function signature.
34 * \tparam OBJ \deduced The class type holding the method.
35 * \tparam Ts \deduced Type template parameter pack.
36 * \param [in] mem_ptr Class method member function pointer
37 * \param [in] obj Class instance.
38 * \param [in] args Arguments to be bound to the underlying function.
39 * \returns The constructed EventImpl.
40 */
41template <typename MEM, typename OBJ, typename... Ts>
42std::enable_if_t<std::is_member_pointer_v<MEM>, EventImpl*> MakeEvent(MEM mem_ptr,
43 OBJ obj,
44 Ts... args);
45
46/**
47 * \ingroup events
48 * \defgroup makeeventfnptr MakeEvent from Function Pointers and Lambdas.
49 *
50 * Create EventImpl instances from function pointers or lambdas which take
51 * varying numbers of arguments.
52 *
53 * @{
54 */
55/**
56 * Make an EventImpl from a function pointer taking varying numbers
57 * of arguments.
58 *
59 * \tparam Us \deduced Formal types of the arguments to the function.
60 * \tparam Ts \deduced Actual types of the arguments to the function.
61 * \param [in] f The function pointer.
62 * \param [in] args Arguments to be bound to the function.
63 * \returns The constructed EventImpl.
64 */
65template <typename... Us, typename... Ts>
66EventImpl* MakeEvent(void (*f)(Us...), Ts... args);
67
68/**
69 * Make an EventImpl from a lambda.
70 *
71 * \param [in] function The lambda
72 * \returns The constructed EventImpl.
73 */
74template <typename T>
75EventImpl* MakeEvent(T function);
76
77/**@}*/
78
79} // namespace ns3
80
81/********************************************************************
82 * Implementation of the templates declared above.
83 ********************************************************************/
84
85#include "event-impl.h"
86
87namespace ns3
88{
89
90namespace internal
91{
92
93/**
94 * \ingroup events
95 * Helper for the MakeEvent functions which take a class method.
96 *
97 * This helper converts a pointer to a reference.
98 *
99 * This is the generic template declaration (with empty body).
100 *
101 * \tparam T \explicit The class type.
102 */
103template <typename T>
104struct EventMemberImplObjTraits;
105
106/**
107 * \ingroup events
108 * Helper for the MakeEvent functions which take a class method.
109 *
110 * This helper converts a pointer to a reference.
111 *
112 * This is the specialization for pointer types.
113 *
114 * \tparam T \explicit The class type.
115 */
116template <typename T>
118{
119 /**
120 * \param [in] p Object pointer.
121 * \return A reference to the object pointed to by p.
122 */
123 static T& GetReference(T* p)
124 {
125 return *p;
126 }
127};
128
129} // namespace internal
130
131template <typename MEM, typename OBJ, typename... Ts>
132std::enable_if_t<std::is_member_pointer_v<MEM>, EventImpl*>
133MakeEvent(MEM mem_ptr, OBJ obj, Ts... args)
134{
135 class EventMemberImpl : public EventImpl
136 {
137 public:
138 EventMemberImpl() = delete;
139
140 EventMemberImpl(OBJ obj, MEM function, Ts... args)
141 : m_function(std::bind(function, obj, args...))
142 {
143 }
144
145 protected:
146 ~EventMemberImpl() override
147 {
148 }
149
150 private:
151 void Notify() override
152 {
153 m_function();
154 }
155
156 std::function<void()> m_function;
157 }* ev = new EventMemberImpl(obj, mem_ptr, args...);
158
159 return ev;
160}
161
162template <typename... Us, typename... Ts>
163EventImpl*
164MakeEvent(void (*f)(Us...), Ts... args)
165{
166 class EventFunctionImpl : public EventImpl
167 {
168 public:
169 EventFunctionImpl(void (*function)(Us...), Ts... args)
170 : m_function(function),
171 m_arguments(args...)
172 {
173 }
174
175 protected:
176 ~EventFunctionImpl() override
177 {
178 }
179
180 private:
181 void Notify() override
182 {
183 std::apply([this](Ts... args) { (*m_function)(args...); }, m_arguments);
184 }
185
186 void (*m_function)(Us...);
187 std::tuple<std::remove_reference_t<Ts>...> m_arguments;
188 }* ev = new EventFunctionImpl(f, args...);
189
190 return ev;
191}
192
193template <typename T>
194EventImpl*
195MakeEvent(T function)
196{
197 class EventImplFunctional : public EventImpl
198 {
199 public:
200 EventImplFunctional(T function)
201 : m_function(function)
202 {
203 }
204
205 ~EventImplFunctional() override
206 {
207 }
208
209 private:
210 void Notify() override
211 {
212 m_function();
213 }
214
215 T m_function;
216 }* ev = new EventImplFunctional(function);
217
218 return ev;
219}
220
221} // namespace ns3
222
223#endif /* MAKE_EVENT_H */
A simulation event.
Definition event-impl.h:35
ns3::EventImpl declarations.
std::enable_if_t< std::is_member_pointer_v< MEM >, EventImpl * > MakeEvent(MEM mem_ptr, OBJ obj, Ts... args)
Make an EventImpl from class method members which take varying numbers of arguments.
Definition make-event.h:133
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Helper for the MakeEvent functions which take a class method.
Definition ptr.h:400