Add a generator that takes an iterator pair

This commit is contained in:
Martin Hořeňovský
2019-10-06 13:49:50 +02:00
parent b8b765d55e
commit 319cb9e1da
8 changed files with 196 additions and 8 deletions

View File

@@ -128,6 +128,39 @@ GeneratorWrapper<T> range(T const& start, T const& end) {
}
template <typename T>
class IteratorGenerator final : public IGenerator<T> {
static_assert(!std::is_same<T, bool>::value,
"IteratorGenerator currently does not support bools"
"because of std::vector<bool> specialization");
std::vector<T> m_elems;
size_t m_current = 0;
public:
template <typename InputIterator, typename InputSentinel>
IteratorGenerator(InputIterator first, InputSentinel last):m_elems(first, last) {
if (m_elems.empty()) {
Catch::throw_exception(GeneratorException("IteratorGenerator received no valid values"));
}
}
T const& get() const override {
return m_elems[m_current];
}
bool next() override {
++m_current;
return m_current != m_elems.size();
}
};
template <typename InputIterator,
typename InputSentinel,
typename ResultType = typename std::iterator_traits<InputIterator>::value_type>
GeneratorWrapper<ResultType> from_range(InputIterator from, InputSentinel to) {
return GeneratorWrapper<ResultType>(pf::make_unique<IteratorGenerator<ResultType>>(from, to));
}
} // namespace Generators
} // namespace Catch