plugify 1.0.0.0
Loading...
Searching...
No Matches
callback.hpp
1#pragma once
2
3#include <asmjit/asmjit.h>
4#include <plugify/mem_addr.hpp>
5#include <plugify/method.hpp>
6#include <string_view>
7#include <utility>
8#include <memory>
9
10namespace plugify {
20 public:
25 explicit JitCallback(std::weak_ptr<asmjit::JitRuntime> rt);
26
31 JitCallback(const JitCallback& other) = delete;
32
37 JitCallback(JitCallback&& other) noexcept;
38
43
48 struct Parameters {
56 template<typename T>
57 void SetArgument(size_t idx, T val) const noexcept {
58 *(T*) GetArgumentPtr(idx) = val;
59 }
60
69 template<typename T>
70 void SetArgumentAt(size_t idx, T val, size_t i = 0) const noexcept {
71 (*(T**) GetArgumentPtr(idx))[i] = val;
72 }
73
80 template<typename T>
81 T GetArgument(size_t idx) const noexcept {
82 return *(T*) GetArgumentPtr(idx);
83 }
84
90 int8_t* GetArgumentPtr(size_t idx) const noexcept {
91 return ((int8_t*) &arguments) + sizeof(uint64_t) * idx;
92 }
93
94 private:
95 volatile uint64_t arguments;
96 };
97
102 struct Return {
107 template<typename T, typename...Args>
108 void ConstructAt(Args&&... args) const noexcept {
109 std::construct_at((T*) GetReturnPtr(), std::forward<Args>(args)...);
110 }
111
118 template<typename T>
119 void SetReturn(T val) const noexcept {
120 *(T*) GetReturnPtr() = val;
121 }
122
128 template<typename T>
129 T GetReturn() const noexcept {
130 return *(T*) GetReturnPtr();
131 }
132
137 int8_t* GetReturnPtr() const noexcept {
138 return (int8_t*) &ret;
139 }
140
141 private:
142 volatile uint64_t ret;
143 };
144
145 using CallbackHandler = void(*)(MethodHandle method, MemAddr data, const Parameters* params, size_t count, const Return* ret);
146 using HiddenParam = bool(*)(ValueType);
147
157 MemAddr GetJitFunc(const asmjit::FuncSignature& sig, MethodHandle method, CallbackHandler callback, MemAddr data, bool hidden);
158
175 MemAddr GetJitFunc(MethodHandle method, CallbackHandler callback, MemAddr data = nullptr, HiddenParam hidden = &ValueUtils::IsHiddenParam);
176
182 MemAddr GetFunction() const noexcept { return _function; }
183
190 MemAddr GetUserData() const noexcept { return _userData; }
191
196 std::string_view GetError() noexcept { return !_function && _errorCode ? _errorCode : ""; }
197
204 JitCallback& operator=(const JitCallback& other) = delete;
205
214
215 private:
216 std::weak_ptr<asmjit::JitRuntime> _rt;
217 MemAddr _function;
218 union {
219 MemAddr _userData;
220 const char* _errorCode{};
221 };
222 };
223} // namespace plugify
Class to create callback objects, that can be passed to functions as callback function pointers....
Definition callback.hpp:19
MemAddr GetJitFunc(const asmjit::FuncSignature &sig, MethodHandle method, CallbackHandler callback, MemAddr data, bool hidden)
Get a dynamically created callback function based on the raw signature.
MemAddr GetFunction() const noexcept
Get a dynamically created function.
Definition callback.hpp:182
JitCallback(std::weak_ptr< asmjit::JitRuntime > rt)
Constructor.
std::string_view GetError() noexcept
Get the error message, if any.
Definition callback.hpp:196
MemAddr GetUserData() const noexcept
Get the user data associated with the object.
Definition callback.hpp:190
JitCallback & operator=(const JitCallback &other)=delete
Copy assignment operator for JitCallback.
MemAddr GetJitFunc(MethodHandle method, CallbackHandler callback, MemAddr data=nullptr, HiddenParam hidden=&ValueUtils::IsHiddenParam)
Get a dynamically created function based on the method.
JitCallback & operator=(JitCallback &&other) noexcept
Move assignment operator for JitCall.
~JitCallback()
Destructor.
JitCallback(JitCallback &&other) noexcept
Move constructor.
JitCallback(const JitCallback &other)=delete
Copy constructor.
A wrapper class for memory addresses, providing utility functions for pointer manipulation.
Definition mem_addr.hpp:11
A handle class for the Method structure.
Definition method.hpp:105
constexpr bool IsHiddenParam(ValueType type) noexcept
Checks if a given ValueType is considered a hidden object parameter.
Structure to represent function parameters.
Definition callback.hpp:48
T GetArgument(size_t idx) const noexcept
Get the value of the argument at the specified index.
Definition callback.hpp:81
void SetArgumentAt(size_t idx, T val, size_t i=0) const noexcept
Set the value of the argument at the specified index and position within a multi-dimensional array.
Definition callback.hpp:70
void SetArgument(size_t idx, T val) const noexcept
Set the value of the argument at the specified index.
Definition callback.hpp:57
int8_t * GetArgumentPtr(size_t idx) const noexcept
Get a pointer to the argument at the specified index.
Definition callback.hpp:90
void ConstructAt(Args &&... args) const noexcept
Constructs an object of type T at the memory location.
Definition callback.hpp:108
int8_t * GetReturnPtr() const noexcept
Get a pointer to the return value.
Definition callback.hpp:137
T GetReturn() const noexcept
Get the return value.
Definition callback.hpp:129
void SetReturn(T val) const noexcept
Set the return value.
Definition callback.hpp:119