From 25460cc527bce343c191625a5aa81dc7de9f97f7 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 24 Feb 2020 14:41:06 +1100 Subject: [PATCH] iterator/cast: add skeleton of a casting iterator --- CMakeLists.txt | 1 + iterator/cast.hpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 iterator/cast.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a08c8632..049abd92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -407,6 +407,7 @@ list ( introspection.hpp io.cpp io.hpp + iterator/cast.hpp iterator/constant.hpp iterator/counting.hpp iterator/dereference.hpp diff --git a/iterator/cast.hpp b/iterator/cast.hpp new file mode 100644 index 00000000..adfb548e --- /dev/null +++ b/iterator/cast.hpp @@ -0,0 +1,69 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright 2020, Danny Robson + */ + +#pragma once + +#include + +namespace cruft::iterator { + /////////////////////////////////////////////////////////////////////////// + /// An iterator that converts the value_type of an underlying iterator to + /// a requested type. + template + class cast { + public: + cast (IteratorT _inner) + : m_inner (_inner) + { ; } + + using iterator_category = typename std::iterator_traits::iterator_category; + using value_type = ValueT; + using difference_type = typename std::iterator_traits::difference_type; + using pointer = value_type*; + using reference = value_type&; + + class wrapper { + public: + using inner_value = typename std::iterator_traits::value; + using inner_pointer = typename std::iterator_traits::pointer; + using inner_reference = typename std::iterator_traits::reference; + + reference + operator= (ValueT const &val) + { + _value = static_cast (val); + return _value; + } + + reference operator= (ValueT &&val) + { + _value = static_cast (val); + } + + inner_pointer + operator-> () + { return &_value; } + + private: + inner_reference _value; + }; + + + wrapper operator* (void) { return wrapper (*m_inner); }; + wrapper operator-> (void) { return wrapper (*m_inner); }; + + cast& operator++ (void) + { + ++m_inner; + return *this; + } + + private: + IteratorT m_inner; + }; +}