Loading...
Searching...
No Matches
ARTnResource.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
13#pragma once
14
26
27#include "eon/DynLib.h"
28#include <mutex>
29#include <stdexcept>
30#include <string>
31
32namespace eonc {
33
35public:
36 // ARTn C API function pointer types (from artn.h)
37 using artn_create_fn = int (*)();
38 using setup_artn_fn = void (*)(const int nat, bool *cerr);
39 using artn_fn = void (*)(const int nat, const double *etot, const double *f,
40 int const *ityp, double *const tau, const int *order,
41 const double *lat, const int *if_pos, int *disp_code,
42 double *disp_vec, bool *lconv);
43 using artn_destroy_fn = void (*)();
44 using set_param_fn = int (*)(const char *const name, const int crank,
45 const int *csize, const void *cval);
46 using get_param_fn = int (*)(const char *name, void *cval);
47 using get_runparam_fn = int (*)(const char *name, void *cval);
48 using get_data_fn = int (*)(const char *name, void **cval);
49 using print_caller_fn = void (*)();
50 using artn_step_fn = void (*)(const int nat, const double etot,
51 double *const force, int const *ityp,
52 double *const pos, const double *box,
53 const int *if_pos, double *displ_vec,
54 bool *lconv);
60 using get_error_fn = int (*)(void **cmsg);
61
64
66 std::mutex library_mutex;
67
69 [[nodiscard]] bool is_loaded() const noexcept { return m_loaded; }
70
72 void require_loaded() const;
73
74 // Function pointer accessors
77 artn_fn get_artn_fn() const { return artn_; }
88
90 ARTnResource(const ARTnResource &) = delete;
92
93private:
96
97 bool m_loaded{false};
99
100 // Loaded function pointers (null if library not found)
103 artn_fn artn_{nullptr};
112
114 template <typename Fn> Fn load_sym(const char *name) const {
115 return reinterpret_cast<Fn>(dynlib::sym(m_handle, name));
116 }
117};
118
121
122} // namespace eonc
get_error_fn get_error_
print_caller_fn print_caller_
int(*)(const char *name, void **cval) get_data_fn
bool is_loaded() const noexcept
True if libartn was successfully loaded.
void(*)() artn_destroy_fn
void require_loaded() const
Throws std::runtime_error if libartn is not available.
void(*)(const int nat, const double etot, double *const force, int const *ityp, double *const pos, const double *box, const int *if_pos, double *displ_vec, bool *lconv) artn_step_fn
get_data_fn get_data_
dynlib::Handle m_handle
void(*)(const int nat, bool *cerr) setup_artn_fn
std::mutex library_mutex
Global mutex to ensure only one thread accesses ARTn library at a time.
void(*)(const int nat, const double *etot, const double *f, int const *ityp, double *const tau, const int *order, const double *lat, const int *if_pos, int *disp_code, double *disp_vec, bool *lconv) artn_fn
void(*)() print_caller_fn
ARTnResource(const ARTnResource &)=delete
Non-copyable, non-movable.
int(*)(const char *const name, const int crank, const int *csize, const void *cval) set_param_fn
ARTnResource & operator=(const ARTnResource &)=delete
artn_create_fn artn_create_
set_param_fn set_param_
set_param_fn get_set_param_fn() const
artn_step_fn artn_step_
get_param_fn get_get_param_fn() const
int(*)() artn_create_fn
artn_step_fn get_artn_step_fn() const
artn_fn get_artn_fn() const
int(*)(const char *name, void *cval) get_runparam_fn
get_runparam_fn get_runparam_
print_caller_fn get_print_caller_fn() const
get_runparam_fn get_get_runparam_fn() const
Fn load_sym(const char *name) const
Try to load a symbol; returns nullptr on failure.
artn_destroy_fn artn_destroy_
get_data_fn get_get_data_fn() const
get_param_fn get_param_
artn_create_fn get_create_fn() const
setup_artn_fn get_setup_fn() const
setup_artn_fn setup_artn_
get_error_fn get_get_error_fn() const
May be null on older pARTn builds that predate the C get_error wrapper; callers must null-check befor...
artn_destroy_fn get_destroy_fn() const
int(*)(void **cmsg) get_error_fn
Retrieve pARTn error code + c_malloc'd message pointer.
int(*)(const char *name, void *cval) get_param_fn
static ARTnResource & instance()
Singleton accessor (Meyer's pattern).
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.
ARTnResource & get_artn_resource()
Global access to thread-safe ARTn resource.