| 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 | void 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 | |
| 132 | void runOnce() const |
| 133 | { |
| 134 | const auto status = bt_graph_run_once(this->libObjPtr()); |
| 135 | |
| 136 | if (status == BT_GRAPH_RUN_ONCE_STATUS_ERROR) { |
| 137 | throw 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) { |
| 141 | throw TryAgain {}; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void run() const |
| 146 | { |
| 147 | const auto status = bt_graph_run(this->libObjPtr()); |
| 148 | |
| 149 | if (status == BT_GRAPH_RUN_STATUS_ERROR) { |
| 150 | throw Error {}; |
| 151 | } else if (status == BT_GRAPH_RUN_STATUS_MEMORY_ERROR) { |
| 152 | throw MemoryError {}; |
| 153 | } else if (status == BT_GRAPH_RUN_STATUS_AGAIN) { |
| 154 | throw TryAgain {}; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | private: |
| 159 | template <typename ConstComponentT, typename ConstComponentClassT, typename InitDataT, |
| 160 | typename AddFuncT> |
| 161 | ConstComponentT |
| 162 | _addComponent(const ConstComponentClassT componentClass, const bt2c::CStringView name, |
| 163 | const OptionalBorrowedObject<ConstMapValue> params, InitDataT * const initData, |
| 164 | const LoggingLevel loggingLevel, AddFuncT&& addFunc) const |
| 165 | { |
| 166 | typename ConstComponentT::LibObjPtr libObjPtr = nullptr; |
| 167 | |
| 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); |
| 172 | |
| 173 | if (status == BT_GRAPH_ADD_COMPONENT_STATUS_ERROR) { |
| 174 | throw Error {}; |
| 175 | } else if (status == BT_GRAPH_ADD_COMPONENT_STATUS_MEMORY_ERROR) { |
| 176 | throw MemoryError {}; |
| 177 | } |
| 178 | |
| 179 | return wrap(libObjPtr); |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | } /* namespace bt2 */ |
| 184 | |
| 185 | #endif /* BABELTRACE_CPP_COMMON_BT2_GRAPH_HPP */ |