CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
RuntimeLibrary.hh
Go to the documentation of this file.
1 #ifndef RuntimeLibrary_HH
2 #define RuntimeLibrary_HH
3 
4 #include <iostream>
5 #include <fstream>
6 #include <string>
7 #include <functional>
8 #include <dlfcn.h>
9 #include <cstdlib>
10 #include "casm/system/Popen.hh"
12 #include "casm/casm_io/Log.hh"
13 
14 namespace CASM {
15 
17  class RuntimeLibrary : public Logging {
18 
19  public:
20 
24  std::string _filename_base,
25  std::string _compile_options,
26  std::string _so_options,
27  std::string compile_msg,
28  const Logging &logging = Logging());
29 
31 
37  template<typename Signature>
38  std::function<Signature> get_function(std::string function_name) const {
39 
40  std::function<Signature> func = reinterpret_cast<Signature *>(dlsym(m_handle, function_name.c_str()));
41 
42  const char *dlsym_error = dlerror();
43  if(dlsym_error) {
44  throw std::runtime_error(std::string("Cannot load symbol " + function_name + " \n" + dlsym_error));
45  }
46 
47  return func;
48  }
49 
51  void rm();
52 
54  static std::pair<std::string, std::string> default_cxxflags();
55 
57  static std::pair<std::string, std::string> default_soflags();
58 
60  static std::pair<std::string, std::string> default_cxx();
61 
63  static std::pair<fs::path, std::string> default_casm_includedir();
64 
66  static std::pair<fs::path, std::string> default_casm_libdir();
67 
69  static std::pair<fs::path, std::string> default_boost_includedir();
70 
72  static std::pair<fs::path, std::string> default_boost_libdir();
73 
74 
75  private:
76 
78  void _compile();
79 
81  void _load();
82 
84  void _close();
85 
86 
87  std::string m_filename_base;
88  std::string m_compile_options;
89  std::string m_so_options;
90 
91  void *m_handle;
92 
93  };
94 
95  std::string include_path(const fs::path &dir);
96 
97  std::string link_path(const fs::path &dir);
98 
99 
100 }
101 
102 #endif
RuntimeLibrary(std::string _filename_base, std::string _compile_options, std::string _so_options, std::string compile_msg, const Logging &logging=Logging())
Construct a RuntimeLibrary object, with the options to be used for compile the '.o' file and the '...
static std::pair< std::string, std::string > default_cxxflags()
Default c++ compiler options.
void _load()
Load a library with a given name.
static std::pair< fs::path, std::string > default_casm_includedir()
Return default includedir for CASM.
std::string include_path(const fs::path &dir)
std::string link_path(const fs::path &dir)
static std::pair< std::string, std::string > default_cxx()
Return default compiler.
void _close()
Close the current library.
std::string m_compile_options
Write, compile, load and use code at runtime.
Main CASM namespace.
Definition: complete.cpp:8
std::string m_so_options
Logging(Log &log=default_log(), Log &debug_log=default_log(), Log &err_log=default_err_log())
Definition: Log.hh:250
static std::pair< fs::path, std::string > default_boost_libdir()
Return default libdir for boost.
static std::pair< fs::path, std::string > default_casm_libdir()
Return default libdir for CASM.
static std::pair< std::string, std::string > default_soflags()
Default c++ compiler options.
std::function< Signature > get_function(std::string function_name) const
Obtain a function from the current library.
std::string m_filename_base
DirectoryStructure & dir
Definition: settings.cc:102
void _compile()
Compile a shared library.
void rm()
Remove the current library and source code.
static std::pair< fs::path, std::string > default_boost_includedir()
Return default includedir for boost.