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  * Copyright UMC Utrecht and contributors
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 
19 #ifndef __itkParameterMapInterface_h
20 #define __itkParameterMapInterface_h
21 
22 #include "itkObject.h"
23 #include "itkObjectFactory.h"
24 #include "itkMacro.h"
25 #include "itkNumericTraits.h"
26 
27 #include "itkParameterFileParser.h"
28 
29 #include <iostream>
30 
31 namespace itk
32 {
33 
73 class ParameterMapInterface : public Object
74 {
75 public:
76 
79  typedef Object Superclass;
81  typedef SmartPointer< const Self > ConstPointer;
82 
84  itkNewMacro( Self );
85 
87  itkTypeMacro( ParameterMapInterface, Object );
88 
92 
94  void SetParameterMap( const ParameterMapType & parMap );
95 
99  // \todo: we could think of a warning level. (maybe you want warnings, but
100  // not when for example a parameter is not found at entry entry_nr but at entry 0 instead
101  itkSetMacro( PrintErrorMessages, bool );
102  itkGetConstMacro( PrintErrorMessages, bool );
103 
105  std::size_t CountNumberOfParameterEntries(
106  const std::string & parameterName ) const;
107 
122  template< class T >
123  bool ReadParameter( T & parameterValue,
124  const std::string & parameterName,
125  const unsigned int entry_nr,
126  const bool printThisErrorMessage,
127  std::string & errorMessage ) const
128  {
130  errorMessage = "";
131 
133  std::size_t numberOfEntries = this->CountNumberOfParameterEntries(
134  parameterName );
135 
137  if( numberOfEntries == 0 )
138  {
139  std::stringstream ss;
140  ss << "WARNING: The parameter \"" << parameterName
141  << "\", requested at entry number " << entry_nr
142  << ", does not exist at all.\n"
143  << " The default value \"" << parameterValue
144  << "\" is used instead." << std::endl;
145  if( printThisErrorMessage && this->m_PrintErrorMessages )
146  {
147  errorMessage = ss.str();
148  }
149 
150  return false;
151  }
152 
154  const ParameterValuesType & vec = this->m_ParameterMap.find( parameterName )->second;
155 
157  if( entry_nr >= numberOfEntries )
158  {
159  std::stringstream ss;
160  ss << "WARNING: The parameter \"" << parameterName
161  << "\" does not exist at entry number " << entry_nr
162  << ".\n The default value \"" << parameterValue
163  << "\" is used instead." << std::endl;
164  if( printThisErrorMessage && this->m_PrintErrorMessages )
165  {
166  errorMessage = ss.str();
167  }
168  return false;
169  }
170 
172  bool castSuccesful = this->StringCast( vec[ entry_nr ], parameterValue );
173 
175  if( !castSuccesful )
176  {
177  std::stringstream ss;
178  ss << "ERROR: Casting entry number " << entry_nr
179  << " for the parameter \"" << parameterName
180  << "\" failed!\n"
181  << " You tried to cast \"" << vec[ entry_nr ]
182  << "\" from std::string to "
183  << typeid( parameterValue ).name() << std::endl;
184 
185  itkExceptionMacro( << ss.str() );
186  }
187 
188  return true;
189 
190  } // end ReadParameter()
191 
192 
194  bool ReadParameter( bool & parameterValue,
195  const std::string & parameterName,
196  const unsigned int entry_nr,
197  const bool printThisErrorMessage,
198  std::string & errorMessage ) const;
199 
203  template< class T >
204  bool ReadParameter( T & parameterValue,
205  const std::string & parameterName,
206  const unsigned int entry_nr,
207  std::string & errorMessage ) const
208  {
209  return this->ReadParameter( parameterValue, parameterName, entry_nr,
210  true, errorMessage );
211  }
212 
213 
219  template< class T >
220  bool ReadParameter( T & parameterValue,
221  const std::string & parameterName,
222  const std::string & prefix,
223  const unsigned int entry_nr,
224  const int default_entry_nr,
225  const bool printThisErrorMessage,
226  std::string & errorMessage ) const
227  {
228  std::string fullname = prefix + parameterName;
229  bool found = false;
230 
232  std::string dummyString = "";
233  if( default_entry_nr >= 0 )
234  {
236  unsigned int uintdefault = static_cast< unsigned int >( default_entry_nr );
237  found |= this->ReadParameter( parameterValue, parameterName, uintdefault,
238  false, dummyString );
239  found |= this->ReadParameter( parameterValue, parameterName, entry_nr,
240  false, dummyString );
241  found |= this->ReadParameter( parameterValue, fullname, uintdefault,
242  false, dummyString );
243  found |= this->ReadParameter( parameterValue, fullname, entry_nr,
244  false, dummyString );
245  }
246  else
247  {
249  found |= this->ReadParameter( parameterValue, parameterName, entry_nr,
250  false, dummyString );
251  found |= this->ReadParameter( parameterValue, fullname, entry_nr,
252  false, dummyString );
253  }
254 
258  if( !found && printThisErrorMessage && this->m_PrintErrorMessages )
259  {
260  return this->ReadParameter( parameterValue, parameterName, entry_nr,
261  true, errorMessage );
262  }
263 
264  return found;
265 
266  }
267 
268 
272  template< class T >
273  bool ReadParameter( T & parameterValue,
274  const std::string & parameterName,
275  const std::string & prefix,
276  const unsigned int entry_nr,
277  const unsigned int default_entry_nr,
278  std::string & errorMessage ) const
279  {
280  return this->ReadParameter( parameterValue, parameterName, prefix,
281  entry_nr, default_entry_nr, true, errorMessage );
282  }
283 
284 
286  template< class T >
288  std::vector< T > & parameterValues,
289  const std::string & parameterName,
290  const unsigned int entry_nr_start,
291  const unsigned int entry_nr_end,
292  const bool printThisErrorMessage,
293  std::string & errorMessage ) const
294  {
296  errorMessage = "";
297 
299  std::size_t numberOfEntries = this->CountNumberOfParameterEntries(
300  parameterName );
301 
303  if( numberOfEntries == 0 )
304  {
305  std::stringstream ss;
306  ss << "WARNING: The parameter \"" << parameterName
307  << "\", requested between entry numbers " << entry_nr_start
308  << " and " << entry_nr_end
309  << ", does not exist at all.\n"
310  << " The default values are used instead." << std::endl;
311  if( printThisErrorMessage && this->m_PrintErrorMessages )
312  {
313  errorMessage = ss.str();
314  }
315  return false;
316  }
317 
319  if( entry_nr_start > entry_nr_end )
320  {
321  std::stringstream ss;
322  ss << "WARNING: The entry number start (" << entry_nr_start
323  << ") should be smaller than entry number end (" << entry_nr_end
324  << "). It was requested for parameter \"" << parameterName
325  << "\"." << std::endl;
326 
328  itkExceptionMacro( << ss.str() );
329  }
330 
332  if( entry_nr_end >= numberOfEntries )
333  {
334  std::stringstream ss;
335  ss << "WARNING: The parameter \"" << parameterName
336  << "\" does not exist at entry number " << entry_nr_end
337  << ".\nThe default value \"" << itk::NumericTraits< T >::Zero
338  << "\" is used instead." << std::endl;
339  itkExceptionMacro( << ss.str() );
340  }
341 
343  const ParameterValuesType & vec = this->m_ParameterMap.find( parameterName )->second;
344 
352  unsigned int j = 0;
353  for( unsigned int i = entry_nr_start; i < entry_nr_end + 1; ++i )
354  {
356  bool castSuccesful = this->StringCast( vec[ i ], parameterValues[ j ] );
357  j++;
358 
360  if( !castSuccesful )
361  {
362  std::stringstream ss;
363  ss << "ERROR: Casting entry number " << i
364  << " for the parameter \"" << parameterName
365  << "\" failed!\n"
366  << " You tried to cast \"" << vec[ i ]
367  << "\" from std::string to "
368  << typeid( parameterValues[ 0 ] ).name() << std::endl;
369 
370  itkExceptionMacro( << ss.str() );
371  }
372  }
373 
374  return true;
375  }
376 
377 
379  bool ReadParameter(
380  std::vector< std::string > & parameterValues,
381  const std::string & parameterName,
382  const unsigned int entry_nr_start,
383  const unsigned int entry_nr_end,
384  const bool printThisErrorMessage,
385  std::string & errorMessage ) const;
386 
387 protected:
388 
390  virtual ~ParameterMapInterface();
391 
392 private:
393 
394  ParameterMapInterface( const Self & ); // purposely not implemented
395  void operator=( const Self & ); // purposely not implemented
396 
398  ParameterMapType m_ParameterMap;
399 
401 
406  template< class T >
407  bool StringCast( const std::string & parameterValue, T & casted ) const
408  {
409  std::stringstream ss( parameterValue );
410 
415  typename NumericTraits< T >::AccumulateType tempCasted;
416  ss >> tempCasted;
417  casted = static_cast< T >( tempCasted );
418  if( ss.bad() || ss.fail() )
419  {
420  return false;
421  }
422  return true;
423 
424  } // end StringCast()
425 
426 
430  bool StringCast( const std::string & parameterValue, std::string & casted ) const;
431 
432 };
433 
434 } // end of namespace itk
435 
436 #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 04-09-2015 for elastix by doxygen 1.8.9.1 elastix logo