OpenWalnut  1.5.0dev
WDataModule.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 WDATAMODULE_H
26 #define WDATAMODULE_H
27 
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 
33 #include "WDataModuleInput.h"
34 #include "WDataModuleInputFilter.h"
35 #include "WModule.h"
36 #include "core/common/WProperties.h"
37 
38 /**
39  * Base for all data loader modules. It provides the basic mechanism to define input filters and some other settings.
40  *
41  * Implementing a WDataModule is nearly the same as a module. Mark your module ready and call your load code. Enter the main loop and maybe react
42  * on property changes.
43  *
44  * \note The reload functionality uses the m_reloadTriggered condition. Use it to wake up your module
45  */
46 class WDataModule: public WModule
47 {
48 public:
49  /**
50  * Convenience typedef for a std::shared_ptr< WDataModule >.
51  */
52  typedef std::shared_ptr< WDataModule > SPtr;
53 
54  /**
55  * Convenience typedef for a std::shared_ptr< const WDataModule >.
56  */
57  typedef std::shared_ptr< const WDataModule > ConstSPtr;
58 
59  /**
60  * Default constructor.
61  */
62  WDataModule();
63 
64  /**
65  * Destructor.
66  */
67  virtual ~WDataModule();
68 
69  /**
70  * Gets the type of the module. This is useful for FAST differentiation between several modules like standard modules and data
71  * modules which play a special role in OpenWalnut/Kernel.
72  *
73  * \return the Type. If you do not overwrite this method, it will return MODULE_ARBITRARY.
74  */
75  virtual MODULE_TYPE getType() const;
76 
77  /**
78  * Allows suppression of colormap registration in data modules. This can be handy if you use data modules in a container to construct more
79  * complex data sets from multiple input files. If a loader does not use colormapping, this setting is ignored.
80  *
81  * \note call this before adding and running the module.
82  *
83  * \param suppress true if suppress
84  */
85  virtual void setSuppressColormaps( bool suppress = true );
86 
87  /**
88  * Checks whether suppression of colormaps is active.
89  *
90  * \return true if colormaps are suppressed.
91  */
92  bool getSuppressColormaps() const;
93 
94  /**
95  * Return a list of input filters. This defines what your module can load.
96  * You can return an empty vector. This causes the data module to be "un-matched" for all kinds of data.
97  *
98  * Implement this function and ensure it works with module prototypes (do not rely on any properties, connectors and similar here).
99  *
100  * \return the list of filters
101  */
102  virtual std::vector< WDataModuleInputFilter::ConstSPtr > getInputFilter() const = 0;
103 
104  /**
105  * Set the input of this data module. This is called after construction and running the module.
106  *
107  * \param input the input to use for loading.
108  */
109  void setInput( WDataModuleInput::SPtr input );
110 
111  /**
112  * Initiate an reloading of the data.
113  */
114  void reload();
115 
116  /**
117  * Get the currently set input or NULL if none was set.
118  *
119  * \return the input
120  */
121  virtual WDataModuleInput::SPtr getInput() const;
122 
123  /**
124  * Get the currently set input or NULL if none was set.
125  *
126  * \return the input. Null if not set or type mismatch.
127  *
128  * \tparam InputType get the input in this type
129  */
130  template< typename InputType >
131  std::shared_ptr< InputType > getInputAs() const;
132 
133  /**
134  * Return the condition that gets triggered upon input change. This will get fired every time setInput was called, but before
135  * \ref handleInputChange.
136  *
137  * \return the condition
138  */
140 protected:
141  /**
142  * Handle a newly set input. Implement this method to load the newly set input. You can get the input using the \ref getInput and \ref getInputAs
143  * methods. Please remember that it is possible to get a NULL pointer here.
144  * This happens when the user explicitly sets no input. In this case, you should clean up and reset your output connectors.
145  *
146  * \note it is very important to NOT load the data inside of this method. It is usually called in the GUI thread. This would block the whole GUI.
147  * Instead, use this method for firing a condition, which then wakes your module thread.
148  */
149  virtual void handleInputChange() = 0;
150 
151 private:
152  /**
153  * If true, data modules are instructed to suppress colormap registration.
154  */
156 
157  /**
158  * The input this data module should use
159  */
161 
162  /**
163  * Condition that fires whenever the input changes via setInput.
164  */
166 };
167 
168 template< typename InputType >
169 std::shared_ptr< InputType > WDataModule::getInputAs() const
170 {
171  if( getInput() )
172  {
173  return std::dynamic_pointer_cast< InputType >( getInput() );
174  }
175  return std::shared_ptr< InputType >();
176 }
177 
178 #endif // WDATAMODULE_H
179 
std::shared_ptr< WCondition > SPtr
Shared pointer type for WCondition.
Definition: WCondition.h:48
std::shared_ptr< const WCondition > ConstSPtr
Const shared pointer type for WCondition.
Definition: WCondition.h:53
std::shared_ptr< WDataModuleInput > SPtr
Convenience typedef for a std::shared_ptr< WDataModuleInput >.
Base for all data loader modules.
Definition: WDataModule.h:47
bool m_suppressColormaps
If true, data modules are instructed to suppress colormap registration.
Definition: WDataModule.h:155
WDataModuleInput::SPtr m_dataModuleInput
The input this data module should use.
Definition: WDataModule.h:160
virtual MODULE_TYPE getType() const
Gets the type of the module.
Definition: WDataModule.cpp:41
WDataModule()
Default constructor.
Definition: WDataModule.cpp:27
std::shared_ptr< InputType > getInputAs() const
Get the currently set input or NULL if none was set.
Definition: WDataModule.h:169
virtual void handleInputChange()=0
Handle a newly set input.
std::shared_ptr< WDataModule > SPtr
Convenience typedef for a std::shared_ptr< WDataModule >.
Definition: WDataModule.h:52
WCondition::SPtr m_inputChanged
Condition that fires whenever the input changes via setInput.
Definition: WDataModule.h:165
WCondition::ConstSPtr getInputChangedCondition() const
Return the condition that gets triggered upon input change.
Definition: WDataModule.cpp:73
void reload()
Initiate an reloading of the data.
Definition: WDataModule.cpp:68
std::shared_ptr< const WDataModule > ConstSPtr
Convenience typedef for a std::shared_ptr< const WDataModule >.
Definition: WDataModule.h:57
bool getSuppressColormaps() const
Checks whether suppression of colormaps is active.
Definition: WDataModule.cpp:51
virtual std::vector< WDataModuleInputFilter::ConstSPtr > getInputFilter() const =0
Return a list of input filters.
virtual ~WDataModule()
Destructor.
Definition: WDataModule.cpp:36
virtual WDataModuleInput::SPtr getInput() const
Get the currently set input or NULL if none was set.
Definition: WDataModule.cpp:63
void setInput(WDataModuleInput::SPtr input)
Set the input of this data module.
Definition: WDataModule.cpp:56
virtual void setSuppressColormaps(bool suppress=true)
Allows suppression of colormap registration in data modules.
Definition: WDataModule.cpp:46
Class representing a single module of OpenWalnut.
Definition: WModule.h:72