Skip to content

Commit

Permalink
chore: make stuff constexpr (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThirdEyeSqueegee authored Oct 3, 2023
1 parent ceb26db commit 8167b42
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 93 deletions.
60 changes: 30 additions & 30 deletions CommonLibSF/include/REL/ID.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ namespace REL
class memory_map
{
public:
memory_map() noexcept = default;
constexpr memory_map() noexcept = default;

memory_map(const memory_map&) = delete;
constexpr memory_map(const memory_map&) = delete;

memory_map(memory_map&& a_rhs) noexcept :
constexpr memory_map(memory_map&& a_rhs) noexcept :
_mapping(a_rhs._mapping),
_view(a_rhs._view)
{
Expand All @@ -34,9 +34,9 @@ namespace REL

~memory_map() { close(); }

memory_map& operator=(const memory_map&) = delete;
constexpr memory_map& operator=(const memory_map&) = delete;

memory_map& operator=(memory_map&& a_rhs) noexcept
constexpr memory_map& operator=(memory_map&& a_rhs) noexcept
{
if (this != std::addressof(a_rhs)) {
_mapping = a_rhs._mapping;
Expand All @@ -48,13 +48,13 @@ namespace REL
return *this;
}

[[nodiscard]] void* data() const noexcept { return _view; }
[[nodiscard]] constexpr void* data() const noexcept { return _view; }

bool open(stl::zwstring a_name, std::size_t a_size);

bool create(stl::zwstring a_name, std::size_t a_size);

void close();
constexpr void close();

private:
void* _mapping{};
Expand All @@ -78,25 +78,25 @@ namespace REL
Offset2ID(std::execution::sequenced_policy{})
{}

[[nodiscard]] std::uint64_t operator()(std::size_t a_offset) const;
[[nodiscard]] constexpr std::uint64_t operator()(std::size_t a_offset) const;

[[nodiscard]] const_iterator begin() const noexcept { return _offset2id.begin(); }
[[nodiscard]] constexpr const_iterator begin() const noexcept { return _offset2id.begin(); }

[[nodiscard]] const_iterator cbegin() const noexcept { return _offset2id.cbegin(); }
[[nodiscard]] constexpr const_iterator cbegin() const noexcept { return _offset2id.cbegin(); }

[[nodiscard]] const_iterator end() const noexcept { return _offset2id.end(); }
[[nodiscard]] constexpr const_iterator end() const noexcept { return _offset2id.end(); }

[[nodiscard]] const_iterator cend() const noexcept { return _offset2id.cend(); }
[[nodiscard]] constexpr const_iterator cend() const noexcept { return _offset2id.cend(); }

[[nodiscard]] const_reverse_iterator rbegin() const noexcept { return _offset2id.rbegin(); }
[[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept { return _offset2id.rbegin(); }

[[nodiscard]] const_reverse_iterator crbegin() const noexcept { return _offset2id.crbegin(); }
[[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept { return _offset2id.crbegin(); }

[[nodiscard]] const_reverse_iterator rend() const noexcept { return _offset2id.rend(); }
[[nodiscard]] constexpr const_reverse_iterator rend() const noexcept { return _offset2id.rend(); }

[[nodiscard]] const_reverse_iterator crend() const noexcept { return _offset2id.crend(); }
[[nodiscard]] constexpr const_reverse_iterator crend() const noexcept { return _offset2id.crend(); }

[[nodiscard]] size_type size() const noexcept { return _offset2id.size(); }
[[nodiscard]] constexpr size_type size() const noexcept { return _offset2id.size(); }

private:
container_type _offset2id;
Expand Down Expand Up @@ -149,22 +149,22 @@ namespace REL
public:
void read(istream_t& a_in);

[[nodiscard]] std::size_t address_count() const noexcept
[[nodiscard]] constexpr std::size_t address_count() const noexcept
{
return _addressCount;
}

[[nodiscard]] std::uint64_t pointer_size() const noexcept
[[nodiscard]] constexpr std::uint64_t pointer_size() const noexcept
{
return _pointerSize;
}

[[nodiscard]] std::string_view name() const noexcept
[[nodiscard]] constexpr std::string_view name() const noexcept
{
return _name;
}

[[nodiscard]] Version version() const noexcept
[[nodiscard]] constexpr Version version() const noexcept
{
return _version;
}
Expand Down Expand Up @@ -198,32 +198,32 @@ namespace REL
return _instance;
}

[[nodiscard]] std::size_t id2offset(std::uint64_t a_id) const;
[[nodiscard]] constexpr std::size_t id2offset(std::uint64_t a_id) const;

private:
friend class Module;

friend class database::Offset2ID;

IDDatabase() = default;
constexpr IDDatabase() = default;

IDDatabase(const IDDatabase&) = delete;
~IDDatabase() = default;

IDDatabase(IDDatabase&&) = delete;
constexpr IDDatabase(const IDDatabase&) = delete;

~IDDatabase() = default;
constexpr IDDatabase(IDDatabase&&) = delete;

IDDatabase& operator=(const IDDatabase&) = delete;
constexpr IDDatabase& operator=(const IDDatabase&) = delete;

IDDatabase& operator=(IDDatabase&&) = delete;
constexpr IDDatabase& operator=(IDDatabase&&) = delete;

std::wstring addresslib_filename();

void load();

bool load_file(stl::zwstring a_filename, Version a_version, bool a_failOnError);

bool unpack_file(database::istream_t& a_in, database::header_t a_header, bool a_failOnError);
bool unpack_file(database::istream_t& a_in, const database::header_t& a_header, bool a_failOnError);

void clear()
{
Expand Down Expand Up @@ -271,7 +271,7 @@ namespace REL
}

private:
[[nodiscard]] static std::uintptr_t base()
[[nodiscard]] static constexpr std::uintptr_t base()
{
return Module::get().base();
}
Expand Down
16 changes: 8 additions & 8 deletions CommonLibSF/include/REL/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ namespace REL
total
};

Segment() noexcept = default;
constexpr Segment() noexcept = default;

Segment(const std::uintptr_t a_proxyBase, const std::uintptr_t a_address, const std::uintptr_t a_size) noexcept :
constexpr Segment(const std::uintptr_t a_proxyBase, const std::uintptr_t a_address, const std::uintptr_t a_size) noexcept :
_proxyBase(a_proxyBase), _address(a_address), _size(a_size)
{}

[[nodiscard]] std::uintptr_t address() const noexcept { return _address; }
[[nodiscard]] std::size_t offset() const noexcept { return address() - _proxyBase; }
[[nodiscard]] std::size_t size() const noexcept { return _size; }
[[nodiscard]] constexpr std::uintptr_t address() const noexcept { return _address; }
[[nodiscard]] constexpr std::size_t offset() const noexcept { return address() - _proxyBase; }
[[nodiscard]] constexpr std::size_t size() const noexcept { return _size; }

[[nodiscard]] void* pointer() const noexcept
[[nodiscard]] constexpr void* pointer() const noexcept
{
return std::bit_cast<void*>(address());
}

template <class T>
[[nodiscard]] T* pointer() const noexcept
[[nodiscard]] constexpr T* pointer() const noexcept
{
return static_cast<T*>(pointer());
}
Expand All @@ -54,7 +54,7 @@ namespace REL
public:
constexpr Module() = delete;
explicit Module(std::uintptr_t a_base);
explicit Module(std::string_view a_filePath);
explicit constexpr Module(std::string_view a_filePath);

[[nodiscard]] constexpr auto base() const noexcept { return _base; }

Expand Down
18 changes: 9 additions & 9 deletions CommonLibSF/include/REL/Relocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ namespace REL
static constexpr bool is_x64_pod_v = is_x64_pod<T>::value;

template <class F, class First, class... Rest>
decltype(auto) invoke_member_function_non_pod(F&& a_func, First&& a_first, Rest&&... a_rest) //
constexpr decltype(auto) invoke_member_function_non_pod(F&& a_func, First&& a_first, Rest&&... a_rest) //
noexcept(std::is_nothrow_invocable_v<F, First, Rest...>)
{
using result_t = std::invoke_result_t<F, First, Rest...>;
Expand Down Expand Up @@ -133,7 +133,7 @@ namespace REL
inline constexpr std::uint8_t INT3 = 0xCC;

template <class F, class... Args>
std::invoke_result_t<F, Args...> invoke(F&& a_func, Args&&... a_args) //
constexpr std::invoke_result_t<F, Args...> invoke(F&& a_func, Args&&... a_args) //
noexcept(std::is_nothrow_invocable_v<F, Args...>)
requires(std::invocable<F, Args...>)
{
Expand Down Expand Up @@ -163,13 +163,13 @@ namespace REL
}

template <std::integral T>
void safe_write(const std::uintptr_t a_dst, const T& a_data)
constexpr void safe_write(const std::uintptr_t a_dst, const T& a_data)
{
safe_write(a_dst, std::addressof(a_data), sizeof(T));
}

template <class T>
void safe_write(const std::uintptr_t a_dst, std::span<T> a_data)
constexpr void safe_write(const std::uintptr_t a_dst, std::span<T> a_data)
{
safe_write(a_dst, a_data.data(), a_data.size_bytes());
}
Expand Down Expand Up @@ -228,7 +228,7 @@ namespace REL

[[nodiscard]] constexpr decltype(auto) address() const noexcept { return _address; }

[[nodiscard]] std::size_t offset() const noexcept { return _address - base(); }
[[nodiscard]] constexpr std::size_t offset() const noexcept { return _address - base(); }

[[nodiscard]] constexpr decltype(auto) operator*() const noexcept
requires(std::is_pointer_v<value_type>)
Expand All @@ -243,14 +243,14 @@ namespace REL
}

template <class... Args>
std::invoke_result_t<const value_type&, Args...> operator()(Args&&... a_args) const //
constexpr std::invoke_result_t<const value_type&, Args...> operator()(Args&&... a_args) const //
noexcept(std::is_nothrow_invocable_v<const value_type&, Args...>)
requires(std::invocable<const value_type&, Args...>)
{
return invoke(get(), std::forward<Args>(a_args)...);
}

std::uintptr_t write_vfunc(const std::size_t a_idx, const std::uintptr_t a_newFunc)
constexpr std::uintptr_t write_vfunc(const std::size_t a_idx, const std::uintptr_t a_newFunc)
requires(std::same_as<value_type, std::uintptr_t>)
{
const auto addr = address() + (sizeof(void*) * a_idx);
Expand All @@ -260,14 +260,14 @@ namespace REL
}

template <class F>
std::uintptr_t write_vfunc(const std::size_t a_idx, F a_newFunc)
constexpr std::uintptr_t write_vfunc(const std::size_t a_idx, F a_newFunc)
requires(std::same_as<value_type, std::uintptr_t>)
{
return write_vfunc(a_idx, stl::unrestricted_cast<std::uintptr_t>(a_newFunc));
}

private:
[[nodiscard]] static std::uintptr_t base() { return Module::get().base(); }
[[nodiscard]] static constexpr std::uintptr_t base() { return Module::get().base(); }

std::uintptr_t _address{};
};
Expand Down
8 changes: 4 additions & 4 deletions CommonLibSF/include/REL/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace REL

[[nodiscard]] constexpr decltype(auto) cend() const noexcept { return _impl.cend(); }

[[nodiscard]] std::strong_ordering constexpr compare(const Version& a_rhs) const noexcept
[[nodiscard]] constexpr std::strong_ordering compare(const Version& a_rhs) const noexcept
{
for (std::size_t i = 0; i < _impl.size(); ++i) {
if ((*this)[i] != a_rhs[i]) {
Expand All @@ -55,7 +55,7 @@ namespace REL
[[nodiscard]] constexpr value_type patch() const noexcept { return _impl[2]; }
[[nodiscard]] constexpr value_type build() const noexcept { return _impl[3]; }

[[nodiscard]] std::string string(const std::string_view a_separator = "."sv) const
[[nodiscard]] constexpr std::string string(const std::string_view a_separator = "."sv) const
{
std::string result;
for (auto&& ver : _impl) {
Expand All @@ -66,7 +66,7 @@ namespace REL
return result;
}

[[nodiscard]] std::wstring wstring(const std::wstring_view a_separator = L"."sv) const
[[nodiscard]] constexpr std::wstring wstring(const std::wstring_view a_separator = L"."sv) const
{
std::wstring result;
for (auto&& ver : _impl) {
Expand Down Expand Up @@ -155,7 +155,7 @@ namespace std
struct formatter<REL::Version, CharT> : formatter<std::string, CharT>
{
template <class FormatContext>
auto format(const REL::Version a_version, FormatContext& a_ctx) const
constexpr auto format(const REL::Version a_version, FormatContext& a_ctx) const
{
return formatter<std::string, CharT>::format(a_version.string(), a_ctx);
}
Expand Down
8 changes: 4 additions & 4 deletions CommonLibSF/include/SFSE/IAT.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace SFSE
[[nodiscard]] std::uintptr_t GetIATAddr(std::string_view a_dll, std::string_view a_function);
[[nodiscard]] std::uintptr_t GetIATAddr(void* a_module, std::string_view a_dll, std::string_view a_function);

[[nodiscard]] void* GetIATPtr(std::string_view a_dll, std::string_view a_function);
[[nodiscard]] constexpr void* GetIATPtr(std::string_view a_dll, std::string_view a_function);

template <class T>
[[nodiscard]] T* GetIATPtr(std::string_view a_dll, std::string_view a_function)
[[nodiscard]] constexpr T* GetIATPtr(std::string_view a_dll, std::string_view a_function)
{
return static_cast<T*>(GetIATPtr(std::move(a_dll), std::move(a_function)));
}

[[nodiscard]] void* GetIATPtr(void* a_module, std::string_view a_dll, std::string_view a_function);
[[nodiscard]] constexpr void* GetIATPtr(void* a_module, std::string_view a_dll, std::string_view a_function);

template <class T>
[[nodiscard]] T* GetIATPtr(void* a_module, std::string_view a_dll, std::string_view a_function)
Expand All @@ -24,7 +24,7 @@ namespace SFSE
std::uintptr_t PatchIAT(std::uintptr_t a_newFunc, std::string_view a_dll, std::string_view a_function);

template <class F>
std::uintptr_t PatchIAT(F a_newFunc, const std::string_view a_dll, const std::string_view a_function)
constexpr std::uintptr_t PatchIAT(F a_newFunc, const std::string_view a_dll, const std::string_view a_function)
{
return PatchIAT(stl::unrestricted_cast<std::uintptr_t>(a_newFunc), a_dll, a_function);
}
Expand Down
12 changes: 6 additions & 6 deletions CommonLibSF/include/SFSE/InputMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ namespace SFSE
kGamepadButtonOffset_RT // 281
};

std::uint32_t GamepadMaskToKeycode(std::uint32_t keyMask);
std::uint32_t GamepadKeycodeToMask(std::uint32_t keyCode);

std::string GetKeyName(std::uint32_t keyCode);
constexpr std::uint32_t GamepadMaskToKeycode(std::uint32_t keyMask);
constexpr std::uint32_t GamepadKeycodeToMask(std::uint32_t keyCode);
constexpr std::string GetKeyName(std::uint32_t keyCode);
std::string GetKeyboardKeyName(std::uint32_t keyCode);
std::string GetMouseButtonName(std::uint32_t keyCode);
std::string GetGamepadButtonName(std::uint32_t a_keyCode);
constexpr std::string GetMouseButtonName(std::uint32_t keyCode);
constexpr std::string GetGamepadButtonName(std::uint32_t a_keyCode);
}
}
Loading

0 comments on commit 8167b42

Please sign in to comment.