OpenWalnut  1.4.0
WKernel.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 WKERNEL_H
26 #define WKERNEL_H
27 
28 #include <string>
29 #include <vector>
30 
31 #ifndef Q_MOC_RUN
32 #include <boost/shared_ptr.hpp>
33 #endif
34 
35 #include "../common/WTimer.h"
36 #include "../scripting/WScriptEngine.h"
37 #include "../graphicsEngine/WGraphicsEngine.h"
38 
39 #include "WBatchLoader.h"
40 
41 // forward declarations
42 class WUI;
43 class WModule;
44 class WModuleContainer;
45 class WModuleFactory;
46 class WROIManager;
47 class WSelectionManager;
48 class WThreadedRunner;
49 
50 /**
51  * \defgroup kernel Kernel
52  *
53  * \brief
54  * This library implements the central part of OpenWalnut that manages
55  * the interaction between UI, GraphicsEngine and DataHandler.
56  */
57 
58 /**
59  * OpenWalnut kernel, managing modules and interaction between
60  * UI, GE and DataHandler
61  * \ingroup kernel
62  */
63 class WKernel: public WThreadedRunner
64 {
65 public:
66  /**
67  * Signal for generic events.
68  *
69  */
70  typedef boost::function< void ( void ) > t_KernelGenericSignalHandlerType;
71 
72  /**
73  * Generic signal type used in the most signals.
74  */
75  typedef boost::signals2::signal< void ( void ) > t_KernelGenericSignalType;
76 
77  /**
78  * Enum of all possible signals WKernel instances can emit.
79  */
80  typedef enum
81  {
82  KERNEL_STARTUPCOMPLETE // when kernel, GE and UI are correctly initialized
83  }
85 
86  /**
87  * Returns pointer to the running kernel or a new if no kernel was there.
88  * If a running kernel exists the function return it and does not check if
89  * GE and UI of the running kernel are equivalent to the ones given as parameters.
90  *
91  * \param ge initialized graphics engine.
92  * \param ui initialized ui.
93  * \return the kernel instance.
94  */
95  static WKernel* instance( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WUI > ui );
96 
97  /**
98  * Destructor.
99  */
100  virtual ~WKernel();
101 
102  /**
103  * Subscribe to several signals.
104  *
105  * \param signal the signal to subscribe
106  * \param notifier the notifier to call
107  *
108  * \return connection variable. Keep this in any case. If not, the connection may be lost.
109  */
110  boost::signals2::connection subscribeSignal( KERNEL_SIGNAL signal, t_KernelGenericSignalHandlerType notifier );
111 
112  /**
113  * Stops execution of the modules in the root container. Note that this does not wait for the kernel thread since this could
114  * cause a dead lock. This is actually an alias for getRootContainer()->stop().
115  */
116  void finalize();
117 
118  /**
119  * Returns pointer to currently running instance of graphics engine.
120  *
121  * \return the graphics engine instance.
122  */
123  boost::shared_ptr< WGraphicsEngine > getGraphicsEngine() const;
124 
125  /**
126  * Returns pointer to the currently running kernel.
127  *
128  * \return the kernel instance.
129  */
130  static WKernel* getRunningKernel();
131 
132  /**
133  * Determines whether all threads should finish.
134  *
135  * \return true if so.
136  */
137  const WBoolFlag& isFinishRequested() const;
138 
139  /**
140  * Load specified datasets. It immediately returns and starts another thread, which actually loads the data.
141  *
142  * \param filenames list of filenames to load. The registered notification handler for the root container will get notified on
143  * error and success.
144  * \param suppressColormaps if true, the data modules are instructed to avoid registration of colormaps. This can be very handy if you
145  * combine multiple data loaders into one new data loader or data set
146  *
147  * \return the batch loader responsible for loading. Can be queried for the list of data modules.
148  */
149  WBatchLoader::SPtr loadDataSets( std::vector< std::string > filenames, bool suppressColormaps = false );
150 
151  /**
152  * Loads the specified files synchronously.
153  *
154  * \param filenames list of filenames to load. The registered notification handler for the root container will get notified on
155  * error and success.
156  * \param suppressColormaps if true, the data modules are instructed to avoid registration of colormaps. This can be very handy if you
157  * combine multiple data loaders into one new data loader or data set
158  *
159  * \return the batch loader responsible for loading. Can be queried for the list of data modules.
160  */
161  WBatchLoader::SPtr loadDataSetsSynchronously( std::vector< std::string > filenames, bool suppressColormaps = false );
162 
163  /**
164  * Function combines to modules. This is a simple alias for "getRootContainer()->applyModule". It runs synchronously, which
165  * could freeze the calling thread for a couple of time.
166  *
167  * \param applyOn the module which already has to be in the container and to apply the other one on.
168  * \param prototype the prototype of the module to apply on the other one specified.
169  *
170  * \return the newly created module connected with the one specified in applyOn.
171  */
172  boost::shared_ptr< WModule > applyModule( boost::shared_ptr< WModule > applyOn, boost::shared_ptr< WModule > prototype );
173 
174  /**
175  * Returns the root module container. This is the actual module graph container.
176  *
177  * \return the root container.
178  */
179  boost::shared_ptr< WModuleContainer > getRootContainer() const;
180 
181  /**
182  * Getter for the associated UI.
183  *
184  * \return the UI.
185  */
186  boost::shared_ptr< WUI > getUI() const;
187 
188  /**
189  * get for roi manager
190  *
191  * \return Pointer to the ROI manager.
192  */
193  boost::shared_ptr< WROIManager> getRoiManager();
194 
195  /**
196  * get for selection manager
197  *
198  * \return Pointer to the selection manager.
199  */
200  boost::shared_ptr< WSelectionManager> getSelectionManager();
201 
202  /**
203  * Get the script engine of this kernel.
204  *
205  * \return A pointer to the script engine.
206  */
207  boost::shared_ptr< WScriptEngine > getScriptEngine();
208 
209  /**
210  * Returns the system timer. If you need timing for animations and similar, use this one. This timer can change to frame based timing if the
211  * user plays back some animation. So, everything which uses this timer can always do accurate per-frame animations even if frame time and
212  * real-time differ.
213  *
214  * \return the timer
215  */
216  WTimer::ConstSPtr getTimer() const;
217 
218 protected:
219  /**
220  * Constructor is protected because this class is a singleton. Awaits an INITIALIZED graphics engine an UI.
221  *
222  * \param ge initialized graphics engine.
223  * \param ui initialized UI.
224  */
225  WKernel( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WUI > ui );
226 
227  /**
228  * Function that has to be overwritten for execution. It gets executed in a separate thread after run()
229  * has been called.
230  */
231  virtual void threadMain();
232 
233  /**
234  * The UI.
235  */
236  boost::shared_ptr< WUI > m_ui;
237 
238  /**
239  * Pointer to an initialized graphics engine.
240  */
241  boost::shared_ptr< WGraphicsEngine > m_graphicsEngine;
242 
243  /**
244  * Pointer to a roi manager
245  */
246  boost::shared_ptr< WROIManager >m_roiManager;
247 
248  /**
249  * pointer to a selection manager
250  */
251  boost::shared_ptr< WSelectionManager >m_selectionManager;
252 
253  /**
254  * The module factory to use.
255  */
256  boost::shared_ptr< WModuleFactory > m_moduleFactory;
257 
258  /**
259  * The container containing the modules.
260  */
261  boost::shared_ptr< WModuleContainer > m_moduleContainer;
262 
263  /**
264  * The script engine to use.
265  */
266  boost::shared_ptr< WScriptEngine > m_scriptEngine;
267 
268 private:
269  /**
270  * Loads all the modules it can find.
271  */
272  void loadModules();
273 
274  /**
275  * Initializes the graphics engine, data handler and so on.
276  */
277  void init();
278 
279  /**
280  * Pointer to the unique instance of this singleton class.
281  */
282  static WKernel* m_kernel;
283 
284  /**
285  * The ow system timer.
286  */
288 
289  /**
290  * Notified when the startup, including GE and UI has been completed.
291  */
293 };
294 
295 #endif // WKERNEL_H
296 
boost::shared_ptr< WScriptEngine > getScriptEngine()
Get the script engine of this kernel.
Definition: WKernel.cpp:219
boost::shared_ptr< WSelectionManager > m_selectionManager
pointer to a selection manager
Definition: WKernel.h:251
boost::shared_ptr< WModuleContainer > getRootContainer() const
Returns the root module container.
Definition: WKernel.cpp:127
Class able to create a new copy of an arbitrary module.
WTimer::ConstSPtr getTimer() const
Returns the system timer.
Definition: WKernel.cpp:224
static WKernel * getRunningKernel()
Returns pointer to the currently running kernel.
Definition: WKernel.cpp:117
virtual void threadMain()
Function that has to be overwritten for execution.
Definition: WKernel.cpp:148
boost::signals2::signal< void(void) > t_KernelGenericSignalType
Generic signal type used in the most signals.
Definition: WKernel.h:75
This class prescribes the interface to the UI.
Definition: WUI.h:52
boost::shared_ptr< WSelectionManager > getSelectionManager()
get for selection manager
Definition: WKernel.cpp:214
boost::shared_ptr< WScriptEngine > m_scriptEngine
The script engine to use.
Definition: WKernel.h:266
WKernel(boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WUI > ui)
Constructor is protected because this class is a singleton.
Definition: WKernel.cpp:56
OpenWalnut kernel, managing modules and interaction between UI, GE and DataHandler.
Definition: WKernel.h:63
Class representing a single module of OpenWalnut.
Definition: WModule.h:83
boost::shared_ptr< WUI > getUI() const
Getter for the associated UI.
Definition: WKernel.cpp:132
boost::shared_ptr< WGraphicsEngine > m_graphicsEngine
Pointer to an initialized graphics engine.
Definition: WKernel.h:241
boost::shared_ptr< WROIManager > m_roiManager
Pointer to a roi manager.
Definition: WKernel.h:246
static WKernel * instance(boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WUI > ui)
Returns pointer to the running kernel or a new if no kernel was there.
Definition: WKernel.cpp:82
WConditionOneShot m_startupCompleted
Notified when the startup, including GE and UI has been completed.
Definition: WKernel.h:292
Implements a WCondition, but can be fired only ONCE.
boost::shared_ptr< WTimer > SPtr
Convenience typedef for a shared_ptr.
Definition: WTimer.h:43
void finalize()
Stops execution of the modules in the root container.
Definition: WKernel.cpp:137
Base class for all classes needing to be executed in a separate thread.
void loadModules()
Loads all the modules it can find.
const WBoolFlag & isFinishRequested() const
Determines whether all threads should finish.
Definition: WKernel.cpp:189
boost::shared_ptr< WROIManager > getRoiManager()
get for roi manager
Definition: WKernel.cpp:209
WBatchLoader::SPtr loadDataSetsSynchronously(std::vector< std::string > filenames, bool suppressColormaps=false)
Loads the specified files synchronously.
Definition: WKernel.cpp:199
boost::shared_ptr< WModule > applyModule(boost::shared_ptr< WModule > applyOn, boost::shared_ptr< WModule > prototype)
Function combines to modules.
Definition: WKernel.cpp:204
boost::shared_ptr< WBatchLoader > SPtr
Shared ptr abbreviation.
Definition: WBatchLoader.h:57
Class to store and manage different ROI's for fiber selection.
Definition: WROIManager.h:42
manages the several selection tools
boost::shared_ptr< WModuleFactory > m_moduleFactory
The module factory to use.
Definition: WKernel.h:256
static WKernel * m_kernel
Pointer to the unique instance of this singleton class.
Definition: WKernel.h:282
boost::signals2::connection subscribeSignal(KERNEL_SIGNAL signal, t_KernelGenericSignalHandlerType notifier)
Subscribe to several signals.
Definition: WKernel.cpp:229
boost::function< void(void) > t_KernelGenericSignalHandlerType
Signal for generic events.
Definition: WKernel.h:70
WTimer::SPtr m_timer
The ow system timer.
Definition: WKernel.h:287
boost::shared_ptr< WUI > m_ui
The UI.
Definition: WKernel.h:236
boost::shared_ptr< WGraphicsEngine > getGraphicsEngine() const
Returns pointer to currently running instance of graphics engine.
Definition: WKernel.cpp:122
Class able to contain other modules.
boost::shared_ptr< WModuleContainer > m_moduleContainer
The container containing the modules.
Definition: WKernel.h:261
WBatchLoader::SPtr loadDataSets(std::vector< std::string > filenames, bool suppressColormaps=false)
Load specified datasets.
Definition: WKernel.cpp:194
boost::shared_ptr< const WTimer > ConstSPtr
Convenience typedef for a const shared_ptr.
Definition: WTimer.h:48
void init()
Initializes the graphics engine, data handler and so on.
Definition: WKernel.cpp:92
virtual ~WKernel()
Destructor.
Definition: WKernel.cpp:76
KERNEL_SIGNAL
Enum of all possible signals WKernel instances can emit.
Definition: WKernel.h:80