Class MapGenerator

Synopsis

#include <include/internal/catch_generators_generic.hpp>

template <typename T, typename U, typename Func>
class MapGenerator : public IGenerator<T>

Description

No description yet.

Mentioned in

Inheritance

Ancestors: IGenerator

Methods

MapGenerator
get
next

Source

Lines 149-174 in include/internal/catch_generators_generic.hpp.

template <typename T, typename U, typename Func>
class MapGenerator : public IGenerator<T> {
    // TBD: provide static assert for mapping function, for friendly error message
    GeneratorWrapper<U> m_generator;
    Func m_function;
    // To avoid returning dangling reference, we have to save the values
    T m_cache;
public:
    template <typename F2 = Func>
    MapGenerator(F2&& function, GeneratorWrapper<U>&& generator) :
        m_generator(std::move(generator)),
        m_function(std::forward<F2>(function)),
        m_cache(m_function(m_generator.get()))
    {}
    T const& get() const override {
        return m_cache;
    }
    bool next() override {
        const auto success = m_generator.next();
        if (success) {
            m_cache = m_function(m_generator.get());
        }
        return success;
    }
};





Add Discussion as Guest

Log in