From a645866fface7eeb88c94569b2d4a5e5f6c055fe Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Wed, 31 Oct 2012 18:55:07 -0500 Subject: [PATCH] Simplified Node::operator[] interface by removing the C-string overloads, using a helper to_value --- include/yaml-cpp/node/impl.h | 83 +++++++++++++++++++++--------------- include/yaml-cpp/node/node.h | 8 ---- 2 files changed, 49 insertions(+), 42 deletions(-) diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h index 65c0e1d..3b466d5 100644 --- a/include/yaml-cpp/node/impl.h +++ b/include/yaml-cpp/node/impl.h @@ -123,7 +123,7 @@ namespace YAML return node.Scalar(); } }; - + // access functions template inline const T Node::as() const @@ -267,12 +267,57 @@ namespace YAML m_pMemory->merge(*rhs.m_pMemory); } + // helpers for indexing + namespace detail { + template + struct to_value_t { + explicit to_value_t(const T& t_): t(t_) {} + const T& t; + typedef const T& return_type; + + const T& operator()() const { return t; } + }; + + template<> + struct to_value_t { + explicit to_value_t(const char *t_): t(t_) {} + const char *t; + typedef std::string return_type; + + const std::string operator()() const { return t; } + }; + + template<> + struct to_value_t { + explicit to_value_t(char *t_): t(t_) {} + const char *t; + typedef std::string return_type; + + const std::string operator()() const { return t; } + }; + + template + struct to_value_t { + explicit to_value_t(const char *t_): t(t_) {} + const char *t; + typedef std::string return_type; + + const std::string operator()() const { return t; } + }; + + // converts C-strings to std::strings so they can be copied + template + inline typename to_value_t::return_type to_value(const T& t) { + return to_value_t(t)(); + } + } + // indexing template inline const Node Node::operator[](const Key& key) const { EnsureNodeExists(); - detail::node& value = static_cast(*m_pNode).get(key, m_pMemory); + detail::node& value = static_cast(*m_pNode).get(detail::to_value(key), m_pMemory); return Node(value, m_pMemory); } @@ -280,7 +325,7 @@ namespace YAML inline Node Node::operator[](const Key& key) { EnsureNodeExists(); - detail::node& value = m_pNode->get(key, m_pMemory); + detail::node& value = m_pNode->get(detail::to_value(key), m_pMemory); return Node(value, m_pMemory); } @@ -288,7 +333,7 @@ namespace YAML inline bool Node::remove(const Key& key) { EnsureNodeExists(); - return m_pNode->remove(key, m_pMemory); + return m_pNode->remove(detail::to_value(key), m_pMemory); } inline const Node Node::operator[](const Node& key) const @@ -313,36 +358,6 @@ namespace YAML key.EnsureNodeExists(); return m_pNode->remove(*key.m_pNode, m_pMemory); } - - inline const Node Node::operator[](const char *key) const - { - return operator[](std::string(key)); - } - - inline Node Node::operator[](const char *key) - { - return operator[](std::string(key)); - } - - inline bool Node::remove(const char *key) - { - return remove(std::string(key)); - } - - inline const Node Node::operator[](char *key) const - { - return operator[](static_cast(key)); - } - - inline Node Node::operator[](char *key) - { - return operator[](static_cast(key)); - } - - inline bool Node::remove(char *key) - { - return remove(static_cast(key)); - } // free functions diff --git a/include/yaml-cpp/node/node.h b/include/yaml-cpp/node/node.h index bdcb36e..6fafd26 100644 --- a/include/yaml-cpp/node/node.h +++ b/include/yaml-cpp/node/node.h @@ -78,15 +78,7 @@ namespace YAML const Node operator[](const Node& key) const; Node operator[](const Node& key); bool remove(const Node& key); - - const Node operator[](const char *key) const; - Node operator[](const char *key); - bool remove(const char *key); - const Node operator[](char *key) const; - Node operator[](char *key); - bool remove(char *key); - private: explicit Node(detail::node& node, detail::shared_memory_holder pMemory);