WModuleLoader.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 WMODULELOADER_H
00026 #define WMODULELOADER_H
00027 
00028 #include <set>
00029 #include <string>
00030 #include <vector>
00031 
00032 // Use filesystem version 2 for compatibility with newer boost versions.
00033 #ifndef BOOST_FILESYSTEM_VERSION
00034     #define BOOST_FILESYSTEM_VERSION 2
00035 #endif
00036 #include <boost/filesystem.hpp>
00037 #include <boost/shared_ptr.hpp>
00038 
00039 #include "../common/WLogger.h"
00040 #include "../common/WSharedAssociativeContainer.h"
00041 #include "../common/WSharedLib.h"
00042 #include "WExportKernel.h"
00043 #include "WModule.h"
00044 
00045 /**
00046  * Loads module prototypes from shared objects in a given directory and injects it into the module factory.
00047  */
00048 class OWKERNEL_EXPORT WModuleLoader
00049 {
00050 public:
00051 
00052     /**
00053      * Constructor. It does not load any files. Use load to do this.
00054      *
00055      */
00056     explicit WModuleLoader();
00057 
00058     /**
00059      * Destructor, closes all handles to shared libraries.
00060      */
00061     ~WModuleLoader();
00062 
00063     /**
00064      * Load the module prototypes from the shared libraries.
00065      *
00066      * \param ticket A write ticket to a shared container.
00067      */
00068     void load( WSharedAssociativeContainer< std::set< boost::shared_ptr< WModule > > >::WriteTicket ticket );
00069 
00070     /**
00071      * Returns the prefix of a shared module library filename.
00072      *
00073      * \return the prefix.
00074      */
00075     static std::string getModulePrefix();
00076 
00077 private:
00078 
00079     /**
00080      * All the loaded shared libraries. Get freed on destruction. So do NOT free this instance while the libs are used.
00081      */
00082     std::vector< boost::shared_ptr< WSharedLib > > m_libs;
00083 
00084     /**
00085      * Load the module prototypes from the shared libraries from the specified directory. It traverses the subdirectories and searches there.
00086      * Traversion depth is 1.
00087      *
00088      * \param ticket A write ticket to a shared container.
00089      * \param dir the directory to load
00090      * \param level the traversion level
00091      */
00092     void load( WSharedAssociativeContainer< std::set< boost::shared_ptr< WModule > > >::WriteTicket ticket, boost::filesystem::path dir,
00093                unsigned int level = 0 );
00094 };
00095 
00096 #endif  // WMODULELOADER_H
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends