-
Notifications
You must be signed in to change notification settings - Fork 14
/
port.h
415 lines (345 loc) · 15.2 KB
/
port.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#pragma once
#ifndef SYDEVS_SYSTEMS_PORT_H_
#define SYDEVS_SYSTEMS_PORT_H_
#include <sydevs/systems/node_interface.h>
namespace sydevs {
namespace systems {
/**
* @brief A base class template for all port classes.
*
* @details
* `port_base` contains indices and references common to all types of ports.
*/
template<data_mode dmode, data_goal dgoal, typename T>
class port_base
{
public:
port_base(const port_base&) = delete; ///< No copy constructor
port_base& operator=(const port_base&) = delete; ///< No copy assignment
port_base(port_base&&) = default; ///< Move constructor
port_base& operator=(port_base&&) = default; ///< Move assignment
virtual ~port_base() = default; ///< Destructor
const std::string& port_name() const; ///< Returns the name of the port.
int64 port_index() const; ///< Returns the index of the port within the encompassing `node_interface` object.
int64 node_index() const; ///< Returns the index of the node within the encompassing `node_structure` object.
const node_interface& external_interface() const; ///< Returns the encomassing `node_interface` object.
protected:
port_base(const std::string& port_name, int64 port_index, node_interface& external_interface);
private:
const std::string port_name_;
const int64 port_index_;
node_interface& external_interface_;
};
/**
* @brief A generic port class template declaration.
*
* @details
* `port<dmode, dgoal>` will never be instantiated, since all four possible
* combinations of `dmode` and `dgoal` values have associated template
* specializations.
*/
template<data_mode dmode, data_goal dgoal, typename T>
class port : public port_base<dmode, dgoal, T>
{
};
/**
* @brief A class template for flow input ports.
*
* @details
* A `port<flow, input>` object is used to supply a `system_node` object with
* a parameter at the beginning of a simulation.
*
* @tparam T The type of data handled by the port.
*/
template<typename T>
class port<flow, input, T> : public port_base<flow, input, T>
{
public:
/**
* @brief Constructs a `port<flow, input>` object.
*
* @details
* The port is created and registered with the encompassing
* `node_interface` object.
*
* @param port_name The name of the port.
* @param external_interface The encompassing `node_interface` object
* associated with the node.
*
* @tparam T The type of data handled by the port.
*/
port(const std::string& port_name, const node_interface& external_interface);
port(port&&) = default; ///< Move constructor
port& operator=(port&&) = default; ///< Move assignment
~port() = default; ///< Destructor
const T& value() const; ///< Returns the current value attached to the port.
void print_on_use(bool flag = true) const; ///< If `flag` is `true`, all values communicated through this port are printed.
};
/**
* @brief A class template for message input ports.
*
* @details
* A `port<message, input>` object is used to supply a `system_node` object
* with messages during a simulation.
*
* @tparam T The type of data handled by the port.
*/
template<typename T>
class port<message, input, T> : public port_base<message, input, T>
{
public:
/**
* @brief Constructs a `port<message, input>` object.
*
* @details
* The port is created and registered with the encompassing
* `node_interface` object.
*
* @param port_name The name of the port.
* @param external_interface The encompassing `node_interface` object
* associated with the node.
*
* @tparam T The type of data handled by the port.
*/
port(const std::string& port_name, const node_interface& external_interface);
port(port&&) = default; ///< Move constructor
port& operator=(port&&) = default; ///< Move assignment
~port() = default; ///< Destructor
bool received() const; ///< Returns `true` if the port was the receipient of the latest incoming message.
const T& value() const; ///< Returns the current value attached to the port.
void print_on_use(bool flag = true) const; ///< If `flag` is `true`, all values communicated through this port are printed.
};
/**
* @brief A class template for message output ports.
*
* @details
* A `port<message, output>` object is used to communicate messages from a
* `system_node` object during a simulation.
*
* @tparam T The type of data handled by the port.
*/
template<typename T>
class port<message, output, T> : public port_base<message, output, T>
{
public:
/**
* @brief Constructs a `port<message, output>` object.
*
* @details
* The port is created and registered with the encompassing
* `node_interface` object.
*
* @param port_name The name of the port.
* @param external_interface The encompassing `node_interface` object
* associated with the node.
*
* @tparam T The type of data handled by the port.
*/
port(const std::string& port_name, const node_interface& external_interface);
port(port&&) = default; ///< Move constructor
port& operator=(port&&) = default; ///< Move assignment
~port() = default; ///< Destructor
void send(const T& val); ///< Sends the outgoing message `val` through this port.
void print_on_use(bool flag = true) const; ///< If `flag` is `true`, all values communicated through this port are printed.
};
/**
* @brief A class template for flow output ports.
*
* @details
* A `port<flow, output>` object is used to communicate a statistic from a
* `system_node` object at the end of a simulation.
*
* @tparam T The type of data handled by the port.
*/
template<typename T>
class port<flow, output, T> : public port_base<flow, output, T>
{
public:
/**
* @brief Constructs a `port<flow, output>` object.
*
* @details
* The port is created and registered with the encompassing
* `node_interface` object.
*
* @param port_name The name of the port.
* @param external_interface The encompassing `node_interface` object
* associated with the node.
*
* @tparam T The type of data handled by the port.
*/
port(const std::string& port_name, const node_interface& external_interface);
port(port&&) = default; ///< Move constructor
port& operator=(port&&) = default; ///< Move assignment
~port() = default; ///< Destructor
void assign(const T& val); ///< Attaches the value `val` to the port.
void print_on_use(bool flag = true) const; ///< If `flag` is `true`, all values communicated through this port are printed.
};
template<data_mode dmode, data_goal dgoal, typename T>
const std::string& port_base<dmode, dgoal, T>::port_name() const
{
return port_name_;
}
template<data_mode dmode, data_goal dgoal, typename T>
int64 port_base<dmode, dgoal, T>::port_index() const
{
return port_index_;
}
template<data_mode dmode, data_goal dgoal, typename T>
int64 port_base<dmode, dgoal, T>::node_index() const
{
return external_interface_.node_index();
}
template<data_mode dmode, data_goal dgoal, typename T>
const node_interface& port_base<dmode, dgoal, T>::external_interface() const
{
return external_interface_;
}
template<data_mode dmode, data_goal dgoal, typename T>
port_base<dmode, dgoal, T>::port_base(const std::string& port_name, int64 port_index, node_interface& external_interface)
: port_name_(port_name)
, port_index_(port_index)
, external_interface_(external_interface)
{
if (!qualified_type<T>::valid) {
const auto& full_node_name = const_cast<node_interface&>(this->external_interface()).full_name();
throw std::logic_error("Invalid data type for port (" + this->port_name() + ") " +
"of node (" + full_node_name + ")");
}
}
template<typename T>
port<flow, input, T>::port(const std::string& port_name, const node_interface& external_interface)
: port_base<flow, input, T>(
port_name,
const_cast<node_interface&>(external_interface).add_flow_input_port(port_name),
const_cast<node_interface&>(external_interface))
{
}
template<typename T>
const T& port<flow, input, T>::value() const
{
if (!this->external_interface().active()) {
const auto& full_node_name = const_cast<node_interface&>(this->external_interface()).full_name();
throw std::logic_error("Attempt to get value on flow input port (" + this->port_name() + ") " +
"of inactive node (" + full_node_name + ")");
}
const auto& val = const_cast<node_interface&>(this->external_interface()).flow_input_port_value(this->port_index());
return const_cast<const T&>(val.template dereference<T>());
}
template<typename T>
void port<flow, input, T>::print_on_use(bool flag) const
{
auto tostring_func = flag ? tostring_converter<T>() : nullptr;
const_cast<node_interface&>(this->external_interface()).set_flow_input_printable(this->port_index(), tostring_func);
}
template<typename T>
port<message, input, T>::port(const std::string& port_name, const node_interface& external_interface)
: port_base<message, input, T>(
port_name,
const_cast<node_interface&>(external_interface).add_message_input_port(port_name),
const_cast<node_interface&>(external_interface))
{
}
template<typename T>
bool port<message, input, T>::received() const
{
if (!this->external_interface().active()) {
const auto& full_node_name = const_cast<node_interface&>(this->external_interface()).full_name();
throw std::logic_error("Attempt to check message input port (" + this->port_name() + ") " +
"of inactive node (" + full_node_name + ")");
}
if (this->external_interface().dmode() != message || this->external_interface().dgoal() != input) {
const auto& full_node_name = const_cast<node_interface&>(this->external_interface()).full_name();
throw std::logic_error("Attempt to check message input port (" + this->port_name() + ") " +
"of node (" + full_node_name + ") outside of unplanned event");
}
return const_cast<node_interface&>(this->external_interface()).message_input_port_index() == this->port_index();
}
template<typename T>
const T& port<message, input, T>::value() const
{
if (!this->external_interface().active()) {
const auto& full_node_name = const_cast<node_interface&>(this->external_interface()).full_name();
throw std::logic_error("Attempt to get value on message input port (" + this->port_name() + ") " +
"of inactive node (" + full_node_name + ")");
}
if (this->external_interface().dmode() != message || this->external_interface().dgoal() != input) {
const auto& full_node_name = const_cast<node_interface&>(this->external_interface()).full_name();
throw std::logic_error("Attempt to get value on message input port (" + this->port_name() + ") " +
"of node (" + full_node_name + ") outside of unplanned event");
}
if (const_cast<node_interface&>(this->external_interface()).message_input_port_index() != this->port_index()) {
const auto& full_node_name = const_cast<node_interface&>(this->external_interface()).full_name();
throw std::logic_error("Attempt to get value on message input port (" + this->port_name() + ") " +
"of node (" + full_node_name + "), which was not the port that received the message.");
}
const auto& val = const_cast<node_interface&>(this->external_interface()).message_input_port_value(this->port_index());
return const_cast<const T&>(val.template dereference<T>());
}
template<typename T>
void port<message, input, T>::print_on_use(bool flag) const
{
auto tostring_func = flag ? tostring_converter<T>() : nullptr;
const_cast<node_interface&>(this->external_interface()).set_message_input_printable(this->port_index(), tostring_func);
}
template<typename T>
port<message, output, T>::port(const std::string& port_name, const node_interface& external_interface)
: port_base<message, output, T>(
port_name,
const_cast<node_interface&>(external_interface).add_message_output_port(port_name),
const_cast<node_interface&>(external_interface))
{
}
template<typename T>
void port<message, output, T>::send(const T& val)
{
if (!this->external_interface().active()) {
const auto& full_node_name = const_cast<node_interface&>(this->external_interface()).full_name();
throw std::logic_error("Attempt to send value on message output port (" + this->port_name() + ") " +
"of inactive node (" + full_node_name + ")");
}
if (this->external_interface().dmode() != message || this->external_interface().dgoal() != output) {
const auto& full_node_name = const_cast<node_interface&>(this->external_interface()).full_name();
throw std::logic_error("Attempt to send value on message output port (" + this->port_name() + ") " +
"of node (" + full_node_name + ") outside of planned event");
}
const_cast<node_interface&>(this->external_interface()).append_message_output(this->port_index(), qualified_type<T>::copy(val));
}
template<typename T>
void port<message, output, T>::print_on_use(bool flag) const
{
auto tostring_func = flag ? tostring_converter<T>() : nullptr;
const_cast<node_interface&>(this->external_interface()).set_message_output_printable(this->port_index(), tostring_func);
}
template<typename T>
port<flow, output, T>::port(const std::string& port_name, const node_interface& external_interface)
: port_base<flow, output, T>(
port_name,
const_cast<node_interface&>(external_interface).add_flow_output_port(port_name),
const_cast<node_interface&>(external_interface))
{
}
template<typename T>
void port<flow, output, T>::assign(const T& val)
{
if (!this->external_interface().active()) {
const auto& full_node_name = const_cast<node_interface&>(this->external_interface()).full_name();
throw std::logic_error("Attempt to assign value on flow output port (" + this->port_name() + ") " +
"of inactive node (" + full_node_name + ")");
}
if (this->external_interface().dmode() != flow) {
const auto& full_node_name = const_cast<node_interface&>(this->external_interface()).full_name();
throw std::logic_error("Attempt to assign value on flow output port (" + this->port_name() + ") " +
"of node (" + full_node_name + ") outside of initialization or finalization event");
}
const_cast<node_interface&>(this->external_interface()).assign_flow_output(this->port_index(), qualified_type<T>::copy(val));
}
template<typename T>
void port<flow, output, T>::print_on_use(bool flag) const
{
auto tostring_func = flag ? tostring_converter<T>() : nullptr;
const_cast<node_interface&>(this->external_interface()).set_flow_output_printable(this->port_index(), tostring_func);
}
} // namespace
} // namespace
#endif