CASM  1.1.0
A Clusters Approach to Statistical Mechanics
RuntimeLibrary.hh
Go to the documentation of this file.
1 #ifndef RuntimeLibrary_HH
2 #define RuntimeLibrary_HH
3 
4 #include <dlfcn.h>
5 
6 #include <cstdlib>
7 #include <fstream>
8 #include <functional>
9 #include <iostream>
10 #include <string>
11 
13 
14 namespace CASM {
15 
16 class runtime_lib_compile_error : public std::runtime_error {
17  public:
18  runtime_lib_compile_error(std::string _filename_base, std::string _cmd,
19  std::string _result, std::string _what);
20 
21  std::string filename_base;
22  std::string cmd;
23  std::string result;
24 
25  void print(std::ostream &sout) const;
26 };
27 
28 class runtime_lib_shared_error : public std::runtime_error {
29  public:
30  runtime_lib_shared_error(std::string _filename_base, std::string _cmd,
31  std::string _result, std::string _what);
32 
33  std::string filename_base;
34  std::string cmd;
35  std::string result;
36 
37  void print(std::ostream &sout) const;
38 };
39 
42  public:
46  RuntimeLibrary(std::string _filename_base, std::string _compile_options,
47  std::string _so_options);
48 
50 
57  template <typename Signature>
58  std::function<Signature> get_function(std::string function_name) const {
59  std::function<Signature> func =
60  reinterpret_cast<Signature *>(dlsym(m_handle, function_name.c_str()));
61 
62  const char *dlsym_error = dlerror();
63  if (dlsym_error) {
64  throw std::runtime_error(std::string(
65  "Cannot load symbol " + function_name + " \n" + dlsym_error));
66  }
67 
68  return func;
69  }
70 
72  void rm();
73 
75  static std::pair<std::string, std::string> default_cxxflags();
76 
78  static std::pair<std::string, std::string> default_soflags();
79 
81  static std::pair<std::string, std::string> default_cxx();
82 
84  static std::pair<fs::path, std::string> default_casm_includedir();
85 
87  static std::pair<fs::path, std::string> default_casm_libdir();
88 
90  static std::pair<fs::path, std::string> default_boost_includedir();
91 
93  static std::pair<fs::path, std::string> default_boost_libdir();
94 
95  private:
97  void _compile();
98 
100  void _load();
101 
103  void _close();
104 
105  std::string m_filename_base;
106  std::string m_compile_options;
107  std::string m_so_options;
108 
109  void *m_handle;
110 };
111 
112 std::string include_path(const fs::path &dir);
113 
114 std::string link_path(const fs::path &dir);
115 
116 } // namespace CASM
117 
118 #endif
Write, compile, load and use code at runtime.
std::string m_compile_options
static std::pair< std::string, std::string > default_soflags()
Default c++ compiler options.
static std::pair< fs::path, std::string > default_boost_includedir()
Return default includedir for boost.
static std::pair< std::string, std::string > default_cxxflags()
Default c++ compiler options.
void _close()
Close the current library.
RuntimeLibrary(std::string _filename_base, std::string _compile_options, std::string _so_options)
Construct a RuntimeLibrary object, with the options to be used for compile the '.o' file and the '....
static std::pair< fs::path, std::string > default_casm_includedir()
Return default includedir for CASM.
static std::pair< fs::path, std::string > default_boost_libdir()
Return default libdir for boost.
void rm()
Remove the current library and source code.
static std::pair< std::string, std::string > default_cxx()
Return default compiler.
void _load()
Load a library with a given name.
std::function< Signature > get_function(std::string function_name) const
Obtain a function from the current library.
void _compile()
Compile a shared library.
static std::pair< fs::path, std::string > default_casm_libdir()
Return default libdir for CASM.
std::string m_filename_base
void print(std::ostream &sout) const
runtime_lib_compile_error(std::string _filename_base, std::string _cmd, std::string _result, std::string _what)
void print(std::ostream &sout) const
runtime_lib_shared_error(std::string _filename_base, std::string _cmd, std::string _result, std::string _what)
Main CASM namespace.
Definition: APICommand.hh:8
std::string include_path(const fs::path &dir)
std::string link_path(const fs::path &dir)
DirectoryStructure const & dir
Definition: settings.cc:136