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