OpenWalnut  1.3.1
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 "../common/WLogger.h"
35 #include "../graphicsEngine/WGraphicsEngine.h"
36 
37 #include "WBatchLoader.h"
38 
39 
40 
41 // forward declarations
42 class WGUI;
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 GUI, GraphicsEngine and DataHandler.
56  */
57 
58 /**
59  * OpenWalnut kernel, managing modules and interaction between
60  * GUI, GE and DataHandler
61  * \ingroup kernel
62  */
63 class WKernel: public WThreadedRunner
64 {
65 public:
66  /**
67  * Returns pointer to the running kernel or a new if no kernel was there.
68  * If a running kernel exists the function return it and does not check if
69  * ge and gui of the running kernel are equivalent to the ones given as parameters.
70  *
71  * \param ge initialized graphics engine.
72  * \param gui initialized gui.
73  * \return the kernel instance.
74  */
75  static WKernel* instance( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WGUI > gui );
76 
77  /**
78  * Destructor.
79  */
80  virtual ~WKernel();
81 
82  /**
83  * Stops execution of the modules in the root container. Note that this does not wait for the kernel thread since this could
84  * cause a dead lock. This is actually an alias for getRootContainer()->stop().
85  */
86  void finalize();
87 
88  /**
89  * Returns pointer to currently running instance of graphics engine.
90  *
91  * \return the graphics engine instance.
92  */
93  boost::shared_ptr< WGraphicsEngine > getGraphicsEngine() const;
94 
95  /**
96  * Returns pointer to the currently running kernel.
97  *
98  * \return the kernel instance.
99  */
100  static WKernel* getRunningKernel();
101 
102  /**
103  * Determines whether all threads should finish.
104  *
105  * \return true if so.
106  */
107  const WBoolFlag& isFinishRequested() const;
108 
109  /**
110  * Load specified datasets. It immediately returns and starts another thread, which actually loads the data.
111  *
112  * \param filenames list of filenames to load. The registered notification handler for the root container will get notified on
113  * error and success.
114  * \param suppressColormaps if true, the data modules are instructed to avoid registration of colormaps. This can be very handy if you
115  * combine multiple data loaders into one new data loader or data set
116  *
117  * \return the batch loader responsible for loading. Can be queried for the list of data modules.
118  */
119  WBatchLoader::SPtr loadDataSets( std::vector< std::string > filenames, bool suppressColormaps = false );
120 
121  /**
122  * Loads the specified files synchronously.
123  *
124  * \param filenames list of filenames to load. The registered notification handler for the root container will get notified on
125  * error and success.
126  * \param suppressColormaps if true, the data modules are instructed to avoid registration of colormaps. This can be very handy if you
127  * combine multiple data loaders into one new data loader or data set
128  *
129  * \return the batch loader responsible for loading. Can be queried for the list of data modules.
130  */
131  WBatchLoader::SPtr loadDataSetsSynchronously( std::vector< std::string > filenames, bool suppressColormaps = false );
132 
133  /**
134  * Function combines to modules. This is a simple alias for "getRootContainer()->applyModule". It runs synchronously, which
135  * could freeze the calling thread for a couple of time.
136  *
137  * \param applyOn the module which already has to be in the container and to apply the other one on.
138  * \param prototype the prototype of the module to apply on the other one specified.
139  *
140  * \return the newly created module connected with the one specified in applyOn.
141  */
142  boost::shared_ptr< WModule > applyModule( boost::shared_ptr< WModule > applyOn, boost::shared_ptr< WModule > prototype );
143 
144  /**
145  * Returns the root module container. This is the actual module graph container.
146  *
147  * \return the root container.
148  */
149  boost::shared_ptr< WModuleContainer > getRootContainer() const;
150 
151  /**
152  * Getter for the associated GUI.
153  *
154  * \return the GUI.
155  */
156  boost::shared_ptr< WGUI > getGui() const;
157 
158  /**
159  * get for roi manager
160  *
161  * \return Pointer to the ROI manager.
162  */
163  boost::shared_ptr< WROIManager> getRoiManager();
164 
165  /**
166  * get for selection manager
167  *
168  * \return Pointer to the selection manager.
169  */
170  boost::shared_ptr< WSelectionManager> getSelectionManager();
171 
172  /**
173  * 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
174  * user plays back some animation. So, everything which uses this timer can always do accurate per-frame animations even if frame time and
175  * real-time differ.
176  *
177  * \return the timer
178  */
179  WTimer::ConstSPtr getTimer() const;
180 
181 protected:
182  /**
183  * Constructor is protected because this class is a singleton. Awaits an INITIALIZED graphics engine an gui.
184  *
185  * \param ge initialized graphics engine.
186  * \param gui initialized gui.
187  */
188  WKernel( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WGUI > gui );
189 
190  /**
191  * Function that has to be overwritten for execution. It gets executed in a separate thread after run()
192  * has been called.
193  */
194  virtual void threadMain();
195 
196  /**
197  * The Gui.
198  */
199  boost::shared_ptr< WGUI > m_gui;
200 
201  /**
202  * Pointer to an initialized graphics engine.
203  */
204  boost::shared_ptr< WGraphicsEngine > m_graphicsEngine;
205 
206  /**
207  * Pointer to a roi manager
208  */
209  boost::shared_ptr< WROIManager >m_roiManager;
210 
211  /**
212  * pointer to a selection manager
213  */
214  boost::shared_ptr< WSelectionManager >m_selectionManager;
215 
216  /**
217  * The module factory to use.
218  */
219  boost::shared_ptr< WModuleFactory > m_moduleFactory;
220 
221  /**
222  * The container containing the modules.
223  */
224  boost::shared_ptr< WModuleContainer > m_moduleContainer;
225 
226 private:
227  /**
228  * Loads all the modules it can find.
229  */
230  void loadModules();
231 
232  /**
233  * Initializes the graphics engine, data handler and so on.
234  */
235  void init();
236 
237  /**
238  * Pointer to the unique instance of this singleton class.
239  */
240  static WKernel* m_kernel;
241 
242  /**
243  * The ow system timer.
244  */
246 };
247 
248 #endif // WKERNEL_H
249