From d1eca90216d34b1aae927eaadf105055111b5eb0 Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Wed, 7 Sep 2011 03:36:50 -0500 Subject: [PATCH] Implemented map get(), and it would work (I think) if we implemented convert() for strings --- include/yaml-cpp/value/detail/impl.h | 53 +++++++++++++++++++++++ include/yaml-cpp/value/detail/node_data.h | 6 +++ include/yaml-cpp/value/impl.h | 9 ---- include/yaml-cpp/value/value.h | 5 +-- 4 files changed, 61 insertions(+), 12 deletions(-) diff --git a/include/yaml-cpp/value/detail/impl.h b/include/yaml-cpp/value/detail/impl.h index c115cf4..e6847a7 100644 --- a/include/yaml-cpp/value/detail/impl.h +++ b/include/yaml-cpp/value/detail/impl.h @@ -31,11 +31,64 @@ namespace YAML template inline shared_node node_data::get(const Key& key, shared_memory_holder pMemory) { + // TODO: check if 'key' is index-like, and we're a sequence + + switch(m_type) { + case ValueType::Undefined: + case ValueType::Null: + case ValueType::Scalar: + m_type = ValueType::Map; + m_map.clear(); + break; + case ValueType::Sequence: + convert_sequence_to_map(); + break; + case ValueType::Map: + break; + } + + for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) { + if(equals(it->first, key, pMemory)) + return it->second; + } + + shared_node pKey = convert_to_node(key, pMemory); + shared_node pValue(new node); + m_map.push_back(kv_pair(pKey, pValue)); + return pValue; } template inline bool node_data::remove(const Key& key, shared_memory_holder pMemory) { + if(m_type != ValueType::Map) + return false; + + for(node_map::iterator it=m_map.begin();it!=m_map.end();++it) { + if(equals(it->first, key, pMemory)) { + m_map.erase(it); + return true; + } + } + + return false; + } + + template + inline bool node_data::equals(detail::shared_node pNode, const T& rhs, detail::shared_memory_holder pMemory) + { + T lhs; + if(convert(Value(pNode, pMemory), lhs)) + return lhs == rhs; + return false; + } + + template + inline shared_node node_data::convert_to_node(const T& rhs, detail::shared_memory_holder pMemory) + { + Value value = convert(rhs); + pMemory->merge(*value.m_pMemory); + return value.m_pNode; } } } diff --git a/include/yaml-cpp/value/detail/node_data.h b/include/yaml-cpp/value/detail/node_data.h index 0f5c9c9..9aa798e 100644 --- a/include/yaml-cpp/value/detail/node_data.h +++ b/include/yaml-cpp/value/detail/node_data.h @@ -40,6 +40,12 @@ namespace YAML private: void convert_sequence_to_map(); + + template + static bool equals(detail::shared_node pNode, const T& rhs, detail::shared_memory_holder pMemory); + + template + static shared_node convert_to_node(const T& rhs, detail::shared_memory_holder pMemory); private: bool m_isDefined; diff --git a/include/yaml-cpp/value/impl.h b/include/yaml-cpp/value/impl.h index f0a03c3..52f06f4 100644 --- a/include/yaml-cpp/value/impl.h +++ b/include/yaml-cpp/value/impl.h @@ -215,15 +215,6 @@ namespace YAML { return false; } - - template - inline bool equals(detail::shared_node pNode, const T& rhs, detail::shared_memory_holder pMemory) - { - T lhs; - if(convert(Value(pNode, pMemory), lhs)) - return lhs == rhs; - return false; - } } #endif // VALUE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/include/yaml-cpp/value/value.h b/include/yaml-cpp/value/value.h index 445b9ae..e269b34 100644 --- a/include/yaml-cpp/value/value.h +++ b/include/yaml-cpp/value/value.h @@ -17,6 +17,8 @@ namespace YAML class Value { public: + friend class detail::node_data; + Value(); explicit Value(ValueType::value type); template explicit Value(const T& rhs); @@ -61,9 +63,6 @@ namespace YAML private: explicit Value(detail::shared_node pNode, detail::shared_memory_holder pMemory); - template - friend bool equals(detail::shared_node pNode, const T& rhs, detail::shared_memory_holder pMemory); - template void Assign(const T& rhs); void Assign(const char *rhs); void Assign(char *rhs);