diff --git a/src/util/LazyRandomEngine.cxx b/src/util/LazyRandomEngine.cxx index d24a1c456..8694e3e87 100644 --- a/src/util/LazyRandomEngine.cxx +++ b/src/util/LazyRandomEngine.cxx @@ -19,12 +19,10 @@ #include "LazyRandomEngine.hxx" -void -LazyRandomEngine::AutoCreate() -{ - if (engine != nullptr) +void LazyRandomEngine::AutoCreate() { + if (engine) return; std::random_device rd; - engine = new std::mt19937(rd()); + engine.emplace(rd()); } diff --git a/src/util/LazyRandomEngine.hxx b/src/util/LazyRandomEngine.hxx index 7e1f561b4..e9cde192e 100644 --- a/src/util/LazyRandomEngine.hxx +++ b/src/util/LazyRandomEngine.hxx @@ -21,21 +21,19 @@ #define MPD_LAZY_RANDOM_ENGINE_HXX #include +#include #include /** * A random engine that will be created and seeded on demand. */ class LazyRandomEngine { - std::mt19937 *engine; + std::optional engine; public: typedef std::mt19937::result_type result_type; - LazyRandomEngine():engine(nullptr) {} - ~LazyRandomEngine() { - delete engine; - } + LazyRandomEngine() : engine(std::nullopt) {} LazyRandomEngine(const LazyRandomEngine &other) = delete; LazyRandomEngine &operator=(const LazyRandomEngine &other) = delete; @@ -46,16 +44,12 @@ public: */ void AutoCreate(); - static constexpr result_type min() { - return std::mt19937::min(); - } + static constexpr result_type min() { return std::mt19937::min(); } - static constexpr result_type max() { - return std::mt19937::max(); - } + static constexpr result_type max() { return std::mt19937::max(); } result_type operator()() { - assert(engine != nullptr); + assert(engine); return engine->operator()(); }