diff --git a/include/yaml-cpp/value.h b/include/yaml-cpp/value.h index 87587e9..0b00c91 100644 --- a/include/yaml-cpp/value.h +++ b/include/yaml-cpp/value.h @@ -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 diff --git a/include/yaml-cpp/value/convert.h b/include/yaml-cpp/value/convert.h new file mode 100644 index 0000000..55d9356 --- /dev/null +++ b/include/yaml-cpp/value/convert.h @@ -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 + +namespace YAML +{ + // std::string + template<> + struct convert { + 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 diff --git a/include/yaml-cpp/value/detail/impl.h b/include/yaml-cpp/value/detail/impl.h index 52a450b..c62a4da 100644 --- a/include/yaml-cpp/value/detail/impl.h +++ b/include/yaml-cpp/value/detail/impl.h @@ -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::decode(Value(pNode, pMemory), lhs)) return lhs == rhs; return false; } @@ -86,7 +86,7 @@ namespace YAML template inline shared_node node_data::convert_to_node(const T& rhs, detail::shared_memory_holder pMemory) { - Value value = convert(rhs); + Value value = convert::encode(rhs); pMemory->merge(*value.m_pMemory); return value.m_pNode; } diff --git a/include/yaml-cpp/value/impl.h b/include/yaml-cpp/value/impl.h index 00a58f2..8b27660 100644 --- a/include/yaml-cpp/value/impl.h +++ b/include/yaml-cpp/value/impl.h @@ -51,7 +51,7 @@ namespace YAML inline const T Value::as() const { T t; - if(convert(*this, t)) + if(convert::decode(*this, t)) return t; throw std::runtime_error("Unable to convert to type"); } @@ -85,7 +85,7 @@ namespace YAML template inline void Value::Assign(const T& rhs) { - AssignData(convert(rhs)); + AssignData(convert::encode(rhs)); } template<> diff --git a/include/yaml-cpp/value/value.h b/include/yaml-cpp/value/value.h index f0a53ba..f288d5e 100644 --- a/include/yaml-cpp/value/value.h +++ b/include/yaml-cpp/value/value.h @@ -83,10 +83,7 @@ namespace YAML bool is(const Value& lhs, const Value& rhs); template - Value convert(const T& rhs); - - template - bool convert(const Value& value, T& rhs); + struct convert; } #endif // VALUE_VALUE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/value/convert.cpp b/src/value/convert.cpp index 47cf659..586ac9a 100644 --- a/src/value/convert.cpp +++ b/src/value/convert.cpp @@ -1,51 +1,45 @@ -#include "yaml-cpp/value.h" -#include +#include "yaml-cpp/value/convert.h" namespace YAML { - // std::string - template<> - Value convert(const std::string& rhs) { - return Value(rhs); - } - - template<> - bool convert(const Value& value, std::string& rhs) { - if(value.Type() != ValueType::Scalar) - return false; - rhs = value.scalar(); - return true; - } -#define YAML_DEFINE_CONVERT_STREAMABLE(type)\ - template<> Value convert(const type& rhs) {\ - std::stringstream stream;\ - stream << rhs;\ - return Value(stream.str());\ - }\ - template<> bool convert(const Value& value, type& rhs) {\ - if(value.Type() != ValueType::Scalar)\ - return false;\ - std::stringstream stream(value.scalar());\ - stream >> rhs;\ - return !!stream;\ - } - - YAML_DEFINE_CONVERT_STREAMABLE(int) - YAML_DEFINE_CONVERT_STREAMABLE(unsigned) - YAML_DEFINE_CONVERT_STREAMABLE(short) - YAML_DEFINE_CONVERT_STREAMABLE(unsigned short) - YAML_DEFINE_CONVERT_STREAMABLE(long) - YAML_DEFINE_CONVERT_STREAMABLE(unsigned long) - YAML_DEFINE_CONVERT_STREAMABLE(long long) - YAML_DEFINE_CONVERT_STREAMABLE(unsigned long long) - - YAML_DEFINE_CONVERT_STREAMABLE(char) - YAML_DEFINE_CONVERT_STREAMABLE(unsigned char) - - YAML_DEFINE_CONVERT_STREAMABLE(float) - YAML_DEFINE_CONVERT_STREAMABLE(double) - YAML_DEFINE_CONVERT_STREAMABLE(long double) - -#undef YAML_DEFINE_CONVERT_STREAMABLE +//#define YAML_DEFINE_CONVERT_STREAMABLE(type)\ +// template<> Value convert(const type& rhs) {\ +// std::stringstream stream;\ +// stream << rhs;\ +// return Value(stream.str());\ +// }\ +// template<> bool convert(const Value& value, type& rhs) {\ +// if(value.Type() != ValueType::Scalar)\ +// return false;\ +// std::stringstream stream(value.scalar());\ +// stream >> rhs;\ +// return !!stream;\ +// } +// +// YAML_DEFINE_CONVERT_STREAMABLE(int) +// YAML_DEFINE_CONVERT_STREAMABLE(unsigned) +// YAML_DEFINE_CONVERT_STREAMABLE(short) +// YAML_DEFINE_CONVERT_STREAMABLE(unsigned short) +// YAML_DEFINE_CONVERT_STREAMABLE(long) +// YAML_DEFINE_CONVERT_STREAMABLE(unsigned long) +// YAML_DEFINE_CONVERT_STREAMABLE(long long) +// YAML_DEFINE_CONVERT_STREAMABLE(unsigned long long) +// +// YAML_DEFINE_CONVERT_STREAMABLE(char) +// YAML_DEFINE_CONVERT_STREAMABLE(unsigned char) +// +// YAML_DEFINE_CONVERT_STREAMABLE(float) +// YAML_DEFINE_CONVERT_STREAMABLE(double) +// YAML_DEFINE_CONVERT_STREAMABLE(long double) +// +//#undef YAML_DEFINE_CONVERT_STREAMABLE +// +// template +// Value convert >(const std::map& rhs) { +// Value value(ValueType::Map); +// for(std::map::const_iterator it=rhs.begin();it!=rhs.end();++it) +// value[it->first] = it->second; +// return value; +// } } diff --git a/util/value.cpp b/util/value.cpp index 48fcfe1..adcabb2 100644 --- a/util/value.cpp +++ b/util/value.cpp @@ -7,10 +7,10 @@ int main() std::cout << value["key"].as() << "\n"; value["key"]["key"] = "value"; std::cout << value["key"]["key"].as() << "\n"; - value[5] = "monkey"; - std::cout << value[5].as() << "\n"; - value["monkey"] = 5; - std::cout << value["monkey"].as() << "\n"; +// value[5] = "monkey"; +// std::cout << value[5].as() << "\n"; +// value["monkey"] = 5; +// std::cout << value["monkey"].as() << "\n"; return 0; }