From 7bd7964d9849fb75ecb2cd4952b3e5e3e6cb4825 Mon Sep 17 00:00:00 2001 From: Avril Date: Wed, 29 Sep 2021 21:24:38 +0100 Subject: [PATCH] Random::iterator skel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for cpprng's current commit: Half blessing − 半吉 --- include/rng.h | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/include/rng.h b/include/rng.h index 1736a42..7fe4018 100644 --- a/include/rng.h +++ b/include/rng.h @@ -34,6 +34,7 @@ struct InvalidRandomSample final : public std::exception { const f64 value; }; +struct ObjectMoved final : public std::exception{}; /// Interface for a simple random number generator /// @@ -48,7 +49,26 @@ struct InvalidRandomSample final : public std::exception { struct Random { template - struct iterator; //TODO: Implement this in another file (has to be header because of template :/) + struct iterator { //TODO: Implement this in another file (has to be header because of template :/) + friend class Random; + + //TODO: Make this work with foreach(), and STL iterator APIs somehow + + inline CTOR_COPY(iterator) : rng(copy.rng){} + inline CTOR_MOVE(iterator) : rng(move.rng) { + *const_cast(&move.rng) = nullptr; + } + + inline virtual ~iterator(){} + + protected: + virtual T _sample() { if (rng) return rng->next(); else throw ObjectMoved(); } + virtual inline void _init() {} + private: + inline explicit iterator(Random& rng) : rng(&rng){ _init(); } + + Random* const rng; + }; constexpr inline Random(){} constexpr inline virtual ~Random(){} @@ -129,7 +149,7 @@ struct Random return std::bit_cast(arr); } template - inline iterator iter(); //TODO: An iterator that yields `next()` forever. + inline iterator iter() { return iterator(*this); } //TODO: An iterator that yields `next()` forever. protected: //constexpr inline virtual i8 _max() const { return 100; } // use limits.h stuff instead.