OpenWalnut  1.4.0
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 #include <boost/filesystem.hpp>
33 #include <boost/shared_ptr.hpp>
34 
35 #include "../common/WSharedAssociativeContainer.h"
36 #include "../common/WSharedLib.h"
37 
38 #include "WModule.h"
39 
40 /**
41  * Loads module prototypes from shared objects in a given directory and injects it into the module factory.
42  */
44 {
45 public:
46  /**
47  * Shared pointer abbreviation.
48  */
49  typedef boost::shared_ptr< WModuleLoader > SPtr;
50 
51  /**
52  * Const pointer abbreviation.
53  */
54  typedef boost::shared_ptr< const WModuleLoader > ConstSPtr;
55 
56  /**
57  * Constructor. It does not load any files. Use load to do this.
58  *
59  */
60  explicit WModuleLoader();
61 
62  /**
63  * Destructor, closes all handles to shared libraries.
64  */
66 
67  /**
68  * Load the module prototypes from the shared libraries.
69  *
70  * \param ticket A write ticket to a shared container.
71  */
72  void load( WSharedAssociativeContainer< std::set< boost::shared_ptr< WModule > > >::WriteTicket ticket );
73 
74  /**
75  * Returns the prefix of a shared module library filename.
76  *
77  * \return the prefix.
78  */
79  static std::string getModulePrefix();
80 
81  /**
82  * The loader also stores information on which library provides the arbitrary registration mechanism. This cannot be called during load, as
83  * OW is not completely initialized at this point. So we do this here. Call this after startup, before project loading.
84  */
85  void initializeExtensions();
86 
87 private:
88  /**
89  * All the loaded shared libraries. Get freed on destruction. So do NOT free this instance while the libs are used.
90  */
91  std::vector< boost::shared_ptr< WSharedLib > > m_libs;
92 
93  /**
94  * Load the module prototypes from the shared libraries from the specified directory. It traverses the subdirectories and searches there.
95  * Traversion depth is 1.
96  *
97  * \param ticket A write ticket to a shared container.
98  * \param dir the directory to load
99  * \param level the traversion level
100  */
101  void load( WSharedAssociativeContainer< std::set< boost::shared_ptr< WModule > > >::WriteTicket ticket, boost::filesystem::path dir,
102  unsigned int level = 0 );
103 
104  /**
105  * Helper to store information on a lib which gets initialized later. This basically is used for the arbitrary registration feature.
106  */
108  {
109  /**
110  * Initialize the class and keep track of the lib (and its reference).
111  *
112  * \param lib the lib to keep
113  * \param path the lib path
114  */
115  PostponedLoad( boost::shared_ptr< WSharedLib > lib, boost::filesystem::path path ):
116  m_lib( lib ),
117  m_path( path )
118  {
119  }
120 
121  /**
122  * The library. Need to keep this to avoid freeing the lib beforehand.
123  */
124  boost::shared_ptr< WSharedLib > m_lib;
125 
126  /**
127  * The path of the resources.
128  */
129  boost::filesystem::path m_path;
130  };
131 
132  /**
133  * The libs which need to be initialized when OW is loaded completely.
134  */
135  std::vector< PostponedLoad > m_arbitraryRegisterLibs;
136 };
137 
138 #endif // WMODULELOADER_H