Skip to content

Commit

Permalink
Make address_t ctor constexpr, add asserts (workaround for static ini…
Browse files Browse the repository at this point in the history
…tialization order fiasco)
  • Loading branch information
Smertig committed Nov 24, 2020
1 parent 44e6226 commit a37bc10
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
3 changes: 3 additions & 0 deletions include/rcmp/codegen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <type_traits>
#include <optional>
#include <cassert>

namespace rcmp {

Expand All @@ -29,10 +30,12 @@ struct hook_impl<generic_signature_t<Ret(Args...), Convention>, Hook, Tags...> {
inline static rcmp::address_t m_original = nullptr;

static Ret call_original(Args... args) {
assert(m_original != nullptr);
return m_original.as<original_sig_t>()(args...);
}

static Ret call_hook(Args... args) {
assert(m_hook != std::nullopt);
return (*m_hook)(call_original, args...);
}

Expand Down
34 changes: 17 additions & 17 deletions include/rcmp/detail/address.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class address_t {
std::uintptr_t m_value;

public:
/* implicit */ address_t(std::nullptr_t = nullptr) noexcept : m_value(0) {
constexpr /* implicit */ address_t(std::nullptr_t = nullptr) noexcept : m_value(0) {
// empty
}

/* implicit */ address_t(std::uintptr_t value) noexcept : m_value(value) {
constexpr /* implicit */ address_t(std::uintptr_t value) noexcept : m_value(value) {
// empty
}

Expand All @@ -37,29 +37,29 @@ class address_t {
return as<T*>();
}

std::uintptr_t as_number() const noexcept {
constexpr std::uintptr_t as_number() const noexcept {
return m_value;
}

#define RCMP_DECLARE_ADDRESS_OPERATOR(OP) friend bool operator OP(address_t lhs, address_t rhs) noexcept { return lhs.m_value OP rhs.m_value; }
#define RCMP_ADDRESS_DECLARE_OPERATOR(OP) friend constexpr bool operator OP(address_t lhs, address_t rhs) noexcept { return lhs.m_value OP rhs.m_value; }

RCMP_DECLARE_ADDRESS_OPERATOR(<);
RCMP_DECLARE_ADDRESS_OPERATOR(>);
RCMP_DECLARE_ADDRESS_OPERATOR(<=);
RCMP_DECLARE_ADDRESS_OPERATOR(>=);
RCMP_DECLARE_ADDRESS_OPERATOR(==);
RCMP_DECLARE_ADDRESS_OPERATOR(!=);
RCMP_ADDRESS_DECLARE_OPERATOR(<);
RCMP_ADDRESS_DECLARE_OPERATOR(>);
RCMP_ADDRESS_DECLARE_OPERATOR(<=);
RCMP_ADDRESS_DECLARE_OPERATOR(>=);
RCMP_ADDRESS_DECLARE_OPERATOR(==);
RCMP_ADDRESS_DECLARE_OPERATOR(!=);

#undef RCMP_DECLARE_ADDRESS_OPERATOR
#undef RCMP_ADDRESS_DECLARE_OPERATOR

friend address_t operator+(address_t lhs, std::ptrdiff_t rhs) noexcept { return { lhs.m_value + rhs }; }
friend address_t operator+(std::ptrdiff_t lhs, address_t rhs) noexcept { return { lhs + rhs.m_value }; }
friend constexpr address_t operator+(address_t lhs, std::ptrdiff_t rhs) noexcept { return { lhs.m_value + rhs }; }
friend constexpr address_t operator+(std::ptrdiff_t lhs, address_t rhs) noexcept { return { lhs + rhs.m_value }; }

friend address_t operator-(address_t lhs, std::ptrdiff_t rhs) noexcept { return { lhs.m_value - rhs }; }
friend std::ptrdiff_t operator-(address_t lhs, address_t rhs) noexcept { return lhs.m_value - rhs.m_value; }
friend constexpr address_t operator-(address_t lhs, std::ptrdiff_t rhs) noexcept { return { lhs.m_value - rhs }; }
friend constexpr std::ptrdiff_t operator-(address_t lhs, address_t rhs) noexcept { return lhs.m_value - rhs.m_value; }

address_t& operator+=(std::ptrdiff_t delta) noexcept { return (*this) = (*this) + delta; }
address_t& operator-=(std::ptrdiff_t delta) noexcept { return (*this) = (*this) - delta; }
constexpr address_t& operator+=(std::ptrdiff_t delta) noexcept { return (*this) = (*this) + delta; }
constexpr address_t& operator-=(std::ptrdiff_t delta) noexcept { return (*this) = (*this) - delta; }
};

} // namespace rcmp

0 comments on commit a37bc10

Please sign in to comment.