Debian Codegraph @

src/shared_object.cpp

raw symbols SCIP
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <[email protected]>
4 //
5 // Distributed under:
6 //
7 // the Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // or (at your opinion) under:
12 //
13 // The MIT License
14 // (See accompanying file MIT.txt or a copy at
15 // http://www.opensource.org/licenses/mit-license.php)
16 //
17 ///////////////////////////////////////////////////////////////////////////////
18 #define CPPDB_SOURCE
19 #include <cppdb/shared_object.h>
20 #include <string>
21
22 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
23 # include <windows.h>
24 # define RTLD_LAZY 0
25
26 namespace cppdb {
27 namespace {
28 void *dlopen(char const *name,int /*unused*/)
29 {
30 return LoadLibrary(name);
31 }
32 void dlclose(void *h)
33 {
34 HMODULE m=(HMODULE)(h);
35 FreeLibrary(m);
36 }
37 void *dlsym(void *h,char const *sym)
38 {
39 HMODULE m=(HMODULE)(h);
40 return (void *)GetProcAddress(m,sym);
41 }
42 }
43 }
44
45 #else
46 # include <dlfcn.h>
47 #endif
48 namespace cppdb {
49 shared_object::shared_object(std::string name,void *h) :
50 dlname_(name),
51 handle_(h)
52 {
53 }
54 shared_object::~shared_object()
55 {
56 dlclose(handle_);
57 }
58 ref_ptr<shared_object> shared_object::open(std::string const &name)
59 {
60 ref_ptr<shared_object> dl;
61 void *h=dlopen(name.c_str(),RTLD_LAZY);
62 if(!h) {
63 return dl;
64 }
65 try {
66 dl.reset(new shared_object(name,h));
67 h=0;
68 return dl;
69 }
70 catch(...) {
71 if(h) {
72 dlclose(h);
73 }
74 throw;
75 }
76 }
77 void *shared_object::sym(std::string const &s)
78 {
79 return dlsym(handle_,s.c_str());
80 }
81 void *shared_object::safe_sym(std::string const &s)
82 {
83 void *p=sym(s);
84 if(!p) {
85 throw cppdb_error("cppdb::shared_object::failed to resolve symbol [" + s +"] in " + dlname_);
86 }
87 return p;
88 }
89 } // cppdb