fw4spl
Posix.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #if defined(linux) || defined(__linux) || defined (__MACOSX__)
8 
9 #include "fwRuntime/Bundle.hpp"
10 #include "fwRuntime/dl/Posix.hpp"
11 
12 namespace fwRuntime
13 {
14 
15 namespace dl
16 {
17 
18 //------------------------------------------------------------------------------
19 
20 Posix::Posix( const boost::filesystem::path & modulePath ) noexcept :
21  Native(modulePath),
22  m_handle ( 0 )
23 {
24 }
25 
26 //------------------------------------------------------------------------------
27 
28 Posix::~Posix() noexcept
29 {
30 }
31 
32 //------------------------------------------------------------------------------
33 
34 bool Posix::isLoaded() const noexcept
35 {
36  return m_handle != 0;
37 }
38 
39 //------------------------------------------------------------------------------
40 
41 void * Posix::getSymbol( const std::string & name ) const
42 {
43  void* result = 0;
44  if(isLoaded() == true)
45  {
46  dlerror(); /* Clear existing error */
47  result = dlsym(m_handle, name.c_str());
48  if(result == 0) /* Check for possible errors */
49  {
50  std::string message(dlerror());
51  if(message.empty() == false)
52  {
53  throw RuntimeException("Symbol retrieval failed. " + message);
54  }
55  }
56  }
57  return result;
58 }
59 
60 //------------------------------------------------------------------------------
61 
62 void Posix::load()
63 {
64  if(m_handle == 0)
65  {
66  // Opens the dynamic library.
67  m_handle = dlopen(getFullPath(true).string().c_str(), RTLD_LAZY|RTLD_GLOBAL);
68  if(m_handle == 0)
69  {
70  std::string message(dlerror());
71  throw RuntimeException("Module load failed. " + message);
72  }
73  }
74 }
75 
76 //------------------------------------------------------------------------------
77 
78 void Posix::unload()
79 {
80  if(m_handle != 0)
81  {
82  int result;
83  result = dlclose(m_handle);
84  if(result != 0)
85  {
86  std::string message(dlerror());
87  throw RuntimeException("Module unload failed. " + message);
88  }
89  m_handle = 0;
90  }
91 }
92 
93 //------------------------------------------------------------------------------
94 
95 } // namespace dl
96 
97 } // namespace fwRuntime
98 
99 
100 #endif // #if defined(linux) || defined(__linux) || defined (__MACOSX__)
The namespace fwRuntime contains classes to manage bundle, configuration element, extension point in ...