I'm basically trying to create some sort of generator, now the problem i ran into is that the interface is well templated and in other places i generally only want to accept the generator interface itself. Any ideas?
#include <iostream>
#include <tuple>
#include <optional>
#include <functional>
#include <memory>
template <typename... ARGS>
class Generator {
public:
virtual ~Generator() = default;
virtual std::optional<std::tuple<ARGS...>> next() = 0;
virtual void reset() = 0;
};
class IntGenerator : public Generator<int> {
public:
IntGenerator(int start, int end) : start(start), end(end) {}
std::optional<std::tuple<int>> next() override {
if (start < end) {
return std::make_tuple(start++);
} else {
return std::nullopt;
}
}
void reset() override { start = 0; }
private:
int start;
int end;
};
void do_something(std::tuple<int> generator) {}
template <typename FUNC>
void foo(Generator &generator, FUNC func) {
while (true) {
auto result = generator.next();
if (result.has_value()) {
func(result.value());
} else {
break;
}
}
}
int main(int argc, char const *argv[]) {
auto generator = std::make_unique<IntGenerator>(0, 10);
foo(*generator, do_something);
return 0;
}
