From 30ce2821983c55a5f2febc46630f75da752bd888 Mon Sep 17 00:00:00 2001 From: beder Date: Sun, 11 Sep 2011 22:51:49 -0500 Subject: [PATCH] Added mutable operator[] for integral types (you can only grow the sequence if you specify the *next* element) --- include/yaml-cpp/node/detail/impl.h | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/include/yaml-cpp/node/detail/impl.h b/include/yaml-cpp/node/detail/impl.h index f8220d3..eded94b 100644 --- a/include/yaml-cpp/node/detail/impl.h +++ b/include/yaml-cpp/node/detail/impl.h @@ -24,18 +24,25 @@ namespace YAML template 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; + return key < sequence.size() ? sequence[key] : 0; + } + + static node *get(std::vector& sequence, const Key& key, shared_memory_holder pMemory) { + if(key > sequence.size()) + return 0; + if(key == sequence.size()) + sequence.push_back(&pMemory->create_node()); + return sequence[key]; } }; template 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); + return key >= 0 ? get_idx::get(sequence, static_cast(key), pMemory) : 0; + } + static node *get(std::vector& sequence, const Key& key, shared_memory_holder pMemory) { + return key >= 0 ? get_idx::get(sequence, static_cast(key), pMemory) : 0; } }; @@ -74,8 +81,10 @@ namespace YAML case NodeType::Undefined: case NodeType::Null: case NodeType::Sequence: - if(node *pNode = get_idx::get(m_sequence, key, pMemory)) + if(node *pNode = get_idx::get(m_sequence, key, pMemory)) { + m_type = NodeType::Sequence; return *pNode; + } convert_to_map(pMemory); break;