Commit | Line | Data |
---|---|---|
7bdf11a1 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_COMPONENT_PORT_HPP | |
8 | #define BABELTRACE_CPP_COMMON_BT2_COMPONENT_PORT_HPP | |
9 | ||
10 | #include <cstdint> | |
11 | #include <string> | |
12 | ||
13 | #include <babeltrace2/babeltrace.h> | |
14 | ||
15 | #include "logging.hpp" | |
16 | ||
17 | #include "borrowed-object-iterator.hpp" | |
18 | #include "borrowed-object.hpp" | |
19 | #include "shared-object.hpp" | |
20 | ||
21 | namespace bt2 { | |
22 | namespace internal { | |
23 | ||
24 | struct ConstComponentRefFuncs final | |
25 | { | |
26 | static void get(const bt_component * const libObjPtr) noexcept | |
27 | { | |
28 | bt_component_get_ref(libObjPtr); | |
29 | } | |
30 | ||
31 | static void put(const bt_component * const libObjPtr) noexcept | |
32 | { | |
33 | bt_component_put_ref(libObjPtr); | |
34 | } | |
35 | }; | |
36 | ||
37 | } /* namespace internal */ | |
38 | ||
39 | class ConstSourceComponent; | |
40 | class ConstFilterComponent; | |
41 | class ConstSinkComponent; | |
42 | ||
43 | class ConstComponent final : public BorrowedObject<const bt_component> | |
44 | { | |
45 | private: | |
46 | using typename BorrowedObject<const bt_component>::_ThisBorrowedObject; | |
47 | ||
48 | public: | |
49 | using Shared = | |
50 | SharedObject<ConstComponent, const bt_component, internal::ConstComponentRefFuncs>; | |
51 | ||
52 | explicit ConstComponent(const bt_component * const libObjPtr) noexcept : | |
53 | _ThisBorrowedObject {libObjPtr} | |
54 | { | |
55 | } | |
56 | ||
57 | explicit ConstComponent(const bt_component_source * const libObjPtr) noexcept : | |
58 | _ThisBorrowedObject {bt_component_source_as_component_const(libObjPtr)} | |
59 | { | |
60 | } | |
61 | ||
62 | explicit ConstComponent(const bt_component_filter * const libObjPtr) noexcept : | |
63 | _ThisBorrowedObject {bt_component_filter_as_component_const(libObjPtr)} | |
64 | { | |
65 | } | |
66 | ||
67 | explicit ConstComponent(const bt_component_sink * const libObjPtr) noexcept : | |
68 | _ThisBorrowedObject {bt_component_sink_as_component_const(libObjPtr)} | |
69 | { | |
70 | } | |
71 | ||
72 | /* Not `explicit` to make them behave like copy constructors */ | |
73 | ConstComponent(ConstSourceComponent other) noexcept; | |
74 | ConstComponent(ConstFilterComponent other) noexcept; | |
75 | ConstComponent(ConstSinkComponent other) noexcept; | |
76 | ||
77 | ConstComponent operator=(ConstSourceComponent other) noexcept; | |
78 | ConstComponent operator=(ConstFilterComponent other) noexcept; | |
79 | ConstComponent operator=(ConstSinkComponent other) noexcept; | |
80 | ||
81 | bool isSource() const noexcept | |
82 | { | |
83 | return static_cast<bool>(bt_component_is_source(this->libObjPtr())); | |
84 | } | |
85 | ||
86 | bool isFilter() const noexcept | |
87 | { | |
88 | return static_cast<bool>(bt_component_is_filter(this->libObjPtr())); | |
89 | } | |
90 | ||
91 | bool isSink() const noexcept | |
92 | { | |
93 | return static_cast<bool>(bt_component_is_sink(this->libObjPtr())); | |
94 | } | |
95 | ||
96 | const char *name() const noexcept | |
97 | { | |
98 | return bt_component_get_name(this->libObjPtr()); | |
99 | } | |
100 | ||
101 | LoggingLevel loggingLevel() const noexcept | |
102 | { | |
103 | return static_cast<LoggingLevel>(bt_component_get_logging_level(this->libObjPtr())); | |
104 | } | |
105 | ||
106 | Shared shared() const noexcept | |
107 | { | |
108 | return Shared::createWithRef(*this); | |
109 | } | |
110 | }; | |
111 | ||
112 | template <typename LibObjT> | |
113 | class ConstSpecificComponent : public BorrowedObject<LibObjT> | |
114 | { | |
115 | public: | |
116 | using typename BorrowedObject<LibObjT>::_LibObjPtr; | |
117 | ||
118 | protected: | |
119 | explicit ConstSpecificComponent(const _LibObjPtr libObjPtr) noexcept : | |
120 | BorrowedObject<LibObjT> {libObjPtr} | |
121 | { | |
122 | } | |
123 | ||
124 | public: | |
125 | const char *name() const noexcept | |
126 | { | |
127 | return this->_constComponent().name(); | |
128 | } | |
129 | ||
130 | LoggingLevel loggingLevel() const noexcept | |
131 | { | |
132 | return this->_constComponent().loggingLevel(); | |
133 | } | |
134 | ||
135 | ConstComponent::Shared sharedComponent() const noexcept | |
136 | { | |
137 | return this->_constComponent().shared(); | |
138 | } | |
139 | ||
140 | private: | |
141 | ConstComponent _constComponent() const noexcept | |
142 | { | |
143 | return ConstComponent {this->libObjPtr()}; | |
144 | } | |
145 | }; | |
146 | ||
147 | namespace internal { | |
148 | ||
149 | template <typename LibCompT, typename LibPortPtrT> | |
150 | struct ConstComponentPortsSpec; | |
151 | ||
152 | template <> | |
153 | struct ConstComponentPortsSpec<const bt_component_source, const bt_port_output> final | |
154 | { | |
155 | static std::uint64_t portCount(const bt_component_source * const libCompPtr) noexcept | |
156 | { | |
157 | return bt_component_source_get_output_port_count(libCompPtr); | |
158 | } | |
159 | ||
160 | static const bt_port_output *portByIndex(const bt_component_source * const libCompPtr, | |
161 | const std::uint64_t index) noexcept | |
162 | { | |
163 | return bt_component_source_borrow_output_port_by_index_const(libCompPtr, index); | |
164 | } | |
165 | ||
166 | static const bt_port_output *portByName(const bt_component_source * const libCompPtr, | |
167 | const char * const name) noexcept | |
168 | { | |
169 | return bt_component_source_borrow_output_port_by_name_const(libCompPtr, name); | |
170 | } | |
171 | }; | |
172 | ||
173 | template <> | |
174 | struct ConstComponentPortsSpec<const bt_component_filter, const bt_port_output> final | |
175 | { | |
176 | static std::uint64_t portCount(const bt_component_filter * const libCompPtr) noexcept | |
177 | { | |
178 | return bt_component_filter_get_output_port_count(libCompPtr); | |
179 | } | |
180 | ||
181 | static const bt_port_output *portByIndex(const bt_component_filter * const libCompPtr, | |
182 | const std::uint64_t index) noexcept | |
183 | { | |
184 | return bt_component_filter_borrow_output_port_by_index_const(libCompPtr, index); | |
185 | } | |
186 | ||
187 | static const bt_port_output *portByName(const bt_component_filter * const libCompPtr, | |
188 | const char * const name) noexcept | |
189 | { | |
190 | return bt_component_filter_borrow_output_port_by_name_const(libCompPtr, name); | |
191 | } | |
192 | }; | |
193 | ||
194 | template <> | |
195 | struct ConstComponentPortsSpec<const bt_component_filter, const bt_port_input> final | |
196 | { | |
197 | static std::uint64_t portCount(const bt_component_filter * const libCompPtr) noexcept | |
198 | { | |
199 | return bt_component_filter_get_input_port_count(libCompPtr); | |
200 | } | |
201 | ||
202 | static const bt_port_input *portByIndex(const bt_component_filter * const libCompPtr, | |
203 | const std::uint64_t index) noexcept | |
204 | { | |
205 | return bt_component_filter_borrow_input_port_by_index_const(libCompPtr, index); | |
206 | } | |
207 | ||
208 | static const bt_port_input *portByName(const bt_component_filter * const libCompPtr, | |
209 | const char * const name) noexcept | |
210 | { | |
211 | return bt_component_filter_borrow_input_port_by_name_const(libCompPtr, name); | |
212 | } | |
213 | }; | |
214 | ||
215 | template <> | |
216 | struct ConstComponentPortsSpec<const bt_component_sink, const bt_port_input> final | |
217 | { | |
218 | static std::uint64_t portCount(const bt_component_sink * const libCompPtr) noexcept | |
219 | { | |
220 | return bt_component_sink_get_input_port_count(libCompPtr); | |
221 | } | |
222 | ||
223 | static const bt_port_input *portByIndex(const bt_component_sink * const libCompPtr, | |
224 | const std::uint64_t index) noexcept | |
225 | { | |
226 | return bt_component_sink_borrow_input_port_by_index_const(libCompPtr, index); | |
227 | } | |
228 | ||
229 | static const bt_port_input *portByName(const bt_component_sink * const libCompPtr, | |
230 | const char * const name) noexcept | |
231 | { | |
232 | return bt_component_sink_borrow_input_port_by_name_const(libCompPtr, name); | |
233 | } | |
234 | }; | |
235 | ||
236 | } /* namespace internal */ | |
237 | ||
238 | template <typename> | |
239 | class ConstPort; | |
240 | ||
241 | template <typename LibCompT, typename LibPortT> | |
242 | class ConstComponentPorts final : public BorrowedObject<LibCompT> | |
243 | { | |
244 | private: | |
245 | using _Spec = internal::ConstComponentPortsSpec<LibCompT, LibPortT>; | |
246 | ||
247 | public: | |
248 | using typename BorrowedObject<LibCompT>::_LibObjPtr; | |
249 | using Port = ConstPort<LibPortT>; | |
250 | using Iterator = BorrowedObjectIterator<ConstComponentPorts>; | |
251 | ||
252 | explicit ConstComponentPorts(const _LibObjPtr libObjPtr) noexcept : | |
253 | BorrowedObject<LibCompT> {libObjPtr} | |
254 | { | |
255 | } | |
256 | ||
257 | std::uint64_t length() const noexcept | |
258 | { | |
259 | return _Spec::portCount(this->libObjPtr()); | |
260 | } | |
261 | ||
262 | Port operator[](std::uint64_t index) const noexcept; | |
263 | Port operator[](const char *name) const noexcept; | |
264 | Port operator[](const std::string& name) const noexcept; | |
265 | Iterator begin() const noexcept; | |
266 | Iterator end() const noexcept; | |
267 | }; | |
268 | ||
269 | namespace internal { | |
270 | ||
271 | struct ConstSourceComponentRefFuncs final | |
272 | { | |
273 | static void get(const bt_component_source * const libObjPtr) noexcept | |
274 | { | |
275 | bt_component_source_get_ref(libObjPtr); | |
276 | } | |
277 | ||
278 | static void put(const bt_component_source * const libObjPtr) noexcept | |
279 | { | |
280 | bt_component_source_put_ref(libObjPtr); | |
281 | } | |
282 | }; | |
283 | ||
284 | } /* namespace internal */ | |
285 | ||
286 | class ConstSourceComponent final : public ConstSpecificComponent<const bt_component_source> | |
287 | { | |
288 | public: | |
289 | using Shared = SharedObject<ConstSourceComponent, const bt_component_source, | |
290 | internal::ConstSourceComponentRefFuncs>; | |
291 | ||
292 | using OutputPorts = ConstComponentPorts<const bt_component_source, const bt_port_output>; | |
293 | ||
294 | explicit ConstSourceComponent(const bt_component_source * const libObjPtr) noexcept : | |
295 | ConstSpecificComponent {libObjPtr} | |
296 | { | |
297 | } | |
298 | ||
299 | OutputPorts outputPorts() const noexcept; | |
300 | ||
301 | Shared shared() const noexcept | |
302 | { | |
303 | return Shared::createWithRef(*this); | |
304 | } | |
305 | }; | |
306 | ||
307 | namespace internal { | |
308 | ||
309 | struct ConstFilterComponentRefFuncs final | |
310 | { | |
311 | static void get(const bt_component_filter * const libObjPtr) noexcept | |
312 | { | |
313 | bt_component_filter_get_ref(libObjPtr); | |
314 | } | |
315 | ||
316 | static void put(const bt_component_filter * const libObjPtr) noexcept | |
317 | { | |
318 | bt_component_filter_put_ref(libObjPtr); | |
319 | } | |
320 | }; | |
321 | ||
322 | } /* namespace internal */ | |
323 | ||
324 | class ConstFilterComponent final : public ConstSpecificComponent<const bt_component_filter> | |
325 | { | |
326 | public: | |
327 | using Shared = SharedObject<ConstFilterComponent, const bt_component_filter, | |
328 | internal::ConstFilterComponentRefFuncs>; | |
329 | ||
330 | using InputPorts = ConstComponentPorts<const bt_component_filter, const bt_port_input>; | |
331 | using OutputPorts = ConstComponentPorts<const bt_component_filter, const bt_port_output>; | |
332 | ||
333 | explicit ConstFilterComponent(const bt_component_filter * const libObjPtr) noexcept : | |
334 | ConstSpecificComponent {libObjPtr} | |
335 | { | |
336 | } | |
337 | ||
338 | InputPorts inputPorts() const noexcept; | |
339 | OutputPorts outputPorts() const noexcept; | |
340 | ||
341 | Shared shared() const noexcept | |
342 | { | |
343 | return Shared::createWithRef(*this); | |
344 | } | |
345 | }; | |
346 | ||
347 | namespace internal { | |
348 | ||
349 | struct ConstSinkComponentRefFuncs final | |
350 | { | |
351 | static void get(const bt_component_sink * const libObjPtr) noexcept | |
352 | { | |
353 | bt_component_sink_get_ref(libObjPtr); | |
354 | } | |
355 | ||
356 | static void put(const bt_component_sink * const libObjPtr) noexcept | |
357 | { | |
358 | bt_component_sink_put_ref(libObjPtr); | |
359 | } | |
360 | }; | |
361 | ||
362 | } /* namespace internal */ | |
363 | ||
364 | class ConstSinkComponent final : public ConstSpecificComponent<const bt_component_sink> | |
365 | { | |
366 | public: | |
367 | using Shared = SharedObject<ConstSinkComponent, const bt_component_sink, | |
368 | internal::ConstSinkComponentRefFuncs>; | |
369 | ||
370 | using InputPorts = ConstComponentPorts<const bt_component_sink, const bt_port_input>; | |
371 | ||
372 | explicit ConstSinkComponent(const bt_component_sink * const libObjPtr) noexcept : | |
373 | ConstSpecificComponent {libObjPtr} | |
374 | { | |
375 | } | |
376 | ||
377 | InputPorts inputPorts() const noexcept; | |
378 | ||
379 | Shared shared() const noexcept | |
380 | { | |
381 | return Shared::createWithRef(*this); | |
382 | } | |
383 | }; | |
384 | ||
385 | inline ConstComponent::ConstComponent(const ConstSourceComponent other) noexcept : | |
386 | ConstComponent {other.libObjPtr()} | |
387 | { | |
388 | } | |
389 | ||
390 | inline ConstComponent::ConstComponent(const ConstFilterComponent other) noexcept : | |
391 | ConstComponent {other.libObjPtr()} | |
392 | { | |
393 | } | |
394 | ||
395 | inline ConstComponent::ConstComponent(const ConstSinkComponent other) noexcept : | |
396 | ConstComponent {other.libObjPtr()} | |
397 | { | |
398 | } | |
399 | ||
400 | inline ConstComponent ConstComponent::operator=(const ConstSourceComponent other) noexcept | |
401 | { | |
402 | *this = ConstComponent {other.libObjPtr()}; | |
403 | return *this; | |
404 | } | |
405 | ||
406 | inline ConstComponent ConstComponent::operator=(const ConstFilterComponent other) noexcept | |
407 | { | |
408 | *this = ConstComponent {other.libObjPtr()}; | |
409 | return *this; | |
410 | } | |
411 | ||
412 | inline ConstComponent ConstComponent::operator=(const ConstSinkComponent other) noexcept | |
413 | { | |
414 | *this = ConstComponent {other.libObjPtr()}; | |
415 | return *this; | |
416 | } | |
417 | ||
418 | namespace internal { | |
419 | ||
420 | template <typename LibObjT> | |
421 | struct ConstPortSpec; | |
422 | ||
423 | /* Functions specific to constant input ports */ | |
424 | template <> | |
425 | struct ConstPortSpec<const bt_port_input> final | |
426 | { | |
427 | static const bt_port *asPort(const bt_port_input * const libObjPtr) noexcept | |
428 | { | |
429 | return bt_port_input_as_port_const(libObjPtr); | |
430 | } | |
431 | }; | |
432 | ||
433 | /* Functions specific to constant output ports */ | |
434 | template <> | |
435 | struct ConstPortSpec<const bt_port_output> final | |
436 | { | |
437 | static const bt_port *asPort(const bt_port_output * const libObjPtr) noexcept | |
438 | { | |
439 | return bt_port_output_as_port_const(libObjPtr); | |
440 | } | |
441 | }; | |
442 | ||
443 | template <typename LibObjT> | |
444 | struct ConstPortRefFuncs final | |
445 | { | |
446 | static void get(LibObjT * const libObjPtr) noexcept | |
447 | { | |
448 | bt_port_get_ref(ConstPortSpec<LibObjT>::port(libObjPtr)); | |
449 | } | |
450 | ||
451 | static void put(LibObjT * const libObjPtr) noexcept | |
452 | { | |
453 | bt_port_put_ref(ConstPortSpec<LibObjT>::port(libObjPtr)); | |
454 | } | |
455 | }; | |
456 | ||
457 | } /* namespace internal */ | |
458 | ||
459 | template <typename LibObjT> | |
460 | class ConstPort final : public BorrowedObject<LibObjT> | |
461 | { | |
462 | public: | |
463 | using typename BorrowedObject<LibObjT>::_LibObjPtr; | |
464 | using Shared = SharedObject<ConstPort, LibObjT, internal::ConstPortRefFuncs<LibObjT>>; | |
465 | ||
466 | explicit ConstPort(const _LibObjPtr libObjPtr) noexcept : BorrowedObject<LibObjT> {libObjPtr} | |
467 | { | |
468 | } | |
469 | ||
470 | const char *name() const noexcept | |
471 | { | |
472 | return bt_port_get_name(this->_libConstPortPtr()); | |
473 | } | |
474 | ||
475 | bool isConnected() const noexcept | |
476 | { | |
477 | return static_cast<bool>(bt_port_is_connected(this->_libConstPortPtr())); | |
478 | } | |
479 | ||
480 | ConstComponent component() const noexcept | |
481 | { | |
482 | return ConstComponent {bt_port_borrow_component_const(this->_libConstPortPtr())}; | |
483 | } | |
484 | ||
485 | Shared shared() const noexcept | |
486 | { | |
487 | return Shared::createWithRef(*this); | |
488 | } | |
489 | ||
490 | private: | |
491 | const bt_port *_libConstPortPtr() const noexcept | |
492 | { | |
493 | return internal::ConstPortSpec<LibObjT>::asPort(this->libObjPtr()); | |
494 | } | |
495 | }; | |
496 | ||
497 | template <typename LibCompT, typename LibPortT> | |
498 | typename ConstComponentPorts<LibCompT, LibPortT>::Port | |
499 | ConstComponentPorts<LibCompT, LibPortT>::operator[](const std::uint64_t index) const noexcept | |
500 | { | |
501 | return Port {_Spec::portByIndex(this->libObjPtr(), index)}; | |
502 | } | |
503 | ||
504 | template <typename LibCompT, typename LibPortT> | |
505 | typename ConstComponentPorts<LibCompT, LibPortT>::Port | |
506 | ConstComponentPorts<LibCompT, LibPortT>::operator[](const char * const name) const noexcept | |
507 | { | |
508 | return Port {_Spec::portByName(this->libObjPtr(), name)}; | |
509 | } | |
510 | ||
511 | template <typename LibCompT, typename LibPortT> | |
512 | typename ConstComponentPorts<LibCompT, LibPortT>::Port | |
513 | ConstComponentPorts<LibCompT, LibPortT>::operator[](const std::string& name) const noexcept | |
514 | { | |
515 | return (*this)[name.data()]; | |
516 | } | |
517 | ||
518 | template <typename LibCompT, typename LibPortT> | |
519 | typename ConstComponentPorts<LibCompT, LibPortT>::Iterator | |
520 | ConstComponentPorts<LibCompT, LibPortT>::begin() const noexcept | |
521 | { | |
522 | return Iterator {*this, 0}; | |
523 | } | |
524 | ||
525 | template <typename LibCompT, typename LibPortT> | |
526 | typename ConstComponentPorts<LibCompT, LibPortT>::Iterator | |
527 | ConstComponentPorts<LibCompT, LibPortT>::end() const noexcept | |
528 | { | |
529 | return Iterator {*this, this->length()}; | |
530 | } | |
531 | ||
532 | using ConstInputPort = ConstPort<const bt_port_input>; | |
533 | using ConstOutputPort = ConstPort<const bt_port_output>; | |
534 | ||
535 | inline ConstSourceComponent::OutputPorts ConstSourceComponent::outputPorts() const noexcept | |
536 | { | |
537 | return OutputPorts {this->libObjPtr()}; | |
538 | } | |
539 | ||
540 | inline ConstFilterComponent::OutputPorts ConstFilterComponent::outputPorts() const noexcept | |
541 | { | |
542 | return OutputPorts {this->libObjPtr()}; | |
543 | } | |
544 | ||
545 | inline ConstFilterComponent::InputPorts ConstFilterComponent::inputPorts() const noexcept | |
546 | { | |
547 | return InputPorts {this->libObjPtr()}; | |
548 | } | |
549 | ||
550 | inline ConstSinkComponent::InputPorts ConstSinkComponent::inputPorts() const noexcept | |
551 | { | |
552 | return InputPorts {this->libObjPtr()}; | |
553 | } | |
554 | ||
555 | } /* namespace bt2 */ | |
556 | ||
557 | #endif /* BABELTRACE_CPP_COMMON_BT2_COMPONENT_PORT_HPP */ |