2016-08-15 07:09:58 +08:00
|
|
|
#pragma once
|
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
#include "utils/iterator/range_iterator.hpp"
|
2016-08-15 07:09:58 +08:00
|
|
|
#include "utils/option.hpp"
|
|
|
|
|
|
|
|
namespace iter
|
|
|
|
{
|
2016-08-18 22:34:36 +08:00
|
|
|
// Class which turns ranged iterator with next() into accessor.
|
|
|
|
// T - type of return value
|
|
|
|
// I - iterator type
|
2016-08-15 07:09:58 +08:00
|
|
|
template <class T, class I>
|
|
|
|
class OneTimeAccessor
|
|
|
|
{
|
|
|
|
public:
|
2016-08-18 22:34:36 +08:00
|
|
|
OneTimeAccessor() : it(Option<RangeIterator<T, I>>()) {}
|
|
|
|
OneTimeAccessor(I &&it) : it(RangeIterator<T, I>(std::move(it))) {}
|
2016-08-15 07:09:58 +08:00
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
RangeIterator<T, I> begin() { return it.take(); }
|
2016-08-15 07:09:58 +08:00
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
RangeIterator<T, I> end() { return RangeIterator<T, I>(); }
|
2016-08-15 07:09:58 +08:00
|
|
|
|
|
|
|
private:
|
2016-08-18 22:34:36 +08:00
|
|
|
Option<RangeIterator<T, I>> it;
|
2016-08-15 07:09:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class I>
|
|
|
|
auto make_one_time_accessor(I &&iter)
|
|
|
|
{
|
2016-08-18 22:34:36 +08:00
|
|
|
// Because function isn't receving or in any way using type T from
|
|
|
|
// OneTimeAccessor compiler can't deduce it thats way there is decltype in
|
|
|
|
// construction of OneTimeAccessor. Resoulting type of iter.next().take() is
|
|
|
|
// T.
|
2016-08-15 07:09:58 +08:00
|
|
|
return OneTimeAccessor<decltype(iter.next().take()), I>(std::move(iter));
|
|
|
|
}
|
|
|
|
}
|