ArkScript
A small, fast, functional and scripting language for video games
Plugin.hpp
Go to the documentation of this file.
1 /**
2  * @file Plugin.hpp
3  * @author Alexandre Plateau (lexplt.dev@gmail.com)
4  * @brief Loads .dll/.so/.dynlib files
5  * @version 0.3
6  * @date 2020-10-27
7  *
8  * @copyright Copyright (c) 2020-2021
9  *
10  */
11 
12 #ifndef ARK_VM_PLUGIN_HPP
13 #define ARK_VM_PLUGIN_HPP
14 
15 #include <Ark/Platform.hpp>
16 
17 #if defined(ARK_OS_WINDOWS)
18 // do not include winsock.h
19 # define WIN32_LEAN_AND_MEAN
20 # include <Windows.h>
21 #elif defined(ARK_OS_LINUX)
22 # include <dlfcn.h>
23 #else
24 # error "Can not identify the platform on which you are running, aborting"
25 #endif
26 
27 #include <string>
28 #include <system_error>
29 #include <exception>
30 
31 namespace Ark::internal
32 {
33  /**
34  * @brief Handling a shared library as an ArkScript plugin
35  *
36  */
38  {
39  public:
40  /**
41  * @brief Construct a new Shared Library object
42  *
43  */
44  SharedLibrary();
45 
46  /**
47  * @brief Disable copy semantics as this contains a pointer.
48  *
49  */
50  SharedLibrary(const SharedLibrary&) = delete;
52 
53  /**
54  * @brief Construct a new Shared Library object
55  *
56  * @param path path to the shared library
57  */
58  explicit SharedLibrary(const std::string& path);
59 
60  /**
61  * @brief Destroy the Shared Library object
62  *
63  */
65 
66  /**
67  * @brief Load a shared library
68  *
69  * @param path path to the shared library
70  */
71  void load(const std::string& path);
72 
73  /**
74  * @brief Unload the shared library
75  *
76  */
77  void unload();
78 
79  inline const std::string& path() const { return m_path; }
80 
81  /**
82  * @brief Return a function from the shared library
83  *
84  * @tparam T the type of the function to retrieve
85  * @param procname the name of the function to retrieve
86  * @return T the function from the shared library, if it was found
87  */
88  template <typename T>
89  T get(const std::string& procname)
90  {
91  T funcptr;
92 
93 #if defined(ARK_OS_WINDOWS)
94  if (NULL == (funcptr = reinterpret_cast<T>(GetProcAddress(m_instance, procname.c_str()))))
95  {
96  throw std::system_error(
97  std::error_code(::GetLastError(), std::system_category()), std::string("PluginError: Couldn't find ") + procname);
98  }
99 #elif defined(ARK_OS_LINUX)
100  if (NULL == (funcptr = reinterpret_cast<T>(dlsym(m_instance, procname.c_str()))))
101  {
102  throw std::system_error(
103  std::error_code(errno, std::system_category()), std::string("PluginError: Couldn't find ") + procname + ", " + std::string(dlerror()));
104  }
105 #endif
106  return funcptr;
107  }
108 
109  private:
110 #if defined(ARK_OS_WINDOWS)
111  HINSTANCE m_instance;
112 #elif defined(ARK_OS_LINUX)
113  void* m_instance;
114 #endif
115  std::string m_path;
116  bool m_loaded;
117  };
118 }
119 
120 #endif
ArkScript configuration macros.
Handling a shared library as an ArkScript plugin.
Definition: Plugin.hpp:38
~SharedLibrary()
Destroy the Shared Library object.
Definition: Plugin.cpp:23
SharedLibrary()
Construct a new Shared Library object.
Definition: Plugin.cpp:9
void unload()
Unload the shared library.
Definition: Plugin.cpp:51
SharedLibrary(const SharedLibrary &)=delete
Disable copy semantics as this contains a pointer.
const std::string & path() const
Definition: Plugin.hpp:79
void load(const std::string &path)
Load a shared library.
Definition: Plugin.cpp:28
SharedLibrary & operator=(const SharedLibrary &)=delete
T get(const std::string &procname)
Return a function from the shared library.
Definition: Plugin.hpp:89