OpenWalnut  1.4.0
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 #include <boost/filesystem.hpp>
00033 #include <boost/shared_ptr.hpp>
00034 
00035 #include "../common/WSharedAssociativeContainer.h"
00036 #include "../common/WSharedLib.h"
00037 
00038 #include "WModule.h"
00039 
00040 /**
00041  * Loads module prototypes from shared objects in a given directory and injects it into the module factory.
00042  */
00043 class  WModuleLoader
00044 {
00045 public:
00046     /**
00047      * Shared pointer abbreviation.
00048      */
00049     typedef boost::shared_ptr< WModuleLoader > SPtr;
00050 
00051     /**
00052      * Const pointer abbreviation.
00053      */
00054     typedef boost::shared_ptr< const WModuleLoader > ConstSPtr;
00055 
00056     /**
00057      * Constructor. It does not load any files. Use load to do this.
00058      *
00059      */
00060     explicit WModuleLoader();
00061 
00062     /**
00063      * Destructor, closes all handles to shared libraries.
00064      */
00065     ~WModuleLoader();
00066 
00067     /**
00068      * Load the module prototypes from the shared libraries.
00069      *
00070      * \param ticket A write ticket to a shared container.
00071      */
00072     void load( WSharedAssociativeContainer< std::set< boost::shared_ptr< WModule > > >::WriteTicket ticket );
00073 
00074     /**
00075      * Returns the prefix of a shared module library filename.
00076      *
00077      * \return the prefix.
00078      */
00079     static std::string getModulePrefix();
00080 
00081     /**
00082      * The loader also stores information on which library provides the arbitrary registration mechanism. This cannot be called during load, as
00083      * OW is not completely initialized at this point. So we do this here. Call this after startup, before project loading.
00084      */
00085     void initializeExtensions();
00086 
00087 private:
00088     /**
00089      * All the loaded shared libraries. Get freed on destruction. So do NOT free this instance while the libs are used.
00090      */
00091     std::vector< boost::shared_ptr< WSharedLib > > m_libs;
00092 
00093     /**
00094      * Load the module prototypes from the shared libraries from the specified directory. It traverses the subdirectories and searches there.
00095      * Traversion depth is 1.
00096      *
00097      * \param ticket A write ticket to a shared container.
00098      * \param dir the directory to load
00099      * \param level the traversion level
00100      */
00101     void load( WSharedAssociativeContainer< std::set< boost::shared_ptr< WModule > > >::WriteTicket ticket, boost::filesystem::path dir,
00102                unsigned int level = 0 );
00103 
00104     /**
00105      * Helper to store information on a lib which gets initialized later. This basically is used for the arbitrary registration feature.
00106      */
00107     struct PostponedLoad
00108     {
00109         /**
00110          * Initialize the class and keep track of the lib (and its reference).
00111          *
00112          * \param lib the lib to keep
00113          * \param path the lib path
00114          */
00115         PostponedLoad( boost::shared_ptr< WSharedLib > lib, boost::filesystem::path path ):
00116             m_lib( lib ),
00117             m_path( path )
00118         {
00119         }
00120 
00121         /**
00122          * The library. Need to keep this to avoid freeing the lib beforehand.
00123          */
00124         boost::shared_ptr< WSharedLib > m_lib;
00125 
00126         /**
00127          * The path of the resources.
00128          */
00129         boost::filesystem::path m_path;
00130     };
00131 
00132     /**
00133      * The libs which need to be initialized when OW is loaded completely.
00134      */
00135     std::vector< PostponedLoad > m_arbitraryRegisterLibs;
00136 };
00137 
00138 #endif  // WMODULELOADER_H