OpenWalnut  1.3.1
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>. For more details, see http://www.informatik.uni-leipzig.de/~hg/openbug .
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  /** Search for an variable in the shared library
103  * \tparam PtrType a pointer type
104  * \param name the name of the variable
105  * \param variable will be set to the variable pointer
106  *
107  * \throw WLibraryFetchFailed if the symbol was not found
108  *
109  * \warning type unsafe, make sure the symbol actually is of the proper type
110  */
111  template < typename PtrType >
112  void fetchVariable( const std::string& name, PtrType& variable ) const;
113 
114  /**
115  * Returns the prefix used for libraries on the system. On Unix this mostly is "lib".
116  *
117  * \return the prefix.
118  */
119  static std::string getSystemPrefix();
120 
121  /**
122  * Returns the suffix for libraries used on the system. On Unix this mostly is "so", Windows uses "dll" and Mac something like "dylib".
123  *
124  * \return the suffix.
125  */
126  static std::string getSystemSuffix();
127 
128  /**
129  * 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,
130  * this will be "../lib", on Windows ".".
131  *
132  * \return the path on the system.
133  */
134  static std::string getSystemLibPath();
135 
136 protected:
137 private:
138  //! neutral function pointer type
139  typedef void (*func_ptr_type)(void);
140 
141  /**
142  * Find the specified function pointer in the library.
143  *
144  * \param name the symbol to search
145  *
146  * \return the pointer to the symbol as function pointer
147  */
148  func_ptr_type findFunction( const std::string& name ) const;
149 
150  /**
151  * Find the specified symbol in the library.
152  *
153  * \param name the symbol to search
154  *
155  * \return the pointer to the symbol as function pointer.
156  */
157  void* findVariable( const std::string& name ) const;
158 
159  //! internal data
160  struct data;
161 
162  //! internal data
163  data* m_data;
164 };
165 
166 template < typename FuncType >
167 void WSharedLib::fetchFunction( const std::string& name, FuncType& func ) const
168 {
169  func = reinterpret_cast< FuncType >( findFunction( name ) );
170 }
171 
172 template < typename PtrType >
173 void WSharedLib::fetchVariable( const std::string& name, PtrType& variable ) const
174 {
175  variable = static_cast< PtrType >( findVariable( name ) );
176 }
177 
178 #endif // WSHAREDLIB_H
179