Skip to content

Commit

Permalink
move-only: Add util/hash_type
Browse files Browse the repository at this point in the history
Can be reviewed with --color-moved=dimmed-zebra
  • Loading branch information
MarcoFalke committed May 11, 2021
1 parent d2f6d29 commit faa921f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 64 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ BITCOIN_CORE_H = \
util/fees.h \
util/getuniquepath.h \
util/golombrice.h \
util/hash_type.h \
util/hasher.h \
util/macros.h \
util/message.h \
Expand Down
65 changes: 1 addition & 64 deletions src/script/standard.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <script/interpreter.h>
#include <uint256.h>
#include <util/hash_type.h>

#include <string>
#include <variant>
Expand All @@ -18,70 +19,6 @@ class CKeyID;
class CScript;
struct ScriptHash;

template<typename HashType>
class BaseHash
{
protected:
HashType m_hash;

public:
BaseHash() : m_hash() {}
explicit BaseHash(const HashType& in) : m_hash(in) {}

unsigned char* begin()
{
return m_hash.begin();
}

const unsigned char* begin() const
{
return m_hash.begin();
}

unsigned char* end()
{
return m_hash.end();
}

const unsigned char* end() const
{
return m_hash.end();
}

operator std::vector<unsigned char>() const
{
return std::vector<unsigned char>{m_hash.begin(), m_hash.end()};
}

std::string ToString() const
{
return m_hash.ToString();
}

bool operator==(const BaseHash<HashType>& other) const noexcept
{
return m_hash == other.m_hash;
}

bool operator!=(const BaseHash<HashType>& other) const noexcept
{
return !(m_hash == other.m_hash);
}

bool operator<(const BaseHash<HashType>& other) const noexcept
{
return m_hash < other.m_hash;
}

size_t size() const
{
return m_hash.size();
}

unsigned char* data() { return m_hash.data(); }
const unsigned char* data() const { return m_hash.data(); }
};

/** A reference to a CScript: the Hash160 of its serialization (see script.h) */
class CScriptID : public BaseHash<uint160>
{
Expand Down
72 changes: 72 additions & 0 deletions src/util/hash_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2020-2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_UTIL_HASH_TYPE_H
#define BITCOIN_UTIL_HASH_TYPE_H

template <typename HashType>
class BaseHash
{
protected:
HashType m_hash;

public:
BaseHash() : m_hash() {}
explicit BaseHash(const HashType& in) : m_hash(in) {}

unsigned char* begin()
{
return m_hash.begin();
}

const unsigned char* begin() const
{
return m_hash.begin();
}

unsigned char* end()
{
return m_hash.end();
}

const unsigned char* end() const
{
return m_hash.end();
}

operator std::vector<unsigned char>() const
{
return std::vector<unsigned char>{m_hash.begin(), m_hash.end()};
}

std::string ToString() const
{
return m_hash.ToString();
}

bool operator==(const BaseHash<HashType>& other) const noexcept
{
return m_hash == other.m_hash;
}

bool operator!=(const BaseHash<HashType>& other) const noexcept
{
return !(m_hash == other.m_hash);
}

bool operator<(const BaseHash<HashType>& other) const noexcept
{
return m_hash < other.m_hash;
}

size_t size() const
{
return m_hash.size();
}

unsigned char* data() { return m_hash.data(); }
const unsigned char* data() const { return m_hash.data(); }
};

#endif // BITCOIN_UTIL_HASH_TYPE_H

0 comments on commit faa921f

Please sign in to comment.