Repository navigation
Expand file tree
/
Copy pathice_function.h
More file actions
81 lines (74 loc) · 2.1 KB
/
Copy pathice_function.h
File metadata and controls
81 lines (74 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Ice multiplatform dynamic library loader.
*
* @copyright 2011 David Rebbe
*/
#ifndef __ICE_FUNCTIONS_H__
#define __ICE_FUNCTIONS_H__
#include "ice_library.h"
#include "ice_exception.h"
#include <string>
#include <sstream>
namespace ice
{
template <class Signature>
class Function;
}
template <class Signature>
class ice::Function
{
ice::Library* m_lib;
public:
Function(ice::Library* library, std::string name)
: m_name(name)
{
m_lib = library;
if (library == NULL)
{
std::stringstream ss;
ss << "Library is NULL, can't call function: '" <<
name << "'";
throw ice::Exception(ss.str());
}
#if (defined(_WIN32) || defined(__WIN32__))
m_func = reinterpret_cast<Signature*>(
GetProcAddress(library->_library(), name.c_str()));
if (m_func == NULL)
{
std::stringstream err;
err << "Failed to Retrieve address of function '" << name <<
"': Windows Error #" << GetLastError() << " for library '" <<
library->name() << "'";
throw ice::Exception(err.str());
}
#else
m_func = reinterpret_cast<Signature*>(
dlsym(library->_library(), name.c_str()));
if (m_func == NULL)
{
std::stringstream err;
err << "Failed to Retrieve address of function '" << name <<
"': " << dlerror() << " for library '" <<
library->name() << "'";
throw ice::Exception(err.str());
}
#endif
}
operator Signature *() const
{
if (m_func == NULL)
{
std::stringstream ss;
ss << "Function address '" << m_name + "' isn't resolved for library: '" <<
m_lib->name() << "'";
throw ice::Exception(ss.str());
}
return m_func;
}
bool isValid() const { return m_func != NULL; }
std::string name() const { return m_name; }
protected:
Signature* m_func;
const std::string m_name;
};
#endif // ice_function.h