Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F140210
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/changelogs/63.fix b/changelogs/63.fix
new file mode 100644
index 0000000..05466cc
--- /dev/null
+++ b/changelogs/63.fix
@@ -0,0 +1 @@
+Include <random> properly in random-generator.hpp
diff --git a/src/client/random-generator.hpp b/src/client/random-generator.hpp
index 2fcac84..def2157 100644
--- a/src/client/random-generator.hpp
+++ b/src/client/random-generator.hpp
@@ -1,111 +1,112 @@
/*
* This file is part of libkazv.
- * SPDX-FileCopyrightText: 2021 Tusooa Zhu <tusooa@kazv.moe>
+ * SPDX-FileCopyrightText: 2021-2024 tusooa <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <libkazv-config.hpp>
#include <memory>
+#include <random>
namespace Kazv
{
class RandomInterface
{
public:
using DataT = unsigned int;
/**
* Construct a RandomInterface using an implementation.
*
* @param obj The random generator implementation.
*
* The implementation obj must be of a movable type and
* must be such that the following
* call is valid, and after that v should contain a random number:
*
* `unsigned int v = obj();`
*/
template<class DeriveT>
RandomInterface(DeriveT obj)
: m_d(std::make_unique<Model<DeriveT>>(std::move(obj)))
{}
/**
* Generate a random number of type DataT.
*
* @return A randomly generated DataT.
*/
DataT operator()() {
return (*m_d)();
}
/**
* Generate a range containing `size` random elements.
*
* RangeT must have a constructor such that
* `RangeT(size, ValueT())` constructs a range of `size`
* elements valued `ValueT()`, where `ValueT` is the value type
* of `RangeT`.
*
* @param size The size of the range to create.
*
* @return A RangeT containing `size` random elements.
*/
template<class RangeT>
RangeT generateRange(std::size_t size) {
using ValueT = std::decay_t<decltype(* (std::declval<RangeT>().begin()))>;
RangeT range(size, ValueT());
return fillRange(std::move(range));
}
/**
* Fill the given range.
*
* @param range The range to fill.
*
* @return A range filled with random elements. Its size will be
* the same as `range`.
*/
template<class RangeT>
RangeT fillRange(RangeT range) {
std::generate(range.begin(), range.end(), [this] { return (*this)(); });
return range;
}
private:
struct Concept
{
virtual ~Concept() = default;
virtual DataT operator()() = 0;
};
template<class DeriveT>
struct Model : public Concept
{
Model(DeriveT obj) : instance(std::move(obj)) {}
~Model() override = default;
DataT operator()() override {
return instance();
}
DeriveT instance;
};
std::unique_ptr<Concept> m_d;
};
/**
* A movable wrapper around std::random_device.
*/
class RandomDeviceGenerator
{
public:
RandomDeviceGenerator() : m_d(std::make_unique<std::random_device>()) {}
auto operator()() { return (*m_d)(); }
private:
std::unique_ptr<std::random_device> m_d;
};
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Jan 19, 3:20 PM (22 h, 58 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55264
Default Alt Text
(3 KB)
Attached To
Mode
rL libkazv
Attached
Detach File
Event Timeline
Log In to Comment