From 8dcd96dbd2cd240fa481fbfa17533e8c5ba29531 Mon Sep 17 00:00:00 2001 From: beder Date: Tue, 13 Sep 2011 14:00:47 -0500 Subject: [PATCH] Added tags to Node --- include/yaml-cpp/node/convert.h | 4 ++-- include/yaml-cpp/node/detail/node.h | 5 +++++ include/yaml-cpp/node/detail/node_data.h | 3 +++ include/yaml-cpp/node/detail/node_ref.h | 2 ++ include/yaml-cpp/node/impl.h | 15 +++++++++++++-- include/yaml-cpp/node/node.h | 8 +++++--- src/node/detail/node_data.cpp | 5 +++++ 7 files changed, 35 insertions(+), 7 deletions(-) diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index db3cb8c..97b1e13 100644 --- a/include/yaml-cpp/node/convert.h +++ b/include/yaml-cpp/node/convert.h @@ -26,7 +26,7 @@ namespace YAML static bool decode(const Node& node, std::string& rhs) { if(node.Type() != NodeType::Scalar) return false; - rhs = node.scalar(); + rhs = node.Scalar(); return true; } }; @@ -54,7 +54,7 @@ namespace YAML static bool decode(const Node& node, type& rhs) {\ if(node.Type() != NodeType::Scalar)\ return false;\ - std::stringstream stream(node.scalar());\ + std::stringstream stream(node.Scalar());\ stream >> rhs;\ return !!stream;\ }\ diff --git a/include/yaml-cpp/node/detail/node.h b/include/yaml-cpp/node/detail/node.h index 41f7c51..8dc84ad 100644 --- a/include/yaml-cpp/node/detail/node.h +++ b/include/yaml-cpp/node/detail/node.h @@ -29,6 +29,7 @@ namespace YAML NodeType::value type() const { return m_pRef->type(); } const std::string& scalar() const { return m_pRef->scalar(); } + const std::string& tag() const { return m_pRef->tag(); } void mark_defined() { if(is_defined()) @@ -71,6 +72,10 @@ namespace YAML mark_defined(); m_pRef->set_scalar(scalar); } + void set_tag(const std::string& tag) { + mark_defined(); + m_pRef->set_tag(tag); + } // size/iterator std::size_t size() const { return m_pRef->size(); } diff --git a/include/yaml-cpp/node/detail/node_data.h b/include/yaml-cpp/node/detail/node_data.h index aab8fc0..bd951f1 100644 --- a/include/yaml-cpp/node/detail/node_data.h +++ b/include/yaml-cpp/node/detail/node_data.h @@ -26,12 +26,14 @@ namespace YAML void mark_defined(); void set_type(NodeType::value type); + void set_tag(const std::string& tag); void set_null(); void set_scalar(const std::string& scalar); bool is_defined() const { return m_isDefined; } NodeType::value type() const { return m_isDefined ? m_type : NodeType::Undefined; } const std::string& scalar() const { return m_scalar; } + const std::string& tag() const { return m_tag; } // size/iterator std::size_t size() const; @@ -78,6 +80,7 @@ namespace YAML private: bool m_isDefined; NodeType::value m_type; + std::string m_tag; // scalar std::string m_scalar; diff --git a/include/yaml-cpp/node/detail/node_ref.h b/include/yaml-cpp/node/detail/node_ref.h index 4929547..5b39661 100644 --- a/include/yaml-cpp/node/detail/node_ref.h +++ b/include/yaml-cpp/node/detail/node_ref.h @@ -24,11 +24,13 @@ namespace YAML bool is_defined() const { return m_pData->is_defined(); } NodeType::value type() const { return m_pData->type(); } const std::string& scalar() const { return m_pData->scalar(); } + const std::string& tag() const { return m_pData->tag(); } void mark_defined() { m_pData->mark_defined(); } void set_data(const node_ref& rhs) { m_pData = rhs.m_pData; } void set_type(NodeType::value type) { m_pData->set_type(type); } + void set_tag(const std::string& tag) { m_pData->set_tag(tag); } void set_null() { m_pData->set_null(); } void set_scalar(const std::string& scalar) { m_pData->set_scalar(scalar); } diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h index 5430b9d..ac4089f 100644 --- a/include/yaml-cpp/node/impl.h +++ b/include/yaml-cpp/node/impl.h @@ -73,14 +73,25 @@ namespace YAML { if(Type() != NodeType::Scalar) throw std::runtime_error("Unable to convert to string, not a scalar"); - return scalar(); + return Scalar(); } - inline const std::string& Node::scalar() const + inline const std::string& Node::Scalar() const { return m_pNode ? m_pNode->scalar() : detail::node_data::empty_scalar; } + inline const std::string& Node::Tag() const + { + return m_pNode ? m_pNode->tag() : detail::node_data::empty_scalar; + } + + inline void Node::SetTag(const std::string& tag) + { + EnsureNodeExists(); + m_pNode->set_tag(tag); + } + // assignment inline bool Node::is(const Node& rhs) const { diff --git a/include/yaml-cpp/node/node.h b/include/yaml-cpp/node/node.h index 47a1db4..6420fa4 100644 --- a/include/yaml-cpp/node/node.h +++ b/include/yaml-cpp/node/node.h @@ -32,13 +32,15 @@ namespace YAML // access template const T as() const; - const std::string& scalar() const; - + const std::string& Scalar() const; + const std::string& Tag() const; + void SetTag(const std::string& tag); + // assignment bool is(const Node& rhs) const; template Node& operator=(const T& rhs); Node& operator=(const Node& rhs); - + // size/iterator std::size_t size() const; diff --git a/src/node/detail/node_data.cpp b/src/node/detail/node_data.cpp index f5e8ea8..2af9931 100644 --- a/src/node/detail/node_data.cpp +++ b/src/node/detail/node_data.cpp @@ -54,6 +54,11 @@ namespace YAML } } + void node_data::set_tag(const std::string& tag) + { + m_tag = tag; + } + void node_data::set_null() { m_isDefined = true;