OpenWalnut  1.5.0dev
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 #include <boost/filesystem.hpp>
32 
33 
34 
35 /**
36  * This class loads shared libraries and provides function pointers. This is especially useful for dynamic loading of shared libraries during
37  * runtime. This works on Windows, Linux and Mac OS and is based on the openbug shared_lib implementation by
38  * Christian Heine <heine@informatik.uni-leipzig.de>.
39  *
40  * \note This class performs locking so that under any system variables of shared_lib may be used in multi-threaded environments.
41  * \warning Because the POSIX standard does not enforce thread safety for the functions dlopen, dlclose, dlerror, and dlsym, these should not
42  * be used simultaneously with variables of this class.
43  */
44 class WSharedLib // NOLINT
45 {
46 public:
47  /**
48  * Constructor. Loads the specified library.
49  *
50  * \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
51  * standard library search directory may be searched.
52  *
53  * \note If the shared library is already loaded, this constructor just
54  * increases its reference count. This is detected even if different
55  * paths were used (e.g. "./somelib.so", "../libs/somelib.so").
56  * \throw WLibraryLoadFailed if the lib could not be loaded. Maybe because of file not found or link errors.
57  */
58  explicit WSharedLib( boost::filesystem::path lib );
59 
60  /**
61  * Copies this instance by increasing the reference counter of the loaded library by 1.
62  *
63  * \param rhs the other Lib.
64  */
65  WSharedLib( const WSharedLib& rhs );
66 
67  /**
68  * Destructor. Decreases the reference counter and unloads the library if the reference count drops to zero.
69  */
70  virtual ~WSharedLib();
71 
72  /**
73  * Copy assignment for shared libraries.
74  *
75  * \param rhs the one to assign
76  *
77  * \return this instance copied from the specified one.
78  */
79  WSharedLib& operator=( const WSharedLib& rhs );
80 
81  /**
82  * Swap to shared libraries.
83  *
84  * \param lhs the one
85  * \param rhs the other
86  */
87  friend
88  void swap( WSharedLib& lhs, WSharedLib& rhs );
89 
90  /** Search for a function in the shared library.
91  * \tparam FuncType a function type
92  * \param name the name of the function
93  * \param func will be set to the function pointer
94  *
95  * \throw WLibraryFetchFailed if the symbol was not found
96  *
97  * \warning type unsafe, make sure the symbol actually is of the proper type
98  */
99  template < typename FuncType >
100  void fetchFunction( const std::string& name, FuncType& func ) const;
101 
102  /**
103  * Check whether the function exists.
104  *
105  * \param name the name of the function
106  *
107  * \return true if it exists.
108  */
109  bool existsFunction( const std::string& name ) const;
110 
111  /** Search for an variable in the shared library
112  * \tparam PtrType a pointer type
113  * \param name the name of the variable
114  * \param variable will be set to the variable pointer
115  *
116  * \throw WLibraryFetchFailed if the symbol was not found
117  *
118  * \warning type unsafe, make sure the symbol actually is of the proper type
119  */
120  template < typename PtrType >
121  void fetchVariable( const std::string& name, PtrType& variable ) const;
122 
123  /**
124  * Returns the prefix used for libraries on the system. On Unix this mostly is "lib".
125  *
126  * \return the prefix.
127  */
128  static std::string getSystemPrefix();
129 
130  /**
131  * Returns the suffix for libraries used on the system. On Unix this mostly is "so", Windows uses "dll" and Mac something like "dylib".
132  *
133  * \return the suffix.
134  */
135  static std::string getSystemSuffix();
136 
137  /**
138  * 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,
139  * this will be "../lib", on Windows ".".
140  *
141  * \return the path on the system.
142  */
143  static std::string getSystemLibPath();
144 
145  /**
146  * Returns the filename of the library without path.
147  *
148  * \return the filename.
149  */
150  std::string getLibraryName();
151 
152 protected:
153 private:
154  //! neutral function pointer type
155  typedef void (*func_ptr_type)(void);
156 
157  /**
158  * Find the specified function pointer in the library.
159  *
160  * \param name the symbol to search
161  *
162  * \return the pointer to the symbol as function pointer
163  */
164  func_ptr_type findFunction( const std::string& name ) const;
165 
166  /**
167  * Find the specified symbol in the library.
168  *
169  * \param name the symbol to search
170  *
171  * \return the pointer to the symbol as function pointer.
172  */
173  void* findVariable( const std::string& name ) const;
174 
175  //! internal data
176  struct data;
177 
178  //! internal data
180 
181  //! path to lib
182  boost::filesystem::path m_libPath;
183 };
184 
185 template < typename FuncType >
186 void WSharedLib::fetchFunction( const std::string& name, FuncType& func ) const
187 {
188  func = reinterpret_cast< FuncType >( findFunction( name ) );
189 }
190 
191 template < typename PtrType >
192 void WSharedLib::fetchVariable( const std::string& name, PtrType& variable ) const
193 {
194  variable = static_cast< PtrType >( findVariable( name ) );
195 }
196 
197 #endif // WSHAREDLIB_H
198 
This class loads shared libraries and provides function pointers.
Definition: WSharedLib.h:45
static std::string getSystemSuffix()
Returns the suffix for libraries used on the system.
Definition: WSharedLib.cpp:316
static std::string getSystemPrefix()
Returns the prefix used for libraries on the system.
Definition: WSharedLib.cpp:311
static std::string getSystemLibPath()
Returns the default path for libraries on the current system.
Definition: WSharedLib.cpp:321
data * m_data
internal data
Definition: WSharedLib.h:176
void fetchFunction(const std::string &name, FuncType &func) const
Search for a function in the shared library.
Definition: WSharedLib.h:186
boost::filesystem::path m_libPath
path to lib
Definition: WSharedLib.h:182
virtual ~WSharedLib()
Destructor.
Definition: WSharedLib.cpp:279
void * findVariable(const std::string &name) const
Find the specified symbol in the library.
Definition: WSharedLib.cpp:301
func_ptr_type findFunction(const std::string &name) const
Find the specified function pointer in the library.
Definition: WSharedLib.cpp:296
friend void swap(WSharedLib &lhs, WSharedLib &rhs)
Swap to shared libraries.
Definition: WSharedLib.cpp:291
bool existsFunction(const std::string &name) const
Check whether the function exists.
Definition: WSharedLib.cpp:306
void(* func_ptr_type)(void)
neutral function pointer type
Definition: WSharedLib.h:155
WSharedLib(boost::filesystem::path lib)
Constructor.
Definition: WSharedLib.cpp:267
void fetchVariable(const std::string &name, PtrType &variable) const
Search for an variable in the shared library.
Definition: WSharedLib.h:192
std::string getLibraryName()
Returns the filename of the library without path.
Definition: WSharedLib.cpp:326
WSharedLib & operator=(const WSharedLib &rhs)
Copy assignment for shared libraries.
Definition: WSharedLib.cpp:284
Simple class holding an opened library.
Definition: WSharedLib.cpp:172