ArkScript
A small, fast, functional and scripting language for video games
Plugin.cpp
Go to the documentation of this file.
1#include <Ark/VM/Plugin.hpp>
2
3#include <sstream>
4#include <iomanip>
5#include <iostream>
6
7namespace Ark::internal
8{
10 m_instance(NULL),
11 m_path(""),
12 m_loaded(false)
13 {}
14
15 SharedLibrary::SharedLibrary(const std::string& path) :
16 m_instance(NULL),
17 m_path(path),
18 m_loaded(false)
19 {
20 load(m_path);
21 }
22
24 {
25 unload();
26 }
27
28 void SharedLibrary::load(const std::string& path)
29 {
30 if (m_loaded)
31 unload();
32
33 m_path = path;
34
35#if defined(ARK_OS_WINDOWS)
36 if (NULL == (m_instance = LoadLibrary(m_path.c_str())))
37 {
38 throw std::system_error(
39 std::error_code(::GetLastError(), std::system_category()), "Couldn't load the library at " + path);
40 }
41#elif defined(ARK_OS_LINUX)
42 if (NULL == (m_instance = dlopen(m_path.c_str(), RTLD_LAZY | RTLD_GLOBAL)))
43 {
44 throw std::system_error(
45 std::error_code(errno, std::system_category()), "Couldn't load the library at " + path + ", " + std::string(dlerror()));
46 }
47#endif
48 m_loaded = true;
49 }
50
52 {
53 if (m_loaded)
54 {
55#if defined(ARK_OS_WINDOWS)
56 FreeLibrary(m_instance);
57#elif defined(ARK_OS_LINUX)
58 dlclose(m_instance);
59#endif
60 }
61 }
62
63}
Loads .dll/.so/.dynlib files.
~SharedLibrary()
Destroy the Shared Library object.
Definition: Plugin.cpp:23
SharedLibrary()
Construct a new Shared Library object.
Definition: Plugin.cpp:9
const std::string & path() const
Definition: Plugin.hpp:79
void unload()
Unload the shared library.
Definition: Plugin.cpp:51
void load(const std::string &path)
Load a shared library.
Definition: Plugin.cpp:28