serious bugfix in Raise_IdxError (number got formatted as string, causing a segmentat...
[vox.git] / src / stdlib / vxlibrary.hpp
blobeff522fdcc6b5cd490b9d77d3ec58c0eebed3b8a
2 #include "helper.hpp"
3 #include "sharedlib.h"
6 class VXSharedLibrary {
7 public:
8 class Error: public std::runtime_error {
9 public:
10 Error():
11 std::runtime_error("VXSharedLibrary::Error")
14 Error(const std::string& msg):
15 std::runtime_error(msg)
18 virtual ~Error() throw()
22 class LoadError: public VXSharedLibrary::Error {
23 public:
24 LoadError(const std::string& msg):
25 VXSharedLibrary::Error(msg.c_str())
30 class SymbolError: public VXSharedLibrary::Error {
31 public:
32 SymbolError(const std::string& msg):
33 VXSharedLibrary::Error(msg.c_str())
38 class CloseError: public VXSharedLibrary::Error {
39 public:
40 CloseError(const std::string& msg):
41 VXSharedLibrary::Error(msg.c_str())
46 class NotOpenError: public VXSharedLibrary::Error {
47 public:
48 NotOpenError(const std::string& msg):
49 VXSharedLibrary::Error(msg.c_str())
54 public:
55 static VXSharedLibrary openSelf()
57 VXSharedLibrary lib(shl_openself());
58 return lib;
62 private:
63 shl_handle_t* m_handle;
64 std::string errormessage;
65 std::string message;
66 bool m_isclosed;
68 private:
69 std::string lasterr()
71 const char* err = shl_geterrstr(m_handle);
72 if(err == NULL)
74 return strerror(errno);
76 return err;
79 void setup()
81 m_isclosed = false;
84 public:
85 VXSharedLibrary()
89 VXSharedLibrary(shl_handle_t* ctx)
91 this->setup();
92 this->m_handle = ctx;
95 VXSharedLibrary(const std::string& path)
97 this->setup();
98 this->open(path);
101 ~VXSharedLibrary()
104 void open(const std::string& path)
106 this->setup();
107 this->m_handle = shl_openlib(path.c_str());
108 if(this->m_handle == NULL)
110 this->destroy();
111 throw VXSharedLibrary::LoadError(this->lasterr());
115 void* resolve(const std::string& funcname)
117 void* ptr;
118 ptr = shl_resolve(this->m_handle, funcname.c_str());
119 if(ptr == NULL)
121 throw VXSharedLibrary::SymbolError(this->lasterr());
123 return ptr;
126 template<typename func_t> func_t resolve(const std::string& name)
128 return forcecast<func_t>(this->resolve(name));
131 void destroy()
133 shl_destroy(this->m_handle);
136 void close()
138 if(this->m_handle != NULL)
140 if(!m_isclosed)
142 shl_closelib(this->m_handle);
143 m_isclosed = true;
145 else
147 shl_destroy(this->m_handle);
150 else
152 shl_destroy(this->m_handle);