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