Menu

[r1556]: / cppdb / trunk / shared_object.cpp  Maximize  Restore  History

Download this file

73 lines (68 with data), 1.3 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
#include "shared_object.h"
#include <string>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
# include <windows.h>
# define RTLD_LAZY 0
namespace cppdb {
namespace {
void *dlopen(char const *name,int /*unused*/)
{
return LoadLibrary(name);
}
void dlclose(void *h)
{
HMODULE m=(HMODULE)(h);
FreeLibrary(m);
}
void *dlsym(void *h,char const *sym)
{
HMODULE m=(HMODULE)(h);
return (void *)GetProcAddress(m,sym);
}
}
}
#else
# include <dlfcn.h>
#endif
namespace cppdb {
shared_object::shared_object(std::string name,void *h) :
dlname_(name),
handle_(h)
{
}
shared_object::~shared_object()
{
dlclose(handle_);
}
ref_ptr<shared_object> shared_object::open(std::string const &name)
{
ref_ptr<shared_object> dl;
void *h=dlopen(name.c_str(),RTLD_LAZY);
if(!h) {
return dl;
}
try {
dl.reset(new shared_object(name,h));
h=0;
return dl;
}
catch(...) {
if(h) {
dlclose(h);
}
throw;
}
}
void *shared_object::sym(std::string const &s)
{
return dlsym(handle_,s.c_str());
}
void *shared_object::safe_sym(std::string const &s)
{
void *p=sym(s);
if(!p) {
throw cppdb_error("cppdb::shared_object::failed to resolve symbol [" + s +"] in " + dlname_);
}
return p;
}
} // cppdb
MongoDB Logo MongoDB