mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Refactored the operator >> and Node::Read default functions, as well as the conversion functions, to more easily read new types as keys (this uncovered an error, in example 2.11 of the spec)
This commit is contained in:
@@ -10,36 +10,34 @@
|
||||
|
||||
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) {
|
||||
inline bool Convert(const std::string& input, std::string& output) {
|
||||
output = input;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <>
|
||||
bool Converter<bool>::Convert(const std::string& input, bool& output);
|
||||
|
||||
template <>
|
||||
bool Converter<_Null>::Convert(const std::string& input, _Null& output);
|
||||
bool Convert(const std::string& input, bool& output);
|
||||
bool Convert(const std::string& input, _Null& output);
|
||||
|
||||
#define YAML_MAKE_STREAM_CONVERT(type) \
|
||||
inline bool Convert(const std::string& input, type& output) { \
|
||||
std::stringstream stream(input); \
|
||||
stream >> output; \
|
||||
return !stream.fail(); \
|
||||
}
|
||||
|
||||
YAML_MAKE_STREAM_CONVERT(char)
|
||||
YAML_MAKE_STREAM_CONVERT(unsigned char)
|
||||
YAML_MAKE_STREAM_CONVERT(int)
|
||||
YAML_MAKE_STREAM_CONVERT(unsigned int)
|
||||
YAML_MAKE_STREAM_CONVERT(short)
|
||||
YAML_MAKE_STREAM_CONVERT(unsigned short)
|
||||
YAML_MAKE_STREAM_CONVERT(long)
|
||||
YAML_MAKE_STREAM_CONVERT(unsigned long)
|
||||
YAML_MAKE_STREAM_CONVERT(float)
|
||||
YAML_MAKE_STREAM_CONVERT(double)
|
||||
YAML_MAKE_STREAM_CONVERT(long double)
|
||||
|
||||
#undef YAML_MAKE_STREAM_CONVERT
|
||||
}
|
||||
|
||||
#endif // CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
@@ -133,5 +133,6 @@ namespace YAML
|
||||
}
|
||||
|
||||
#include "nodeimpl.h"
|
||||
#include "nodereadimpl.h"
|
||||
|
||||
#endif // NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
@@ -9,15 +9,6 @@
|
||||
namespace YAML
|
||||
{
|
||||
// implementation of templated things
|
||||
template <typename T>
|
||||
inline bool Node::Read(T& value) const {
|
||||
std::string scalar;
|
||||
if(!GetScalar(scalar))
|
||||
return false;
|
||||
|
||||
return Convert(scalar, value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline const T Node::Read() const {
|
||||
T value;
|
||||
@@ -32,7 +23,7 @@ namespace YAML
|
||||
|
||||
template <typename T>
|
||||
inline void operator >> (const Node& node, T& value) {
|
||||
if(!node.Read(value))
|
||||
if(!ConvertScalar(node, value))
|
||||
throw InvalidScalar(node.m_mark);
|
||||
}
|
||||
|
||||
|
65
include/nodereadimpl.h
Normal file
65
include/nodereadimpl.h
Normal file
@@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
// implementation for Node::Read
|
||||
// (the goal is to call ConvertScalar if we can, and fall back to operator >> if not)
|
||||
// thanks to litb from stackoverflow.com
|
||||
// http://stackoverflow.com/questions/1386183/how-to-call-a-templated-function-if-it-exists-and-something-else-otherwise/1386390#1386390
|
||||
|
||||
template<bool>
|
||||
struct read_impl;
|
||||
|
||||
// ConvertScalar available
|
||||
template<>
|
||||
struct read_impl<true> {
|
||||
template<typename T>
|
||||
static bool read(const Node& node, T& value) {
|
||||
return ConvertScalar(node, value);
|
||||
}
|
||||
};
|
||||
|
||||
// ConvertScalar not available
|
||||
template<>
|
||||
struct read_impl<false> {
|
||||
template<typename T>
|
||||
static bool read(const Node& node, T& value) {
|
||||
try {
|
||||
node >> value;
|
||||
} catch(const Exception&) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
namespace fallback {
|
||||
// sizeof > 1
|
||||
struct flag { char c[2]; };
|
||||
flag Convert(...);
|
||||
|
||||
char (& operator,(flag, flag) )[1];
|
||||
|
||||
template<typename T>
|
||||
void operator,(flag, T const&);
|
||||
|
||||
char (& operator,(char(&)[1], flag) )[1];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool Node::Read(T& value) const {
|
||||
using namespace fallback;
|
||||
|
||||
return read_impl<sizeof (fallback::flag(), Convert(std::string(), value), fallback::flag()) != 1>::read(*this, value);
|
||||
}
|
||||
|
||||
// the main conversion function
|
||||
template <typename T>
|
||||
inline bool ConvertScalar(const Node& node, T& value) {
|
||||
std::string scalar;
|
||||
if(!node.GetScalar(scalar))
|
||||
return false;
|
||||
|
||||
return Convert(scalar, value);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user