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