From 4770ec798c1930245b852dabc8b47f9bf272984f Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Sun, 11 Sep 2011 21:51:04 -0500 Subject: [PATCH] Implemented operator[] specialization, but only const (should the sequence be mutable?) --- include/yaml-cpp/node/detail/impl.h | 25 ++++++++++++++++--------- test/new-api/nodetests.cpp | 20 ++++++++++++++++++-- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/include/yaml-cpp/node/detail/impl.h b/include/yaml-cpp/node/detail/impl.h index 752ea03..f8220d3 100644 --- a/include/yaml-cpp/node/detail/impl.h +++ b/include/yaml-cpp/node/detail/impl.h @@ -8,29 +8,36 @@ #include "yaml-cpp/node/detail/node.h" #include "yaml-cpp/node/detail/node_data.h" +#include namespace YAML { namespace detail { template - struct get_idx_helper { - static node *get(const Key& /* key */, shared_memory_holder /* pMemory */) { + struct get_idx { + static node *get(const std::vector& /* sequence */, const Key& /* key */, shared_memory_holder /* pMemory */) { return 0; } }; template - struct get_idx_helper >::type> { - static node *get(const Key& key, shared_memory_holder pMemory) { + struct get_idx >::type> { + static node *get(const std::vector& sequence, const Key& key, shared_memory_holder pMemory) { + if(key < sequence.size()) + return sequence[key]; return 0; } }; template - inline node *get_idx(const Key& key, shared_memory_holder pMemory) { - return get_idx_helper::get(key, pMemory); - } + struct get_idx >::type> { + static node *get(const std::vector& sequence, const Key& key, shared_memory_holder pMemory) { + if(key < 0) + return 0; + return get_idx::get(sequence, static_cast(key), pMemory); + } + }; // indexing template @@ -43,7 +50,7 @@ namespace YAML case NodeType::Null: return pMemory->create_node(); case NodeType::Sequence: - if(node *pNode = get_idx(key, pMemory)) + if(node *pNode = get_idx::get(m_sequence, key, pMemory)) return *pNode; return pMemory->create_node(); case NodeType::Scalar: @@ -67,7 +74,7 @@ namespace YAML case NodeType::Undefined: case NodeType::Null: case NodeType::Sequence: - if(node *pNode = get_idx(key, pMemory)) + if(node *pNode = get_idx::get(m_sequence, key, pMemory)) return *pNode; convert_to_map(pMemory); diff --git a/test/new-api/nodetests.cpp b/test/new-api/nodetests.cpp index 3293c28..0579b62 100644 --- a/test/new-api/nodetests.cpp +++ b/test/new-api/nodetests.cpp @@ -34,7 +34,7 @@ namespace Test return true; } - TEST SimpleSequence() + TEST SimpleAppendSequence() { YAML::Node node; node.append(10); @@ -49,6 +49,21 @@ namespace Test return true; } + TEST SimpleAssignSequence() + { + YAML::Node node; + node[0] = 10; + node[1] = "foo"; + node[2] = "monkey"; + YAML_ASSERT(node.Type() == YAML::NodeType::Sequence); + YAML_ASSERT(node.size() == 3); + YAML_ASSERT(node[0].as() == 10); + YAML_ASSERT(node[1].as() == "foo"); + YAML_ASSERT(node[2].as() == "monkey"); + YAML_ASSERT(node.Type() == YAML::NodeType::Sequence); + return true; + } + TEST SimpleMap() { YAML::Node node; @@ -113,7 +128,8 @@ namespace Test RunNodeTest(&Node::SimpleScalar, "simple scalar", passed, total); RunNodeTest(&Node::IntScalar, "int scalar", passed, total); - RunNodeTest(&Node::SimpleSequence, "simple sequence", passed, total); + RunNodeTest(&Node::SimpleAppendSequence, "simple append sequence", passed, total); + RunNodeTest(&Node::SimpleAssignSequence, "simple assign sequence", passed, total); RunNodeTest(&Node::SimpleMap, "simple map", passed, total); RunNodeTest(&Node::MapWithUndefinedValues, "map with undefined values", passed, total); RunNodeTest(&Node::MapIteratorWithUndefinedValues, "map iterator with undefined values", passed, total);