go home Home | Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages
itkParameterMapInterface.h
Go to the documentation of this file.
1 /*======================================================================
2 
3 This file is part of the elastix software.
4 
5 Copyright (c) University Medical Center Utrecht. All rights reserved.
6 See src/CopyrightElastix.txt or http://elastix.isi.uu.nl/legal.php for
7 details.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
12 
13 ======================================================================*/
14 #ifndef __itkParameterMapInterface_h
15 #define __itkParameterMapInterface_h
16 
17 #include "itkObject.h"
18 #include "itkObjectFactory.h"
19 #include "itkMacro.h"
20 #include "itkNumericTraits.h"
21 
22 #include "itkParameterFileParser.h"
23 
24 #include <iostream>
25 
26 
27 namespace itk
28 {
29 
69 class ParameterMapInterface : public Object
70 {
71 public:
72 
75  typedef Object Superclass;
76  typedef SmartPointer< Self > Pointer;
77  typedef SmartPointer< const Self > ConstPointer;
78 
80  itkNewMacro( Self );
81 
83  itkTypeMacro( ParameterMapInterface, Object );
84 
88 
90  void SetParameterMap( const ParameterMapType & parMap );
91 
95  // \todo: we could think of a warning level. (maybe you want warnings, but
96  // not when for example a parameter is not found at entry entry_nr but at entry 0 instead
97  itkSetMacro( PrintErrorMessages, bool );
98  itkGetConstMacro( PrintErrorMessages, bool );
99 
101  std::size_t CountNumberOfParameterEntries(
102  const std::string & parameterName ) const;
103 
118  template <class T>
119  bool ReadParameter( T & parameterValue,
120  const std::string & parameterName,
121  const unsigned int entry_nr,
122  const bool printThisErrorMessage,
123  std::string & errorMessage ) const
124  {
126  errorMessage = "";
127 
129  std::size_t numberOfEntries = this->CountNumberOfParameterEntries(
130  parameterName );
131 
133  if ( numberOfEntries == 0 )
134  {
135  std::stringstream ss;
136  ss << "WARNING: The parameter \"" << parameterName
137  << "\", requested at entry number " << entry_nr
138  << ", does not exist at all.\n"
139  << " The default value \"" << parameterValue
140  << "\" is used instead." << std::endl;
141  if ( printThisErrorMessage && this->m_PrintErrorMessages )
142  {
143  errorMessage = ss.str();
144  }
145 
146  return false;
147  }
148 
150  const ParameterValuesType & vec = this->m_ParameterMap.find( parameterName )->second;
151 
153  if ( entry_nr >= numberOfEntries )
154  {
155  std::stringstream ss;
156  ss << "WARNING: The parameter \"" << parameterName
157  << "\" does not exist at entry number " << entry_nr
158  << ".\n The default value \"" << parameterValue
159  << "\" is used instead." << std::endl;
160  if ( printThisErrorMessage && this->m_PrintErrorMessages )
161  {
162  errorMessage = ss.str();
163  }
164  return false;
165  }
166 
168  bool castSuccesful = this->StringCast( vec[ entry_nr ], parameterValue );
169 
171  if ( !castSuccesful )
172  {
173  std::stringstream ss;
174  ss << "ERROR: Casting entry number " << entry_nr
175  << " for the parameter \"" << parameterName
176  << "\" failed!\n"
177  << " You tried to cast \"" << vec[ entry_nr ]
178  << "\" from std::string to "
179  << typeid( parameterValue ).name() << std::endl;
180 
181  itkExceptionMacro( << ss.str() );
182  }
183 
184  return true;
185 
186  } // end ReadParameter()
187 
189  bool ReadParameter( bool & parameterValue,
190  const std::string & parameterName,
191  const unsigned int entry_nr,
192  const bool printThisErrorMessage,
193  std::string & errorMessage ) const;
194 
198  template <class T>
199  bool ReadParameter( T & parameterValue,
200  const std::string & parameterName,
201  const unsigned int entry_nr,
202  std::string & errorMessage ) const
203  {
204  return this->ReadParameter( parameterValue, parameterName, entry_nr,
205  true, errorMessage );
206  }
207 
213  template <class T>
214  bool ReadParameter( T & parameterValue,
215  const std::string & parameterName,
216  const std::string & prefix,
217  const unsigned int entry_nr,
218  const int default_entry_nr,
219  const bool printThisErrorMessage,
220  std::string & errorMessage ) const
221  {
222  std::string fullname = prefix + parameterName;
223  bool found = false;
224 
226  std::string dummyString = "";
227  if ( default_entry_nr >= 0 )
228  {
230  unsigned int uintdefault = static_cast<unsigned int>( default_entry_nr );
231  found |= this->ReadParameter( parameterValue, parameterName, uintdefault,
232  false, dummyString );
233  found |= this->ReadParameter( parameterValue, parameterName, entry_nr,
234  false, dummyString );
235  found |= this->ReadParameter( parameterValue, fullname, uintdefault,
236  false, dummyString );
237  found |= this->ReadParameter( parameterValue, fullname, entry_nr,
238  false, dummyString );
239  }
240  else
241  {
243  found |= this->ReadParameter( parameterValue, parameterName, entry_nr,
244  false, dummyString );
245  found |= this->ReadParameter( parameterValue, fullname, entry_nr,
246  false, dummyString );
247  }
248 
252  if ( !found && printThisErrorMessage && this->m_PrintErrorMessages )
253  {
254  return this->ReadParameter( parameterValue, parameterName, entry_nr,
255  true, errorMessage );
256  }
257 
258  return found;
259 
260  }
261 
265  template <class T>
266  bool ReadParameter( T & parameterValue,
267  const std::string & parameterName,
268  const std::string & prefix,
269  const unsigned int entry_nr,
270  const unsigned int default_entry_nr,
271  std::string & errorMessage ) const
272  {
273  return this->ReadParameter( parameterValue, parameterName, prefix,
274  entry_nr, default_entry_nr, true, errorMessage );
275  }
276 
278  template <class T>
280  std::vector< T > & parameterValues,
281  const std::string & parameterName,
282  const unsigned int entry_nr_start,
283  const unsigned int entry_nr_end,
284  const bool printThisErrorMessage,
285  std::string & errorMessage ) const
286  {
288  errorMessage = "";
289 
291  std::size_t numberOfEntries = this->CountNumberOfParameterEntries(
292  parameterName );
293 
295  if ( numberOfEntries == 0 )
296  {
297  std::stringstream ss;
298  ss << "WARNING: The parameter \"" << parameterName
299  << "\", requested between entry numbers " << entry_nr_start
300  << " and " << entry_nr_end
301  << ", does not exist at all.\n"
302  << " The default values are used instead." << std::endl;
303  if ( printThisErrorMessage && this->m_PrintErrorMessages )
304  {
305  errorMessage = ss.str();
306  }
307  return false;
308  }
309 
311  if ( entry_nr_start > entry_nr_end )
312  {
313  std::stringstream ss;
314  ss << "WARNING: The entry number start (" << entry_nr_start
315  << ") should be smaller than entry number end (" << entry_nr_end
316  << "). It was requested for parameter \"" << parameterName
317  << "\"." << std::endl;
318 
320  itkExceptionMacro( << ss.str() );
321  }
322 
324  if ( entry_nr_end >= numberOfEntries )
325  {
326  std::stringstream ss;
327  ss << "WARNING: The parameter \"" << parameterName
328  << "\" does not exist at entry number " << entry_nr_end
329  << ".\nThe default value \"" << itk::NumericTraits<T>::Zero
330  << "\" is used instead." << std::endl;
331  itkExceptionMacro( << ss.str() );
332  }
333 
335  const ParameterValuesType & vec = this->m_ParameterMap.find( parameterName )->second;
336 
344  unsigned int j = 0;
345  for ( unsigned int i = entry_nr_start; i < entry_nr_end + 1; ++i )
346  {
348  bool castSuccesful = this->StringCast( vec[ i ], parameterValues[ j ] );
349  j++;
350 
352  if ( !castSuccesful )
353  {
354  std::stringstream ss;
355  ss << "ERROR: Casting entry number " << i
356  << " for the parameter \"" << parameterName
357  << "\" failed!\n"
358  << " You tried to cast \"" << vec[ i ]
359  << "\" from std::string to "
360  << typeid( parameterValues[ 0 ] ).name() << std::endl;
361 
362  itkExceptionMacro( << ss.str() );
363  }
364  }
365 
366  return true;
367  }
368 
370  bool ReadParameter(
371  std::vector<std::string> & parameterValues,
372  const std::string & parameterName,
373  const unsigned int entry_nr_start,
374  const unsigned int entry_nr_end,
375  const bool printThisErrorMessage,
376  std::string & errorMessage ) const;
377 
378 protected:
380  virtual ~ParameterMapInterface();
381 
382 private:
383  ParameterMapInterface(const Self&); // purposely not implemented
384  void operator=(const Self&); // purposely not implemented
385 
388 
390 
395  template <class T>
396  bool StringCast( const std::string & parameterValue, T & casted ) const
397  {
398  std::stringstream ss( parameterValue );
399 
404  typename NumericTraits<T>::AccumulateType tempCasted;
405  ss >> tempCasted;
406  casted = static_cast<T>( tempCasted );
407  if ( ss.bad() || ss.fail() )
408  {
409  return false;
410  }
411  return true;
412 
413  } // end StringCast()
414 
418  bool StringCast( const std::string & parameterValue, std::string & casted ) const;
419 
420 }; // end class ParameterMapInterface
421 
422 } // end of namespace itk
423 
424 #endif // end __itkParameterMapInterface_h
std::map< std::string, ParameterValuesType > ParameterMapType
bool ReadParameter(T &parameterValue, const std::string &parameterName, const std::string &prefix, const unsigned int entry_nr, const int default_entry_nr, const bool printThisErrorMessage, std::string &errorMessage) const
void SetParameterMap(const ParameterMapType &parMap)
Implements functionality to get parameters from a parameter map.
ParameterFileParser::ParameterMapType ParameterMapType
SmartPointer< const Self > ConstPointer
ParameterFileParser::ParameterValuesType ParameterValuesType
bool ReadParameter(T &parameterValue, const std::string &parameterName, const std::string &prefix, const unsigned int entry_nr, const unsigned int default_entry_nr, std::string &errorMessage) const
bool ReadParameter(T &parameterValue, const std::string &parameterName, const unsigned int entry_nr, std::string &errorMessage) const
bool ReadParameter(std::vector< T > &parameterValues, const std::string &parameterName, const unsigned int entry_nr_start, const unsigned int entry_nr_end, const bool printThisErrorMessage, std::string &errorMessage) const
std::vcl_size_t CountNumberOfParameterEntries(const std::string &parameterName) const
void operator=(const Self &)
std::vector< std::string > ParameterValuesType
bool StringCast(const std::string &parameterValue, T &casted) const
bool ReadParameter(T &parameterValue, const std::string &parameterName, const unsigned int entry_nr, const bool printThisErrorMessage, std::string &errorMessage) const


Generated on 05-01-2014 for elastix by doxygen 1.8.5 elastix logo