Skip to content

Commit

Permalink
C++: Refactor {address,bytes32} constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed May 20, 2022
1 parent fe0d43f commit 02a2af1
Showing 1 changed file with 12 additions and 58 deletions.
70 changes: 12 additions & 58 deletions include/evmc/evmc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,17 @@ struct address : evmc_address
/// Default and converting constructor.
///
/// Initializes bytes to zeros if not other @p init value provided.
constexpr address(evmc_address init = {}) noexcept : evmc_address{init} {}
inline constexpr address(evmc_address init = {}) noexcept : evmc_address{init} {}

/// Converting constructor from unsigned integer value.
///
/// This constructor assigns the @p v value to the last 8 bytes [12:19]
/// in big-endian order.
constexpr explicit address(uint64_t v) noexcept
: evmc_address{{0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
static_cast<uint8_t>(v >> 56),
static_cast<uint8_t>(v >> 48),
static_cast<uint8_t>(v >> 40),
static_cast<uint8_t>(v >> 32),
static_cast<uint8_t>(v >> 24),
static_cast<uint8_t>(v >> 16),
static_cast<uint8_t>(v >> 8),
static_cast<uint8_t>(v >> 0)}}
{}
inline constexpr explicit address(uint64_t v) noexcept : evmc_address{}
{
for (size_t i = sizeof(bytes) - sizeof(v); i < sizeof(bytes); ++i)
bytes[i] = static_cast<uint8_t>(v >> (8 * (sizeof(bytes) - 1 - i)));
}

/// Explicit operator converting to bool.
inline constexpr explicit operator bool() const noexcept;
Expand All @@ -75,46 +58,17 @@ struct bytes32 : evmc_bytes32
/// Default and converting constructor.
///
/// Initializes bytes to zeros if not other @p init value provided.
constexpr bytes32(evmc_bytes32 init = {}) noexcept : evmc_bytes32{init} {}
inline constexpr bytes32(evmc_bytes32 init = {}) noexcept : evmc_bytes32{init} {}

/// Converting constructor from unsigned integer value.
///
/// This constructor assigns the @p v value to the last 8 bytes [24:31]
/// in big-endian order.
constexpr explicit bytes32(uint64_t v) noexcept
: evmc_bytes32{{0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
static_cast<uint8_t>(v >> 56),
static_cast<uint8_t>(v >> 48),
static_cast<uint8_t>(v >> 40),
static_cast<uint8_t>(v >> 32),
static_cast<uint8_t>(v >> 24),
static_cast<uint8_t>(v >> 16),
static_cast<uint8_t>(v >> 8),
static_cast<uint8_t>(v >> 0)}}
{}
inline constexpr explicit bytes32(uint64_t v) noexcept : evmc_bytes32{}
{
for (size_t i = sizeof(bytes) - sizeof(v); i < sizeof(bytes); ++i)
bytes[i] = static_cast<uint8_t>(v >> (8 * (sizeof(bytes) - 1 - i)));
}

/// Explicit operator converting to bool.
inline constexpr explicit operator bool() const noexcept;
Expand Down

0 comments on commit 02a2af1

Please sign in to comment.