ome-common  5.3.2
module.h
Go to the documentation of this file.
1 /*
2  * #%L
3  * OME-COMMON C++ library for C++ compatibility/portability
4  * %%
5  * Copyright © 2006 - 2016 Open Microscopy Environment:
6  * - Massachusetts Institute of Technology
7  * - National Institutes of Health
8  * - University of Dundee
9  * - Board of Regents of the University of Wisconsin-Madison
10  * - Glencoe Software, Inc.
11  * %%
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * The views and conclusions contained in the software and documentation are
34  * those of the authors and should not be interpreted as representing official
35  * policies, either expressed or implied, of any organization.
36  * #L%
37  */
38 
46 #ifndef OME_COMMON_MODULE_H
47 #define OME_COMMON_MODULE_H
48 
49 #include <boost/filesystem/operations.hpp>
50 #include <boost/filesystem/path.hpp>
51 
52 #include <ome/common/config.h>
53 
57 namespace ome
58 {
59  namespace common
60  {
61 
75  const boost::filesystem::path&
76  module_runtime_path(const std::string& dtype);
77 
92  void
94 
103  struct Module
104  {
106  std::string name;
109  std::string envvar;
112  std::string module_envvar;
115  std::string root_envvar;
118  boost::filesystem::path abspath;
120  boost::filesystem::path relpath;
122  boost::filesystem::path install_prefix;
124  boost::filesystem::path shlibpath;
126  boost::filesystem::path realpath;
131  boost::filesystem::path (*module_path)();
132 
149  Module(const std::string& name,
150  const std::string& envvar,
151  const std::string& module_envvar,
152  const std::string& root_envvar,
153  const boost::filesystem::path& abspath,
154  const boost::filesystem::path& relpath,
155  const boost::filesystem::path& install_prefix,
156  const boost::filesystem::path& shlibpath,
157  boost::filesystem::path (*module_path)());
158  };
159 
167  {
169  std::string name;
172 
191  RegisterModule(const std::string& name,
192  const std::string& envvar,
193  const std::string& module_envvar,
194  const std::string& root_envvar,
195  const boost::filesystem::path& abspath,
196  const boost::filesystem::path& relpath,
197  const boost::filesystem::path& install_prefix,
198  const boost::filesystem::path& shlibpath,
199  boost::filesystem::path (*module_path)());
200 
206  ~RegisterModule();
207  };
208  }
209 }
210 
211 // Set to include introspection functionality (used for registering
212 // paths; not for normal use).
213 #ifdef OME_COMMON_MODULE_INTROSPECTION
214 
215 #ifdef OME_HAVE_DLADDR
216 #ifndef _GNU_SOURCE
217 # define _GNU_SOURCE 1
218 #endif
219 #include <dlfcn.h>
220 #endif // OME_HAVE_DLADDR
221 
222 #ifdef _MSC_VER
223 # include <windows.h>
224 #endif
225 
226 namespace
227 {
228 
229 #ifdef OME_HAVE_DLADDR
230  Dl_info this_module;
231 
232  __attribute__((constructor))
233  void
234  find_module(void)
235  {
236  if(!dladdr(reinterpret_cast<void *>(find_module), &this_module))
237  {
238  this_module.dli_fname = 0;
239  }
240  }
241 
242  boost::filesystem::path
243  module_path()
244  {
245  if (this_module.dli_fname)
246  return boost::filesystem::canonical(boost::filesystem::path(this_module.dli_fname));
247  return boost::filesystem::path();
248  }
249 #elif _MSC_VER
250  HMODULE
251  find_module(void)
252  {
253  static bool found_module = false;
254  static HMODULE this_module;
255 
256  if (!found_module)
257  {
258  if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
259  GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
260  reinterpret_cast<LPCWSTR>(&find_module),
261  &this_module))
262  {
263  this_module = 0;
264  }
265  found_module = true;
266  }
267  return this_module;
268  }
269 
270  boost::filesystem::path
271  module_path()
272  {
273  HMODULE this_module = find_module();
274  if (this_module)
275  {
276  WCHAR win_wide_path[MAX_PATH];
277  GetModuleFileNameW(this_module, win_wide_path, sizeof(win_wide_path));
278  return boost::filesystem::path(win_wide_path);
279  }
280  return boost::filesystem::path();
281  }
282 #else // No introspection available
283  boost::filesystem::path
284  module_path()
285  {
286  return boost::filesystem::path();
287  }
288 #endif // _MSC_VER
289 }
290 #endif // OME_COMMON_MODULE_INTROSPECTION
291 
292 #endif // OME_COMMON_MODULE_H
293 
294 /*
295  * Local Variables:
296  * mode:C++
297  * End:
298  */
boost::filesystem::path realpath
The detected path (used to cache search result).
Definition: module.h:126
std::string root_envvar
Name of the environment variable used to specify the installation root.
Definition: module.h:115
std::string name
Name of the path, e.g. "bin" or "ome-xml-schema".
Definition: module.h:169
boost::filesystem::path abspath
Absolute path (used when configured to use an absolute install path).
Definition: module.h:118
const fs::path & module_runtime_path(const std::string &dtype)
Get the runtime installation prefix path for a module.
Definition: module.cpp:372
boost::filesystem::path shlibpath
Shared library path (used for relocatable installs).
Definition: module.h:124
std::string name
Name of the path, e.g. "bin" or "ome-xml-schema".
Definition: module.h:106
boost::filesystem::path relpath
Relative path (used for relocatable installs).
Definition: module.h:120
Module(const std::string &name, const std::string &envvar, const std::string &module_envvar, const std::string &root_envvar, const boost::filesystem::path &abspath, const boost::filesystem::path &relpath, const boost::filesystem::path &install_prefix, const boost::filesystem::path &shlibpath, boost::filesystem::path(*module_path)())
Constructor.
Definition: module.cpp:273
bool registered
Is the path registered in the path map?
Definition: module.h:171
Open Microscopy Environment C++.
Definition: base64.h:49
A run-time path for a given module.
Definition: module.h:103
boost::filesystem::path(* module_path)()
Function to obtain the absolute path of the module providing the path (from the shared library or DLL...
Definition: module.h:131
std::string module_envvar
Name of the environment variable used to specify the module installation root.
Definition: module.h:112
std::string envvar
Name of the environment variable used to override the autodetected path.
Definition: module.h:109
Register a module to make it available to module_runtime_path().
Definition: module.h:166
boost::filesystem::path install_prefix
Absolute installation path (used for non-relocatable installs).
Definition: module.h:122
void register_module_paths()
Register OME-Common module paths.
Definition: module.cpp:489