Vcsn  2.5
Be Rational
xltdl.cc
Go to the documentation of this file.
1 #include <cstdio>
2 #include <iostream>
3 
4 #include <boost/filesystem.hpp>
5 
6 #include <lib/vcsn/misc/xltdl.hh>
7 
8 namespace vcsn
9 {
10  namespace detail
11  {
12  namespace
13  {
14  void init()
15  {
16  static bool first = true;
17  if (first)
18  {
19  lt_dlinit();
20  first = false;
21  }
22  }
23  }
24 
25  /*-------------.
26  | xlt_advise. |
27  `-------------*/
28 
30  : path_()
31  {
32  init();
33  if (lt_dladvise_init(&advise_))
34  raise("failed to initialize dladvise: ", lt_dlerror());
35  }
36 
37  // FIXME: Bad: dtors must not throw.
39  {
40  // FIXME: lt_dlexit when we refcount.
41  if (lt_dladvise_destroy(&advise_))
42  raise("failed to destroy dladvise: ", lt_dlerror());
43  }
44 
45  xlt_advise&
47  {
48  if (global ? lt_dladvise_global(&advise_) : lt_dladvise_local(&advise_))
49  raise("failed to set dladvise to ", global ? "global" : "local",
50  ": ", lt_dlerror());
51  return *this;
52  }
53 
54  xlt_advise&
56  {
57  if (lt_dladvise_ext(&advise_))
58  raise("failed to set dladvise to ext: ", lt_dlerror());
59  return *this;
60  }
61 
62  const file_library&
63  xlt_advise::path() const noexcept
64  {
65  return path_;
66  }
67 
69  xlt_advise::path() noexcept
70  {
71  return path_;
72  }
73 
74  xlt_advise&
75  xlt_advise::path(const file_library& p) noexcept
76  {
77  path_ = p;
78  return *this;
79  }
80 
81  lt_dlhandle
83  {
84  return lt_dlopenadvise(s.c_str(), advise_);
85  }
86 
88  xlt_advise::open(const std::string& s)
89  {
90  auto res = lt_dlhandle{nullptr};
91  // We cannot simply use search_file in file_library, because we
92  // don't know the extension of the file we are looking for (*.la,
93  // *.so, *.dyld etc.). That's an implementation detail that ltdl
94  // saves us from.
95  using path = file_library::path;
96  if (path_.search_path_get().empty() || path(s).is_absolute())
97  res = dlopen_(s);
98  else
99  for (const auto& p: path_.search_path_get())
100  if ((res = dlopen_(p / s)))
101  break;
102 
103  if (!res)
104  raise("failed to dlopen module ", s, ": ", lt_dlerror());
105 
106  return res;
107  }
108 
109 
110  /*-------------.
111  | xlt_handle. |
112  `-------------*/
113 
114  xlt_handle::xlt_handle(lt_dlhandle h)
115  : handle(h)
116  {}
117 
119  {
120  // FIXME: We can't close -- yet. We need to keep track of the
121  // number of trackers. Otherwise a simple "handle h =
122  // advise.open" will close the handle when cleaning the temporary
123  // made by "advise.open".
124 
125  // close();
126  }
127 
128  void
130  {
131  if (handle)
132  {
133  int errors = lt_dlclose(handle);
134  handle = nullptr;
135  if (errors)
136  raise("failed to dlclose module: ", lt_dlerror());
137  }
138  }
139 
140  void
142  {
143  assert(handle);
144  handle = nullptr;
145  }
146 
147  void
148  xlt_handle::attach(lt_dlhandle h)
149  {
150  assert(!handle);
151  handle = h;
152  }
153 
154 
155  /*-------------.
156  | Standalone. |
157  `-------------*/
158 
159  xlt_handle
160  xlt_openext(const std::string& s, bool global)
161  {
162  return xlt_advise().global(global).ext().open(s);
163  }
164  }
165 }
xlt_handle open(const std::string &s)
Definition: xltdl.cc:88
Definition: a-star.hh:8
lt_dlhandle dlopen_(const file_library::path &s) const
Does not use the search path. Can return 0.
Definition: xltdl.cc:82
return res
Definition: multiply.hh:398
Manage search paths.
Definition: file-library.hh:22
lt_dladvise advise_
Definition: xltdl.hh:44
lt_dlhandle handle
The handle.
Definition: xltdl.hh:77
const file_library & path() const noexcept
Definition: xltdl.cc:63
xlt_advise & ext()
Definition: xltdl.cc:55
xlt_handle xlt_openext(const std::string &s, bool global)
Wrapper around lt_dlopenext.
Definition: xltdl.cc:160
xlt_handle(lt_dlhandle h=nullptr)
Definition: xltdl.cc:114
void attach(lt_dlhandle h)
Detach so that destruction does not close.
Definition: xltdl.cc:148
boost::filesystem::path path
Definition: file-library.hh:27
Explicit path representation.
Definition: path.hh:16
const path_list_type & search_path_get() const
void detach()
Detach so that destruction does not close.
Definition: xltdl.cc:141
xlt_advise & global(bool global)
Definition: xltdl.cc:46
file_library path_
Definition: xltdl.hh:45
void close()
Close the handle.
Definition: xltdl.cc:129