From a95baeafd6556cb2eba346b7877c46dc89a321ab Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Wed, 7 Sep 2011 15:49:01 -0500 Subject: [PATCH] Implemented conversion for std::string, including a bypass-accessor to the scalar value --- include/yaml-cpp/value/detail/node.h | 5 +++-- include/yaml-cpp/value/detail/node_data.h | 2 +- include/yaml-cpp/value/detail/node_ref.h | 1 + include/yaml-cpp/value/impl.h | 13 +++++++++++++ include/yaml-cpp/value/value.h | 1 + src/value/convert.cpp | 17 +++++++++++++++++ util/value.cpp | 5 +++++ 7 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 src/value/convert.cpp diff --git a/include/yaml-cpp/value/detail/node.h b/include/yaml-cpp/value/detail/node.h index 97b3c3e..2521df9 100644 --- a/include/yaml-cpp/value/detail/node.h +++ b/include/yaml-cpp/value/detail/node.h @@ -20,9 +20,10 @@ namespace YAML public: explicit node(shared_node_ref pRef): m_pRef(pRef) {} - ValueType::value type() const { return m_pRef->type(); } - bool is(const node& rhs) const { return m_pRef == rhs.m_pRef; } + ValueType::value type() const { return m_pRef->type(); } + + const std::string& scalar() const { return m_pRef->scalar(); } void set_ref(const node& rhs) { m_pRef = rhs.m_pRef; } void set_data(const node& rhs) { m_pRef->set_data(*rhs.m_pRef); } diff --git a/include/yaml-cpp/value/detail/node_data.h b/include/yaml-cpp/value/detail/node_data.h index 7fe8405..db1a341 100644 --- a/include/yaml-cpp/value/detail/node_data.h +++ b/include/yaml-cpp/value/detail/node_data.h @@ -27,7 +27,7 @@ namespace YAML void set_scalar(const std::string& scalar); ValueType::value type() const { return m_isDefined ? m_type : ValueType::Undefined; } - const std::string scalar() const { return m_scalar; } + const std::string& scalar() const { return m_scalar; } // indexing template shared_node get(const Key& key, shared_memory_holder pMemory) const; diff --git a/include/yaml-cpp/value/detail/node_ref.h b/include/yaml-cpp/value/detail/node_ref.h index 6c34042..6c81569 100644 --- a/include/yaml-cpp/value/detail/node_ref.h +++ b/include/yaml-cpp/value/detail/node_ref.h @@ -21,6 +21,7 @@ namespace YAML node_ref(): m_pData(new node_data) {} ValueType::value type() const { return m_pData->type(); } + const std::string& scalar() const { return m_pData->scalar(); } void set_data(const node_ref& rhs) { m_pData = rhs.m_pData; } diff --git a/include/yaml-cpp/value/impl.h b/include/yaml-cpp/value/impl.h index c4625f8..00a58f2 100644 --- a/include/yaml-cpp/value/impl.h +++ b/include/yaml-cpp/value/impl.h @@ -56,6 +56,19 @@ namespace YAML throw std::runtime_error("Unable to convert to type"); } + template<> + inline const std::string Value::as() const + { + if(Type() != ValueType::Scalar) + throw std::runtime_error("Unable to convert to string, not a scalar"); + return scalar(); + } + + inline const std::string& Value::scalar() const + { + return m_pNode->scalar(); + } + // assignment inline bool Value::is(const Value& rhs) const { diff --git a/include/yaml-cpp/value/value.h b/include/yaml-cpp/value/value.h index 039dfe2..f0a53ba 100644 --- a/include/yaml-cpp/value/value.h +++ b/include/yaml-cpp/value/value.h @@ -29,6 +29,7 @@ namespace YAML // access template const T as() const; + const std::string& scalar() const; // assignment bool is(const Value& rhs) const; diff --git a/src/value/convert.cpp b/src/value/convert.cpp new file mode 100644 index 0000000..8bfd9cb --- /dev/null +++ b/src/value/convert.cpp @@ -0,0 +1,17 @@ +#include "yaml-cpp/value.h" + +namespace YAML +{ + 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; + } +} diff --git a/util/value.cpp b/util/value.cpp index e8f57a8..136ca45 100644 --- a/util/value.cpp +++ b/util/value.cpp @@ -4,6 +4,11 @@ int main() { YAML::Value value; value["key"] = "value"; + 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"; return 0; }