2 * Copyright (c) 2024 EfficiOS, Inc.
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_GRAPH_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_GRAPH_HPP
12 #include <babeltrace2/babeltrace.h>
14 #include "borrowed-object.hpp"
15 #include "component-class.hpp"
17 #include "shared-object.hpp"
22 struct GraphRefFuncs final
24 static void get(const bt_graph * const libObjPtr) noexcept
26 bt_graph_get_ref(libObjPtr);
29 static void put(const bt_graph * const libObjPtr) noexcept
31 bt_graph_put_ref(libObjPtr);
35 } /* namespace internal */
37 class Graph final : public BorrowedObject<bt_graph>
40 using Shared = SharedObject<Graph, bt_graph, internal::GraphRefFuncs>;
42 explicit Graph(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
46 static Shared create(const std::uint64_t mipVersion)
48 const auto libObjPtr = bt_graph_create(mipVersion);
54 return Shared::createWithoutRef(libObjPtr);
57 ConstSourceComponent addComponent(const ConstSourceComponentClass componentClass,
58 const bt2c::CStringView name,
59 const OptionalBorrowedObject<ConstMapValue> params = {},
60 const LoggingLevel loggingLevel = LoggingLevel::NONE) const
62 return this->_addComponent<ConstSourceComponent>(
63 componentClass, name, params, static_cast<void *>(nullptr), loggingLevel,
64 bt_graph_add_source_component_with_initialize_method_data);
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
73 return this->_addComponent<ConstSourceComponent>(
74 componentClass, name, params, &initData, loggingLevel,
75 bt_graph_add_source_component_with_initialize_method_data);
78 ConstFilterComponent addComponent(const ConstFilterComponentClass componentClass,
79 const bt2c::CStringView name,
80 const OptionalBorrowedObject<ConstMapValue> params = {},
81 const LoggingLevel loggingLevel = LoggingLevel::NONE) const
83 return this->_addComponent<ConstFilterComponent>(
84 componentClass, name, params, static_cast<void *>(nullptr), loggingLevel,
85 bt_graph_add_filter_component_with_initialize_method_data);
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
94 return this->_addComponent<ConstFilterComponent>(
95 componentClass, name, params, &initData, loggingLevel,
96 bt_graph_add_filter_component_with_initialize_method_data);
99 ConstSinkComponent addComponent(const ConstSinkComponentClass componentClass,
100 const bt2c::CStringView name,
101 const OptionalBorrowedObject<ConstMapValue> params = {},
102 const LoggingLevel loggingLevel = LoggingLevel::NONE) const
104 return this->_addComponent<ConstSinkComponent>(
105 componentClass, name, params, static_cast<void *>(nullptr), loggingLevel,
106 bt_graph_add_sink_component_with_initialize_method_data);
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
115 return this->_addComponent<ConstSinkComponent>(
116 componentClass, name, params, &initData, loggingLevel,
117 bt_graph_add_sink_component_with_initialize_method_data);
120 void connectPorts(const ConstOutputPort outputPort, const ConstInputPort inputPort) const
122 const auto status = bt_graph_connect_ports(this->libObjPtr(), outputPort.libObjPtr(),
123 inputPort.libObjPtr(), nullptr);
125 if (status == BT_GRAPH_CONNECT_PORTS_STATUS_ERROR) {
127 } else if (status == BT_GRAPH_CONNECT_PORTS_STATUS_MEMORY_ERROR) {
128 throw MemoryError {};
134 const auto status = bt_graph_run_once(this->libObjPtr());
136 if (status == BT_GRAPH_RUN_ONCE_STATUS_ERROR) {
138 } else if (status == BT_GRAPH_RUN_ONCE_STATUS_MEMORY_ERROR) {
139 throw MemoryError {};
140 } else if (status == BT_GRAPH_RUN_ONCE_STATUS_AGAIN) {
147 const auto status = bt_graph_run(this->libObjPtr());
149 if (status == BT_GRAPH_RUN_STATUS_ERROR) {
151 } else if (status == BT_GRAPH_RUN_STATUS_MEMORY_ERROR) {
152 throw MemoryError {};
153 } else if (status == BT_GRAPH_RUN_STATUS_AGAIN) {
159 template <typename ConstComponentT, typename ConstComponentClassT, typename InitDataT,
162 _addComponent(const ConstComponentClassT componentClass, const bt2c::CStringView name,
163 const OptionalBorrowedObject<ConstMapValue> params, InitDataT * const initData,
164 const LoggingLevel loggingLevel, AddFuncT&& addFunc) const
166 typename ConstComponentT::LibObjPtr libObjPtr = nullptr;
168 const auto status = addFunc(this->libObjPtr(), componentClass.libObjPtr(), name,
169 params ? params->libObjPtr() : nullptr,
170 const_cast<void *>(static_cast<const void *>(initData)),
171 static_cast<bt_logging_level>(loggingLevel), &libObjPtr);
173 if (status == BT_GRAPH_ADD_COMPONENT_STATUS_ERROR) {
175 } else if (status == BT_GRAPH_ADD_COMPONENT_STATUS_MEMORY_ERROR) {
176 throw MemoryError {};
179 return wrap(libObjPtr);
183 } /* namespace bt2 */
185 #endif /* BABELTRACE_CPP_COMMON_BT2_GRAPH_HPP */