OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WModuleLoader.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 WMODULELOADER_H
26 #define WMODULELOADER_H
27 
28 #include <set>
29 #include <string>
30 #include <vector>
31 
32 // Use filesystem version 2 for compatibility with newer boost versions.
33 #ifndef BOOST_FILESYSTEM_VERSION
34  #define BOOST_FILESYSTEM_VERSION 2
35 #endif
36 #include <boost/filesystem.hpp>
37 #include <boost/shared_ptr.hpp>
38 
39 #include "../common/WLogger.h"
40 #include "../common/WSharedAssociativeContainer.h"
41 #include "../common/WSharedLib.h"
42 #include "WExportKernel.h"
43 #include "WModule.h"
44 
45 /**
46  * Loads module prototypes from shared objects in a given directory and injects it into the module factory.
47  */
48 class OWKERNEL_EXPORT WModuleLoader
49 {
50 public:
51 
52  /**
53  * Constructor. It does not load any files. Use load to do this.
54  *
55  */
56  explicit WModuleLoader();
57 
58  /**
59  * Destructor, closes all handles to shared libraries.
60  */
61  ~WModuleLoader();
62 
63  /**
64  * Load the module prototypes from the shared libraries.
65  *
66  * \param ticket A write ticket to a shared container.
67  */
68  void load( WSharedAssociativeContainer< std::set< boost::shared_ptr< WModule > > >::WriteTicket ticket );
69 
70  /**
71  * Returns the prefix of a shared module library filename.
72  *
73  * \return the prefix.
74  */
75  static std::string getModulePrefix();
76 
77 private:
78 
79  /**
80  * All the loaded shared libraries. Get freed on destruction. So do NOT free this instance while the libs are used.
81  */
82  std::vector< boost::shared_ptr< WSharedLib > > m_libs;
83 
84  /**
85  * Load the module prototypes from the shared libraries from the specified directory. It traverses the subdirectories and searches there.
86  * Traversion depth is 1.
87  *
88  * \param ticket A write ticket to a shared container.
89  * \param dir the directory to load
90  * \param level the traversion level
91  */
92  void load( WSharedAssociativeContainer< std::set< boost::shared_ptr< WModule > > >::WriteTicket ticket, boost::filesystem::path dir,
93  unsigned int level = 0 );
94 };
95 
96 #endif // WMODULELOADER_H