MCPcopy Create free account
hub / github.com/ObEngine/ObEngine / RepeatGenerator

Class RepeatGenerator

extlibs/catch/include/catch/catch.hpp:4092–4140  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4090
4091 template <typename T>
4092 class RepeatGenerator : public IGenerator<T> {
4093 static_assert(!std::is_same<T, bool>::value,
4094 "RepeatGenerator currently does not support bools"
4095 "because of std::vector<bool> specialization");
4096 GeneratorWrapper<T> m_generator;
4097 mutable std::vector<T> m_returned;
4098 size_t m_target_repeats;
4099 size_t m_current_repeat = 0;
4100 size_t m_repeat_index = 0;
4101 public:
4102 RepeatGenerator(size_t repeats, GeneratorWrapper<T>&& generator):
4103 m_generator(std::move(generator)),
4104 m_target_repeats(repeats)
4105 {
4106 assert(m_target_repeats > 0 && "Repeat generator must repeat at least once");
4107 }
4108
4109 T const& get() const override {
4110 if (m_current_repeat == 0) {
4111 m_returned.push_back(m_generator.get());
4112 return m_returned.back();
4113 }
4114 return m_returned[m_repeat_index];
4115 }
4116
4117 bool next() override {
4118 // There are 2 basic cases:
4119 // 1) We are still reading the generator
4120 // 2) We are reading our own cache
4121
4122 // In the first case, we need to poke the underlying generator.
4123 // If it happily moves, we are left in that state, otherwise it is time to start reading from our cache
4124 if (m_current_repeat == 0) {
4125 const auto success = m_generator.next();
4126 if (!success) {
4127 ++m_current_repeat;
4128 }
4129 return m_current_repeat < m_target_repeats;
4130 }
4131
4132 // In the second case, we need to move indices forward and check that we haven't run up against the end
4133 ++m_repeat_index;
4134 if (m_repeat_index == m_returned.size()) {
4135 m_repeat_index = 0;
4136 ++m_current_repeat;
4137 }
4138 return m_current_repeat < m_target_repeats;
4139 }
4140 };
4141
4142 template <typename T>
4143 GeneratorWrapper<T> repeat(size_t repeats, GeneratorWrapper<T>&& generator) {

Callers

nothing calls this directly

Calls 2

push_backMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected