A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
23
namespace
ns3
24
{
25
26
class
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
*/
41
template
<
typename
MEM,
typename
OBJ,
typename
... Ts>
42
std::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
*/
65
template
<
typename
... Us,
typename
... Ts>
66
EventImpl*
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
*/
74
template
<
typename
T>
75
EventImpl*
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
87
namespace
ns3
88
{
89
90
namespace
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
*/
103
template
<
typename
T>
104
struct
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
*/
116
template
<
typename
T>
117
struct
EventMemberImplObjTraits
<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
131
template
<
typename
MEM,
typename
OBJ,
typename
... Ts>
132
std::enable_if_t<std::is_member_pointer_v<MEM>, EventImpl*>
133
MakeEvent
(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
162
template
<
typename
... Us,
typename
... Ts>
163
EventImpl*
164
MakeEvent
(
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
193
template
<
typename
T>
194
EventImpl*
195
MakeEvent
(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 */
ns3::EventImpl
A simulation event.
Definition
event-impl.h:35
event-impl.h
ns3::EventImpl declarations.
ns3::MakeEvent
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
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::internal::EventMemberImplObjTraits< T * >::GetReference
static T & GetReference(T *p)
Definition
make-event.h:123
ns3::internal::EventMemberImplObjTraits
Helper for the MakeEvent functions which take a class method.
Definition
ptr.h:400
warnings.h
src
core
model
make-event.h
Generated on Fri Nov 8 2024 13:58:59 for ns-3 by
1.11.0