CommonLibSSE (Parapets fork)
Loading...
Searching...
No Matches
NativeLatentFunction.h
Go to the documentation of this file.
1#pragma once
2
3#include "RE/B/BSCoreTypes.h"
7
8namespace RE
9{
10 namespace BSScript
11 {
12 template <class R>
13 struct LatentPromise;
14
15 template <class R = void, class = std::enable_if_t<is_valid_return_v<R>>>
16 struct LatentResult : std::coroutine_handle<LatentPromise<R>>
17 {
18 using super = std::coroutine_handle<LatentPromise<R>>;
19 using result_type = R;
21
22 constexpr LatentResult() noexcept = default;
23
24 LatentResult(const super& a_other) :
25 super(a_other) {}
26
27 LatentResult(super&& a_other) :
28 super(std::forward<super>(a_other)) {}
29
30 operator bool() const noexcept { return true; }
31 };
32
34 {
36 vm(a_vm), stackID(a_stackID)
37 {
38 }
39
41 {
42 if (!returned) {
44 }
45 }
46
47 virtual Variable GetResult() = 0;
48
50 {
51 returned = true;
53 }
54
55 std::suspend_never initial_suspend() { return {}; }
56
57 std::suspend_always final_suspend() noexcept { return {}; }
58
59 void unhandled_exception() { exception = std::current_exception(); }
60
63 bool returned{ false };
64 std::exception_ptr exception;
65 };
66
67 template <class R>
68 struct LatentPromise final : public LatentPromiseBase
69 {
70 template <class... Args>
71 LatentPromise(IVirtualMachine* a_vm, VMStackID a_stackID, Args...) :
72 LatentPromiseBase(a_vm, a_stackID)
73 {
74 }
75
77 {
78 Variable var;
79 var.Pack(value);
80 return var;
81 }
82
84
85 void return_value(R&& a_value)
86 {
87 value = std::forward<R>(a_value);
89 }
90
92 };
93
94 template <>
95 struct LatentPromise<void> final : public LatentPromiseBase
96 {
97 template <class... Args>
98 LatentPromise(IVirtualMachine* a_vm, VMStackID a_stackID, Args...) :
99 LatentPromiseBase(a_vm, a_stackID)
100 {
101 }
102
103 Variable GetResult() override { return {}; }
104
106
108 };
109
110 template <class F, class R, class Base, class... Args>
111 class NativeLatentFunction : public NativeFunction<true, F, bool, Base, Args...>
112 {
113 private:
114 using super = NativeFunction<true, F, bool, Base, Args...>;
115
116 public:
117 using result_type = typename R::result_type;
118 using base_type = typename super::base_type;
120
121 using super::super;
122
123 NativeLatentFunction(std::string_view a_fnName, std::string_view a_className, function_type a_callback) :
124 super(a_fnName, a_className, a_callback)
125 {
126 super::_retType = GetRawType<result_type>();
127 super::_isLatent = true;
128 }
129 };
130 }
131
132 template <class Int, class R, class Cls, class... Args>
133 class NativeFunction<R(BSScript::IVirtualMachine*, Int, Cls, Args...), std::enable_if_t<BSScript::is_valid_latent_sig_v<Int, R, Cls, Args...>>> :
134 public BSScript::NativeLatentFunction<R(BSScript::IVirtualMachine*, Int, Cls, Args...), R, Cls, Args...>
135 {
136 private:
137 using super = BSScript::NativeLatentFunction<R(BSScript::IVirtualMachine*, Int, Cls, Args...), R, Cls, Args...>;
138
139 public:
141 using base_type = typename super::base_type;
143
144 using super::super;
145 };
146}
Definition: IVirtualMachine.h:39
virtual void ReturnFromLatent(VMStackID a_stackID, const Variable &a_val)=0
TypeInfo _retType
Definition: NativeFunctionBase.h:62
bool _isLatent
Definition: NativeFunctionBase.h:66
Definition: NativeFunction.h:55
Definition: NativeLatentFunction.h:112
typename super::base_type base_type
Definition: NativeLatentFunction.h:118
NativeLatentFunction(std::string_view a_fnName, std::string_view a_className, function_type a_callback)
Definition: NativeLatentFunction.h:123
typename R::result_type result_type
Definition: NativeLatentFunction.h:117
typename super::function_type function_type
Definition: NativeLatentFunction.h:119
Definition: Variable.h:15
void Pack(T &&a_src)
Definition: PackUnpack.h:292
Definition: NativeFunction.h:121
Definition: AbsorbEffect.h:6
std::uint32_t VMStackID
Definition: BSCoreTypes.h:8
Definition: NiBinaryStream.h:94
Variable GetResult() override
Definition: NativeLatentFunction.h:103
LatentResult get_return_object()
Definition: NativeLatentFunction.h:105
void return_void()
Definition: NativeLatentFunction.h:107
LatentPromise(IVirtualMachine *a_vm, VMStackID a_stackID, Args...)
Definition: NativeLatentFunction.h:98
Definition: NativeLatentFunction.h:34
bool returned
Definition: NativeLatentFunction.h:63
virtual Variable GetResult()=0
std::exception_ptr exception
Definition: NativeLatentFunction.h:64
VMStackID stackID
Definition: NativeLatentFunction.h:62
std::suspend_always final_suspend() noexcept
Definition: NativeLatentFunction.h:57
LatentPromiseBase(IVirtualMachine *a_vm, VMStackID a_stackID)
Definition: NativeLatentFunction.h:35
IVirtualMachine * vm
Definition: NativeLatentFunction.h:61
virtual ~LatentPromiseBase()
Definition: NativeLatentFunction.h:40
void unhandled_exception()
Definition: NativeLatentFunction.h:59
void ReturnFromLatent()
Definition: NativeLatentFunction.h:49
std::suspend_never initial_suspend()
Definition: NativeLatentFunction.h:55
Definition: NativeLatentFunction.h:69
LatentResult< R > get_return_object()
Definition: NativeLatentFunction.h:83
LatentPromise(IVirtualMachine *a_vm, VMStackID a_stackID, Args...)
Definition: NativeLatentFunction.h:71
R value
Definition: NativeLatentFunction.h:91
Variable GetResult() override
Definition: NativeLatentFunction.h:76
void return_value(R &&a_value)
Definition: NativeLatentFunction.h:85
Definition: NativeLatentFunction.h:17
R result_type
Definition: NativeLatentFunction.h:19
std::coroutine_handle< LatentPromise< R > > super
Definition: NativeLatentFunction.h:18
constexpr LatentResult() noexcept=default
LatentResult(super &&a_other)
Definition: NativeLatentFunction.h:27