Loading...
Searching...
No Matches
LammpsLoader.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
19
20#include "eon/DynLib.h"
21
22#include <stdexcept>
23#include <string>
24
25#ifdef EONMPI
26#include <mpi.h>
27#endif
28
29namespace eonc {
30
31// ---------------------------------------------------------------------------
32// LammpsLoader: singleton that loads liblammps function pointers at runtime
33// ---------------------------------------------------------------------------
35public:
36 // LAMMPS C API function pointer types (from library.h)
37 using open_no_mpi_fn = void *(*)(int, char **, void **);
38 using close_fn = void (*)(void *);
39 using command_fn = char *(*)(void *, const char *);
40 using file_fn = void (*)(void *, const char *);
41 using scatter_atoms_fn = void (*)(void *, const char *, int, int, void *);
42 using extract_var_fn = void *(*)(void *, const char *, const char *);
43#ifdef EONMPI
44 using open_mpi_fn = void *(*)(int, char **, MPI_Comm, void **);
45#endif
46
48 static LammpsLoader &instance();
49
50 // Loaded function pointers (null if library not found)
52 close_fn close{nullptr};
54 file_fn file{nullptr};
57#ifdef EONMPI
58 open_mpi_fn open_mpi{nullptr};
59#endif
60
62 [[nodiscard]] bool is_loaded() const noexcept { return m_loaded; }
63
66 [[nodiscard]] bool available() const;
67
69 void require_loaded();
70
71 LammpsLoader(const LammpsLoader &) = delete;
73
74private:
75 LammpsLoader() = default;
77
78 void ensure_loaded();
79
80 bool m_loaded{false};
81 bool m_tried{false};
83
85 template <typename Fn> Fn load_sym(const char *name) {
86 return reinterpret_cast<Fn>(dynlib::sym(m_handle, name));
87 }
88};
89
90} // namespace eonc
char *(*)(void *, const char *) command_fn
void *(*)(void *, const char *, const char *) extract_var_fn
scatter_atoms_fn scatter_atoms
bool available() const
Filesystem probe: a candidate liblammps file is visible without dlopen, so LAMMPS static initializers...
void require_loaded()
Load on first use, then throw if liblammps is not available.
open_no_mpi_fn open_no_mpi
void(*)(void *, const char *, int, int, void *) scatter_atoms_fn
LammpsLoader(const LammpsLoader &)=delete
Fn load_sym(const char *name)
Try to load a symbol; returns nullptr on failure.
LammpsLoader & operator=(const LammpsLoader &)=delete
void(*)(void *) close_fn
void *(*)(int, char **, void **) open_no_mpi_fn
static LammpsLoader & instance()
Thread-safe singleton accessor (Meyer's pattern). Does not dlopen.
dynlib::Handle m_handle
LammpsLoader()=default
void(*)(void *, const char *) file_fn
extract_var_fn extract_variable
bool is_loaded() const noexcept
True if liblammps was successfully dlopened and the C ABI resolved.
void * sym(Handle h, const char *name) noexcept
Definition DynLib.h:75
void * Handle
Definition DynLib.h:60
RAII resource manager for the ARTn C library with global synchronization.