Avoid reinterpret casts

This commit is contained in:
Piotr Wójcik 2024-10-24 22:46:33 +02:00
commit 57d263df1e
2 changed files with 52 additions and 46 deletions

View file

@ -747,7 +747,12 @@ nano::hash_or_account::hash_or_account () :
}
nano::hash_or_account::hash_or_account (uint64_t value_a) :
raw (value_a)
raw{ value_a }
{
}
nano::hash_or_account::hash_or_account (uint256_union const & value_a) :
raw{ value_a }
{
}
@ -781,11 +786,6 @@ std::string nano::hash_or_account::to_account () const
return account.to_account ();
}
nano::block_hash const & nano::root::previous () const
{
return hash;
}
std::string nano::to_string_hex (uint64_t const value_a)
{
std::stringstream stream;
@ -892,33 +892,3 @@ double nano::difficulty::to_multiplier (uint64_t const difficulty_a, uint64_t co
#ifdef _WIN32
#pragma warning(pop)
#endif
nano::public_key::operator nano::link const & () const
{
return reinterpret_cast<nano::link const &> (*this);
}
nano::public_key::operator nano::root const & () const
{
return reinterpret_cast<nano::root const &> (*this);
}
nano::public_key::operator nano::hash_or_account const & () const
{
return reinterpret_cast<nano::hash_or_account const &> (*this);
}
nano::block_hash::operator nano::link const & () const
{
return reinterpret_cast<nano::link const &> (*this);
}
nano::block_hash::operator nano::root const & () const
{
return reinterpret_cast<nano::root const &> (*this);
}
nano::block_hash::operator nano::hash_or_account const & () const
{
return reinterpret_cast<nano::hash_or_account const &> (*this);
}

View file

@ -165,10 +165,6 @@ class block_hash final : public uint256_union
public:
using uint256_union::uint256_union;
operator nano::link const & () const;
operator nano::root const & () const;
operator nano::hash_or_account const & () const;
public: // Keep operators inlined
auto operator<=> (nano::block_hash const & other) const
{
@ -182,6 +178,18 @@ public: // Keep operators inlined
{
return number ();
}
operator nano::link () const
{
return nano::link{ *this };
}
operator nano::root () const
{
return nano::root{ *this };
}
operator nano::hash_or_account () const
{
return nano::hash_or_account{ *this };
}
};
class public_key final : public uint256_union
@ -234,7 +242,8 @@ class hash_or_account
{
public:
hash_or_account ();
hash_or_account (uint64_t value_a);
hash_or_account (uint64_t);
explicit hash_or_account (uint256_union const &);
bool is_zero () const;
void clear ();
@ -278,6 +287,10 @@ public: // Keep operators inlined
{
return hash;
}
nano::uint256_union const & as_union () const
{
return raw;
}
};
// A link can either be a destination account or source hash
@ -285,6 +298,16 @@ class link final : public hash_or_account
{
public:
using hash_or_account::hash_or_account;
public: // Keep operators inlined
auto operator<=> (nano::link const & other) const
{
return hash_or_account::operator<=> (other);
}
bool operator== (nano::link const & other) const
{
return *this <=> other == 0;
}
};
// A root can either be an open block hash or a previous hash
@ -293,7 +316,20 @@ class root final : public hash_or_account
public:
using hash_or_account::hash_or_account;
nano::block_hash const & previous () const;
nano::block_hash const & previous () const
{
return hash;
}
public: // Keep operators inlined
auto operator<=> (nano::root const & other) const
{
return hash_or_account::operator<=> (other);
}
bool operator== (nano::root const & other) const
{
return *this <=> other == 0;
}
};
// The seed or private key
@ -364,13 +400,13 @@ class qualified_root : public uint512_union
public:
using uint512_union::uint512_union;
nano::root const & root () const
nano::root root () const
{
return reinterpret_cast<nano::root const &> (uint256s[0]);
return nano::root{ uint256s[0] };
}
nano::block_hash const & previous () const
nano::block_hash previous () const
{
return reinterpret_cast<nano::block_hash const &> (uint256s[1]);
return nano::block_hash{ uint256s[1] };
}
};