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