OpenWalnut  1.5.0dev
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 <memory>
29 #include <string>
30 #include <vector>
31 
32 
33 #include "../common/WTimer.h"
34 #include "WSelectionManager.h"
35 #include "../graphicsEngine/WGraphicsEngine.h"
36 #include "../scripting/WScriptEngine.h"
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 WThreadedRunner;
46 
47 /**
48  * \defgroup kernel Kernel
49  *
50  * \brief
51  * This library implements the central part of OpenWalnut that manages
52  * the interaction between UI, GraphicsEngine and DataHandler.
53  */
54 
55 /**
56  * OpenWalnut kernel, managing modules and interaction between
57  * UI, GE and DataHandler
58  * \ingroup kernel
59  */
60 class WKernel: public WThreadedRunner
61 {
62 public:
63  /**
64  * Signal for generic events.
65  *
66  */
67  typedef boost::function< void ( void ) > t_KernelGenericSignalHandlerType;
68 
69  /**
70  * Generic signal type used in the most signals.
71  */
72  typedef boost::signals2::signal< void ( void ) > t_KernelGenericSignalType;
73 
74  /**
75  * Enum of all possible signals WKernel instances can emit.
76  */
77  typedef enum
78  {
79  KERNEL_STARTUPCOMPLETE // when kernel, GE and UI are correctly initialized
80  }
82 
83  /**
84  * Returns pointer to the running kernel or a new if no kernel was there.
85  * If a running kernel exists the function return it and does not check if
86  * GE and UI of the running kernel are equivalent to the ones given as parameters.
87  *
88  * \param ge initialized graphics engine.
89  * \param ui initialized ui.
90  * \return the kernel instance.
91  */
92  static WKernel* instance( std::shared_ptr< WGraphicsEngine > ge, std::shared_ptr< WUI > ui );
93 
94  /**
95  * Destructor.
96  */
97  virtual ~WKernel();
98 
99  // Tell the compiler that we really want to use the base's subscribeSignal.
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  std::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  std::shared_ptr< WModule > applyModule( std::shared_ptr< WModule > applyOn, std::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  std::shared_ptr< WModuleContainer > getRootContainer() const;
180 
181  /**
182  * Getter for the associated UI.
183  *
184  * \return the UI.
185  */
186  std::shared_ptr< WUI > getUI() const;
187 
188  /**
189  * get for roi manager
190  *
191  * \return Pointer to the ROI manager.
192  */
193  std::shared_ptr< WROIManager> getRoiManager();
194 
195  /**
196  * get for selection manager
197  *
198  * \return Pointer to the selection manager.
199  */
200  std::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  std::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( std::shared_ptr< WGraphicsEngine > ge, std::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  std::shared_ptr< WUI > m_ui;
237 
238  /**
239  * Pointer to an initialized graphics engine.
240  */
241  std::shared_ptr< WGraphicsEngine > m_graphicsEngine;
242 
243  /**
244  * Pointer to a roi manager
245  */
246  std::shared_ptr< WROIManager >m_roiManager;
247 
248  /**
249  * pointer to a selection manager
250  */
251  std::shared_ptr< WSelectionManager >m_selectionManager;
252 
253  /**
254  * The module factory to use.
255  */
256  std::shared_ptr< WModuleFactory > m_moduleFactory;
257 
258  /**
259  * The container containing the modules.
260  */
261  std::shared_ptr< WModuleContainer > m_moduleContainer;
262 
263  /**
264  * The script engine to use.
265  */
266  std::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 
std::shared_ptr< WBatchLoader > SPtr
Shared ptr abbreviation.
Definition: WBatchLoader.h:48
Implements a WCondition, but can be fired only ONCE.
OpenWalnut kernel, managing modules and interaction between UI, GE and DataHandler.
Definition: WKernel.h:61
WConditionOneShot m_startupCompleted
Notified when the startup, including GE and UI has been completed.
Definition: WKernel.h:292
virtual boost::signals2::connection subscribeSignal(THREAD_SIGNAL signal, t_ThreadErrorSignalHandlerType notifier)
Connects a specified notify function with a signal this thread instance is offering.
std::shared_ptr< WGraphicsEngine > m_graphicsEngine
Pointer to an initialized graphics engine.
Definition: WKernel.h:241
std::shared_ptr< WScriptEngine > m_scriptEngine
The script engine to use.
Definition: WKernel.h:266
std::shared_ptr< WSelectionManager > getSelectionManager()
get for selection manager
Definition: WKernel.cpp:214
std::shared_ptr< WModuleFactory > m_moduleFactory
The module factory to use.
Definition: WKernel.h:256
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
WKernel(std::shared_ptr< WGraphicsEngine > ge, std::shared_ptr< WUI > ui)
Constructor is protected because this class is a singleton.
Definition: WKernel.cpp:56
static WKernel * m_kernel
Pointer to the unique instance of this singleton class.
Definition: WKernel.h:282
static WKernel * instance(std::shared_ptr< WGraphicsEngine > ge, std::shared_ptr< WUI > ui)
Returns pointer to the running kernel or a new if no kernel was there.
Definition: WKernel.cpp:82
WTimer::SPtr m_timer
The ow system timer.
Definition: WKernel.h:287
void loadModules()
Loads all the modules it can find.
const WBoolFlag & isFinishRequested() const
Determines whether all threads should finish.
Definition: WKernel.cpp:189
std::shared_ptr< WModuleContainer > m_moduleContainer
The container containing the modules.
Definition: WKernel.h:261
std::shared_ptr< WModule > applyModule(std::shared_ptr< WModule > applyOn, std::shared_ptr< WModule > prototype)
Function combines to modules.
Definition: WKernel.cpp:204
WBatchLoader::SPtr loadDataSetsSynchronously(std::vector< std::string > filenames, bool suppressColormaps=false)
Loads the specified files synchronously.
Definition: WKernel.cpp:199
boost::signals2::signal< void(void) > t_KernelGenericSignalType
Generic signal type used in the most signals.
Definition: WKernel.h:72
std::shared_ptr< WUI > getUI() const
Getter for the associated UI.
Definition: WKernel.cpp:132
std::shared_ptr< WSelectionManager > m_selectionManager
pointer to a selection manager
Definition: WKernel.h:251
std::shared_ptr< WROIManager > m_roiManager
Pointer to a roi manager.
Definition: WKernel.h:246
std::shared_ptr< WModuleContainer > getRootContainer() const
Returns the root module container.
Definition: WKernel.cpp:127
void finalize()
Stops execution of the modules in the root container.
Definition: WKernel.cpp:137
std::shared_ptr< WScriptEngine > getScriptEngine()
Get the script engine of this kernel.
Definition: WKernel.cpp:219
void init()
Initializes the graphics engine, data handler and so on.
Definition: WKernel.cpp:92
KERNEL_SIGNAL
Enum of all possible signals WKernel instances can emit.
Definition: WKernel.h:78
WTimer::ConstSPtr getTimer() const
Returns the system timer.
Definition: WKernel.cpp:224
boost::function< void(void) > t_KernelGenericSignalHandlerType
Signal for generic events.
Definition: WKernel.h:67
std::shared_ptr< WROIManager > getRoiManager()
get for roi manager
Definition: WKernel.cpp:209
virtual ~WKernel()
Destructor.
Definition: WKernel.cpp:76
WBatchLoader::SPtr loadDataSets(std::vector< std::string > filenames, bool suppressColormaps=false)
Load specified datasets.
Definition: WKernel.cpp:194
std::shared_ptr< WGraphicsEngine > getGraphicsEngine() const
Returns pointer to currently running instance of graphics engine.
Definition: WKernel.cpp:122
std::shared_ptr< WUI > m_ui
The UI.
Definition: WKernel.h:236
Class able to contain other modules.
Class able to create a new copy of an arbitrary module.
Class representing a single module of OpenWalnut.
Definition: WModule.h:72
Class to store and manage different ROI's for fiber selection.
Definition: WROIManager.h:39
Base class for all classes needing to be executed in a separate thread.
virtual boost::signals2::connection subscribeSignal(THREAD_SIGNAL signal, t_ThreadErrorSignalHandlerType notifier)
Connects a specified notify function with a signal this thread instance is offering.
std::shared_ptr< const WTimer > ConstSPtr
Convenience typedef for a const shared_ptr.
Definition: WTimer.h:47
std::shared_ptr< WTimer > SPtr
Convenience typedef for a shared_ptr.
Definition: WTimer.h:42
This class prescribes the interface to the UI.
Definition: WUI.h:51