0927cd30a0f2483aa22363dcd835844b9239c2f5
[babeltrace.git] / src / cpp-common / bt2 / graph.hpp
1 /*
2 * Copyright (c) 2024 EfficiOS, Inc.
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 #ifndef BABELTRACE_CPP_COMMON_BT2_GRAPH_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_GRAPH_HPP
9
10 #include <cstdint>
11
12 #include <babeltrace2/babeltrace.h>
13
14 #include "borrowed-object.hpp"
15 #include "component-class.hpp"
16 #include "exc.hpp"
17 #include "shared-object.hpp"
18
19 namespace bt2 {
20 namespace internal {
21
22 struct GraphRefFuncs final
23 {
24 static void get(const bt_graph * const libObjPtr) noexcept
25 {
26 bt_graph_get_ref(libObjPtr);
27 }
28
29 static void put(const bt_graph * const libObjPtr) noexcept
30 {
31 bt_graph_put_ref(libObjPtr);
32 }
33 };
34
35 } /* namespace internal */
36
37 class Graph final : public BorrowedObject<bt_graph>
38 {
39 public:
40 using Shared = SharedObject<Graph, bt_graph, internal::GraphRefFuncs>;
41
42 explicit Graph(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
43 {
44 }
45
46 static Shared create(const std::uint64_t mipVersion)
47 {
48 const auto libObjPtr = bt_graph_create(mipVersion);
49
50 if (!libObjPtr) {
51 throw MemoryError {};
52 }
53
54 return Shared::createWithoutRef(libObjPtr);
55 }
56
57 ConstSourceComponent addComponent(const ConstSourceComponentClass componentClass,
58 const bt2c::CStringView name,
59 const OptionalBorrowedObject<ConstMapValue> params = {},
60 const LoggingLevel loggingLevel = LoggingLevel::NONE) const
61 {
62 return this->_addComponent<ConstSourceComponent>(
63 componentClass, name, params, static_cast<void *>(nullptr), loggingLevel,
64 bt_graph_add_source_component_with_initialize_method_data);
65 }
66
67 template <typename InitDataT>
68 ConstSourceComponent addComponent(const ConstSourceComponentClass componentClass,
69 const bt2c::CStringView name, InitDataT&& initData,
70 const OptionalBorrowedObject<ConstMapValue> params = {},
71 const LoggingLevel loggingLevel = LoggingLevel::NONE) const
72 {
73 return this->_addComponent<ConstSourceComponent>(
74 componentClass, name, params, &initData, loggingLevel,
75 bt_graph_add_source_component_with_initialize_method_data);
76 }
77
78 ConstFilterComponent addComponent(const ConstFilterComponentClass componentClass,
79 const bt2c::CStringView name,
80 const OptionalBorrowedObject<ConstMapValue> params = {},
81 const LoggingLevel loggingLevel = LoggingLevel::NONE) const
82 {
83 return this->_addComponent<ConstFilterComponent>(
84 componentClass, name, params, static_cast<void *>(nullptr), loggingLevel,
85 bt_graph_add_filter_component_with_initialize_method_data);
86 }
87
88 template <typename InitDataT>
89 ConstFilterComponent addComponent(const ConstFilterComponentClass componentClass,
90 const bt2c::CStringView name, InitDataT&& initData,
91 const OptionalBorrowedObject<ConstMapValue> params = {},
92 const LoggingLevel loggingLevel = LoggingLevel::NONE) const
93 {
94 return this->_addComponent<ConstFilterComponent>(
95 componentClass, name, params, &initData, loggingLevel,
96 bt_graph_add_filter_component_with_initialize_method_data);
97 }
98
99 ConstSinkComponent addComponent(const ConstSinkComponentClass componentClass,
100 const bt2c::CStringView name,
101 const OptionalBorrowedObject<ConstMapValue> params = {},
102 const LoggingLevel loggingLevel = LoggingLevel::NONE) const
103 {
104 return this->_addComponent<ConstSinkComponent>(
105 componentClass, name, params, static_cast<void *>(nullptr), loggingLevel,
106 bt_graph_add_sink_component_with_initialize_method_data);
107 }
108
109 template <typename InitDataT>
110 ConstSinkComponent addComponent(const ConstSinkComponentClass componentClass,
111 const bt2c::CStringView name, InitDataT&& initData,
112 const OptionalBorrowedObject<ConstMapValue> params = {},
113 const LoggingLevel loggingLevel = LoggingLevel::NONE) const
114 {
115 return this->_addComponent<ConstSinkComponent>(
116 componentClass, name, params, &initData, loggingLevel,
117 bt_graph_add_sink_component_with_initialize_method_data);
118 }
119
120 Graph connectPorts(const ConstOutputPort outputPort, const ConstInputPort inputPort) const
121 {
122 const auto status = bt_graph_connect_ports(this->libObjPtr(), outputPort.libObjPtr(),
123 inputPort.libObjPtr(), nullptr);
124
125 if (status == BT_GRAPH_CONNECT_PORTS_STATUS_ERROR) {
126 throw Error {};
127 } else if (status == BT_GRAPH_CONNECT_PORTS_STATUS_MEMORY_ERROR) {
128 throw MemoryError {};
129 }
130
131 return *this;
132 }
133
134 Graph runOnce() const
135 {
136 const auto status = bt_graph_run_once(this->libObjPtr());
137
138 if (status == BT_GRAPH_RUN_ONCE_STATUS_ERROR) {
139 throw Error {};
140 } else if (status == BT_GRAPH_RUN_ONCE_STATUS_MEMORY_ERROR) {
141 throw MemoryError {};
142 } else if (status == BT_GRAPH_RUN_ONCE_STATUS_AGAIN) {
143 throw TryAgain {};
144 }
145
146 return *this;
147 }
148
149 Graph run() const
150 {
151 const auto status = bt_graph_run(this->libObjPtr());
152
153 if (status == BT_GRAPH_RUN_STATUS_ERROR) {
154 throw Error {};
155 } else if (status == BT_GRAPH_RUN_STATUS_MEMORY_ERROR) {
156 throw MemoryError {};
157 } else if (status == BT_GRAPH_RUN_STATUS_AGAIN) {
158 throw TryAgain {};
159 }
160
161 return *this;
162 }
163
164 private:
165 template <typename ConstComponentT, typename ConstComponentClassT, typename InitDataT,
166 typename AddFuncT>
167 ConstComponentT
168 _addComponent(const ConstComponentClassT componentClass, const bt2c::CStringView name,
169 const OptionalBorrowedObject<ConstMapValue> params, InitDataT * const initData,
170 const LoggingLevel loggingLevel, AddFuncT&& addFunc) const
171 {
172 typename ConstComponentT::LibObjPtr libObjPtr = nullptr;
173
174 const auto status = addFunc(this->libObjPtr(), componentClass.libObjPtr(), name,
175 params ? params->libObjPtr() : nullptr,
176 const_cast<void *>(static_cast<const void *>(initData)),
177 static_cast<bt_logging_level>(loggingLevel), &libObjPtr);
178
179 if (status == BT_GRAPH_ADD_COMPONENT_STATUS_ERROR) {
180 throw Error {};
181 } else if (status == BT_GRAPH_ADD_COMPONENT_STATUS_MEMORY_ERROR) {
182 throw MemoryError {};
183 }
184
185 return ConstComponentT {libObjPtr};
186 }
187 };
188
189 } /* namespace bt2 */
190
191 #endif /* BABELTRACE_CPP_COMMON_BT2_GRAPH_HPP */
This page took 0.03353 seconds and 4 git commands to generate.