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 
15 #ifndef __itkParameterMapInterface_h
16 #define __itkParameterMapInterface_h
17 
18 #include "itkObject.h"
19 #include "itkObjectFactory.h"
20 #include "itkMacro.h"
21 #include "itkNumericTraits.h"
22 
23 #include "itkParameterFileParser.h"
24 
25 #include <iostream>
26 
27 
28 namespace itk
29 {
30 
70 class ParameterMapInterface : public Object
71 {
72 public:
73 
76  typedef Object Superclass;
77  typedef SmartPointer< Self > Pointer;
78  typedef SmartPointer< const Self > ConstPointer;
79 
81  itkNewMacro( Self );
82 
84  itkTypeMacro( ParameterMapInterface, Object );
85 
89 
91  void SetParameterMap( const ParameterMapType & parMap );
92 
96  // \todo: we could think of a warning level. (maybe you want warnings, but
97  // not when for example a parameter is not found at entry entry_nr but at entry 0 instead
98  itkSetMacro( PrintErrorMessages, bool );
99  itkGetConstMacro( PrintErrorMessages, bool );
100 
102  std::size_t CountNumberOfParameterEntries(
103  const std::string & parameterName ) const;
104 
119  template <class T>
120  bool ReadParameter( T & parameterValue,
121  const std::string & parameterName,
122  const unsigned int entry_nr,
123  const bool printThisErrorMessage,
124  std::string & errorMessage ) const
125  {
127  errorMessage = "";
128 
130  std::size_t numberOfEntries = this->CountNumberOfParameterEntries(
131  parameterName );
132 
134  if ( numberOfEntries == 0 )
135  {
136  std::stringstream ss;
137  ss << "WARNING: The parameter \"" << parameterName
138  << "\", requested at entry number " << entry_nr
139  << ", does not exist at all.\n"
140  << " The default value \"" << parameterValue
141  << "\" is used instead." << std::endl;
142  if ( printThisErrorMessage && this->m_PrintErrorMessages )
143  {
144  errorMessage = ss.str();
145  }
146 
147  return false;
148  }
149 
151  const ParameterValuesType & vec = this->m_ParameterMap.find( parameterName )->second;
152 
154  if ( entry_nr >= numberOfEntries )
155  {
156  std::stringstream ss;
157  ss << "WARNING: The parameter \"" << parameterName
158  << "\" does not exist at entry number " << entry_nr
159  << ".\n The default value \"" << parameterValue
160  << "\" is used instead." << std::endl;
161  if ( printThisErrorMessage && this->m_PrintErrorMessages )
162  {
163  errorMessage = ss.str();
164  }
165  return false;
166  }
167 
169  bool castSuccesful = this->StringCast( vec[ entry_nr ], parameterValue );
170 
172  if ( !castSuccesful )
173  {
174  std::stringstream ss;
175  ss << "ERROR: Casting entry number " << entry_nr
176  << " for the parameter \"" << parameterName
177  << "\" failed!\n"
178  << " You tried to cast \"" << vec[ entry_nr ]
179  << "\" from std::string to "
180  << typeid( parameterValue ).name() << std::endl;
181 
182  itkExceptionMacro( << ss.str() );
183  }
184 
185  return true;
186 
187  } // end ReadParameter()
188 
190  bool ReadParameter( bool & parameterValue,
191  const std::string & parameterName,
192  const unsigned int entry_nr,
193  const bool printThisErrorMessage,
194  std::string & errorMessage ) const;
195 
199  template <class T>
200  bool ReadParameter( T & parameterValue,
201  const std::string & parameterName,
202  const unsigned int entry_nr,
203  std::string & errorMessage ) const
204  {
205  return this->ReadParameter( parameterValue, parameterName, entry_nr,
206  true, errorMessage );
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 
266  template <class T>
267  bool ReadParameter( T & parameterValue,
268  const std::string & parameterName,
269  const std::string & prefix,
270  const unsigned int entry_nr,
271  const unsigned int default_entry_nr,
272  std::string & errorMessage ) const
273  {
274  return this->ReadParameter( parameterValue, parameterName, prefix,
275  entry_nr, default_entry_nr, true, errorMessage );
276  }
277 
279  template <class T>
281  std::vector< T > & parameterValues,
282  const std::string & parameterName,
283  const unsigned int entry_nr_start,
284  const unsigned int entry_nr_end,
285  const bool printThisErrorMessage,
286  std::string & errorMessage ) const
287  {
289  errorMessage = "";
290 
292  std::size_t numberOfEntries = this->CountNumberOfParameterEntries(
293  parameterName );
294 
296  if ( numberOfEntries == 0 )
297  {
298  std::stringstream ss;
299  ss << "WARNING: The parameter \"" << parameterName
300  << "\", requested between entry numbers " << entry_nr_start
301  << " and " << entry_nr_end
302  << ", does not exist at all.\n"
303  << " The default values are used instead." << std::endl;
304  if ( printThisErrorMessage && this->m_PrintErrorMessages )
305  {
306  errorMessage = ss.str();
307  }
308  return false;
309  }
310 
312  if ( entry_nr_start > entry_nr_end )
313  {
314  std::stringstream ss;
315  ss << "WARNING: The entry number start (" << entry_nr_start
316  << ") should be smaller than entry number end (" << entry_nr_end
317  << "). It was requested for parameter \"" << parameterName
318  << "\"." << std::endl;
319 
321  itkExceptionMacro( << ss.str() );
322  }
323 
325  if ( entry_nr_end >= numberOfEntries )
326  {
327  std::stringstream ss;
328  ss << "WARNING: The parameter \"" << parameterName
329  << "\" does not exist at entry number " << entry_nr_end
330  << ".\nThe default value \"" << itk::NumericTraits<T>::Zero
331  << "\" is used instead." << std::endl;
332  itkExceptionMacro( << ss.str() );
333  }
334 
336  const ParameterValuesType & vec = this->m_ParameterMap.find( parameterName )->second;
337 
345  unsigned int j = 0;
346  for ( unsigned int i = entry_nr_start; i < entry_nr_end + 1; ++i )
347  {
349  bool castSuccesful = this->StringCast( vec[ i ], parameterValues[ j ] );
350  j++;
351 
353  if ( !castSuccesful )
354  {
355  std::stringstream ss;
356  ss << "ERROR: Casting entry number " << i
357  << " for the parameter \"" << parameterName
358  << "\" failed!\n"
359  << " You tried to cast \"" << vec[ i ]
360  << "\" from std::string to "
361  << typeid( parameterValues[ 0 ] ).name() << std::endl;
362 
363  itkExceptionMacro( << ss.str() );
364  }
365  }
366 
367  return true;
368  }
369 
371  bool ReadParameter(
372  std::vector<std::string> & parameterValues,
373  const std::string & parameterName,
374  const unsigned int entry_nr_start,
375  const unsigned int entry_nr_end,
376  const bool printThisErrorMessage,
377  std::string & errorMessage ) const;
378 
379 protected:
381  virtual ~ParameterMapInterface();
382 
383 private:
384  ParameterMapInterface(const Self&); // purposely not implemented
385  void operator=(const Self&); // purposely not implemented
386 
389 
391 
396  template <class T>
397  bool StringCast( const std::string & parameterValue, T & casted ) const
398  {
399  std::stringstream ss( parameterValue );
400  ss >> casted;
401  if ( ss.bad() || ss.fail() )
402  {
403  return false;
404  }
405  return true;
406 
407  } // end StringCast()
408 
412  bool StringCast( const std::string & parameterValue, std::string & casted ) const;
413 
414 }; // end class ParameterMapInterface
415 
416 } // end of namespace itk
417 
418 #endif // end __itkParameterMapInterface_h


Generated on 21-03-2014 for elastix by doxygen 1.8.1.2 elastix logo