iterator/cast: add skeleton of a casting iterator

This commit is contained in:
Danny Robson 2020-02-24 14:41:06 +11:00
parent 28a7890681
commit 25460cc527
2 changed files with 70 additions and 0 deletions

View File

@ -407,6 +407,7 @@ list (
introspection.hpp introspection.hpp
io.cpp io.cpp
io.hpp io.hpp
iterator/cast.hpp
iterator/constant.hpp iterator/constant.hpp
iterator/counting.hpp iterator/counting.hpp
iterator/dereference.hpp iterator/dereference.hpp

69
iterator/cast.hpp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#pragma once
#include <iterator>
namespace cruft::iterator {
///////////////////////////////////////////////////////////////////////////
/// An iterator that converts the value_type of an underlying iterator to
/// a requested type.
template <typename ValueT, typename IteratorT>
class cast {
public:
cast (IteratorT _inner)
: m_inner (_inner)
{ ; }
using iterator_category = typename std::iterator_traits<IteratorT>::iterator_category;
using value_type = ValueT;
using difference_type = typename std::iterator_traits<IteratorT>::difference_type;
using pointer = value_type*;
using reference = value_type&;
class wrapper {
public:
using inner_value = typename std::iterator_traits<IteratorT>::value;
using inner_pointer = typename std::iterator_traits<IteratorT>::pointer;
using inner_reference = typename std::iterator_traits<IteratorT>::reference;
reference
operator= (ValueT const &val)
{
_value = static_cast<inner_value> (val);
return _value;
}
reference operator= (ValueT &&val)
{
_value = static_cast<inner_value> (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;
};
}