ome-common  5.5.0
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 #include <ome/common/filesystem.h>
54 
58 namespace ome
59 {
60  namespace common
61  {
62 
76  const boost::filesystem::path&
77  module_runtime_path(const std::string& dtype);
78 
93  void
95 
104  struct Module
105  {
107  std::string name;
110  std::string envvar;
113  std::string module_envvar;
116  std::string root_envvar;
119  boost::filesystem::path abspath;
121  boost::filesystem::path relpath;
123  boost::filesystem::path install_prefix;
125  boost::filesystem::path shlibpath;
127  boost::filesystem::path realpath;
132  boost::filesystem::path (*module_path)();
133 
150  Module(const std::string& name,
151  const std::string& envvar,
152  const std::string& module_envvar,
153  const std::string& root_envvar,
154  const boost::filesystem::path& abspath,
155  const boost::filesystem::path& relpath,
156  const boost::filesystem::path& install_prefix,
157  const boost::filesystem::path& shlibpath,
158  boost::filesystem::path (*module_path)());
159  };
160 
168  {
170  std::string name;
173 
192  RegisterModule(const std::string& name,
193  const std::string& envvar,
194  const std::string& module_envvar,
195  const std::string& root_envvar,
196  const boost::filesystem::path& abspath,
197  const boost::filesystem::path& relpath,
198  const boost::filesystem::path& install_prefix,
199  const boost::filesystem::path& shlibpath,
200  boost::filesystem::path (*module_path)());
201 
207  ~RegisterModule();
208  };
209  }
210 }
211 
212 // Set to include introspection functionality (used for registering
213 // paths; not for normal use).
214 #ifdef OME_COMMON_MODULE_INTROSPECTION
215 
216 #ifdef OME_HAVE_DLADDR
217 #ifndef _GNU_SOURCE
218 # define _GNU_SOURCE 1
219 #endif
220 #include <dlfcn.h>
221 #endif // OME_HAVE_DLADDR
222 
223 #ifdef _MSC_VER
224 # include <windows.h>
225 #endif
226 
227 namespace
228 {
229 
230 #ifdef OME_HAVE_DLADDR
231  Dl_info this_module;
232 
233  __attribute__((constructor))
234  void
235  find_module(void)
236  {
237  if(!dladdr(reinterpret_cast<void *>(find_module), &this_module))
238  {
239  this_module.dli_fname = 0;
240  }
241  }
242 
243  boost::filesystem::path
244  module_path()
245  {
246  if (this_module.dli_fname)
247  return canonical(boost::filesystem::path(this_module.dli_fname));
248  return boost::filesystem::path();
249  }
250 #elif _MSC_VER
251  HMODULE
252  find_module(void)
253  {
254  static bool found_module = false;
255  static HMODULE this_module;
256 
257  if (!found_module)
258  {
259  if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
260  GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
261  reinterpret_cast<LPCWSTR>(&find_module),
262  &this_module))
263  {
264  this_module = 0;
265  }
266  found_module = true;
267  }
268  return this_module;
269  }
270 
271  boost::filesystem::path
272  module_path()
273  {
274  HMODULE this_module = find_module();
275  if (this_module)
276  {
277  WCHAR win_wide_path[MAX_PATH];
278  GetModuleFileNameW(this_module, win_wide_path, sizeof(win_wide_path));
279  return boost::filesystem::path(win_wide_path);
280  }
281  return boost::filesystem::path();
282  }
283 #else // No introspection available
284  boost::filesystem::path
285  module_path()
286  {
287  return boost::filesystem::path();
288  }
289 #endif // _MSC_VER
290 }
291 #endif // OME_COMMON_MODULE_INTROSPECTION
292 
293 #endif // OME_COMMON_MODULE_H
294 
295 /*
296  * Local Variables:
297  * mode:C++
298  * End:
299  */
boost::filesystem::path canonical(const boost::filesystem::path &p, const boost::filesystem::path &base=boost::filesystem::current_path(), boost::system::error_code *ec=0)
Get a canonical path.
Definition: filesystem.h:107
boost::filesystem::path realpath
The detected path (used to cache search result).
Definition: module.h:127
std::string root_envvar
Name of the environment variable used to specify the installation root.
Definition: module.h:116
std::string name
Name of the path, e.g. "bin" or "ome-xml-schema".
Definition: module.h:170
boost::filesystem::path abspath
Absolute path (used when configured to use an absolute install path).
Definition: module.h:119
const fs::path & module_runtime_path(const std::string &dtype)
Get the runtime installation prefix path for a module.
Definition: module.cpp:373
boost::filesystem::path shlibpath
Shared library path (used for relocatable installs).
Definition: module.h:125
std::string name
Name of the path, e.g. "bin" or "ome-xml-schema".
Definition: module.h:107
boost::filesystem::path relpath
Relative path (used for relocatable installs).
Definition: module.h:121
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:274
bool registered
Is the path registered in the path map?
Definition: module.h:172
Boost.Filesystem compatibility.
Open Microscopy Environment C++.
Definition: base64.h:48
A run-time path for a given module.
Definition: module.h:104
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:132
std::string module_envvar
Name of the environment variable used to specify the module installation root.
Definition: module.h:113
std::string envvar
Name of the environment variable used to override the autodetected path.
Definition: module.h:110
Register a module to make it available to module_runtime_path().
Definition: module.h:167
boost::filesystem::path install_prefix
Absolute installation path (used for non-relocatable installs).
Definition: module.h:123
void register_module_paths()
Register OME-Common module paths.
Definition: module.cpp:490