From 8153d048c2af00b753492e5b17df833887e9d383 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 13 Apr 2012 11:28:37 +1000 Subject: [PATCH] Add referencing_iterator to build A simple iterator which converts a random access iterator to a dereferencable type into a random access iterator to a reference of said type. --- iterator.hpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 iterator.hpp diff --git a/iterator.hpp b/iterator.hpp new file mode 100644 index 00000000..632e31b2 --- /dev/null +++ b/iterator.hpp @@ -0,0 +1,73 @@ +/* + * This file is part of libgim. + * + * libgim is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * libgim is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with libgim. If not, see . + * + * Copyright 2010-2012 Danny Robson + */ + + +#ifndef __UTIL_ITERATOR_HPP +#define __UTIL_ITERATOR_HPP + +#include "types.hpp" + + +template +class referencing_iterator { + protected: + typedef typename std::enable_if< + is_dereferencable< + typename Base::value_type + >::value, + typename Base::value_type + >::type base_value_type; + + public: + typedef typename dereferenced_type::type value_type ; + typedef typename Base::difference_type difference_type ; + typedef value_type& reference ; + typedef value_type* pointer; + typedef typename Base::iterator_category iterator_category; + + protected: + Base m_base; + + public: + explicit referencing_iterator (Base _base): + m_base (_base) + { ; } + + referencing_iterator& operator++() { ++m_base; return *this; } + referencing_iterator operator++(int) { auto val = *this; ++m_base; return val; } + + bool operator== (const referencing_iterator &rhs) { return m_base == rhs.m_base; } + bool operator!= (const referencing_iterator &rhs) { return m_base != rhs.m_base; } + bool operator>= (const referencing_iterator &rhs) { return m_base >= rhs.m_base; } + bool operator<= (const referencing_iterator &rhs) { return m_base <= rhs.m_base; } + bool operator> (const referencing_iterator &rhs) { return m_base > rhs.m_base; } + bool operator< (const referencing_iterator &rhs) { return m_base < rhs.m_base; } + + const reference operator*() const + { return **m_base; } + reference operator*() + { return **m_base; } + + difference_type operator-(const referencing_iterator& rhs) const { return m_base - rhs.m_base; } + referencing_iterator operator-(int rhs) const { return referencing_iterator (m_base - rhs); } + referencing_iterator operator+(int rhs) const { return referencing_iterator (m_base + rhs); } + +}; + +#endif