Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace various comparison operators with spaceship-operator #11

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions src/lib/es3n1n/common/memory/address.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,16 @@ namespace memory {
return static_cast<bool>(address_);
}

#define MATH_OPERATOR(type, operation) /* NOLINT(ppcoreguidelines-macro-usage) */ \
constexpr type operator operation(const address& rhs) const { /* NOLINT(bugprone-macro-parentheses) */ \
return static_cast<type>(address_ operation rhs.address_); \
}
// \note: @annihilatorq: intentionally left with no return type, see #11
constexpr auto operator<=>(const address&) const = default;

MATH_OPERATOR(bool, ==)
MATH_OPERATOR(bool, !=)
MATH_OPERATOR(bool, >)
MATH_OPERATOR(bool, <)
MATH_OPERATOR(bool, <=)
MATH_OPERATOR(bool, >=)
MATH_OPERATOR(address, +)
MATH_OPERATOR(address, -)
constexpr address operator+(const address& rhs) const {
return static_cast<address>(address_ + rhs.address_);
}

#undef MATH_OPERATOR
constexpr address operator-(const address& rhs) const {
return static_cast<address>(address_ - rhs.address_);
}

private:
std::uintptr_t address_ = 0;
Expand Down
34 changes: 34 additions & 0 deletions src/tests/memory/address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,37 @@ TEST(address, aligns) {
EXPECT_EQ(memory::address(0x2234).align_up(0x1000), 0x3000);
EXPECT_EQ(memory::address(0x2000).align_up(0x1000), 0x2000);
}

TEST(address, operators) {
memory::address addr1(0x1000);
memory::address addr2(0x2000);
memory::address addr3(0x1000);

EXPECT_EQ((addr1 + addr2).inner(), 0x3000);
EXPECT_EQ((addr2 - addr1).inner(), 0x1000);

EXPECT_TRUE(addr1 < addr2);
EXPECT_TRUE(addr2 > addr1);
EXPECT_TRUE(addr1 <= addr3);
EXPECT_TRUE(addr1 >= addr3);

EXPECT_TRUE(addr1 == addr3);
EXPECT_TRUE(addr1 != addr2);

EXPECT_TRUE(static_cast<bool>(addr1));
EXPECT_FALSE(static_cast<bool>(memory::address(nullptr)));
EXPECT_EQ(static_cast<std::uintptr_t>(addr1), 0x1000);
}

TEST(address, casting) {
memory::address addr(0x12345678);

EXPECT_EQ(addr.cast<std::uint32_t>(), static_cast<std::uint32_t>(0x12345678));
EXPECT_EQ(addr.cast<std::uint64_t>(), static_cast<std::uint64_t>(0x12345678));
EXPECT_EQ(addr.cast<std::int32_t>(), static_cast<std::int32_t>(0x12345678));

EXPECT_EQ(addr.as<std::uintptr_t>(), static_cast<std::uintptr_t>(0x12345678));

int* ptr = addr.as<int*>();
EXPECT_EQ(reinterpret_cast<std::uintptr_t>(ptr), static_cast<std::uintptr_t>(0x12345678));
}