From ff1a8fc59a2bc6e876b1c3736c16a8226e54f59b Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Fri, 9 Sep 2011 16:17:59 -0500 Subject: [PATCH] Started writing new iterators --- include/yaml-cpp/value.h | 1 + include/yaml-cpp/value/detail/node.h | 10 ++++++ include/yaml-cpp/value/detail/node_data.h | 11 ++++++ include/yaml-cpp/value/detail/node_ref.h | 10 ++++++ include/yaml-cpp/value/impl.h | 11 +++--- include/yaml-cpp/value/iterator.h | 5 +-- include/yaml-cpp/value/value.h | 4 ++- src/value/detail/node_data.cpp | 43 +++++++++++++++++++++++ 8 files changed, 87 insertions(+), 8 deletions(-) diff --git a/include/yaml-cpp/value.h b/include/yaml-cpp/value.h index 0b00c91..a05ae48 100644 --- a/include/yaml-cpp/value.h +++ b/include/yaml-cpp/value.h @@ -9,6 +9,7 @@ #include "yaml-cpp/value/value.h" #include "yaml-cpp/value/impl.h" #include "yaml-cpp/value/convert.h" +#include "yaml-cpp/value/iterator.h" #include "yaml-cpp/value/detail/impl.h" #endif // VALUE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/include/yaml-cpp/value/detail/node.h b/include/yaml-cpp/value/detail/node.h index 21c147f..48d96f9 100644 --- a/include/yaml-cpp/value/detail/node.h +++ b/include/yaml-cpp/value/detail/node.h @@ -32,7 +32,17 @@ namespace YAML void set_type(ValueType::value type) { m_pRef->set_type(type); } void set_null() { m_pRef->set_null(); } void set_scalar(const std::string& scalar) { m_pRef->set_scalar(scalar); } + + // size/iterator + std::size_t size() const { return m_pRef->size(); } + const_iterator begin(shared_memory_holder pMemory) const { return static_cast(*m_pRef).begin(pMemory); } + iterator begin(shared_memory_holder pMemory) { return m_pRef->begin(pMemory); } + + const_iterator end(shared_memory_holder pMemory) const { return static_cast(*m_pRef).end(pMemory); } + iterator end(shared_memory_holder pMemory) { return m_pRef->end(pMemory); } + + // sequence void append(node& node, shared_memory_holder pMemory) { m_pRef->append(node, pMemory); } // indexing diff --git a/include/yaml-cpp/value/detail/node_data.h b/include/yaml-cpp/value/detail/node_data.h index 44287b0..ad6dfea 100644 --- a/include/yaml-cpp/value/detail/node_data.h +++ b/include/yaml-cpp/value/detail/node_data.h @@ -7,6 +7,7 @@ #include "yaml-cpp/dll.h" +#include "yaml-cpp/value/iterator.h" #include "yaml-cpp/value/ptr.h" #include "yaml-cpp/value/type.h" #include @@ -30,6 +31,16 @@ namespace YAML ValueType::value type() const { return m_isDefined ? m_type : ValueType::Undefined; } const std::string& scalar() const { return m_scalar; } + // size/iterator + std::size_t size() const; + + const_iterator begin(shared_memory_holder pMemory) const; + iterator begin(shared_memory_holder pMemory); + + const_iterator end(shared_memory_holder pMemory) const; + iterator end(shared_memory_holder pMemory); + + // sequence void append(node& node, shared_memory_holder pMemory); // indexing diff --git a/include/yaml-cpp/value/detail/node_ref.h b/include/yaml-cpp/value/detail/node_ref.h index fca208c..668ece1 100644 --- a/include/yaml-cpp/value/detail/node_ref.h +++ b/include/yaml-cpp/value/detail/node_ref.h @@ -30,6 +30,16 @@ namespace YAML void set_null() { ensure_data_exists(); m_pData->set_null(); } void set_scalar(const std::string& scalar) { ensure_data_exists(); m_pData->set_scalar(scalar); } + // size/iterator + std::size_t size() const { return m_pData ? m_pData->size() : 0; } + + const_iterator begin(shared_memory_holder pMemory) const { return m_pData ? static_cast(*m_pData).begin(pMemory) : const_iterator(); } + iterator begin(shared_memory_holder pMemory) {return m_pData ? m_pData->begin(pMemory) : iterator(); } + + const_iterator end(shared_memory_holder pMemory) const { return m_pData ? static_cast(*m_pData).end(pMemory) : const_iterator(); } + iterator end(shared_memory_holder pMemory) {return m_pData ? m_pData->end(pMemory) : iterator(); } + + // sequence void append(node& node, shared_memory_holder pMemory) { ensure_data_exists(); m_pData->append(node, pMemory); } // indexing diff --git a/include/yaml-cpp/value/impl.h b/include/yaml-cpp/value/impl.h index 7bf6edc..92be737 100644 --- a/include/yaml-cpp/value/impl.h +++ b/include/yaml-cpp/value/impl.h @@ -7,6 +7,7 @@ #include "yaml-cpp/value/value.h" +#include "yaml-cpp/value/iterator.h" #include "yaml-cpp/value/detail/memory.h" #include "yaml-cpp/value/detail/node.h" #include @@ -127,27 +128,27 @@ namespace YAML // size/iterator inline std::size_t Value::size() const { - return 0; + return m_pNode->size(); } inline const_iterator Value::begin() const { - return const_iterator(); + return static_cast(*m_pNode).begin(m_pMemory); } inline iterator Value::begin() { - return iterator(); + return m_pNode->begin(m_pMemory); } inline const_iterator Value::end() const { - return const_iterator(); + return static_cast(*m_pNode).end(m_pMemory); } inline iterator Value::end() { - return iterator(); + return m_pNode->end(m_pMemory); } // sequence diff --git a/include/yaml-cpp/value/iterator.h b/include/yaml-cpp/value/iterator.h index ba9887f..117b3e6 100644 --- a/include/yaml-cpp/value/iterator.h +++ b/include/yaml-cpp/value/iterator.h @@ -7,11 +7,12 @@ #include "yaml-cpp/dll.h" +#include "yaml-cpp/value/detail/iterator.h" namespace YAML { - struct iterator {}; - struct const_iterator {}; + class iterator: public detail::iterator_base {}; + class const_iterator: public detail::iterator_base {}; } #endif // VALUE_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/include/yaml-cpp/value/value.h b/include/yaml-cpp/value/value.h index fa64ea6..51f6b19 100644 --- a/include/yaml-cpp/value/value.h +++ b/include/yaml-cpp/value/value.h @@ -7,13 +7,15 @@ #include "yaml-cpp/dll.h" -#include "yaml-cpp/value/iterator.h" #include "yaml-cpp/value/ptr.h" #include "yaml-cpp/value/type.h" #include namespace YAML { + class iterator; + class const_iterator; + class Value { public: diff --git a/src/value/detail/node_data.cpp b/src/value/detail/node_data.cpp index cb85c77..01f8a7f 100644 --- a/src/value/detail/node_data.cpp +++ b/src/value/detail/node_data.cpp @@ -60,6 +60,49 @@ namespace YAML m_scalar = scalar; } + // size/iterator + std::size_t node_data::size() const + { + return 0; + } + + const_iterator node_data::begin(shared_memory_holder pMemory) const + { + switch(m_type) { + case ValueType::Sequence: return const_iterator(pMemory, m_sequence.begin()); + case ValueType::Map: return const_iterator(pMemory, m_map.begin()); + default: return const_iterator(); + } + } + + iterator node_data::begin(shared_memory_holder pMemory) + { + switch(m_type) { + case ValueType::Sequence: return iterator(pMemory, m_sequence.begin()); + case ValueType::Map: return iterator(pMemory, m_map.begin()); + default: return iterator(); + } + } + + const_iterator node_data::end(shared_memory_holder pMemory) const + { + switch(m_type) { + case ValueType::Sequence: return const_iterator(pMemory, m_sequence.end()); + case ValueType::Map: return const_iterator(pMemory, m_map.end()); + default: return const_iterator(); + } + } + + iterator node_data::end(shared_memory_holder pMemory) + { + switch(m_type) { + case ValueType::Sequence: return iterator(pMemory, m_sequence.end()); + case ValueType::Map: return iterator(pMemory, m_map.end()); + default: return iterator(); + } + } + + // sequence void node_data::append(node& node, shared_memory_holder /* pMemory */) { if(m_type != ValueType::Sequence)