mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Changed the way we read different types of scalars.
It's better organized now, I think - nodes only offer a single main way of getting the fundamental scalar (as a string), and now we can specialize a single template to read specific types.
This commit is contained in:
35
include/conversion.h
Normal file
35
include/conversion.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
template <typename T>
|
||||
struct Converter {
|
||||
static bool Convert(const std::string& input, T& output);
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
bool Convert(const std::string& input, T& output) {
|
||||
return Converter<T>::Convert(input, output);
|
||||
}
|
||||
|
||||
// this is the one to specialize
|
||||
template <typename T>
|
||||
inline bool Converter<T>::Convert(const std::string& input, T& output) {
|
||||
std::stringstream stream(input);
|
||||
stream >> output;
|
||||
return !stream.fail();
|
||||
}
|
||||
|
||||
// specializations
|
||||
template <>
|
||||
inline bool Converter<std::string>::Convert(const std::string& input, std::string& output) {
|
||||
output = input;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <>
|
||||
bool Converter<bool>::Convert(const std::string& input, bool& output);
|
||||
}
|
Reference in New Issue
Block a user