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 ([email protected])
4 * @brief Loads .dll/.so/.dynlib files
5 * @version 1.0
6 * @date 2020-10-27
7 *
8 * @copyright Copyright (c) 2020-2024
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# define NOMINMAX
21# include <Windows.h>
22#elif defined(ARK_OS_LINUX)
23# include <dlfcn.h>
24#else
25# error "Can not identify the platform on which you are running, aborting"
26#endif
27
28#include <string>
29#include <system_error>
30
31#include <fmt/core.h>
32
33namespace Ark::internal
34{
35 /**
36 * @brief Handling a shared library as an ArkScript plugin
37 *
38 */
40 {
41 public:
42 /**
43 * @brief Construct a new Shared Library object
44 *
45 */
47
48 /**
49 * @brief Disable copy semantics as this contains a pointer.
50 *
51 */
52 SharedLibrary(const SharedLibrary&) = delete;
53
54 /**
55 * @brief Disable copy semantics as this contains a pointer.
56 *
57 */
59
60 /**
61 * @brief Construct a new Shared Library object
62 *
63 * @param path path to the shared library
64 */
65 explicit SharedLibrary(std::string path);
66
67 /**
68 * @brief Destroy the Shared Library object
69 *
70 */
72
73 /**
74 * @brief Load a shared library
75 *
76 * @param path path to the shared library
77 */
78 void load(const std::string& path);
79
80 /**
81 * @brief Unload the shared library
82 *
83 */
84 void unload() const;
85
86 [[nodiscard]] const std::string& path() const { return m_path; }
87
88 /**
89 * @brief Return a function from the shared library
90 *
91 * @tparam T the type of the function to retrieve
92 * @param procname the name of the function to retrieve
93 * @return T the function from the shared library, if it was found
94 */
95 template <typename T>
96 T get(const std::string& procname)
97 {
98 T funcptr;
99
100#if defined(ARK_OS_WINDOWS)
101 if (NULL == (funcptr = reinterpret_cast<T>(GetProcAddress(m_instance, procname.c_str()))))
102 {
103 throw std::system_error(
104 std::error_code(::GetLastError(), std::system_category()), std::string("PluginError: Couldn't find ") + procname);
105 }
106#elif defined(ARK_OS_LINUX)
107 if (NULL == (funcptr = reinterpret_cast<T>(dlsym(m_instance, procname.c_str()))))
108 {
109 throw std::system_error(
110 std::error_code(errno, std::system_category()), fmt::format("PluginError: Couldn't find {}, {}", procname, dlerror()));
111 }
112#endif
113 return funcptr;
114 }
115
116 private:
117#if defined(ARK_OS_WINDOWS)
118 HINSTANCE m_instance;
119#elif defined(ARK_OS_LINUX)
120 void* m_instance;
121#endif
122 std::string m_path;
124 };
125}
126
127#endif
ArkScript configuration macros.
Handling a shared library as an ArkScript plugin.
Definition Plugin.hpp:40
~SharedLibrary()
Destroy the Shared Library object.
Definition Plugin.cpp:21
SharedLibrary()
Construct a new Shared Library object.
Definition Plugin.cpp:8
const std::string & path() const
Definition Plugin.hpp:86
SharedLibrary(const SharedLibrary &)=delete
Disable copy semantics as this contains a pointer.
void load(const std::string &path)
Load a shared library.
Definition Plugin.cpp:26
SharedLibrary & operator=(const SharedLibrary &)=delete
Disable copy semantics as this contains a pointer.
void unload() const
Unload the shared library.
Definition Plugin.cpp:49
T get(const std::string &procname)
Return a function from the shared library.
Definition Plugin.hpp:96