| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_RANDOM_ANY_ENGINE_HPP |
| 4 |
#define MPT_RANDOM_ANY_ENGINE_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/macros.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
#include "mpt/random/random.hpp" |
| 11 |
|
| 12 |
#include <limits> |
| 13 |
#include <memory> |
| 14 |
|
| 15 |
#include <cstddef> |
| 16 |
|
| 17 |
|
| 18 |
namespace mpt { |
| 19 |
inline namespace MPT_INLINE_NS { |
| 20 |
|
| 21 |
|
| 22 |
template <typename Tvalue> |
| 23 |
class any_engine { |
| 24 |
public: |
| 25 |
using result_type = Tvalue; |
| 26 |
protected: |
| 27 |
any_engine() = default; |
| 28 |
public: |
| 29 |
static MPT_CONSTEXPRINLINE result_type min() { |
| 30 |
return static_cast<result_type>(std::numeric_limits<result_type>::min()); |
| 31 |
} |
| 32 |
static MPT_CONSTEXPRINLINE result_type max() { |
| 33 |
return static_cast<result_type>(std::numeric_limits<result_type>::max()); |
| 34 |
} |
| 35 |
static MPT_CONSTEXPRINLINE int result_bits() { |
| 36 |
return static_cast<int>(sizeof(result_type) * 8); |
| 37 |
} |
| 38 |
public: |
| 39 |
virtual result_type operator()() = 0; |
| 40 |
public: |
| 41 |
virtual ~any_engine() = default; |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
template <typename Trng, typename Tvalue = typename Trng::result_type> |
| 46 |
class any_engine_wrapper : public any_engine<Tvalue> { |
| 47 |
private: |
| 48 |
Trng & m_prng; |
| 49 |
public: |
| 50 |
any_engine_wrapper(Trng & prng) |
| 51 |
: m_prng(prng) { |
| 52 |
return; |
| 53 |
} |
| 54 |
public: |
| 55 |
typename any_engine<Tvalue>::result_type operator()() override { |
| 56 |
return mpt::random<typename any_engine<Tvalue>::result_type>(m_prng); |
| 57 |
} |
| 58 |
public: |
| 59 |
virtual ~any_engine_wrapper() = default; |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
} // namespace MPT_INLINE_NS |
| 64 |
} // namespace mpt |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
#endif // MPT_RANDOM_ANY_ENGINE_HPP |