Loading...
Searching...
No Matches
PluginLoader.h
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12#pragma once
13
30
31#include "eon/DynLib.h"
32
33#include <mutex>
34#include <stdexcept>
35#include <string>
36#include <unordered_map>
37#include <vector>
38
39namespace eonc {
40
42public:
44 static PluginLoader &instance();
45
49 void add_config_paths(const std::string &colon_paths);
50
55 template <typename Fn>
56 Fn load_sym(const char *lib_base, const char *sym_name) {
57 dynlib::Handle h = open_lib(lib_base);
58 if (!h)
59 return nullptr;
60 return dynlib::loadSym<Fn>(h, sym_name);
61 }
62
64 [[noreturn]] void throw_not_found(const char *lib_base,
65 const char *description) const;
66
70 [[nodiscard]] bool lib_present(const char *lib_base) const;
71
73 [[nodiscard]] const std::vector<std::string> &search_paths() const noexcept {
74 return m_search_paths;
75 }
76
77 PluginLoader(const PluginLoader &) = delete;
79
80private:
83
84 dynlib::Handle open_lib(const char *lib_base);
85 std::vector<std::string> lib_names(const char *lib_base) const;
86 void append_paths(const std::string &path_str, char sep);
87
88 std::vector<std::string> m_search_paths;
89 mutable std::mutex m_mutex;
90 std::unordered_map<std::string, dynlib::Handle> m_handles;
92 std::string m_last_load_error;
94};
95
96} // namespace eonc
void add_config_paths(const std::string &colon_paths)
Inject search paths from the eOn config file.
std::vector< std::string > m_search_paths
bool lib_present(const char *lib_base) const
Filesystem probe: a matching library file is on the configured search path.
Fn load_sym(const char *lib_base, const char *sym_name)
Load a symbol from a named potential library.
static PluginLoader & instance()
Thread-safe singleton accessor (Meyer's pattern).
void append_paths(const std::string &path_str, char sep)
std::string m_last_load_error
Last dynlib::error() from a failed open (for throw_not_found).
PluginLoader(const PluginLoader &)=delete
std::vector< std::string > lib_names(const char *lib_base) const
dynlib::Handle open_lib(const char *lib_base)
std::unordered_map< std::string, dynlib::Handle > m_handles
void throw_not_found(const char *lib_base, const char *description) const
Throw a descriptive error for a missing potential library.
const std::vector< std::string > & search_paths() const noexcept
Get the current search paths (for diagnostics).
PluginLoader & operator=(const PluginLoader &)=delete
void * Handle
Definition DynLib.h:60
Fn loadSym(Handle h, const char *name) noexcept
Load a symbol and cast to a function pointer type.
Definition DynLib.h:99
RAII resource manager for the ARTn C library with global synchronization.