VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
HookResult.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <type_traits>
5#include <utility>
6
7namespace VoltMod
8{
9
10/** What a before-handler decides should happen to the call it intercepted. */
11enum class HookAction : uint8_t
12{
13 Allow = 0, ///< Let the engine's own handler run and keep its return value.
14 Replace, ///< Let it run, but return this value instead of its own.
15 Block, ///< Skip it entirely and return this value.
16};
17
18/**
19 * @brief A hook before-handler's verdict.
20 *
21 * Default-constructs to @ref HookAction::Allow, so `return {};` passes the call through. A handler
22 * that returns nothing at all is treated the same way.
23 *
24 * Names no hooking machinery, so a header may return one without reaching
25 * `<VoltMod/Unsafe/Hook.hpp>`.
26 */
27template <class Ret>
29{
30 // A passing handler supplies no value, so one has to be synthesised for the engine.
31 static_assert(std::is_default_constructible_v<Ret>, "a hooked return type must default-construct");
32
33public:
34 HookResult() = default;
35
36 /** Run the original, then return @p value rather than what it produced. */
37 static HookResult Replace(Ret value) { return {HookAction::Replace, std::move(value)}; }
38
39 /** Do not run the original at all; return @p value instead. */
40 static HookResult Block(Ret value) { return {HookAction::Block, std::move(value)}; }
41
42 HookAction Action() const noexcept { return _action; }
43
44 /** Meaningless unless @ref Action is Replace or Block. */
45 const Ret& Value() const& noexcept { return _value; }
46 Ret Value() && { return std::move(_value); }
47
48private:
49 HookResult(HookAction action, Ret value) : _action(action), _value(std::move(value)) {}
50
52 Ret _value{};
53};
54
55/** Nothing to substitute, so there is no Replace: a void call either runs or it does not. */
56template <>
58{
59public:
60 HookResult() = default;
61
62 /** Skip the engine's own handler. */
64
65 HookAction Action() const noexcept { return _action; }
66
67private:
68 explicit HookResult(HookAction action) noexcept : _action(action) {}
69
71};
72
73} // namespace VoltMod
static HookResult Block()
HookAction Action() const noexcept
A hook before-handler's verdict.
static HookResult Replace(Ret value)
HookAction Action() const noexcept
static HookResult Block(Ret value)
const Ret & Value() const &noexcept
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
@ Replace
Let it run, but return this value instead of its own.
@ Allow
Let the engine's own handler run and keep its return value.
@ Block
Skip it entirely and return this value.