OpenWalnut 1.2.5

WSharedLib.h

00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WSHAREDLIB_H
00026 #define WSHAREDLIB_H
00027 
00028 #include <algorithm>
00029 #include <string>
00030 
00031 // Use filesystem version 2 for compatibility with newer boost versions.
00032 #ifndef BOOST_FILESYSTEM_VERSION
00033     #define BOOST_FILESYSTEM_VERSION 2
00034 #endif
00035 #include <boost/filesystem.hpp>
00036 
00037 #include "WExportCommon.h"
00038 
00039 /**
00040  * This class loads shared libraries and provides function pointers. This is especially useful for dynamic loading of shared libraries during
00041  * runtime. This works on Windows, Linux and Mac OS and is based on the openbug shared_lib implementation by
00042  * Christian Heine <heine@informatik.uni-leipzig.de>. For more details, see http://www.informatik.uni-leipzig.de/~hg/openbug .
00043  *
00044  * \note This class performs locking so that under any system variables of shared_lib may be used in multi-threaded environments.
00045  * \warning Because the POSIX standard does not enforce thread safety for the functions dlopen, dlclose, dlerror, and dlsym, these should not
00046  *          be used simultaneously with variables of this class.
00047  */
00048 class OWCOMMON_EXPORT WSharedLib // NOLINT
00049 {
00050 public:
00051 
00052     /**
00053      * Constructor. Loads the specified library.
00054      *
00055      * \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
00056      * standard library search directory may be searched.
00057      *
00058      * \note If the shared library is already loaded, this constructor just
00059      *       increases its reference count. This is detected even if different
00060      *       paths were used (e.g. "./somelib.so", "../libs/somelib.so").
00061      * \throw WLibraryLoadFailed if the lib could not be loaded. Maybe because of file not found or link errors.
00062      */
00063     explicit WSharedLib( boost::filesystem::path lib );
00064 
00065     /**
00066      * Copies this instance by increasing the reference counter of the loaded library by 1.
00067      *
00068      * \param rhs the other Lib.
00069      */
00070     WSharedLib( const WSharedLib& rhs );
00071 
00072     /**
00073      * Destructor. Decreases the reference counter and unloads the library if the reference count drops to zero.
00074      */
00075     virtual ~WSharedLib();
00076 
00077     /**
00078      * Copy assignment for shared libraries.
00079      *
00080      * \param rhs the one to assign
00081      *
00082      * \return this instance copied from the specified one.
00083      */
00084     WSharedLib& operator=( const WSharedLib& rhs );
00085 
00086     /**
00087      * Swap to shared libraries.
00088      *
00089      * \param lhs the one
00090      * \param rhs the other
00091      */
00092     friend
00093     void swap( WSharedLib& lhs, WSharedLib& rhs );
00094 
00095     /** Search for a function in the shared library.
00096      * \tparam FuncType a function type
00097      * \param name the name of the function
00098      * \param func will be set to the function pointer
00099      *
00100      * \throw WLibraryFetchFailed if the symbol was not found
00101      *
00102      * \warning type unsafe, make sure the symbol actually is of the proper type
00103      */
00104     template < typename FuncType >
00105     void fetchFunction( const std::string& name, FuncType& func ) const;
00106 
00107     /** Search for an variable in the shared library
00108      * \tparam PtrType a pointer type
00109      * \param name the name of the variable
00110      * \param variable will be set to the variable pointer
00111      *
00112      * \throw WLibraryFetchFailed if the symbol was not found
00113      *
00114      * \warning type unsafe, make sure the symbol actually is of the proper type
00115      */
00116     template < typename PtrType >
00117     void fetchVariable( const std::string& name, PtrType& variable ) const;
00118 
00119     /**
00120      * Returns the prefix used for libraries on the system. On Unix this mostly is "lib".
00121      *
00122      * \return the prefix.
00123      */
00124     static std::string getSystemPrefix();
00125 
00126     /**
00127      * Returns the suffix for libraries used on the system. On Unix this mostly is "so", Windows uses "dll" and Mac something like "dylib".
00128      *
00129      * \return the suffix.
00130      */
00131     static std::string getSystemSuffix();
00132 
00133     /**
00134      * 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,
00135      * this will be "../lib", on Windows ".".
00136      *
00137      * \return the path on the system.
00138      */
00139     static std::string getSystemLibPath();
00140 
00141 protected:
00142 
00143 private:
00144 
00145     //! neutral function pointer type
00146     typedef void (*func_ptr_type)(void);
00147 
00148     /**
00149      * Find the specified function pointer in the library.
00150      *
00151      * \param name the symbol to search
00152      *
00153      * \return the pointer to the symbol as function pointer
00154      */
00155     func_ptr_type findFunction( const std::string& name ) const;
00156 
00157     /**
00158      * Find the specified symbol in the library.
00159      *
00160      * \param name the symbol to search
00161      *
00162      * \return the pointer to the symbol as function pointer.
00163      */
00164     void* findVariable( const std::string& name ) const;
00165 
00166     //! internal data
00167     class data;
00168 
00169     //! internal data
00170     data* m_data;
00171 };
00172 
00173 template < typename FuncType >
00174 void WSharedLib::fetchFunction( const std::string& name, FuncType& func ) const
00175 {
00176     func = reinterpret_cast< FuncType >( findFunction( name ) );
00177 }
00178 
00179 template < typename PtrType >
00180 void WSharedLib::fetchVariable( const std::string& name, PtrType& variable ) const
00181 {
00182     variable = static_cast< PtrType >( findVariable( name ) );
00183 }
00184 
00185 #endif  // WSHAREDLIB_H
00186 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends