Commit | Line | Data |
---|---|---|
785712e1 SM |
1 | /* |
2 | * Copyright (c) 2024 EfficiOS, Inc. | |
3 | * | |
4 | * SPDX-License-Identifier: MIT | |
5 | */ | |
6 | ||
7 | #ifndef BABELTRACE_CPP_COMMON_BT2_PLUGIN_HPP | |
8 | #define BABELTRACE_CPP_COMMON_BT2_PLUGIN_HPP | |
9 | ||
10 | #include <babeltrace2/babeltrace.h> | |
11 | ||
12 | #include "common/common.h" | |
13 | #include "cpp-common/bt2/borrowed-object.hpp" | |
14 | #include "cpp-common/bt2/exc.hpp" | |
15 | #include "cpp-common/bt2/shared-object.hpp" | |
16 | #include "cpp-common/bt2c/c-string-view.hpp" | |
17 | ||
18 | namespace bt2 { | |
19 | namespace internal { | |
20 | ||
21 | struct PluginSetRefFuncs | |
22 | { | |
23 | static void get(const bt_plugin_set * const libObjPtr) noexcept | |
24 | { | |
25 | bt_plugin_set_get_ref(libObjPtr); | |
26 | } | |
27 | ||
28 | static void put(const bt_plugin_set * const libObjPtr) noexcept | |
29 | { | |
30 | bt_plugin_set_put_ref(libObjPtr); | |
31 | } | |
32 | }; | |
33 | ||
34 | } /* namespace internal */ | |
35 | ||
36 | class ConstPluginSet final : public BorrowedObject<const bt_plugin_set> | |
37 | { | |
38 | public: | |
39 | using Shared = SharedObject<ConstPluginSet, const bt_plugin_set, internal::PluginSetRefFuncs>; | |
40 | ||
41 | explicit ConstPluginSet(const bt_plugin_set * const plugin_set) : | |
42 | _ThisBorrowedObject {plugin_set} | |
43 | { | |
44 | } | |
45 | ||
46 | std::uint64_t length() const noexcept | |
47 | { | |
48 | return bt_plugin_set_get_plugin_count(this->libObjPtr()); | |
49 | } | |
50 | }; | |
51 | ||
52 | inline ConstPluginSet::Shared findAllPluginsFromDir(const bt2c::CStringView path, | |
53 | const bool recurse, const bool failOnLoadError) | |
54 | { | |
55 | const bt_plugin_set *pluginSet; | |
56 | ||
57 | switch (bt_plugin_find_all_from_dir(path, recurse, failOnLoadError, &pluginSet)) { | |
58 | case BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_OK: | |
59 | return ConstPluginSet::Shared::createWithoutRef(pluginSet); | |
60 | case BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_NOT_FOUND: | |
61 | return ConstPluginSet::Shared {}; | |
62 | case BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_MEMORY_ERROR: | |
63 | throw MemoryError {}; | |
64 | case BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_ERROR: | |
65 | throw Error {}; | |
66 | } | |
67 | ||
68 | bt_common_abort(); | |
69 | } | |
70 | ||
71 | } /* namespace bt2 */ | |
72 | ||
73 | #endif /* BABELTRACE_CPP_COMMON_BT2_PLUGIN_HPP */ |