mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Switched convert to a templated struct that can be specialized (so we can partially specialize it)
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "yaml-cpp/value/value.h"
|
||||
#include "yaml-cpp/value/impl.h"
|
||||
#include "yaml-cpp/value/convert.h"
|
||||
#include "yaml-cpp/value/detail/impl.h"
|
||||
|
||||
#endif // VALUE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
30
include/yaml-cpp/value/convert.h
Normal file
30
include/yaml-cpp/value/convert.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef VALUE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define VALUE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
|
||||
#include "yaml-cpp/value/value.h"
|
||||
#include <sstream>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
// std::string
|
||||
template<>
|
||||
struct convert<std::string> {
|
||||
static Value encode(const std::string& rhs) {
|
||||
return Value(rhs);
|
||||
}
|
||||
|
||||
static bool decode(const Value& value, std::string& rhs) {
|
||||
if(value.Type() != ValueType::Scalar)
|
||||
return false;
|
||||
rhs = value.scalar();
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // VALUE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
@@ -78,7 +78,7 @@ namespace YAML
|
||||
inline bool node_data::equals(detail::shared_node pNode, const T& rhs, detail::shared_memory_holder pMemory)
|
||||
{
|
||||
T lhs;
|
||||
if(convert(Value(pNode, pMemory), lhs))
|
||||
if(convert<T>::decode(Value(pNode, pMemory), lhs))
|
||||
return lhs == rhs;
|
||||
return false;
|
||||
}
|
||||
@@ -86,7 +86,7 @@ namespace YAML
|
||||
template<typename T>
|
||||
inline shared_node node_data::convert_to_node(const T& rhs, detail::shared_memory_holder pMemory)
|
||||
{
|
||||
Value value = convert(rhs);
|
||||
Value value = convert<T>::encode(rhs);
|
||||
pMemory->merge(*value.m_pMemory);
|
||||
return value.m_pNode;
|
||||
}
|
||||
|
@@ -51,7 +51,7 @@ namespace YAML
|
||||
inline const T Value::as() const
|
||||
{
|
||||
T t;
|
||||
if(convert<T>(*this, t))
|
||||
if(convert<T>::decode(*this, t))
|
||||
return t;
|
||||
throw std::runtime_error("Unable to convert to type");
|
||||
}
|
||||
@@ -85,7 +85,7 @@ namespace YAML
|
||||
template<typename T>
|
||||
inline void Value::Assign(const T& rhs)
|
||||
{
|
||||
AssignData(convert<T>(rhs));
|
||||
AssignData(convert<T>::encode(rhs));
|
||||
}
|
||||
|
||||
template<>
|
||||
|
@@ -83,10 +83,7 @@ namespace YAML
|
||||
bool is(const Value& lhs, const Value& rhs);
|
||||
|
||||
template<typename T>
|
||||
Value convert(const T& rhs);
|
||||
|
||||
template<typename T>
|
||||
bool convert(const Value& value, T& rhs);
|
||||
struct convert;
|
||||
}
|
||||
|
||||
#endif // VALUE_VALUE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
Reference in New Issue
Block a user