Class FilterGenerator

Synopsis

#include <include/internal/catch_generators_generic.hpp>

template <typename T, typename Predicate>
class FilterGenerator : public IGenerator<T>

Description

No description yet.

Mentioned in

Inheritance

Ancestors: IGenerator

Methods

FilterGenerator
get
next

Source

Lines 53-85 in include/internal/catch_generators_generic.hpp.

template <typename T, typename Predicate>
class FilterGenerator : public IGenerator<T> {
    GeneratorWrapper<T> m_generator;
    Predicate m_predicate;
public:
    template <typename P = Predicate>
    FilterGenerator(P&& pred, GeneratorWrapper<T>&& generator):
        m_generator(std::move(generator)),
        m_predicate(std::forward<P>(pred))
    {
        if (!m_predicate(m_generator.get())) {
            // It might happen that there are no values that pass the
            // filter. In that case we throw an exception.
            auto has_initial_value = next();
            if (!has_initial_value) {
                Catch::throw_exception(GeneratorException("No valid value found in filtered generator"));
            }
        }
    }
    T const& get() const override {
        return m_generator.get();
    }
    bool next() override {
        bool success = m_generator.next();
        if (!success) {
            return false;
        }
        while (!m_predicate(m_generator.get()) && (success = m_generator.next()) == true);
        return success;
    }
};





Add Discussion as Guest

Log in