/* * 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; }; }