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