2016-08-18 22:34:36 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "utils/iterator/iterator_base.hpp"
|
|
|
|
|
|
|
|
namespace iter
|
|
|
|
{
|
2016-08-19 00:43:06 +08:00
|
|
|
// Wraps lambda into interator with next().
|
2016-08-18 22:34:36 +08:00
|
|
|
// T - type of return value
|
2016-08-19 00:43:06 +08:00
|
|
|
// F - type of wraped lambda
|
2016-08-18 22:34:36 +08:00
|
|
|
template <class T, class F>
|
2016-08-19 00:43:06 +08:00
|
|
|
class LambdaIterator : public IteratorBase<T>
|
2016-08-18 22:34:36 +08:00
|
|
|
{
|
|
|
|
public:
|
2016-08-30 12:29:30 +08:00
|
|
|
LambdaIterator(F &&f, size_t count) : func(std::move(f)), _count(count) {}
|
2016-08-18 22:34:36 +08:00
|
|
|
|
2016-08-30 12:29:30 +08:00
|
|
|
LambdaIterator(LambdaIterator &&other)
|
|
|
|
: func(std::move(other.func)), _count(other._count)
|
|
|
|
{
|
|
|
|
}
|
2016-08-28 22:47:13 +08:00
|
|
|
|
|
|
|
~LambdaIterator() final {}
|
|
|
|
|
2016-08-30 12:29:30 +08:00
|
|
|
Option<T> next() final
|
|
|
|
{
|
|
|
|
_count = _count > 0 ? _count - 1 : 0;
|
|
|
|
return func();
|
|
|
|
}
|
|
|
|
|
|
|
|
Count count() final { return Count(_count); }
|
2016-08-18 22:34:36 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
F func;
|
2016-08-30 12:29:30 +08:00
|
|
|
size_t _count;
|
2016-08-18 22:34:36 +08:00
|
|
|
};
|
|
|
|
|
2016-08-19 00:43:06 +08:00
|
|
|
// Wraps lambda which returns options as an iterator.
|
2016-08-18 22:34:36 +08:00
|
|
|
template <class F>
|
2016-08-30 12:29:30 +08:00
|
|
|
auto make_iterator(F &&f, size_t count)
|
2016-08-18 22:34:36 +08:00
|
|
|
{
|
|
|
|
// Because function isn't receving or in any way using type T from
|
|
|
|
// FunctionIterator compiler can't deduce it thats way there is decltype in
|
|
|
|
// construction of FunctionIterator. Resoulting type of iter.next().take()
|
|
|
|
// is T.
|
2016-08-30 12:29:30 +08:00
|
|
|
return LambdaIterator<decltype(f().take()), F>(std::move(f), count);
|
2016-08-18 22:34:36 +08:00
|
|
|
}
|
|
|
|
}
|