Improve MTdataHolder design and use it in math_brute_force (#1490)

Improve the design of the MTdataHolder wrapper:

 * Make it a class instead of a struct with a private member, to make
   it clearer that there is no direct access to the MTdata member.

 * Make the 1-arg constructor `explicit` to avoid unintended
   conversions.

 * Forbid copy construction/assignment as MTdataHolder is never
   initialised from an MTdataHolder object in the codebase.

 * Define move construction/assignment as per the "rule of five".

Use the MTdataHolder class throughout math_brute_force, to simplify
code by avoiding manual resource management.

Original patch by Marco Antognini.

Signed-off-by: Marco Antognini <marco.antognini@arm.com>
Signed-off-by: Sven van Haastregt <sven.vanhaastregt@arm.com>

Signed-off-by: Marco Antognini <marco.antognini@arm.com>
Signed-off-by: Sven van Haastregt <sven.vanhaastregt@arm.com>
This commit is contained in:
Sven van Haastregt
2022-09-20 16:52:22 +01:00
committed by GitHub
parent a87e686757
commit 8f9c1960ff
14 changed files with 56 additions and 79 deletions

View File

@@ -94,23 +94,42 @@ double genrand_res53(MTdata /*data*/);
bool genrand_bool(MTdata /*data*/);
#include <cassert>
#include <utility>
struct MTdataHolder
{
MTdataHolder(cl_uint seed)
class MTdataHolder {
public:
MTdataHolder() = default;
explicit MTdataHolder(cl_uint seed)
{
m_mtdata = init_genrand(seed);
assert(m_mtdata != nullptr);
}
MTdataHolder(MTdata mtdata): m_mtdata(mtdata) {}
// Forbid copy.
MTdataHolder(const MTdataHolder&) = delete;
MTdataHolder& operator=(const MTdataHolder&) = delete;
~MTdataHolder() { free_mtdata(m_mtdata); }
// Support move semantics.
MTdataHolder(MTdataHolder&& h) { std::swap(m_mtdata, h.m_mtdata); }
MTdataHolder& operator=(MTdataHolder&& h)
{
std::swap(m_mtdata, h.m_mtdata);
return *this;
}
operator MTdata() const { return m_mtdata; }
~MTdataHolder()
{
if (m_mtdata) free_mtdata(m_mtdata);
}
operator MTdata() const
{
assert(m_mtdata && "Object wasn't initialised");
return m_mtdata;
}
private:
MTdata m_mtdata;
MTdata m_mtdata = nullptr;
};
#endif // #ifdef __cplusplus