Util--  1.1
dll.hh
Go to the documentation of this file.
1 // Copyright Vladimir Prus 2004.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef BOOST_DLL_HPP_VP_2004_08_24
7 #define BOOST_DLL_HPP_VP_2004_08_24
8 
9 #include <string>
10 #include <stdexcept>
11 #include <boost/shared_ptr.hpp>
12 #include <boost/bind.hpp>
13 #include <boost/type_traits/remove_pointer.hpp>
14 
15 // FIXME: that's for Linux only
16 #include <dlfcn.h>
17 
18 #include <iostream>
19 
20 namespace utilmm { namespace plugin {
21 
22  struct killer
23  {
24  killer(void* h) : h(h) {}
25  template<class T>
26  void operator()(T)
27  {
28  std::cout << "Killing DLL\n";
29  dlclose(h);
30  }
31  void* h;
32  };
33 
34  class dll {
35  public:
36  dll() {} // TODO: should remove this or make non-public
37  dll(const std::string& name) : m_name(name) {}
38 
39  template<typename SymbolType>
40  boost::shared_ptr<
41  typename boost::remove_pointer<SymbolType>::type>
42  get(const std::string& symbol_name) const
43  {
44  // TODO: static assert that SymbolType is a pointer.
45  typedef typename boost::remove_pointer<SymbolType>::type PointedType;
46 
47  // Open the library. Yes, we do it on every access to
48  // a symbol, see the design discussion in the documentation.
49  void* handle = dlopen(m_name.c_str(), RTLD_LAZY|RTLD_GLOBAL);
50  if (!handle) {
51  throw std::logic_error("Could not open DLL");
52  }
53  // Clear the error state.
54  dlerror();
55  void* address = dlsym(handle, symbol_name.c_str());
56  char* error = dlerror();
57  if (error) {
58  throw std::logic_error(error);
59  }
60  // Cast the to right type.
61  SymbolType s = (SymbolType)(address);
62 
63  boost::shared_ptr<PointedType> result(s,
64  //killer(handle));
65  boost::bind(dlclose, handle));
66 
67  return result;
68  }
69 
70  private:
71  std::string m_name;
72  };
73 
74 }}
75 
76 #endif
Definition: dll.hh:22
Definition: dll.hh:34
dll(const std::string &name)
Definition: dll.hh:37
killer(void *h)
Definition: dll.hh:24
void operator()(T)
Definition: dll.hh:26
Definition: auto_flag.hh:6
dll()
Definition: dll.hh:36
void * h
Definition: dll.hh:31

Generated on Mon Oct 8 2018 08:38:45 for Util-- by doxygen 1.8.11
SourceForge.net Project Page