dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
SharedLibrary.hpp
1// SPDX-FileCopyrightText: 2026 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <expected>
13#include <memory>
14
15// Project includes
16#include <dfx-utilities/CompilerSupport.hpp>
19#include <dfx-utilities/FileSystem.hpp>
21
22namespace dfx::Plugins
23{
32{
33public:
35 enum class LoadFlag
36 {
37 Lazy = 0x01,
38 Now = 0x02,
39 Global = 0x04,
40 Local = 0x08,
41 NoDelete = 0x10,
42 };
43 DFX_DECLARE_FLAGS(LoadFlags, LoadFlag);
44
46 static constexpr LoadFlags defaultLoadFlags = LoadFlags(LoadFlag::Now) | LoadFlag::Local;
47
48public:
52 explicit SharedLibrary(fs::path libPath, LoadFlags loadFlags = defaultLoadFlags);
53
55 SharedLibrary(SharedLibrary && other) noexcept;
56
59
61
64
65public:
67 fs::path const & libPath() const noexcept { return _libPath; }
68
70 bool isLoaded() const noexcept { return _handle != nullptr; }
71
72public:
79 template<typename T>
80 std::expected<T, std::string> resolveSymbol(std::string const & name) const noexcept
81 {
82 auto const result = _resolveSymbolImpl(name.c_str());
83 if (!result.has_value())
84 return std::unexpected(result.error());
85
86 DFX_DIAGNOSTIC_PUSH
87 DFX_DIAGNOSTIC_IGNORE("-Wconditionally-supported")
88 return reinterpret_cast<T>(result.value());
89 DFX_DIAGNOSTIC_POP
90 }
91
92private:
93 void _close() noexcept;
94 std::expected<void *, std::string> _resolveSymbolImpl(char const * name) const noexcept;
95
96private:
97 fs::path _libPath;
98 void * _handle = nullptr;
99};
100
102using SharedLibraryPtr = std::unique_ptr<SharedLibrary>;
103} // !namespace dfx::Plugins
104
Convenience macros to explicitly control copy and move semantics.
#define DFX_DISABLE_COPY(ClassName)
Disable copy (copy ctor + copy-assign)
Definition CopyMoveControl.hpp:27
Macro-based enum <-> string utilities for dfx.
#define DFX_DECLARE_ENUM_STRING_FUNCTIONS(E)
Declare the enum string API (and std::formatter) for enum type E.
Definition EnumString.hpp:327
Typesafe enum bitmask wrapper and helper macros.
#define DFX_DECLARE_OPERATOR_FOR_FLAGS(FlagsName)
Declare a free operator| to combine two enum values into a Flags.
Definition Flags.hpp:325
#define DFX_DECLARE_STD_FORMATTER_FOR_FLAGS(FlagsName)
Declare a std::formatter<FlagsName> that prints set flags joined by |.
Definition Flags.hpp:342
#define DFX_DECLARE_FLAGS(FlagsName, Enum)
Declare a convenient alias for dfx::Utils::Flags<Enum>.
Definition Flags.hpp:309
RAII wrapper for dynamic shared library loading.
Definition SharedLibrary.hpp:32
LoadFlag
Flags controlling how the library is loaded into the process address space.
Definition SharedLibrary.hpp:36
@ Now
Resolve all undefined symbols before dlopen() returns.
Definition SharedLibrary.hpp:38
@ Lazy
Perform lazy binding. Only resolve symbols as they are needed.
Definition SharedLibrary.hpp:37
@ Global
Make symbols in this library available for subsequently loaded libraries.
Definition SharedLibrary.hpp:39
@ Local
Symbols are not made available to resolve references in subsequently loaded libraries.
Definition SharedLibrary.hpp:40
@ NoDelete
Do not unload the library during dlclose().
Definition SharedLibrary.hpp:41
~SharedLibrary()
Destructor. Calls dlclose() if the handle is valid.
fs::path const & libPath() const noexcept
Returns the filesystem path to the shared library.
Definition SharedLibrary.hpp:67
bool isLoaded() const noexcept
Checks if the library is currently loaded.
Definition SharedLibrary.hpp:70
SharedLibrary(SharedLibrary &&other) noexcept
Move constructor. Transfers ownership of the library handle.
std::expected< T, std::string > resolveSymbol(std::string const &name) const noexcept
Resolves a symbol address and casts it to the requested type.
Definition SharedLibrary.hpp:80
SharedLibrary & operator=(SharedLibrary &&other) noexcept
Move assigment operator. Transfers ownership of the library handle.
SharedLibrary(fs::path libPath, LoadFlags loadFlags=defaultLoadFlags)
Loads a shared library from the specified path.
static constexpr LoadFlags defaultLoadFlags
Default flags for plugin loading: immediate resolution and local visibility.
Definition SharedLibrary.hpp:46
Definition MessageApi.hpp:16
std::unique_ptr< SharedLibrary > SharedLibraryPtr
Unique ownership pointer type for SharedLibrary.
Definition SharedLibrary.hpp:102
Definition Message.hpp:21
STL namespace.