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 Graph 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 Graph runOnce() const
136 const auto status = bt_graph_run_once(this->libObjPtr());
138 if (status == BT_GRAPH_RUN_ONCE_STATUS_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) {
151 const auto status = bt_graph_run(this->libObjPtr());
153 if (status == BT_GRAPH_RUN_STATUS_ERROR) {
155 } else if (status == BT_GRAPH_RUN_STATUS_MEMORY_ERROR) {
156 throw MemoryError {};
157 } else if (status == BT_GRAPH_RUN_STATUS_AGAIN) {
165 template <typename ConstComponentT, typename ConstComponentClassT, typename InitDataT,
168 _addComponent(const ConstComponentClassT componentClass, const bt2c::CStringView name,
169 const OptionalBorrowedObject<ConstMapValue> params, InitDataT * const initData,
170 const LoggingLevel loggingLevel, AddFuncT&& addFunc) const
172 typename ConstComponentT::LibObjPtr libObjPtr = nullptr;
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);
179 if (status == BT_GRAPH_ADD_COMPONENT_STATUS_ERROR) {
181 } else if (status == BT_GRAPH_ADD_COMPONENT_STATUS_MEMORY_ERROR) {
182 throw MemoryError {};
185 return ConstComponentT {libObjPtr};
189 } /* namespace bt2 */
191 #endif /* BABELTRACE_CPP_COMMON_BT2_GRAPH_HPP */