From a37bc1078d7ed9893c262de1e82cd513c6e05cb2 Mon Sep 17 00:00:00 2001 From: int3 Date: Tue, 24 Nov 2020 10:18:56 +0300 Subject: [PATCH] Make address_t ctor constexpr, add asserts (workaround for static initialization order fiasco) --- include/rcmp/codegen.hpp | 3 +++ include/rcmp/detail/address.hpp | 34 ++++++++++++++++----------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/include/rcmp/codegen.hpp b/include/rcmp/codegen.hpp index 83bcd98..817571c 100644 --- a/include/rcmp/codegen.hpp +++ b/include/rcmp/codegen.hpp @@ -7,6 +7,7 @@ #include #include +#include namespace rcmp { @@ -29,10 +30,12 @@ struct hook_impl, Hook, Tags...> { inline static rcmp::address_t m_original = nullptr; static Ret call_original(Args... args) { + assert(m_original != nullptr); return m_original.as()(args...); } static Ret call_hook(Args... args) { + assert(m_hook != std::nullopt); return (*m_hook)(call_original, args...); } diff --git a/include/rcmp/detail/address.hpp b/include/rcmp/detail/address.hpp index 47ddfd2..8418857 100644 --- a/include/rcmp/detail/address.hpp +++ b/include/rcmp/detail/address.hpp @@ -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 } @@ -37,29 +37,29 @@ class address_t { return as(); } - 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