OpenWalnut  1.4.0
WModuleFactory.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 WMODULEFACTORY_H
26 #define WMODULEFACTORY_H
27 
28 #include <map>
29 #include <set>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 #ifndef Q_MOC_RUN
35 #include <boost/shared_ptr.hpp>
36 #endif
37 #ifndef Q_MOC_RUN
38 #include <boost/weak_ptr.hpp>
39 #endif
40 
41 #include "../common/WSharedAssociativeContainer.h"
42 #include "WModuleCombinerTypes.h"
43 #include "WModule.h"
44 #include "WModuleLoader.h"
45 
46 /**
47  * Class able to create a new copy of an arbitrary module. It uses the Factory and Prototype design pattern.
48  */
49 class WModuleFactory // NOLINT
50 {
51 friend class WModuleFactoryTest;
52 public:
53  /**
54  * Shared pointer to a WModule.
55  */
56  typedef boost::shared_ptr< WModuleFactory > SPtr;
57 
58  /**
59  * Shared pointer to a const WModule.
60  */
61  typedef boost::shared_ptr< const WModuleFactory > ConstSPtr;
62 
63  /**
64  * For shortening: a type defining a shared set of WModule pointers.
65  */
66  typedef std::set< boost::shared_ptr< WModule > > PrototypeContainerType;
67 
68  /**
69  * Const iterator for the prototype set.
70  */
71  typedef std::set< boost::shared_ptr< WModule > >::const_iterator PrototypeContainerConstIteratorType;
72 
73  /**
74  * Iterator for the prototype set.
75  */
76  typedef std::set< boost::shared_ptr< WModule > >::iterator PrototypeContainerIteratorType;
77 
78  /**
79  * The alias for a shared container.
80  */
82 
83  /**
84  * Destructor.
85  */
86  virtual ~WModuleFactory();
87 
88  /**
89  * Loads the modules and creates prototypes.
90  */
91  void load();
92 
93  /**
94  * Create a new and initialized module using the specified prototype.
95  *
96  * \param prototype the prototype to clone.
97  * \param uuid the uuid to use for the created module. If you specify an empty string (default), a uuid will be created. This parameter is
98  * useful for the project loader.
99  *
100  * \return the module created using the prototype.
101  */
102  boost::shared_ptr< WModule > create( boost::shared_ptr< WModule > prototype, std::string uuid = "" );
103 
104  /**
105  * Returns instance of the module factory to use to create modules.
106  *
107  * \return the running module factory.
108  */
109  static SPtr getModuleFactory();
110 
111  /**
112  * Returns instance of the module loader.
113  *
114  * \return the running module loader.
115  */
116  static boost::shared_ptr< WModuleLoader > getModuleLoader();
117 
118  /**
119  * Searches a prototype by name. It returns the prototype, or a NULL pointer if it is not found. The difference to
120  * getPrototypeByName() is, that an unavailable prototype does not throw an exception. This is nice for checking whether a
121  * prototype exists or not.
122  *
123  * \param name name of the prototype to search
124  *
125  * \return the prototype if it exists, or NULL if not.
126  */
127  const boost::shared_ptr< WModule > isPrototypeAvailable( std::string name );
128 
129  /**
130  * Finds a prototype using the specified name.
131  *
132  * \param name the name.
133  *
134  * \return the prototype whose name is equal to the specified one.
135  */
136  const boost::shared_ptr< WModule > getPrototypeByName( std::string name );
137 
138  /**
139  * Finds a prototype using an instance of a module. This uses the type_info to find a proper prototype.
140  *
141  * \param instance the instance to use.
142  *
143  * \return the prototype.
144  * \throw WPrototypeUnknown if prototype can not be found.
145  */
146  const boost::shared_ptr< WModule > getPrototypeByInstance( boost::shared_ptr< WModule > instance );
147 
148  /**
149  * Finds a prototype using an type.
150  *
151  * \param type the type of module.
152  *
153  * \return the prototypes as list.
154  */
155  std::vector< WModule::ConstSPtr > getPrototypesByType( MODULE_TYPE type );
156 
157  /**
158  * This method gives read access to the list of all prototypes.
159  *
160  * \return the read ticket for the prototype list
161  */
163 
164  /**
165  * Checks whether the first instance can be casted to the second one.
166  *
167  * \param module the module to check.
168  *
169  * \return true if the dynamic_cast is successful
170  */
171  template <typename T>
172  static bool isA( boost::shared_ptr< WModule > module );
173 
174  /**
175  * Returns a set of module combiners with module combinations compatible with the specified one.
176  *
177  * \param module the module to find the compatibles for.
178  *
179  * \note as the default parameter denotes, providing a NULL pointer (or calling the method without a parameter) returns the list of modules
180  * which are compatible to every other module. In other words, it returns all modules without input connectors. If the specified module is
181  * not NULL, the modules without input are not listed.
182  *
183  * \return set of compatible combiners.
184  */
185  WCombinerTypes::WCompatiblesList getCompatiblePrototypes(
186  boost::shared_ptr< WModule > module = boost::shared_ptr< WModule >()
187  );
188 
189  /**
190  * Creates a list of \ref WApplyCombiner for all modules known by the factory.
191  *
192  * \return list of apply combiner.
193  */
194  WCombinerTypes::WCompatiblesList getAllPrototypes();
195 
196  /**
197  * This method uses a newly created instance of WModule and initializes it properly. After using this method, the module is
198  * properly initialized and ready to be used.
199  *
200  * \param module the module to initialize.
201  */
202  static void initializeModule( boost::shared_ptr< WModule > module );
203 
204  /**
205  * Checks whether the specified module is a prototype or an instantiated module.
206  *
207  * \param module the module to check
208  *
209  * \return true if it is a prototype
210  */
211  static bool isPrototype( boost::shared_ptr< WModule > module );
212 
213  /**
214  * Find a module instance by UUID.
215  *
216  * \param uuid the uuid to search for.
217  *
218  * \return the module, or NULL if not found
219  */
220  static WModule::SPtr findByUUID( std::string uuid );
221 
222 protected:
223  /**
224  * Constructors are protected because this is a Singleton.
225  */
226  WModuleFactory();
227 
228  /**
229  * The module prototypes available.
230  */
231  PrototypeSharedContainerType m_prototypes;
232 
233  /**
234  * Checks whether the specified module is a prototype or an instantiated module. Use isPrototype if no ticket acquired yet.
235  *
236  * \param module the module to check
237  * \param ticket ticket which already has read lock.
238  *
239  * \return true if it is a prototype
240  */
241  bool checkPrototype( boost::shared_ptr< WModule > module, PrototypeSharedContainerType::ReadTicket ticket );
242 
243 private:
244  /**
245  * Loader class managing dynamically loaded modules in OpenWalnut.
246  */
248 
249  /**
250  * Singleton instance of WModuleFactory.
251  */
252  static boost::shared_ptr< WModuleFactory > m_instance;
253 
254  /**
255  * Mapping between a UUID and a module.
256  */
257  typedef std::map< std::string, boost::weak_ptr< WModule > > UuidModuleMap;
258 
259  /**
260  * Keep track of uuids of each created module. This is needed to find module pointers using uuid.
261  */
263 };
264 
265 template <typename T>
266 bool WModuleFactory::isA( boost::shared_ptr< WModule > module )
267 {
268  // NOTE: this is RTTI. Everybody says: do not use it. We ignore them.
269  return ( dynamic_cast< T* >( module.get() ) ); // NOLINT
270 }
271 
272 #endif // WMODULEFACTORY_H
273 
std::set< boost::shared_ptr< WModule > > PrototypeContainerType
For shortening: a type defining a shared set of WModule pointers.
const boost::shared_ptr< WModule > getPrototypeByInstance(boost::shared_ptr< WModule > instance)
Finds a prototype using an instance of a module.
const boost::shared_ptr< WModule > getPrototypeByName(std::string name)
Finds a prototype using the specified name.
WCombinerTypes::WCompatiblesList getAllPrototypes()
Creates a list of WApplyCombiner for all modules known by the factory.
Class able to create a new copy of an arbitrary module.
static bool isA(boost::shared_ptr< WModule > module)
Checks whether the first instance can be casted to the second one.
std::set< boost::shared_ptr< WModule > >::iterator PrototypeContainerIteratorType
Iterator for the prototype set.
WSharedAssociativeContainer< PrototypeContainerType > PrototypeSharedContainerType
The alias for a shared container.
static boost::shared_ptr< WModuleFactory > m_instance
Singleton instance of WModuleFactory.
boost::shared_ptr< const WModuleFactory > ConstSPtr
Shared pointer to a const WModule.
boost::shared_ptr< WModule > create(boost::shared_ptr< WModule > prototype, std::string uuid="")
Create a new and initialized module using the specified prototype.
static bool isPrototype(boost::shared_ptr< WModule > module)
Checks whether the specified module is a prototype or an instantiated module.
WModuleLoader::SPtr m_moduleLoader
Loader class managing dynamically loaded modules in OpenWalnut.
static boost::shared_ptr< WModuleLoader > getModuleLoader()
Returns instance of the module loader.
static WModule::SPtr findByUUID(std::string uuid)
Find a module instance by UUID.
boost::shared_ptr< WModuleFactory > SPtr
Shared pointer to a WModule.
PrototypeSharedContainerType::ReadTicket getPrototypes() const
This method gives read access to the list of all prototypes.
virtual ~WModuleFactory()
Destructor.
WModuleFactory()
Constructors are protected because this is a Singleton.
boost::shared_ptr< WModule > SPtr
Shared pointer to a WModule.
Definition: WModule.h:121
static SPtr getModuleFactory()
Returns instance of the module factory to use to create modules.
static void initializeModule(boost::shared_ptr< WModule > module)
This method uses a newly created instance of WModule and initializes it properly. ...
std::vector< WModule::ConstSPtr > getPrototypesByType(MODULE_TYPE type)
Finds a prototype using an type.
std::set< boost::shared_ptr< WModule > >::const_iterator PrototypeContainerConstIteratorType
Const iterator for the prototype set.
WSharedAssociativeContainer< UuidModuleMap > m_uuidModuleMap
Keep track of uuids of each created module.
boost::shared_ptr< WModuleLoader > SPtr
Shared pointer abbreviation.
Definition: WModuleLoader.h:53
void load()
Loads the modules and creates prototypes.
PrototypeSharedContainerType m_prototypes
The module prototypes available.
const boost::shared_ptr< WModule > isPrototypeAvailable(std::string name)
Searches a prototype by name.
std::map< std::string, boost::weak_ptr< WModule > > UuidModuleMap
Mapping between a UUID and a module.
bool checkPrototype(boost::shared_ptr< WModule > module, PrototypeSharedContainerType::ReadTicket ticket)
Checks whether the specified module is a prototype or an instantiated module.
boost::shared_ptr< WSharedObjectTicketRead< PrototypeContainerType > > ReadTicket
Type for read tickets.
Definition: WSharedObject.h:64
WCombinerTypes::WCompatiblesList getCompatiblePrototypes(boost::shared_ptr< WModule > module=boost::shared_ptr< WModule >())
Returns a set of module combiners with module combinations compatible with the specified one...