OpenWalnut  1.4.0
WSharedLib.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WSHAREDLIB_H
26 #define WSHAREDLIB_H
27 
28 #include <algorithm>
29 #include <string>
30 
31 #ifndef Q_MOC_RUN
32 #include <boost/filesystem.hpp>
33 #endif
34 
35 
36 
37 /**
38  * This class loads shared libraries and provides function pointers. This is especially useful for dynamic loading of shared libraries during
39  * runtime. This works on Windows, Linux and Mac OS and is based on the openbug shared_lib implementation by
40  * Christian Heine <heine@informatik.uni-leipzig.de>. For more details, see http://www.informatik.uni-leipzig.de/~hg/openbug .
41  *
42  * \note This class performs locking so that under any system variables of shared_lib may be used in multi-threaded environments.
43  * \warning Because the POSIX standard does not enforce thread safety for the functions dlopen, dlclose, dlerror, and dlsym, these should not
44  * be used simultaneously with variables of this class.
45  */
46 class WSharedLib // NOLINT
47 {
48 public:
49  /**
50  * Constructor. Loads the specified library.
51  *
52  * \param lib the library to load. Can be a DLL,SO or DYLIB (depending on system). This can be an absolut or relative path. Otherwise
53  * standard library search directory may be searched.
54  *
55  * \note If the shared library is already loaded, this constructor just
56  * increases its reference count. This is detected even if different
57  * paths were used (e.g. "./somelib.so", "../libs/somelib.so").
58  * \throw WLibraryLoadFailed if the lib could not be loaded. Maybe because of file not found or link errors.
59  */
60  explicit WSharedLib( boost::filesystem::path lib );
61 
62  /**
63  * Copies this instance by increasing the reference counter of the loaded library by 1.
64  *
65  * \param rhs the other Lib.
66  */
67  WSharedLib( const WSharedLib& rhs );
68 
69  /**
70  * Destructor. Decreases the reference counter and unloads the library if the reference count drops to zero.
71  */
72  virtual ~WSharedLib();
73 
74  /**
75  * Copy assignment for shared libraries.
76  *
77  * \param rhs the one to assign
78  *
79  * \return this instance copied from the specified one.
80  */
81  WSharedLib& operator=( const WSharedLib& rhs );
82 
83  /**
84  * Swap to shared libraries.
85  *
86  * \param lhs the one
87  * \param rhs the other
88  */
89  friend
90  void swap( WSharedLib& lhs, WSharedLib& rhs );
91 
92  /** Search for a function in the shared library.
93  * \tparam FuncType a function type
94  * \param name the name of the function
95  * \param func will be set to the function pointer
96  *
97  * \throw WLibraryFetchFailed if the symbol was not found
98  *
99  * \warning type unsafe, make sure the symbol actually is of the proper type
100  */
101  template < typename FuncType >
102  void fetchFunction( const std::string& name, FuncType& func ) const;
103 
104  /**
105  * Check whether the function exists.
106  *
107  * \param name the name of the function
108  *
109  * \return true if it exists.
110  */
111  bool existsFunction( const std::string& name ) const;
112 
113  /** Search for an variable in the shared library
114  * \tparam PtrType a pointer type
115  * \param name the name of the variable
116  * \param variable will be set to the variable pointer
117  *
118  * \throw WLibraryFetchFailed if the symbol was not found
119  *
120  * \warning type unsafe, make sure the symbol actually is of the proper type
121  */
122  template < typename PtrType >
123  void fetchVariable( const std::string& name, PtrType& variable ) const;
124 
125  /**
126  * Returns the prefix used for libraries on the system. On Unix this mostly is "lib".
127  *
128  * \return the prefix.
129  */
130  static std::string getSystemPrefix();
131 
132  /**
133  * Returns the suffix for libraries used on the system. On Unix this mostly is "so", Windows uses "dll" and Mac something like "dylib".
134  *
135  * \return the suffix.
136  */
137  static std::string getSystemSuffix();
138 
139  /**
140  * Returns the default path for libraries on the current system. This is the directory where to search for .so,.dll or .dylib files. On Unix,
141  * this will be "../lib", on Windows ".".
142  *
143  * \return the path on the system.
144  */
145  static std::string getSystemLibPath();
146 
147  /**
148  * Returns the filename of the library without path.
149  *
150  * \return the filename.
151  */
152  std::string getLibraryName();
153 
154 protected:
155 private:
156  //! neutral function pointer type
157  typedef void (*func_ptr_type)(void);
158 
159  /**
160  * Find the specified function pointer in the library.
161  *
162  * \param name the symbol to search
163  *
164  * \return the pointer to the symbol as function pointer
165  */
166  func_ptr_type findFunction( const std::string& name ) const;
167 
168  /**
169  * Find the specified symbol in the library.
170  *
171  * \param name the symbol to search
172  *
173  * \return the pointer to the symbol as function pointer.
174  */
175  void* findVariable( const std::string& name ) const;
176 
177  //! internal data
178  struct data;
179 
180  //! internal data
181  data* m_data;
182 
183  //! path to lib
184  boost::filesystem::path m_libPath;
185 };
186 
187 template < typename FuncType >
188 void WSharedLib::fetchFunction( const std::string& name, FuncType& func ) const
189 {
190  func = reinterpret_cast< FuncType >( findFunction( name ) );
191 }
192 
193 template < typename PtrType >
194 void WSharedLib::fetchVariable( const std::string& name, PtrType& variable ) const
195 {
196  variable = static_cast< PtrType >( findVariable( name ) );
197 }
198 
199 #endif // WSHAREDLIB_H
200 
void * findVariable(const std::string &name) const
Find the specified symbol in the library.
Definition: WSharedLib.cpp:301
static std::string getSystemSuffix()
Returns the suffix for libraries used on the system.
Definition: WSharedLib.cpp:316
void(* func_ptr_type)(void)
neutral function pointer type
Definition: WSharedLib.h:157
void fetchVariable(const std::string &name, PtrType &variable) const
Search for an variable in the shared library.
Definition: WSharedLib.h:194
std::string getLibraryName()
Returns the filename of the library without path.
Definition: WSharedLib.cpp:326
static std::string getSystemPrefix()
Returns the prefix used for libraries on the system.
Definition: WSharedLib.cpp:311
void fetchFunction(const std::string &name, FuncType &func) const
Search for a function in the shared library.
Definition: WSharedLib.h:188
static std::string getSystemLibPath()
Returns the default path for libraries on the current system.
Definition: WSharedLib.cpp:321
WSharedLib & operator=(const WSharedLib &rhs)
Copy assignment for shared libraries.
Definition: WSharedLib.cpp:284
func_ptr_type findFunction(const std::string &name) const
Find the specified function pointer in the library.
Definition: WSharedLib.cpp:296
bool existsFunction(const std::string &name) const
Check whether the function exists.
Definition: WSharedLib.cpp:306
boost::filesystem::path m_libPath
path to lib
Definition: WSharedLib.h:184
Simple class holding an opened library.
Definition: WSharedLib.cpp:171
friend void swap(WSharedLib &lhs, WSharedLib &rhs)
Swap to shared libraries.
Definition: WSharedLib.cpp:291
WSharedLib(boost::filesystem::path lib)
Constructor.
Definition: WSharedLib.cpp:267
data * m_data
internal data
Definition: WSharedLib.h:178
This class loads shared libraries and provides function pointers.
Definition: WSharedLib.h:46
virtual ~WSharedLib()
Destructor.
Definition: WSharedLib.cpp:279