19#ifndef itkParameterMapInterface_h
20#define itkParameterMapInterface_h
25#include "itkObjectFactory.h"
27#include "itkNumericTraits.h"
108 itkSetMacro(PrintErrorMessages,
bool);
109 itkGetConstMacro(PrintErrorMessages,
bool);
139 const std::string & parameterName,
140 const unsigned int entry_nr,
141 const bool produceWarningMessage,
142 std::string & warningMessage)
const
151 if (numberOfEntries == 0)
155 std::ostringstream outputStringStream;
156 outputStringStream <<
"WARNING: The parameter \"" << parameterName <<
"\", requested at entry number "
157 << entry_nr <<
", does not exist at all.\n"
158 <<
" The default value \"" << parameterValue <<
"\" is used instead.";
159 warningMessage = outputStringStream.str();
169 if (entry_nr >= numberOfEntries)
173 std::ostringstream outputStringStream;
174 outputStringStream <<
"WARNING: The parameter \"" << parameterName <<
"\" does not exist at entry number "
175 << entry_nr <<
".\n The default value \"" << parameterValue <<
"\" is used instead.";
176 warningMessage = outputStringStream.str();
187 itkExceptionMacro(
"ERROR: Casting entry number "
188 << entry_nr <<
" for the parameter \"" << parameterName <<
"\" failed!\n"
189 <<
" You tried to cast \"" << vec[entry_nr] <<
"\" from std::string to "
190 <<
typeid(parameterValue).name() <<
'\n');
201 const std::string & parameterName,
202 const unsigned int entry_nr,
203 const bool produceWarningMessage,
204 std::string & warningMessage)
const;
212 const std::string & parameterName,
213 const unsigned int entry_nr,
214 std::string & warningMessage)
const
216 return this->
ReadParameter(parameterValue, parameterName, entry_nr,
true, warningMessage);
228 const std::string & parameterName,
229 const std::string & prefix,
230 const unsigned int entry_nr,
231 const int default_entry_nr,
232 const bool produceWarningMessage,
233 std::string & warningMessage)
const
235 std::string fullname = prefix + parameterName;
239 std::string dummyString =
"";
240 if (default_entry_nr >= 0)
243 unsigned int uintdefault =
static_cast<unsigned int>(default_entry_nr);
244 found |= this->
ReadParameter(parameterValue, parameterName, uintdefault,
false, dummyString);
245 found |= this->
ReadParameter(parameterValue, parameterName, entry_nr,
false, dummyString);
246 found |= this->
ReadParameter(parameterValue, fullname, uintdefault,
false, dummyString);
247 found |= this->
ReadParameter(parameterValue, fullname, entry_nr,
false, dummyString);
252 found |= this->
ReadParameter(parameterValue, parameterName, entry_nr,
false, dummyString);
253 found |= this->
ReadParameter(parameterValue, fullname, entry_nr,
false, dummyString);
261 return this->
ReadParameter(parameterValue, parameterName, entry_nr,
true, warningMessage);
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 & warningMessage)
const
280 return this->
ReadParameter(parameterValue, parameterName, prefix, entry_nr, default_entry_nr,
true, warningMessage);
288 const std::string & parameterName,
289 const unsigned int entry_nr_start,
290 const unsigned int entry_nr_end,
291 const bool produceWarningMessage,
292 std::string & warningMessage)
const
301 if (numberOfEntries == 0)
305 std::ostringstream outputStringStream;
306 outputStringStream <<
"WARNING: The parameter \"" << parameterName <<
"\", requested between entry numbers "
307 << entry_nr_start <<
" and " << entry_nr_end <<
", does not exist at all.\n"
308 <<
" The default values are used instead.";
309 warningMessage = outputStringStream.str();
315 if (entry_nr_start > entry_nr_end)
318 itkExceptionMacro(
"WARNING: The entry number start ("
319 << entry_nr_start <<
") should be smaller than entry number end (" << entry_nr_end
320 <<
"). It was requested for parameter \"" << parameterName <<
"\".\n");
324 if (entry_nr_end >= numberOfEntries)
326 itkExceptionMacro(
"WARNING: The parameter \"" << parameterName <<
"\" does not exist at entry number "
327 << entry_nr_end <<
".\nThe default value \"" << T{}
328 <<
"\" is used instead.");
342 for (
unsigned int i = entry_nr_start; i < entry_nr_end + 1; ++i)
351 itkExceptionMacro(
"ERROR: Casting entry number "
352 << i <<
" for the parameter \"" << parameterName <<
"\" failed!\n"
353 <<
" You tried to cast \"" << vec[i] <<
"\" from std::string to "
354 <<
typeid(parameterValues[0]).name() <<
'\n');
365 const std::string & parameterName,
366 const unsigned int entry_nr_start,
367 const unsigned int entry_nr_end,
368 const bool produceWarningMessage,
369 std::string & warningMessage)
const;
373 std::vector<std::string>
377 return (found ==
m_ParameterMap.cend()) ? std::vector<std::string>{} : found->second;
384 template <
typename T>
385 std::unique_ptr<std::vector<T>>
393 std::vector<T> result;
394 result.reserve(found->second.size());
396 for (
const std::string & str : found->second)
402 result.push_back(value);
406 const auto entry_nr = &str - found->second.data();
407 itkExceptionMacro(
"Failed to cast parameter \"" << parameterName <<
"\" entry number " << entry_nr
408 <<
" value \"" << str <<
"\" to type \"" <<
typeid(T).name()
412 return std::make_unique<std::vector<T>>(std::move(result));
static bool StringToValue(const std::string &str, T &value)
std::map< std::string, ParameterValuesType > ParameterMapType
std::vector< std::string > ParameterValuesType
Implements functionality to get parameters from a parameter map.
ParameterFileParser::ParameterValuesType ParameterValuesType
bool ReadParameter(T ¶meterValue, const std::string ¶meterName, const unsigned int entry_nr, const bool produceWarningMessage, std::string &warningMessage) const
bool ReadParameter(bool ¶meterValue, const std::string ¶meterName, const unsigned int entry_nr, const bool produceWarningMessage, std::string &warningMessage) const
bool m_PrintErrorMessages
ParameterMapType m_ParameterMap
void SetParameterMap(const ParameterMapType &parMap)
ITK_DISALLOW_COPY_AND_MOVE(ParameterMapInterface)
~ParameterMapInterface() override
std::vector< std::string > GetValues(const std::string ¶meterName) const
bool HasParameter(const std::string ¶meterName) const
ParameterFileParser::ParameterMapType ParameterMapType
bool ReadParameter(T ¶meterValue, const std::string ¶meterName, const std::string &prefix, const unsigned int entry_nr, const int default_entry_nr, const bool produceWarningMessage, std::string &warningMessage) const
std::vcl_size_t CountNumberOfParameterEntries(const std::string ¶meterName) const
bool ReadParameter(std::vector< std::string > ¶meterValues, const std::string ¶meterName, const unsigned int entry_nr_start, const unsigned int entry_nr_end, const bool produceWarningMessage, std::string &warningMessage) const
bool ReadParameter(std::vector< T > ¶meterValues, const std::string ¶meterName, const unsigned int entry_nr_start, const unsigned int entry_nr_end, const bool produceWarningMessage, std::string &warningMessage) const
std::unique_ptr< std::vector< T > > RetrieveValues(const std::string ¶meterName) const
SmartPointer< const Self > ConstPointer
bool ReadParameter(T ¶meterValue, const std::string ¶meterName, const std::string &prefix, const unsigned int entry_nr, const unsigned int default_entry_nr, std::string &warningMessage) const
bool ReadParameter(T ¶meterValue, const std::string ¶meterName, const unsigned int entry_nr, std::string &warningMessage) const