Replace Hasher CRTP base with a concept.
This commit is contained in:
+8
-31
@@ -1,42 +1,19 @@
|
||||
#ifndef CHAOS_HASH_HASHER_HPP
|
||||
#define CHAOS_HASH_HASHER_HPP
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "Hash.hpp"
|
||||
|
||||
namespace Chaos::Hash
|
||||
{
|
||||
|
||||
template<typename T>
|
||||
class Hasher
|
||||
concept Hasher = requires(T hasher, uint8_t * begin, uint8_t * end)
|
||||
{
|
||||
public:
|
||||
void Reset()
|
||||
{
|
||||
Impl().Reset();
|
||||
}
|
||||
|
||||
template<typename InputIt>
|
||||
void Update(InputIt begin, InputIt end)
|
||||
{
|
||||
Impl().Update(begin, end);
|
||||
}
|
||||
|
||||
auto Finish()
|
||||
{
|
||||
return Impl().Finish();
|
||||
}
|
||||
|
||||
protected:
|
||||
Hasher() = default;
|
||||
|
||||
private:
|
||||
const T & Impl() const
|
||||
{
|
||||
return static_cast<const T &>(*this);
|
||||
}
|
||||
|
||||
T & Impl()
|
||||
{
|
||||
return static_cast<T &>(*this);
|
||||
}
|
||||
hasher.Reset();
|
||||
hasher.Update(begin, end);
|
||||
{ hasher.Finish() } -> Hash;
|
||||
};
|
||||
|
||||
} // namespace Chaos::Hash
|
||||
|
||||
+3
-1
@@ -163,7 +163,7 @@ struct Md4Hash
|
||||
|
||||
static_assert(Hash<Md4Hash>);
|
||||
|
||||
class Md4Hasher : public Hasher<Md4Hasher>
|
||||
class Md4Hasher
|
||||
{
|
||||
public:
|
||||
using HashType = Md4Hash;
|
||||
@@ -304,6 +304,8 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
static_assert(Hasher<Md4Hasher>);
|
||||
|
||||
} // namespace Chaos::Hash::Md4
|
||||
|
||||
#endif // CHAOS_HASH_MD4_HPP
|
||||
|
||||
+3
-1
@@ -187,7 +187,7 @@ struct Md5Hash
|
||||
|
||||
static_assert(Hash<Md5Hash>);
|
||||
|
||||
class Md5Hasher : public Hasher<Md5Hasher>
|
||||
class Md5Hasher
|
||||
{
|
||||
public:
|
||||
using HashType = Md5Hash;
|
||||
@@ -328,6 +328,8 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
static_assert(Hasher<Md5Hasher>);
|
||||
|
||||
} // namespace Chaos::Hash::Md5
|
||||
|
||||
#endif // CHAOS_HASH_MD5_HPP
|
||||
|
||||
+3
-1
@@ -157,7 +157,7 @@ struct Sha1Hash
|
||||
|
||||
static_assert(Hash<Sha1Hash>);
|
||||
|
||||
class Sha1Hasher : public Hasher<Sha1Hasher>
|
||||
class Sha1Hasher
|
||||
{
|
||||
public:
|
||||
using HashType = Sha1Hash;
|
||||
@@ -298,6 +298,8 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
static_assert(Hasher<Sha1Hasher>);
|
||||
|
||||
} // namespace Chaos::Hash::Sha1
|
||||
|
||||
#endif // CHAOS_HASH_SHA1_HPP
|
||||
|
||||
Reference in New Issue
Block a user