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 namespace itk
27 {
28 
68 class ParameterMapInterface : public Object
69 {
70 public:
71 
74  typedef Object Superclass;
75  typedef SmartPointer< Self > Pointer;
76  typedef SmartPointer< const Self > ConstPointer;
77 
79  itkNewMacro( Self );
80 
82  itkTypeMacro( ParameterMapInterface, Object );
83 
87 
89  void SetParameterMap( const ParameterMapType & parMap );
90 
94  // \todo: we could think of a warning level. (maybe you want warnings, but
95  // not when for example a parameter is not found at entry entry_nr but at entry 0 instead
96  itkSetMacro( PrintErrorMessages, bool );
97  itkGetConstMacro( PrintErrorMessages, bool );
98 
100  std::size_t CountNumberOfParameterEntries(
101  const std::string & parameterName ) const;
102 
117  template< class T >
118  bool ReadParameter( T & parameterValue,
119  const std::string & parameterName,
120  const unsigned int entry_nr,
121  const bool printThisErrorMessage,
122  std::string & errorMessage ) const
123  {
125  errorMessage = "";
126 
128  std::size_t numberOfEntries = this->CountNumberOfParameterEntries(
129  parameterName );
130 
132  if( numberOfEntries == 0 )
133  {
134  std::stringstream ss;
135  ss << "WARNING: The parameter \"" << parameterName
136  << "\", requested at entry number " << entry_nr
137  << ", does not exist at all.\n"
138  << " The default value \"" << parameterValue
139  << "\" is used instead." << std::endl;
140  if( printThisErrorMessage && this->m_PrintErrorMessages )
141  {
142  errorMessage = ss.str();
143  }
144 
145  return false;
146  }
147 
149  const ParameterValuesType & vec = this->m_ParameterMap.find( parameterName )->second;
150 
152  if( entry_nr >= numberOfEntries )
153  {
154  std::stringstream ss;
155  ss << "WARNING: The parameter \"" << parameterName
156  << "\" does not exist at entry number " << entry_nr
157  << ".\n The default value \"" << parameterValue
158  << "\" is used instead." << std::endl;
159  if( printThisErrorMessage && this->m_PrintErrorMessages )
160  {
161  errorMessage = ss.str();
162  }
163  return false;
164  }
165 
167  bool castSuccesful = this->StringCast( vec[ entry_nr ], parameterValue );
168 
170  if( !castSuccesful )
171  {
172  std::stringstream ss;
173  ss << "ERROR: Casting entry number " << entry_nr
174  << " for the parameter \"" << parameterName
175  << "\" failed!\n"
176  << " You tried to cast \"" << vec[ entry_nr ]
177  << "\" from std::string to "
178  << typeid( parameterValue ).name() << std::endl;
179 
180  itkExceptionMacro( << ss.str() );
181  }
182 
183  return true;
184 
185  } // end ReadParameter()
186 
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 
208 
214  template< class T >
215  bool ReadParameter( T & parameterValue,
216  const std::string & parameterName,
217  const std::string & prefix,
218  const unsigned int entry_nr,
219  const int default_entry_nr,
220  const bool printThisErrorMessage,
221  std::string & errorMessage ) const
222  {
223  std::string fullname = prefix + parameterName;
224  bool found = false;
225 
227  std::string dummyString = "";
228  if( default_entry_nr >= 0 )
229  {
231  unsigned int uintdefault = static_cast< unsigned int >( default_entry_nr );
232  found |= this->ReadParameter( parameterValue, parameterName, uintdefault,
233  false, dummyString );
234  found |= this->ReadParameter( parameterValue, parameterName, entry_nr,
235  false, dummyString );
236  found |= this->ReadParameter( parameterValue, fullname, uintdefault,
237  false, dummyString );
238  found |= this->ReadParameter( parameterValue, fullname, entry_nr,
239  false, dummyString );
240  }
241  else
242  {
244  found |= this->ReadParameter( parameterValue, parameterName, entry_nr,
245  false, dummyString );
246  found |= this->ReadParameter( parameterValue, fullname, entry_nr,
247  false, dummyString );
248  }
249 
253  if( !found && printThisErrorMessage && this->m_PrintErrorMessages )
254  {
255  return this->ReadParameter( parameterValue, parameterName, entry_nr,
256  true, errorMessage );
257  }
258 
259  return found;
260 
261  }
262 
263 
267  template< class T >
268  bool ReadParameter( T & parameterValue,
269  const std::string & parameterName,
270  const std::string & prefix,
271  const unsigned int entry_nr,
272  const unsigned int default_entry_nr,
273  std::string & errorMessage ) const
274  {
275  return this->ReadParameter( parameterValue, parameterName, prefix,
276  entry_nr, default_entry_nr, true, errorMessage );
277  }
278 
279 
281  template< class T >
283  std::vector< T > & parameterValues,
284  const std::string & parameterName,
285  const unsigned int entry_nr_start,
286  const unsigned int entry_nr_end,
287  const bool printThisErrorMessage,
288  std::string & errorMessage ) const
289  {
291  errorMessage = "";
292 
294  std::size_t numberOfEntries = this->CountNumberOfParameterEntries(
295  parameterName );
296 
298  if( numberOfEntries == 0 )
299  {
300  std::stringstream ss;
301  ss << "WARNING: The parameter \"" << parameterName
302  << "\", requested between entry numbers " << entry_nr_start
303  << " and " << entry_nr_end
304  << ", does not exist at all.\n"
305  << " The default values are used instead." << std::endl;
306  if( printThisErrorMessage && this->m_PrintErrorMessages )
307  {
308  errorMessage = ss.str();
309  }
310  return false;
311  }
312 
314  if( entry_nr_start > entry_nr_end )
315  {
316  std::stringstream ss;
317  ss << "WARNING: The entry number start (" << entry_nr_start
318  << ") should be smaller than entry number end (" << entry_nr_end
319  << "). It was requested for parameter \"" << parameterName
320  << "\"." << std::endl;
321 
323  itkExceptionMacro( << ss.str() );
324  }
325 
327  if( entry_nr_end >= numberOfEntries )
328  {
329  std::stringstream ss;
330  ss << "WARNING: The parameter \"" << parameterName
331  << "\" does not exist at entry number " << entry_nr_end
332  << ".\nThe default value \"" << itk::NumericTraits< T >::Zero
333  << "\" is used instead." << std::endl;
334  itkExceptionMacro( << ss.str() );
335  }
336 
338  const ParameterValuesType & vec = this->m_ParameterMap.find( parameterName )->second;
339 
347  unsigned int j = 0;
348  for( unsigned int i = entry_nr_start; i < entry_nr_end + 1; ++i )
349  {
351  bool castSuccesful = this->StringCast( vec[ i ], parameterValues[ j ] );
352  j++;
353 
355  if( !castSuccesful )
356  {
357  std::stringstream ss;
358  ss << "ERROR: Casting entry number " << i
359  << " for the parameter \"" << parameterName
360  << "\" failed!\n"
361  << " You tried to cast \"" << vec[ i ]
362  << "\" from std::string to "
363  << typeid( parameterValues[ 0 ] ).name() << std::endl;
364 
365  itkExceptionMacro( << ss.str() );
366  }
367  }
368 
369  return true;
370  }
371 
372 
374  bool ReadParameter(
375  std::vector< std::string > & parameterValues,
376  const std::string & parameterName,
377  const unsigned int entry_nr_start,
378  const unsigned int entry_nr_end,
379  const bool printThisErrorMessage,
380  std::string & errorMessage ) const;
381 
382 protected:
383 
385  virtual ~ParameterMapInterface();
386 
387 private:
388 
389  ParameterMapInterface( const Self & ); // purposely not implemented
390  void operator=( const Self & ); // purposely not implemented
391 
394 
396 
401  template< class T >
402  bool StringCast( const std::string & parameterValue, T & casted ) const
403  {
404  std::stringstream ss( parameterValue );
405 
410  typename NumericTraits< T >::AccumulateType tempCasted;
411  ss >> tempCasted;
412  casted = static_cast< T >( tempCasted );
413  if( ss.bad() || ss.fail() )
414  {
415  return false;
416  }
417  return true;
418 
419  } // end StringCast()
420 
421 
425  bool StringCast( const std::string & parameterValue, std::string & casted ) const;
426 
427 };
428 
429 } // end of namespace itk
430 
431 #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 11-03-2014 for elastix by doxygen 1.8.6 elastix logo