cpp-common/bt2c: move `uuid-view.*pp` contents to `uuid.hpp`
[babeltrace.git] / src / cpp-common / bt2 / component-port.hpp
CommitLineData
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
21namespace bt2 {
22namespace internal {
23
24struct 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
39class ConstSourceComponent;
40class ConstFilterComponent;
41class ConstSinkComponent;
42
43class ConstComponent final : public BorrowedObject<const bt_component>
44{
45private:
46 using typename BorrowedObject<const bt_component>::_ThisBorrowedObject;
47
48public:
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
112template <typename LibObjT>
113class ConstSpecificComponent : public BorrowedObject<LibObjT>
114{
115public:
116 using typename BorrowedObject<LibObjT>::_LibObjPtr;
117
118protected:
119 explicit ConstSpecificComponent(const _LibObjPtr libObjPtr) noexcept :
120 BorrowedObject<LibObjT> {libObjPtr}
121 {
122 }
123
124public:
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
140private:
141 ConstComponent _constComponent() const noexcept
142 {
143 return ConstComponent {this->libObjPtr()};
144 }
145};
146
147namespace internal {
148
149template <typename LibCompT, typename LibPortPtrT>
150struct ConstComponentPortsSpec;
151
152template <>
153struct 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
173template <>
174struct 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
194template <>
195struct 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
215template <>
216struct 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
238template <typename>
239class ConstPort;
240
241template <typename LibCompT, typename LibPortT>
242class ConstComponentPorts final : public BorrowedObject<LibCompT>
243{
244private:
245 using _Spec = internal::ConstComponentPortsSpec<LibCompT, LibPortT>;
246
247public:
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
269namespace internal {
270
271struct 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
286class ConstSourceComponent final : public ConstSpecificComponent<const bt_component_source>
287{
288public:
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
307namespace internal {
308
309struct 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
324class ConstFilterComponent final : public ConstSpecificComponent<const bt_component_filter>
325{
326public:
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
347namespace internal {
348
349struct 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
364class ConstSinkComponent final : public ConstSpecificComponent<const bt_component_sink>
365{
366public:
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
385inline ConstComponent::ConstComponent(const ConstSourceComponent other) noexcept :
386 ConstComponent {other.libObjPtr()}
387{
388}
389
390inline ConstComponent::ConstComponent(const ConstFilterComponent other) noexcept :
391 ConstComponent {other.libObjPtr()}
392{
393}
394
395inline ConstComponent::ConstComponent(const ConstSinkComponent other) noexcept :
396 ConstComponent {other.libObjPtr()}
397{
398}
399
400inline ConstComponent ConstComponent::operator=(const ConstSourceComponent other) noexcept
401{
402 *this = ConstComponent {other.libObjPtr()};
403 return *this;
404}
405
406inline ConstComponent ConstComponent::operator=(const ConstFilterComponent other) noexcept
407{
408 *this = ConstComponent {other.libObjPtr()};
409 return *this;
410}
411
412inline ConstComponent ConstComponent::operator=(const ConstSinkComponent other) noexcept
413{
414 *this = ConstComponent {other.libObjPtr()};
415 return *this;
416}
417
418namespace internal {
419
420template <typename LibObjT>
421struct ConstPortSpec;
422
423/* Functions specific to constant input ports */
424template <>
425struct 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 */
434template <>
435struct 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
443template <typename LibObjT>
444struct 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
459template <typename LibObjT>
460class ConstPort final : public BorrowedObject<LibObjT>
461{
462public:
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
490private:
491 const bt_port *_libConstPortPtr() const noexcept
492 {
493 return internal::ConstPortSpec<LibObjT>::asPort(this->libObjPtr());
494 }
495};
496
497template <typename LibCompT, typename LibPortT>
498typename ConstComponentPorts<LibCompT, LibPortT>::Port
499ConstComponentPorts<LibCompT, LibPortT>::operator[](const std::uint64_t index) const noexcept
500{
501 return Port {_Spec::portByIndex(this->libObjPtr(), index)};
502}
503
504template <typename LibCompT, typename LibPortT>
505typename ConstComponentPorts<LibCompT, LibPortT>::Port
506ConstComponentPorts<LibCompT, LibPortT>::operator[](const char * const name) const noexcept
507{
508 return Port {_Spec::portByName(this->libObjPtr(), name)};
509}
510
511template <typename LibCompT, typename LibPortT>
512typename ConstComponentPorts<LibCompT, LibPortT>::Port
513ConstComponentPorts<LibCompT, LibPortT>::operator[](const std::string& name) const noexcept
514{
515 return (*this)[name.data()];
516}
517
518template <typename LibCompT, typename LibPortT>
519typename ConstComponentPorts<LibCompT, LibPortT>::Iterator
520ConstComponentPorts<LibCompT, LibPortT>::begin() const noexcept
521{
522 return Iterator {*this, 0};
523}
524
525template <typename LibCompT, typename LibPortT>
526typename ConstComponentPorts<LibCompT, LibPortT>::Iterator
527ConstComponentPorts<LibCompT, LibPortT>::end() const noexcept
528{
529 return Iterator {*this, this->length()};
530}
531
532using ConstInputPort = ConstPort<const bt_port_input>;
533using ConstOutputPort = ConstPort<const bt_port_output>;
534
535inline ConstSourceComponent::OutputPorts ConstSourceComponent::outputPorts() const noexcept
536{
537 return OutputPorts {this->libObjPtr()};
538}
539
540inline ConstFilterComponent::OutputPorts ConstFilterComponent::outputPorts() const noexcept
541{
542 return OutputPorts {this->libObjPtr()};
543}
544
545inline ConstFilterComponent::InputPorts ConstFilterComponent::inputPorts() const noexcept
546{
547 return InputPorts {this->libObjPtr()};
548}
549
550inline 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 */
This page took 0.041893 seconds and 4 git commands to generate.