OpenWalnut  1.5.0dev
WMWriteRawData.cpp
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 #include <fstream>
26 #include <memory>
27 #include <string>
28 
29 #include "WMWriteRawData.h"
30 #include "WMWriteRawData.xpm"
31 #include "core/common/WPathHelper.h"
32 #include "core/kernel/WKernel.h"
33 
34 // This line is needed by the module loader to actually find your module. Do not remove. Do NOT add a ";" here.
35 W_LOADABLE_MODULE( WMWriteRawData )
36 
38  WModule()
39 {
40 }
41 
43 {
44  // Cleanup!
45 }
46 
47 std::shared_ptr< WModule > WMWriteRawData::factory() const
48 {
49  return std::shared_ptr< WModule >( new WMWriteRawData() );
50 }
51 
52 const char** WMWriteRawData::getXPMIcon() const
53 {
54  return WMWriteRawData_xpm; // Please put a real icon here.
55 }
56 const std::string WMWriteRawData::getName() const
57 {
58  return "Write Raw Data";
59 }
60 
61 const std::string WMWriteRawData::getDescription() const
62 {
63  return "Write scalar data defined on uniform lattices"
64  "in raw format, i.e., plain three-dimensional arrays of data. Data format is defined by data type of dataset (float, byte, ...).";
65 }
66 
68 {
69  m_input = std::shared_ptr< WModuleInputData< WDataSetScalar > >(
70  new WModuleInputData< WDataSetScalar >( shared_from_this(), "Data", "The data to write." ) );
71 
73 
75 }
76 
78 {
79  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
80  m_dataFile = m_properties->addProperty( "File", "", WPathHelper::getAppPath(), m_propCondition );
81 
83 }
84 
86 {
87  // Put the code for your requirements here. See "src/modules/template/" for an extensively documented example.
88 }
89 
90 /**
91  * Visitor for discriminating the type of the first valueset.
92  */
93 class VisitorVSet: public boost::static_visitor<>
94 {
95 public:
96  /**
97  * Create visitor instance and convert it to the given input type
98  *
99  * \param fn the filename
100  */
101  explicit VisitorVSet( boost::filesystem::path fn ):
102  boost::static_visitor<>(),
103  m_filename( fn )
104  {
105  }
106 
107  /**
108  * Called by boost::varying during static visiting. Creates new, converted valueset
109  *
110  * \tparam T the real integral type of the first value set.
111  * \param vals the first valueset currently visited.
112  */
113  template < typename T >
114  void operator()( const WValueSet< T >* const& vals ) const // NOLINT
115  {
116  try
117  {
118  std::ofstream file( m_filename.string().c_str(), std::ios::out | std::ofstream::binary );
119  file.write( reinterpret_cast<const char *>( vals->rawData() ) , sizeof( T ) * vals->rawSize() );
120  file.close();
121  }
122  catch( int e )
123  {
124  }
125  }
126 private:
127  /**
128  * Where to write
129  */
130  boost::filesystem::path m_filename;
131 };
132 
134 {
136  m_moduleState.add( m_input->getDataChangedCondition() );
137  ready();
138 
139  // loop until the module container requests the module to quit
140  while( !m_shutdownFlag() )
141  {
142  // this waits for m_moduleState to fire. By default, this is only the m_shutdownFlag condition.
143  // NOTE: you can add your own conditions to m_moduleState using m_moduleState.add( ... )
145  if( m_shutdownFlag() )
146  {
147  debugLog() << "hallo";
148  break;
149  }
150 
151  // acquire data from the input connector
152  bool dataSetChanged = !( m_dataSet == m_input->getData() );
153  m_dataSet = m_input->getData();
154  if( m_dataSet && ( dataSetChanged || m_dataFile->changed() ) )
155  {
156  debugLog() << "Writing " << m_dataFile->get().string() << ".";
157  std::shared_ptr< WProgress > progress( new WProgress( "Write File", 2 ) );
158  m_progress->addSubProgress( progress );
159 
160  // call visitor for this job
161  VisitorVSet visitor( m_dataFile->get( true ) );
162  m_dataSet->getValueSet()->applyFunction( visitor );
163 
164  progress->finish();
165  }
166  }
167 }
168 
Visitor for discriminating the type of the first valueset.
boost::filesystem::path m_filename
Where to write.
void operator()(const WValueSet< T > *const &vals) const
Called by boost::varying during static visiting.
VisitorVSet(boost::filesystem::path fn)
Create visitor instance and convert it to the given input type.
virtual void wait() const
Wait for the condition.
virtual void add(std::shared_ptr< WCondition > condition)
Adds another condition to the set of conditions to wait for.
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:42
A module to read scalar data stored as array of raw data.
std::shared_ptr< WModuleInputData< WDataSetScalar > > m_input
Output connector provided by this module.
virtual void moduleMain()
Entry point after loading the module.
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
WMWriteRawData()
Constructor of module.
virtual void connectors()
Initialize the connectors this module is using.
virtual const std::string getDescription() const
Gives back a description of this module.
virtual ~WMWriteRawData()
Destructor of module.
std::shared_ptr< WDataSetScalar > m_dataSet
This data set is provided as output through the connector.
virtual void requirements()
Initialize requirements for this module.
virtual void properties()
Initialize the properties for this module.
virtual std::shared_ptr< WModule > factory() const
Due to the prototype design pattern used to build modules, this method returns a new instance of this...
WPropFilename m_dataFile
The data will be read from this file.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
virtual const std::string getName() const
Gives back the name of this module.
Class offering an instantiate-able data connection between modules.
Class representing a single module of OpenWalnut.
Definition: WModule.h:72
virtual void properties()
Initialize properties in this function.
Definition: WModule.cpp:212
wlog::WStreamedLogger debugLog() const
Logger instance for comfortable debug logging.
Definition: WModule.cpp:575
void addConnector(std::shared_ptr< WModuleInputConnector > con)
Adds the specified connector to the list of inputs.
Definition: WModule.cpp:108
std::shared_ptr< WProperties > m_properties
The property object for the module.
Definition: WModule.h:640
void ready()
Call this whenever your module is ready and can react on property changes.
Definition: WModule.cpp:505
WConditionSet m_moduleState
The internal state of the module.
Definition: WModule.h:703
std::shared_ptr< WProgressCombiner > m_progress
Progress indicator used as parent for all progress' of this module.
Definition: WModule.h:652
virtual void connectors()
Initialize connectors in this function.
Definition: WModule.cpp:208
static boost::filesystem::path getAppPath()
The path where the binary file resides in.
Definition: WPathHelper.cpp:93
Class managing progress inside of modules.
Definition: WProgress.h:42
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
Base Class for all value set types.
Definition: WValueSet.h:47