Commit | Line | Data |
---|---|---|
b6f09a6b PP |
1 | /* |
2 | * Copyright (c) 2023 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: MIT | |
5 | */ | |
6 | ||
7 | #ifndef BABELTRACE_CPP_COMMON_BT2_SELF_COMPONENT_PORT_HPP | |
8 | #define BABELTRACE_CPP_COMMON_BT2_SELF_COMPONENT_PORT_HPP | |
9 | ||
10 | #include <cstdint> | |
11 | #include <string> | |
12 | ||
13 | #include <babeltrace2/babeltrace.h> | |
14 | ||
15 | #include "logging.hpp" | |
16 | ||
17 | #include "common/assert.h" | |
e7f0f07b | 18 | #include "cpp-common/bt2c/c-string-view.hpp" |
b6f09a6b PP |
19 | |
20 | #include "borrowed-object-iterator.hpp" | |
21 | #include "borrowed-object.hpp" | |
22 | #include "component-port.hpp" | |
23 | #include "message-iterator.hpp" | |
24 | ||
25 | namespace bt2 { | |
26 | ||
27 | class SelfSourceComponent; | |
28 | class SelfFilterComponent; | |
29 | class SelfSinkComponent; | |
30 | ||
31 | class SelfComponent final : public BorrowedObject<bt_self_component> | |
32 | { | |
33 | public: | |
d246c457 | 34 | explicit SelfComponent(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} |
b6f09a6b PP |
35 | { |
36 | } | |
37 | ||
38 | explicit SelfComponent(bt_self_component_source * const libObjPtr) noexcept : | |
39 | _ThisBorrowedObject {bt_self_component_source_as_self_component(libObjPtr)} | |
40 | { | |
41 | } | |
42 | ||
43 | explicit SelfComponent(bt_self_component_filter * const libObjPtr) noexcept : | |
44 | _ThisBorrowedObject {bt_self_component_filter_as_self_component(libObjPtr)} | |
45 | { | |
46 | } | |
47 | ||
48 | explicit SelfComponent(bt_self_component_sink * const libObjPtr) noexcept : | |
49 | _ThisBorrowedObject {bt_self_component_sink_as_self_component(libObjPtr)} | |
50 | { | |
51 | } | |
52 | ||
53 | /* Not `explicit` to make them behave like copy constructors */ | |
54 | SelfComponent(SelfSourceComponent other) noexcept; | |
55 | SelfComponent(SelfFilterComponent other) noexcept; | |
56 | SelfComponent(SelfSinkComponent other) noexcept; | |
57 | ||
58 | SelfComponent operator=(SelfSourceComponent other) noexcept; | |
59 | SelfComponent operator=(SelfFilterComponent other) noexcept; | |
60 | SelfComponent operator=(SelfSinkComponent other) noexcept; | |
61 | ||
62 | ConstComponent asConstComponent() const noexcept | |
63 | { | |
64 | return ConstComponent {bt_self_component_as_component(this->libObjPtr())}; | |
65 | } | |
66 | ||
67 | bool isSource() const noexcept | |
68 | { | |
69 | return this->asConstComponent().isSource(); | |
70 | } | |
71 | ||
72 | bool isFilter() const noexcept | |
73 | { | |
74 | return this->asConstComponent().isFilter(); | |
75 | } | |
76 | ||
77 | bool isSink() const noexcept | |
78 | { | |
79 | return this->asConstComponent().isSink(); | |
80 | } | |
81 | ||
e7f0f07b | 82 | bt2c::CStringView name() const noexcept |
b6f09a6b PP |
83 | { |
84 | return this->asConstComponent().name(); | |
85 | } | |
86 | ||
87 | LoggingLevel loggingLevel() const noexcept | |
88 | { | |
89 | return this->asConstComponent().loggingLevel(); | |
90 | } | |
91 | ||
92 | std::uint64_t graphMipVersion() const noexcept | |
93 | { | |
94 | return bt_self_component_get_graph_mip_version(this->libObjPtr()); | |
95 | } | |
96 | ||
97 | template <typename T> | |
98 | T& data() const noexcept | |
99 | { | |
100 | return *static_cast<T *>(bt_self_component_get_data(this->libObjPtr())); | |
101 | } | |
102 | ||
103 | template <typename T> | |
104 | void data(T& obj) const noexcept | |
105 | { | |
106 | bt_self_component_set_data(this->libObjPtr(), static_cast<void *>(&obj)); | |
107 | } | |
108 | }; | |
109 | ||
110 | template <typename LibObjT> | |
111 | class SelfSpecificComponent : public BorrowedObject<LibObjT> | |
112 | { | |
113 | private: | |
114 | using typename BorrowedObject<LibObjT>::_ThisBorrowedObject; | |
115 | ||
116 | public: | |
d246c457 | 117 | using typename BorrowedObject<LibObjT>::LibObjPtr; |
b6f09a6b PP |
118 | |
119 | protected: | |
d246c457 | 120 | explicit SelfSpecificComponent(const LibObjPtr libObjPtr) noexcept : |
b6f09a6b PP |
121 | _ThisBorrowedObject {libObjPtr} |
122 | { | |
123 | } | |
124 | ||
125 | template <typename PortT, typename LibPortT, typename AddPortFuncT, typename DataT> | |
126 | PortT _addPort(const char * const name, DataT * const data, AddPortFuncT&& func) const | |
127 | { | |
128 | LibPortT *libPortPtr; | |
129 | ||
130 | const auto status = func(this->libObjPtr(), name, static_cast<void *>(data), &libPortPtr); | |
131 | ||
132 | switch (status) { | |
133 | case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK: | |
134 | return PortT {libPortPtr}; | |
135 | case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR: | |
136 | throw MemoryError {}; | |
137 | case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR: | |
138 | throw Error {}; | |
139 | default: | |
140 | bt_common_abort(); | |
141 | } | |
142 | } | |
143 | ||
144 | public: | |
e7f0f07b | 145 | bt2c::CStringView name() const noexcept |
b6f09a6b PP |
146 | { |
147 | return this->_selfComponent().name(); | |
148 | } | |
149 | ||
150 | LoggingLevel loggingLevel() const noexcept | |
151 | { | |
152 | return this->_selfComponent().loggingLevel(); | |
153 | } | |
154 | ||
155 | std::uint64_t graphMipVersion() const noexcept | |
156 | { | |
157 | return this->_selfComponent().graphMipVersion(); | |
158 | } | |
159 | ||
160 | template <typename T> | |
161 | T& data() const noexcept | |
162 | { | |
163 | return this->_selfComponent().template data<T>(); | |
164 | } | |
165 | ||
166 | template <typename T> | |
167 | void data(T& obj) const noexcept | |
168 | { | |
169 | this->_selfComponent().data(obj); | |
170 | } | |
171 | ||
172 | private: | |
173 | SelfComponent _selfComponent() const noexcept | |
174 | { | |
175 | return SelfComponent {this->libObjPtr()}; | |
176 | } | |
177 | }; | |
178 | ||
179 | namespace internal { | |
180 | ||
181 | template <typename LibSelfCompT, typename LibSelfCompPortPtrT> | |
182 | struct SelfComponentPortsSpec; | |
183 | ||
184 | template <> | |
185 | struct SelfComponentPortsSpec<bt_self_component_source, bt_self_component_port_output> final | |
186 | { | |
187 | static std::uint64_t portCount(bt_self_component_source * const libCompPtr) noexcept | |
188 | { | |
189 | return bt_component_source_get_output_port_count( | |
190 | bt_self_component_source_as_component_source(libCompPtr)); | |
191 | } | |
192 | ||
193 | static bt_self_component_port_output *portByIndex(bt_self_component_source * const libCompPtr, | |
194 | const std::uint64_t index) noexcept | |
195 | { | |
196 | return bt_self_component_source_borrow_output_port_by_index(libCompPtr, index); | |
197 | } | |
198 | ||
199 | static bt_self_component_port_output *portByName(bt_self_component_source * const libCompPtr, | |
200 | const char * const name) noexcept | |
201 | { | |
202 | return bt_self_component_source_borrow_output_port_by_name(libCompPtr, name); | |
203 | } | |
204 | }; | |
205 | ||
206 | template <> | |
207 | struct SelfComponentPortsSpec<bt_self_component_filter, bt_self_component_port_output> final | |
208 | { | |
209 | static std::uint64_t portCount(bt_self_component_filter * const libCompPtr) noexcept | |
210 | { | |
211 | return bt_component_filter_get_output_port_count( | |
212 | bt_self_component_filter_as_component_filter(libCompPtr)); | |
213 | } | |
214 | ||
215 | static bt_self_component_port_output *portByIndex(bt_self_component_filter * const libCompPtr, | |
216 | const std::uint64_t index) noexcept | |
217 | { | |
218 | return bt_self_component_filter_borrow_output_port_by_index(libCompPtr, index); | |
219 | } | |
220 | ||
221 | static bt_self_component_port_output *portByName(bt_self_component_filter * const libCompPtr, | |
222 | const char * const name) noexcept | |
223 | { | |
224 | return bt_self_component_filter_borrow_output_port_by_name(libCompPtr, name); | |
225 | } | |
226 | }; | |
227 | ||
228 | template <> | |
229 | struct SelfComponentPortsSpec<bt_self_component_filter, bt_self_component_port_input> final | |
230 | { | |
231 | static std::uint64_t portCount(bt_self_component_filter * const libCompPtr) noexcept | |
232 | { | |
233 | return bt_component_filter_get_input_port_count( | |
234 | bt_self_component_filter_as_component_filter(libCompPtr)); | |
235 | } | |
236 | ||
237 | static bt_self_component_port_input *portByIndex(bt_self_component_filter * const libCompPtr, | |
238 | const std::uint64_t index) noexcept | |
239 | { | |
240 | return bt_self_component_filter_borrow_input_port_by_index(libCompPtr, index); | |
241 | } | |
242 | ||
243 | static bt_self_component_port_input *portByName(bt_self_component_filter * const libCompPtr, | |
244 | const char * const name) noexcept | |
245 | { | |
246 | return bt_self_component_filter_borrow_input_port_by_name(libCompPtr, name); | |
247 | } | |
248 | }; | |
249 | ||
250 | template <> | |
251 | struct SelfComponentPortsSpec<bt_self_component_sink, bt_self_component_port_input> final | |
252 | { | |
253 | static std::uint64_t portCount(bt_self_component_sink * const libCompPtr) noexcept | |
254 | { | |
255 | return bt_component_sink_get_input_port_count( | |
256 | bt_self_component_sink_as_component_sink(libCompPtr)); | |
257 | } | |
258 | ||
259 | static bt_self_component_port_input *portByIndex(bt_self_component_sink * const libCompPtr, | |
260 | const std::uint64_t index) noexcept | |
261 | { | |
262 | return bt_self_component_sink_borrow_input_port_by_index(libCompPtr, index); | |
263 | } | |
264 | ||
265 | static bt_self_component_port_input *portByName(bt_self_component_sink * const libCompPtr, | |
266 | const char * const name) noexcept | |
267 | { | |
268 | return bt_self_component_sink_borrow_input_port_by_name(libCompPtr, name); | |
269 | } | |
270 | }; | |
271 | ||
272 | } /* namespace internal */ | |
273 | ||
274 | template <typename LibSelfCompPortT, typename LibPortT> | |
275 | class SelfComponentPort; | |
276 | ||
277 | template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT> | |
278 | class SelfComponentPorts final : public BorrowedObject<LibSelfCompT> | |
279 | { | |
280 | private: | |
281 | using typename BorrowedObject<LibSelfCompT>::_ThisBorrowedObject; | |
282 | using _Spec = internal::SelfComponentPortsSpec<LibSelfCompT, LibSelfCompPortT>; | |
283 | ||
284 | public: | |
d246c457 | 285 | using typename BorrowedObject<LibSelfCompT>::LibObjPtr; |
b6f09a6b PP |
286 | using Port = SelfComponentPort<LibSelfCompPortT, LibPortT>; |
287 | using Iterator = BorrowedObjectIterator<SelfComponentPorts>; | |
288 | ||
d246c457 | 289 | explicit SelfComponentPorts(const LibObjPtr libObjPtr) noexcept : |
b6f09a6b PP |
290 | _ThisBorrowedObject {libObjPtr} |
291 | { | |
292 | } | |
293 | ||
294 | std::uint64_t length() const noexcept | |
295 | { | |
296 | return _Spec::portCount(this->libObjPtr()); | |
297 | } | |
298 | ||
299 | Port operator[](std::uint64_t index) const noexcept; | |
300 | Port operator[](const char *name) const noexcept; | |
301 | Port operator[](const std::string& name) const noexcept; | |
302 | Iterator begin() const noexcept; | |
303 | Iterator end() const noexcept; | |
304 | Port front() const noexcept; | |
305 | Port back() const noexcept; | |
306 | }; | |
307 | ||
308 | class SelfSourceComponent final : public SelfSpecificComponent<bt_self_component_source> | |
309 | { | |
310 | public: | |
311 | using OutputPorts = SelfComponentPorts<bt_self_component_source, bt_self_component_port_output, | |
312 | const bt_port_output>; | |
313 | ||
314 | explicit SelfSourceComponent(bt_self_component_source * const libObjPtr) noexcept : | |
315 | SelfSpecificComponent {libObjPtr} | |
316 | { | |
317 | } | |
318 | ||
319 | ConstSourceComponent asConstComponent() const noexcept | |
320 | { | |
321 | return ConstSourceComponent { | |
322 | bt_self_component_source_as_component_source(this->libObjPtr())}; | |
323 | } | |
324 | ||
325 | template <typename DataT> | |
326 | OutputPorts::Port addOutputPort(const char *name, DataT& data) const; | |
327 | ||
328 | OutputPorts::Port addOutputPort(const char *name) const; | |
329 | ||
330 | template <typename DataT> | |
331 | OutputPorts::Port addOutputPort(const std::string& name, DataT& data) const; | |
332 | ||
333 | OutputPorts::Port addOutputPort(const std::string& name) const; | |
334 | OutputPorts outputPorts() const noexcept; | |
335 | ||
336 | private: | |
337 | template <typename DataT> | |
338 | OutputPorts::Port _addOutputPort(const char *name, DataT *data) const; | |
339 | }; | |
340 | ||
341 | class SelfFilterComponent final : public SelfSpecificComponent<bt_self_component_filter> | |
342 | { | |
343 | public: | |
344 | using InputPorts = SelfComponentPorts<bt_self_component_filter, bt_self_component_port_input, | |
345 | const bt_port_input>; | |
346 | using OutputPorts = SelfComponentPorts<bt_self_component_filter, bt_self_component_port_output, | |
347 | const bt_port_output>; | |
348 | ||
349 | explicit SelfFilterComponent(bt_self_component_filter * const libObjPtr) noexcept : | |
350 | SelfSpecificComponent {libObjPtr} | |
351 | { | |
352 | } | |
353 | ||
354 | ConstFilterComponent asConstComponent() const noexcept | |
355 | { | |
356 | return ConstFilterComponent { | |
357 | bt_self_component_filter_as_component_filter(this->libObjPtr())}; | |
358 | } | |
359 | ||
360 | template <typename DataT> | |
361 | InputPorts::Port addInputPort(const char *name, DataT& data) const; | |
362 | ||
363 | InputPorts::Port addInputPort(const char *name) const; | |
364 | ||
365 | template <typename DataT> | |
366 | InputPorts::Port addInputPort(const std::string& name, DataT& data) const; | |
367 | ||
368 | InputPorts::Port addInputPort(const std::string& name) const; | |
369 | InputPorts inputPorts() const noexcept; | |
370 | ||
371 | template <typename DataT> | |
372 | OutputPorts::Port addOutputPort(const char *name, DataT& data) const; | |
373 | ||
374 | OutputPorts::Port addOutputPort(const char *name) const; | |
375 | ||
376 | template <typename DataT> | |
377 | OutputPorts::Port addOutputPort(const std::string& name, DataT& data) const; | |
378 | ||
379 | OutputPorts::Port addOutputPort(const std::string& name) const; | |
380 | OutputPorts outputPorts() const noexcept; | |
381 | ||
382 | private: | |
383 | template <typename DataT> | |
384 | InputPorts::Port _addInputPort(const char *name, DataT *data) const; | |
385 | ||
386 | template <typename DataT> | |
387 | OutputPorts::Port _addOutputPort(const char *name, DataT *data) const; | |
388 | }; | |
389 | ||
390 | class SelfSinkComponent final : public SelfSpecificComponent<bt_self_component_sink> | |
391 | { | |
392 | public: | |
393 | using InputPorts = SelfComponentPorts<bt_self_component_sink, bt_self_component_port_input, | |
394 | const bt_port_input>; | |
395 | ||
396 | explicit SelfSinkComponent(bt_self_component_sink * const libObjPtr) noexcept : | |
397 | SelfSpecificComponent {libObjPtr} | |
398 | { | |
399 | } | |
400 | ||
401 | ConstSinkComponent asConstComponent() const noexcept | |
402 | { | |
403 | return ConstSinkComponent {bt_self_component_sink_as_component_sink(this->libObjPtr())}; | |
404 | } | |
405 | ||
406 | MessageIterator::Shared createMessageIterator(InputPorts::Port port) const; | |
407 | ||
408 | bool isInterrupted() const noexcept | |
409 | { | |
410 | return static_cast<bool>(bt_self_component_sink_is_interrupted(this->libObjPtr())); | |
411 | } | |
412 | ||
413 | template <typename DataT> | |
414 | InputPorts::Port addInputPort(const char *name, DataT& data) const; | |
415 | ||
416 | InputPorts::Port addInputPort(const char *name) const; | |
417 | ||
418 | template <typename DataT> | |
419 | InputPorts::Port addInputPort(const std::string& name, DataT& data) const; | |
420 | ||
421 | InputPorts::Port addInputPort(const std::string& name) const; | |
422 | InputPorts inputPorts() const noexcept; | |
423 | ||
424 | private: | |
425 | template <typename DataT> | |
426 | InputPorts::Port _addInputPort(const char *name, DataT *data) const; | |
427 | }; | |
428 | ||
429 | inline SelfComponent::SelfComponent(const SelfSourceComponent other) noexcept : | |
430 | SelfComponent {other.libObjPtr()} | |
431 | { | |
432 | } | |
433 | ||
434 | inline SelfComponent::SelfComponent(const SelfFilterComponent other) noexcept : | |
435 | SelfComponent {other.libObjPtr()} | |
436 | { | |
437 | } | |
438 | ||
439 | inline SelfComponent::SelfComponent(const SelfSinkComponent other) noexcept : | |
440 | SelfComponent {other.libObjPtr()} | |
441 | { | |
442 | } | |
443 | ||
444 | inline SelfComponent SelfComponent::operator=(const SelfSourceComponent other) noexcept | |
445 | { | |
446 | *this = SelfComponent {other.libObjPtr()}; | |
447 | return *this; | |
448 | } | |
449 | ||
450 | inline SelfComponent SelfComponent::operator=(const SelfFilterComponent other) noexcept | |
451 | { | |
452 | *this = SelfComponent {other.libObjPtr()}; | |
453 | return *this; | |
454 | } | |
455 | ||
456 | inline SelfComponent SelfComponent::operator=(const SelfSinkComponent other) noexcept | |
457 | { | |
458 | *this = SelfComponent {other.libObjPtr()}; | |
459 | return *this; | |
460 | } | |
461 | ||
462 | namespace internal { | |
463 | ||
464 | template <typename LibObjT> | |
465 | struct SelfComponentPortSpec; | |
466 | ||
467 | /* Functions specific to self component input ports */ | |
468 | template <> | |
469 | struct SelfComponentPortSpec<bt_self_component_port_input> final | |
470 | { | |
471 | static bt_self_component_port * | |
472 | asSelfCompPort(bt_self_component_port_input * const libObjPtr) noexcept | |
473 | { | |
474 | return bt_self_component_port_input_as_self_component_port(libObjPtr); | |
475 | } | |
476 | ||
477 | static const bt_port_input * | |
478 | asConstPort(const bt_self_component_port_input * const libObjPtr) noexcept | |
479 | { | |
480 | return bt_self_component_port_input_as_port_input(libObjPtr); | |
481 | } | |
482 | }; | |
483 | ||
484 | /* Functions specific to self component output ports */ | |
485 | template <> | |
486 | struct SelfComponentPortSpec<bt_self_component_port_output> final | |
487 | { | |
488 | static bt_self_component_port * | |
489 | asSelfCompPort(bt_self_component_port_output * const libObjPtr) noexcept | |
490 | { | |
491 | return bt_self_component_port_output_as_self_component_port(libObjPtr); | |
492 | } | |
493 | ||
494 | static const bt_port_output * | |
495 | asConstPort(bt_self_component_port_output * const libObjPtr) noexcept | |
496 | { | |
497 | return bt_self_component_port_output_as_port_output(libObjPtr); | |
498 | } | |
499 | }; | |
500 | ||
501 | } /* namespace internal */ | |
502 | ||
503 | template <typename LibSelfCompPortT, typename LibPortT> | |
504 | class SelfComponentPort final : public BorrowedObject<LibSelfCompPortT> | |
505 | { | |
506 | public: | |
d246c457 | 507 | using typename BorrowedObject<LibSelfCompPortT>::LibObjPtr; |
b6f09a6b | 508 | |
d246c457 | 509 | explicit SelfComponentPort(const LibObjPtr libObjPtr) noexcept : |
b6f09a6b PP |
510 | BorrowedObject<LibSelfCompPortT> {libObjPtr} |
511 | { | |
512 | } | |
513 | ||
514 | ConstPort<LibPortT> asConstPort() const noexcept | |
515 | { | |
516 | return ConstPort<LibPortT> { | |
517 | internal::SelfComponentPortSpec<LibSelfCompPortT>::asConstPort(this->libObjPtr())}; | |
518 | } | |
519 | ||
e7f0f07b | 520 | bt2c::CStringView name() const noexcept |
b6f09a6b PP |
521 | { |
522 | return this->asConstPort().name(); | |
523 | } | |
524 | ||
525 | bool isConnected() const noexcept | |
526 | { | |
527 | return this->asConstPort().isConnected(); | |
528 | } | |
529 | ||
530 | SelfComponent component() const noexcept | |
531 | { | |
532 | return SelfComponent {bt_self_component_port_borrow_component(this->_libSelfCompPortPtr())}; | |
533 | } | |
534 | ||
535 | template <typename T> | |
536 | T& data() const noexcept | |
537 | { | |
538 | *static_cast<T *>(bt_self_component_port_get_data(this->_libSelfCompPortPtr())); | |
539 | } | |
540 | ||
541 | private: | |
542 | bt_self_component_port *_libSelfCompPortPtr() const noexcept | |
543 | { | |
544 | return internal::SelfComponentPortSpec<LibSelfCompPortT>::asSelfCompPort(this->libObjPtr()); | |
545 | } | |
546 | }; | |
547 | ||
548 | template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT> | |
549 | typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port | |
550 | SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::operator[]( | |
551 | const std::uint64_t index) const noexcept | |
552 | { | |
553 | return Port {_Spec::portByIndex(this->libObjPtr(), index)}; | |
554 | } | |
555 | ||
556 | template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT> | |
557 | typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port | |
558 | SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::operator[]( | |
559 | const char * const name) const noexcept | |
560 | { | |
561 | return Port {_Spec::portByName(this->libObjPtr(), name)}; | |
562 | } | |
563 | ||
564 | template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT> | |
565 | typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port | |
566 | SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::operator[]( | |
567 | const std::string& name) const noexcept | |
568 | { | |
569 | return (*this)[name.data()]; | |
570 | } | |
571 | ||
572 | template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT> | |
573 | typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Iterator | |
574 | SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::begin() const noexcept | |
575 | { | |
576 | return Iterator {*this, 0}; | |
577 | } | |
578 | ||
579 | template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT> | |
580 | typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Iterator | |
581 | SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::end() const noexcept | |
582 | { | |
583 | return Iterator {*this, this->length()}; | |
584 | } | |
585 | ||
586 | template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT> | |
587 | typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port | |
588 | SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::front() const noexcept | |
589 | { | |
590 | return (*this)[0]; | |
591 | } | |
592 | ||
593 | template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT> | |
594 | typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port | |
595 | SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::back() const noexcept | |
596 | { | |
597 | return (*this)[this->length() - 1]; | |
598 | } | |
599 | ||
600 | using SelfComponentInputPort = SelfComponentPort<bt_self_component_port_input, const bt_port_input>; | |
601 | ||
602 | using SelfComponentOutputPort = | |
603 | SelfComponentPort<bt_self_component_port_output, const bt_port_output>; | |
604 | ||
605 | template <typename DataT> | |
606 | SelfSourceComponent::OutputPorts::Port SelfSourceComponent::_addOutputPort(const char * const name, | |
607 | DataT * const data) const | |
608 | { | |
609 | return this->_addPort<SelfSourceComponent::OutputPorts::Port, bt_self_component_port_output>( | |
610 | name, data, bt_self_component_source_add_output_port); | |
611 | } | |
612 | ||
613 | template <typename DataT> | |
614 | SelfSourceComponent::OutputPorts::Port SelfSourceComponent::addOutputPort(const char * const name, | |
615 | DataT& data) const | |
616 | { | |
617 | return this->_addOutputPort(name, &data); | |
618 | } | |
619 | ||
620 | inline SelfSourceComponent::OutputPorts::Port | |
621 | SelfSourceComponent::addOutputPort(const char * const name) const | |
622 | { | |
623 | return this->_addOutputPort<void>(name, nullptr); | |
624 | } | |
625 | ||
626 | template <typename DataT> | |
627 | SelfSourceComponent::OutputPorts::Port SelfSourceComponent::addOutputPort(const std::string& name, | |
628 | DataT& data) const | |
629 | { | |
630 | return this->_addOutputPort(name.data(), &data); | |
631 | } | |
632 | ||
633 | inline SelfSourceComponent::OutputPorts::Port | |
634 | SelfSourceComponent::addOutputPort(const std::string& name) const | |
635 | { | |
636 | return this->_addOutputPort<void>(name.data(), nullptr); | |
637 | } | |
638 | ||
639 | inline SelfSourceComponent::OutputPorts SelfSourceComponent::outputPorts() const noexcept | |
640 | { | |
641 | return OutputPorts {this->libObjPtr()}; | |
642 | } | |
643 | ||
644 | template <typename DataT> | |
645 | SelfFilterComponent::OutputPorts::Port SelfFilterComponent::_addOutputPort(const char * const name, | |
646 | DataT * const data) const | |
647 | { | |
648 | return this->_addPort<SelfFilterComponent::OutputPorts::Port, bt_self_component_port_output>( | |
649 | name, data, bt_self_component_filter_add_output_port); | |
650 | } | |
651 | ||
652 | template <typename DataT> | |
653 | SelfFilterComponent::OutputPorts::Port SelfFilterComponent::addOutputPort(const char * const name, | |
654 | DataT& data) const | |
655 | { | |
656 | return this->_addOutputPort(name, &data); | |
657 | } | |
658 | ||
659 | inline SelfFilterComponent::OutputPorts::Port | |
660 | SelfFilterComponent::addOutputPort(const char * const name) const | |
661 | { | |
662 | return this->_addOutputPort<void>(name, nullptr); | |
663 | } | |
664 | ||
665 | template <typename DataT> | |
666 | SelfFilterComponent::OutputPorts::Port SelfFilterComponent::addOutputPort(const std::string& name, | |
667 | DataT& data) const | |
668 | { | |
669 | return this->_addOutputPort(name.data(), &data); | |
670 | } | |
671 | ||
672 | inline SelfFilterComponent::OutputPorts::Port | |
673 | SelfFilterComponent::addOutputPort(const std::string& name) const | |
674 | { | |
675 | return this->_addOutputPort<void>(name.data(), nullptr); | |
676 | } | |
677 | ||
678 | inline SelfFilterComponent::OutputPorts SelfFilterComponent::outputPorts() const noexcept | |
679 | { | |
680 | return OutputPorts {this->libObjPtr()}; | |
681 | } | |
682 | ||
683 | template <typename DataT> | |
684 | SelfFilterComponent::InputPorts::Port SelfFilterComponent::_addInputPort(const char * const name, | |
685 | DataT * const data) const | |
686 | { | |
687 | return this->_addPort<SelfFilterComponent::InputPorts::Port, bt_self_component_port_input>( | |
688 | name, data, bt_self_component_filter_add_input_port); | |
689 | } | |
690 | ||
691 | template <typename DataT> | |
692 | SelfFilterComponent::InputPorts::Port SelfFilterComponent::addInputPort(const char * const name, | |
693 | DataT& data) const | |
694 | { | |
695 | return this->_addInputPort(name, &data); | |
696 | } | |
697 | ||
698 | inline SelfFilterComponent::InputPorts::Port | |
699 | SelfFilterComponent::addInputPort(const char * const name) const | |
700 | { | |
701 | return this->_addInputPort<void>(name, nullptr); | |
702 | } | |
703 | ||
704 | template <typename DataT> | |
705 | SelfFilterComponent::InputPorts::Port SelfFilterComponent::addInputPort(const std::string& name, | |
706 | DataT& data) const | |
707 | { | |
708 | return this->_addInputPort(name.data(), &data); | |
709 | } | |
710 | ||
711 | inline SelfFilterComponent::InputPorts::Port | |
712 | SelfFilterComponent::addInputPort(const std::string& name) const | |
713 | { | |
714 | return this->_addInputPort<void>(name.data(), nullptr); | |
715 | } | |
716 | ||
717 | inline SelfFilterComponent::InputPorts SelfFilterComponent::inputPorts() const noexcept | |
718 | { | |
719 | return InputPorts {this->libObjPtr()}; | |
720 | } | |
721 | ||
722 | inline MessageIterator::Shared | |
723 | SelfSinkComponent::createMessageIterator(const InputPorts::Port port) const | |
724 | { | |
725 | bt_message_iterator *libMsgIterPtr = nullptr; | |
726 | ||
727 | const auto status = bt_message_iterator_create_from_sink_component( | |
728 | this->libObjPtr(), port.libObjPtr(), &libMsgIterPtr); | |
729 | ||
730 | switch (status) { | |
731 | case BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK: | |
732 | BT_ASSERT(libMsgIterPtr); | |
733 | return MessageIterator::Shared::createWithoutRef(libMsgIterPtr); | |
734 | case BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_MEMORY_ERROR: | |
735 | throw MemoryError {}; | |
736 | case BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_ERROR: | |
737 | throw Error {}; | |
738 | default: | |
739 | bt_common_abort(); | |
740 | } | |
741 | } | |
742 | ||
743 | template <typename DataT> | |
744 | SelfSinkComponent::InputPorts::Port SelfSinkComponent::_addInputPort(const char * const name, | |
745 | DataT * const data) const | |
746 | { | |
747 | return this->_addPort<SelfSinkComponent::InputPorts::Port, bt_self_component_port_input>( | |
748 | name, data, bt_self_component_sink_add_input_port); | |
749 | } | |
750 | ||
751 | template <typename DataT> | |
752 | SelfSinkComponent::InputPorts::Port SelfSinkComponent::addInputPort(const char * const name, | |
753 | DataT& data) const | |
754 | { | |
755 | return this->_addInputPort(name, &data); | |
756 | } | |
757 | ||
758 | inline SelfSinkComponent::InputPorts::Port | |
759 | SelfSinkComponent::addInputPort(const char * const name) const | |
760 | { | |
761 | return this->_addInputPort<void>(name, nullptr); | |
762 | } | |
763 | ||
764 | template <typename DataT> | |
765 | SelfSinkComponent::InputPorts::Port SelfSinkComponent::addInputPort(const std::string& name, | |
766 | DataT& data) const | |
767 | { | |
768 | return this->_addInputPort(name.data(), &data); | |
769 | } | |
770 | ||
771 | inline SelfSinkComponent::InputPorts::Port | |
772 | SelfSinkComponent::addInputPort(const std::string& name) const | |
773 | { | |
774 | return this->_addInputPort<void>(name.data(), nullptr); | |
775 | } | |
776 | ||
777 | inline SelfSinkComponent::InputPorts SelfSinkComponent::inputPorts() const noexcept | |
778 | { | |
779 | return InputPorts {this->libObjPtr()}; | |
780 | } | |
781 | ||
782 | } /* namespace bt2 */ | |
783 | ||
784 | #endif /* BABELTRACE_CPP_COMMON_BT2_SELF_COMPONENT_PORT_HPP */ |