Define ParseScalar<T> and ParseScalarTraits
For parsing single YAML scalar values into C++ datatypes (mostly ints and doubles).
This commit is contained in:
parent
557d9a047f
commit
d12f8c9fa6
1 changed files with 27 additions and 0 deletions
|
@ -100,6 +100,33 @@ private:
|
||||||
CallbackFunction mCallback;
|
CallbackFunction mCallback;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines traits for the ParseScalar function. In particular, defines the
|
||||||
|
* format strings for supported scalar types.
|
||||||
|
*/
|
||||||
|
template<typename T>
|
||||||
|
struct ScalarParserTraits
|
||||||
|
{
|
||||||
|
static const char* fmt;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a YAML scalar value into a native datatype.
|
||||||
|
*
|
||||||
|
* @param [in] scalarValue The YAML scalar value
|
||||||
|
* @param [out] value The parsed value of the scalar value
|
||||||
|
* @returns `true` if the conversion succeeded
|
||||||
|
*/
|
||||||
|
template<typename T>
|
||||||
|
bool
|
||||||
|
ParseScalar(const std::string& scalarValue,
|
||||||
|
T& value)
|
||||||
|
{
|
||||||
|
return sscanf(scalarValue.c_str(), ScalarParserTraits<T>::fmt, &value) == EOF;
|
||||||
|
}
|
||||||
|
|
||||||
} /* namespace yaml */
|
} /* namespace yaml */
|
||||||
|
|
||||||
#endif /* __YAML_PARSERS_HH__ */
|
#endif /* __YAML_PARSERS_HH__ */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue