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);
|
||||
}
|
@@ -7,6 +7,7 @@
|
||||
#include "parserstate.h"
|
||||
#include "exceptions.h"
|
||||
#include "iterator.h"
|
||||
#include "conversion.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
@@ -37,18 +38,11 @@ namespace YAML
|
||||
unsigned size() const;
|
||||
|
||||
// extraction of scalars
|
||||
bool Read(std::string& s) const;
|
||||
bool Read(int& i) const;
|
||||
bool Read(unsigned& u) const;
|
||||
bool Read(long& l) const;
|
||||
bool Read(float& f) const;
|
||||
bool Read(double& d) const;
|
||||
bool Read(char& c) const;
|
||||
bool Read(bool& b) const;
|
||||
bool GetScalar(std::string& s) const;
|
||||
|
||||
// so you can specialize for other values
|
||||
// we can specialize this for other values
|
||||
template <typename T>
|
||||
friend bool Read(const Node& node, T& value);
|
||||
bool Read(T& value) const;
|
||||
|
||||
template <typename T>
|
||||
friend void operator >> (const Node& node, T& value);
|
||||
@@ -100,15 +94,18 @@ namespace YAML
|
||||
|
||||
// templated things we need to keep inline in the header
|
||||
template <typename T>
|
||||
inline bool Read(const Node& node, T& value)
|
||||
{
|
||||
return node.Read(value);
|
||||
inline bool Node::Read(T& value) const {
|
||||
std::string scalar;
|
||||
if(!GetScalar(scalar))
|
||||
return false;
|
||||
|
||||
return Convert(scalar, value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void operator >> (const Node& node, T& value)
|
||||
{
|
||||
if(!Read(node, value))
|
||||
if(!node.Read(value))
|
||||
throw InvalidScalar(node.m_line, node.m_column);
|
||||
}
|
||||
|
||||
@@ -120,7 +117,7 @@ namespace YAML
|
||||
|
||||
for(Iterator it=begin();it!=end();++it) {
|
||||
T t;
|
||||
if(YAML::Read(it.first(), t)) {
|
||||
if(it.first().Read(t)) {
|
||||
if(key == t)
|
||||
return it.second();
|
||||
}
|
||||
|
Reference in New Issue
Block a user