mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 20:51:16 +00:00
Base iterator stuff compiles :)
This commit is contained in:
110
include/yaml-cpp/value/detail/iterator.h
Normal file
110
include/yaml-cpp/value/detail/iterator.h
Normal file
@@ -0,0 +1,110 @@
|
||||
#ifndef VALUE_DETAIL_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define VALUE_DETAIL_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
|
||||
#include "yaml-cpp/dll.h"
|
||||
#include "yaml-cpp/value/ptr.h"
|
||||
#include "yaml-cpp/value/value.h"
|
||||
#include <boost/iterator/iterator_facade.hpp>
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
#include <list>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
class node;
|
||||
|
||||
struct iterator_value: public Value, std::pair<Value, Value> {
|
||||
iterator_value() {}
|
||||
explicit iterator_value(const Value& rhs): Value(rhs) {}
|
||||
explicit iterator_value(const Value& key, const Value& value): std::pair<Value, Value>(key, value) {}
|
||||
};
|
||||
|
||||
struct iterator_type { enum value { None, Sequence, Map }; };
|
||||
|
||||
template<typename V, typename SeqIter, typename MapIter>
|
||||
class iterator_base: public boost::iterator_facade<iterator_base<V, SeqIter, MapIter>, V, std::bidirectional_iterator_tag>
|
||||
{
|
||||
private:
|
||||
struct enabler {};
|
||||
|
||||
public:
|
||||
iterator_base(): m_type(iterator_type::None) {}
|
||||
explicit iterator_base(shared_memory_holder pMemory, SeqIter seqIt): m_type(iterator_type::Sequence), m_pMemory(pMemory), m_seqIt(seqIt) {}
|
||||
explicit iterator_base(shared_memory_holder pMemory, MapIter mapIt): m_type(iterator_type::Map), m_pMemory(pMemory), m_mapIt(mapIt) {}
|
||||
|
||||
template<typename W, typename I, typename J>
|
||||
explicit iterator_base(const iterator_base<W, I, J>& rhs, typename boost::enable_if<boost::is_convertible<W*, V*>, enabler>::type = enabler())
|
||||
: m_type(rhs.m_type), m_pMemory(rhs.m_pMemory), m_seqIt(rhs.m_seqIt), m_mapIt(rhs.m_mapIt) {}
|
||||
|
||||
private:
|
||||
friend class boost::iterator_core_access;
|
||||
template<typename, typename, typename> friend class iterator_base;
|
||||
|
||||
template<typename W, typename I, typename J>
|
||||
bool equal(const iterator_base<W, I, J>& rhs) const {
|
||||
if(m_type != rhs.m_type || m_pMemory != rhs.m_pMemory)
|
||||
return false;
|
||||
|
||||
switch(m_type) {
|
||||
case iterator_type::None: return true;
|
||||
case iterator_type::Sequence: return m_seqIt == rhs.m_seqIt;
|
||||
case iterator_type::Map: return m_mapIt == rhs.m_mapIt;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void increment() {
|
||||
switch(m_type) {
|
||||
case iterator_type::None: break;
|
||||
case iterator_type::Sequence: ++m_seqIt; break;
|
||||
case iterator_type::Map: ++m_mapIt; break;
|
||||
}
|
||||
}
|
||||
|
||||
void decrement() {
|
||||
switch(m_type) {
|
||||
case iterator_type::None: break;
|
||||
case iterator_type::Sequence: --m_seqIt; break;
|
||||
case iterator_type::Map: --m_mapIt; break;
|
||||
}
|
||||
}
|
||||
|
||||
V dereference() {
|
||||
switch(m_type) {
|
||||
case iterator_type::None: return V();
|
||||
case iterator_type::Sequence: return V(Value(*m_seqIt, m_pMemory));
|
||||
case iterator_type::Map: return V(Value(*m_mapIt->first, m_pMemory), Value(*m_mapIt->second, m_pMemory));
|
||||
}
|
||||
return V();
|
||||
}
|
||||
|
||||
private:
|
||||
typename iterator_type::value m_type;
|
||||
|
||||
shared_memory_holder m_pMemory;
|
||||
|
||||
SeqIter m_seqIt;
|
||||
MapIter m_mapIt;
|
||||
};
|
||||
|
||||
typedef std::vector<node *> node_seq;
|
||||
typedef std::pair<node *, node *> kv_pair;
|
||||
typedef std::list<kv_pair> node_map;
|
||||
|
||||
typedef node_seq::iterator node_seq_iterator;
|
||||
typedef node_seq::const_iterator node_seq_const_iterator;
|
||||
|
||||
typedef node_map::iterator node_map_iterator;
|
||||
typedef node_map::const_iterator node_map_const_iterator;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // VALUE_DETAIL_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
@@ -11,8 +11,28 @@
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class iterator: public detail::iterator_base<detail::iterator_value, detail::node_seq_iterator, detail::node_map_iterator> {};
|
||||
class const_iterator: public detail::iterator_base<const detail::iterator_value, detail::node_seq_const_iterator, detail::node_map_const_iterator> {};
|
||||
class iterator: public detail::iterator_base<detail::iterator_value, detail::node_seq_iterator, detail::node_map_iterator>
|
||||
{
|
||||
private:
|
||||
typedef detail::iterator_base<detail::iterator_value, detail::node_seq_iterator, detail::node_map_iterator> base;
|
||||
|
||||
public:
|
||||
iterator() {}
|
||||
explicit iterator(detail::shared_memory_holder pMemory, detail::node_seq_iterator seqIt): base(pMemory, seqIt) {}
|
||||
explicit iterator(detail::shared_memory_holder pMemory, detail::node_map_iterator mapIt): base(pMemory, mapIt) {}
|
||||
};
|
||||
|
||||
class const_iterator: public detail::iterator_base<const detail::iterator_value, detail::node_seq_const_iterator, detail::node_map_const_iterator>
|
||||
{
|
||||
private:
|
||||
typedef detail::iterator_base<const detail::iterator_value, detail::node_seq_const_iterator, detail::node_map_const_iterator> base;
|
||||
|
||||
public:
|
||||
const_iterator() {}
|
||||
explicit const_iterator(detail::shared_memory_holder pMemory, detail::node_seq_const_iterator seqIt): base(pMemory, seqIt) {}
|
||||
explicit const_iterator(detail::shared_memory_holder pMemory, detail::node_map_const_iterator mapIt): base(pMemory, mapIt) {}
|
||||
explicit const_iterator(const iterator& rhs): base(rhs) {}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // VALUE_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
Reference in New Issue
Block a user