Loading...
Searching...
No Matches
eonc::PluginLoader Class Reference

#include <PluginLoader.h>

Inheritance diagram for eonc::PluginLoader:

Classes

class  Key

Public Member Functions

void add_config_paths (const std::string &colon_paths) override
 Inject search paths from the eOn config file.
bool lib_present (const char *lib_base) const override
 Filesystem probe: a matching library file is on the configured search path.
const std::vector< std::string > & search_paths () const noexcept override
 Get the current search paths (for diagnostics).
void throw_not_found (const char *lib_base, const char *description) const override
 Throw a descriptive error for a missing potential library.
 ~PluginLoader () override
 PluginLoader (const PluginLoader &)=delete
PluginLoader & operator= (const PluginLoader &)=delete
 PluginLoader (Key)
Public Member Functions inherited from eonc::IPluginLoader
template<typename Fn>
Fn load_sym (const char *lib_base, const char *sym_name)
 Load a symbol from a named potential library.
virtual ~IPluginLoader ()=default
 IPluginLoader (const IPluginLoader &)=delete
IPluginLoader & operator= (const IPluginLoader &)=delete

Static Public Member Functions

static PluginLoader & instance ()
 Thread-safe singleton accessor (Meyer's pattern).

Private Member Functions

 PluginLoader ()
dynlib::Handle open_lib (const char *lib_base) override
std::vector< std::string > lib_names (const char *lib_base) const
void append_paths (const std::string &path_str, char sep)

Private Attributes

std::vector< std::string > m_search_paths
std::mutex m_mutex
std::unordered_map< std::string, dynlib::Handle > m_handles
std::string m_last_load_error
 Last dynlib::error() from a failed open (for throw_not_found).
bool m_last_load_saw_file = false

Friends

class Runtime

Additional Inherited Members

Protected Member Functions inherited from eonc::IPluginLoader
 IPluginLoader ()=default

Detailed Description

Definition at line 90 of file PluginLoader.h.

Constructor & Destructor Documentation

◆ ~PluginLoader()

eonc::PluginLoader::~PluginLoader ( )
override

Definition at line 41 of file PluginLoader.cpp.

41 {
42 for (auto &[name, handle] : m_handles) {
43 dynlib::close(handle);
44 }
45}
std::unordered_map< std::string, dynlib::Handle > m_handles
void close(Handle h) noexcept
Definition DynLib.h:77

◆ PluginLoader() [1/3]

eonc::PluginLoader::PluginLoader ( const PluginLoader & )
delete

◆ PluginLoader() [2/3]

eonc::PluginLoader::PluginLoader ( )
private

Definition at line 28 of file PluginLoader.cpp.

28 {
29 // Seed from EON_POTENTIALS_PATH env var (colon-separated on POSIX).
30 // Config-file paths are prepended later via add_config_paths().
31 const char *env = std::getenv("EON_POTENTIALS_PATH");
32 if (env && *env) {
33#ifdef _WIN32
34 append_paths(env, ';');
35#else
36 append_paths(env, ':');
37#endif
38 }
39}
void append_paths(const std::string &path_str, char sep)

◆ PluginLoader() [3/3]

eonc::PluginLoader::PluginLoader ( Key )
explicit

Definition at line 25 of file PluginLoader.cpp.

Member Function Documentation

◆ add_config_paths()

void eonc::PluginLoader::add_config_paths ( const std::string & colon_paths)
overridevirtual

Inject search paths from the eOn config file.

Parameters
colon_pathscolon-separated directory list (may be empty) These paths are inserted before the EON_POTENTIALS_PATH paths.

Implements eonc::IPluginLoader.

Definition at line 69 of file PluginLoader.cpp.

69 {
70 if (colon_paths.empty())
71 return;
72 std::lock_guard<std::mutex> lock(m_mutex);
73 // Config paths have higher priority than env-var paths: insert at front.
74 std::vector<std::string> new_paths;
75#ifdef _WIN32
76 const char sep = ';';
77#else
78 const char sep = ':';
79#endif
80 std::string::size_type start = 0;
81 while (start < colon_paths.size()) {
82 auto pos = colon_paths.find(sep, start);
83 if (pos == std::string::npos)
84 pos = colon_paths.size();
85 if (pos > start) {
86 std::string p = colon_paths.substr(start, pos - start);
87 bool dup = false;
88 for (const auto &existing : m_search_paths) {
89 if (existing == p) {
90 dup = true;
91 break;
92 }
93 }
94 if (!dup)
95 new_paths.push_back(std::move(p));
96 }
97 start = pos + 1;
98 }
99 m_search_paths.insert(m_search_paths.begin(), new_paths.begin(),
100 new_paths.end());
101}
std::vector< std::string > m_search_paths

◆ append_paths()

void eonc::PluginLoader::append_paths ( const std::string & path_str,
char sep )
private

Definition at line 47 of file PluginLoader.cpp.

47 {
48 std::string::size_type start = 0;
49 while (start < path_str.size()) {
50 auto pos = path_str.find(sep, start);
51 if (pos == std::string::npos)
52 pos = path_str.size();
53 if (pos > start) {
54 std::string p = path_str.substr(start, pos - start);
55 bool dup = false;
56 for (const auto &existing : m_search_paths) {
57 if (existing == p) {
58 dup = true;
59 break;
60 }
61 }
62 if (!dup)
63 m_search_paths.push_back(std::move(p));
64 }
65 start = pos + 1;
66 }
67}

◆ instance()

PluginLoader & eonc::PluginLoader::instance ( )
static

Thread-safe singleton accessor (Meyer's pattern).

Definition at line 20 of file PluginLoader.cpp.

20 {
21 static PluginLoader loader;
22 return loader;
23}
PluginLoader(const PluginLoader &)=delete

◆ lib_names()

std::vector< std::string > eonc::PluginLoader::lib_names ( const char * lib_base) const
private

Definition at line 103 of file PluginLoader.cpp.

103 {
104 std::vector<std::string> names;
105#ifdef _WIN32
106 names.push_back(std::string(lib_base) + ".dll");
107 names.push_back(std::string("lib") + lib_base + ".dll");
108#elif defined(__APPLE__)
109 names.push_back(std::string("lib") + lib_base + ".dylib");
110#else
111 names.push_back(std::string("lib") + lib_base + ".so");
112#endif
113 return names;
114}

◆ lib_present()

bool eonc::PluginLoader::lib_present ( const char * lib_base) const
nodiscardoverridevirtual

Filesystem probe: a matching library file is on the configured search path.

Does not dlopen, so plugin static initializers (banners) do not run.

Implements eonc::IPluginLoader.

Definition at line 167 of file PluginLoader.cpp.

167 {
168 std::lock_guard<std::mutex> lock(m_mutex);
169 auto names = lib_names(lib_base);
170 for (const auto &dir : m_search_paths) {
171 for (const auto &name : names) {
172 std::error_code ec;
173 if (std::filesystem::exists(std::filesystem::path(dir) / name, ec))
174 return true;
175 }
176 }
177 return false;
178}
std::vector< std::string > lib_names(const char *lib_base) const

◆ open_lib()

dynlib::Handle eonc::PluginLoader::open_lib ( const char * lib_base)
overrideprivatevirtual

Implements eonc::IPluginLoader.

Definition at line 116 of file PluginLoader.cpp.

116 {
117 std::lock_guard<std::mutex> lock(m_mutex);
118
119 auto it = m_handles.find(lib_base);
120 if (it != m_handles.end()) {
121 return it->second;
122 }
123
124 auto names = lib_names(lib_base);
125 dynlib::Handle h{};
126 std::string last_err;
127 bool any_file = false;
128
129 // Probe the filesystem first. dlopen of a present plugin runs that
130 // library's static initializers (Fortran / LAMMPS banners), so skip
131 // names that are not on disk.
132 for (const auto &dir : m_search_paths) {
133 for (const auto &name : names) {
134 std::string full = (std::filesystem::path(dir) / name).string();
135 std::error_code ec;
136 if (!std::filesystem::exists(full, ec))
137 continue;
138 any_file = true;
139 h = dynlib::open(full.c_str());
140 if (h) {
141 m_handles[lib_base] = h;
142 return h;
143 }
144 // Capture immediately after LoadLibrary/dlopen (GetLastError/dlerror).
145 last_err = dynlib::error();
146 }
147 }
148
149 // Fall back to system search (LD_LIBRARY_PATH, etc.)
150 for (const auto &name : names) {
151 h = dynlib::open(name.c_str());
152 if (h) {
153 m_handles[lib_base] = h;
154 return h;
155 }
156 last_err = dynlib::error();
157 }
158
159 // Stash diagnostics for throw_not_found (thread-local; open is
160 // mutex-guarded).
161 m_last_load_error = last_err;
162 m_last_load_saw_file = any_file;
163 m_handles[lib_base] = nullptr;
164 return nullptr;
165}
std::string m_last_load_error
Last dynlib::error() from a failed open (for throw_not_found).
void * Handle
Definition DynLib.h:60
std::string error()
Definition DynLib.h:82
Handle open(const char *name) noexcept
Definition DynLib.h:62

◆ operator=()

PluginLoader & eonc::PluginLoader::operator= ( const PluginLoader & )
delete

◆ search_paths()

const std::vector< std::string > & eonc::PluginLoader::search_paths ( ) const
inlinenodiscardoverridevirtualnoexcept

Get the current search paths (for diagnostics).

Implements eonc::IPluginLoader.

Definition at line 100 of file PluginLoader.h.

100 {
101 return m_search_paths;
102 }

◆ throw_not_found()

void eonc::PluginLoader::throw_not_found ( const char * lib_base,
const char * description ) const
overridevirtual

Throw a descriptive error for a missing potential library.

Implements eonc::IPluginLoader.

Definition at line 180 of file PluginLoader.cpp.

181 {
182 auto names = lib_names(lib_base);
183 std::ostringstream oss;
184 oss << description << " requested but library not found.\n"
185 << "Looked for: ";
186 for (size_t i = 0; i < names.size(); ++i) {
187 if (i > 0)
188 oss << ", ";
189 oss << names[i];
190 }
191 oss << "\n";
192 if (!m_search_paths.empty()) {
193 oss << "Search paths (config potentials_path + EON_POTENTIALS_PATH):\n";
194 for (const auto &p : m_search_paths) {
195 oss << " " << p << "\n";
196 }
197 }
199 oss << "A matching file was present but LoadLibrary/dlopen failed";
200 if (!m_last_load_error.empty())
201 oss << ": " << m_last_load_error;
202 oss << "\n(often a missing flang_rt / FortranRuntime dependency on "
203 "PATH).\n";
204 } else if (!m_last_load_error.empty()) {
205 oss << "Loader error: " << m_last_load_error << "\n";
206 }
207 oss << "Set [Potential] potentials_path in config.ini, "
208 "or set EON_POTENTIALS_PATH,\n"
209 "or ensure the libraries are in LD_LIBRARY_PATH.";
210 throw std::runtime_error(oss.str());
211}

◆ Runtime

friend class Runtime
friend

Definition at line 113 of file PluginLoader.h.

Member Data Documentation

◆ m_handles

std::unordered_map<std::string, dynlib::Handle> eonc::PluginLoader::m_handles
private

Definition at line 129 of file PluginLoader.h.

◆ m_last_load_error

std::string eonc::PluginLoader::m_last_load_error
private

Last dynlib::error() from a failed open (for throw_not_found).

Definition at line 131 of file PluginLoader.h.

◆ m_last_load_saw_file

bool eonc::PluginLoader::m_last_load_saw_file = false
private

Definition at line 132 of file PluginLoader.h.

◆ m_mutex

std::mutex eonc::PluginLoader::m_mutex
mutableprivate

Definition at line 128 of file PluginLoader.h.

◆ m_search_paths

std::vector<std::string> eonc::PluginLoader::m_search_paths
private

Definition at line 127 of file PluginLoader.h.


The documentation for this class was generated from the following files: