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