#ifndef RAPIDXML_ITERATORS_HPP_INCLUDED
#define RAPIDXML_ITERATORS_HPP_INCLUDED

// Copyright (C) 2006, 2009 Marcin Kalicinski
// Version 1.13
// Revision $DateTime: 2009/05/15 23:02:39 $
//! \file rapidxml_iterators.hpp This file contains rapidxml iterators

#include "rapidxml.hpp"

namespace rapidxml
{

    //! Iterator of child nodes of xml_node
    template<class Ch = char>
    class node_iterator
    {
    
    public:

        typedef xml_node<Ch> value_type;
        typedef xml_node<Ch> &reference;
        typedef xml_node<Ch> *pointer;
        typedef std::ptrdiff_t difference_type;
        typedef std::bidirectional_iterator_tag iterator_category;
        
        node_iterator()
            : m_node(0)
        {
        }

        node_iterator(const xml_node<Ch> *node)
            : m_node(node->first_node())
        {
        }
        
        reference operator *() const
        {
            assert(m_node);
            return *m_node;
        }

        pointer operator->() const
        {
            assert(m_node);
            return m_node;
        }

        node_iterator& operator++()
        {
            assert(m_node);
            m_node = m_node->next_sibling();
            return *this;
        }

        node_iterator operator++(int)
        {
            node_iterator tmp = *this;
            this->operator++();
            return tmp;
        }

        node_iterator &operator--()
        {
            assert(m_node && m_node->previous_sibling());
            m_node = m_node->previous_sibling();
            return *this;
        }

        node_iterator operator--(int)
        {
            node_iterator tmp = *this;
            this->operator--();
            return tmp;
        }

        bool operator==(const node_iterator<Ch> &rhs) const
        {
            return m_node == rhs.m_node;
        }

        bool operator!=(const node_iterator<Ch> &rhs) const
        {
            return m_node != rhs.m_node;
        }

    private:

        xml_node<Ch> *m_node;

    };

    //! Iterator of child attributes of xml_node
    template<class Ch = char>
    class attribute_iterator
    {
    
    public:

        typedef xml_attribute<Ch> value_type;
        typedef xml_attribute<Ch> &reference;
        typedef xml_attribute<Ch> *pointer;
        typedef std::ptrdiff_t difference_type;
        typedef std::bidirectional_iterator_tag iterator_category;
        
        attribute_iterator()
            : m_attribute(0)
        {
        }

        attribute_iterator(const xml_node<Ch> *node)
            : m_attribute(node->first_attribute())
        {
        }
        
        reference operator *() const
        {
            assert(m_attribute);
            return *m_attribute;
        }

        pointer operator->() const
        {
            assert(m_attribute);
            return m_attribute;
        }

        attribute_iterator &operator++()
        {
            assert(m_attribute);
            m_attribute = m_attribute->next_attribute();
            return *this;
        }

        attribute_iterator operator++(int)
        {
            attribute_iterator tmp = *this;
            this->operator++();
            return tmp;
        }

        attribute_iterator& operator--()
        {
            assert(m_attribute && m_attribute->previous_attribute());
            m_attribute = m_attribute->previous_attribute();
            return *this;
        }

        attribute_iterator operator--(int)
        {
            attribute_iterator tmp = *this;
            this->operator--();
            return tmp;
        }

        bool operator ==(const attribute_iterator<Ch> &rhs) const
        {
            return m_attribute == rhs.m_attribute;
        }

        bool operator !=(const attribute_iterator<Ch> &rhs) const
        {
            return m_attribute != rhs.m_attribute;
        }

    private:

        xml_attribute<Ch> *m_attribute;
    };

    // Range-based for loop support
    template<class Iterator>
    class iterator_range
    {

    public:
        using const_iterator = Iterator;
        using iterator = Iterator;

        iterator_range(Iterator first, Iterator last) noexcept
            : m_first(first)
            , m_last(last)
        {
        }

        Iterator begin() const noexcept
        {
            return m_first;
        }
        Iterator end() const noexcept
        {
            return m_last;
        }

    private:
        Iterator m_first;
        Iterator m_last;
    };

    template<class Ch>
    using node_range = iterator_range<node_iterator<Ch>>;

    template<class Ch>
    using attribute_range = iterator_range<attribute_iterator<Ch>>;

    //! \pre `parent` is not equal to `nullptr`.
    //! \return A range of pointers to the children of `parent`.
    template<class Ch>
    node_range<Ch> nodes(const xml_node<Ch> *parent) noexcept
    {
        return {node_iterator<Ch>{parent}, {}};
    }

    //! \pre `node` is not equal to `nullptr`.
    //! \remarks The range offers range-based for loop support.
    template<class Ch>
    attribute_range<Ch> attributes(const xml_node<Ch> *node) noexcept
    {
        return {attribute_iterator<Ch>{node}, {}};
    }
}

#endif
