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