plugify 1.2.8
Loading...
Searching...
No Matches
callback.hpp
1#pragma once
2
3#include <memory>
4#include <string>
5#include <utility>
6
7#include "plugify/global.h"
8#include "plugify/mem_addr.hpp"
9#include "plugify/method.hpp"
10#include "plugify/property.hpp"
11#include "plugify/signarure.hpp"
12#include "plugify/value_type.hpp"
13#include "plg/inplace_vector.hpp"
14
15namespace plugify {
24 class PLUGIFY_API JitCallback {
25 public:
30
35 JitCallback(const JitCallback& other) = delete;
36
41 JitCallback(JitCallback&& other) noexcept;
42
47
55 using CallbackHandler = void (*)(
56 const Method* method,
57 MemAddr data,
58 uint64_t* params,
59 size_t count,
60 /*uint128_t*/ void* ret
61 );
62
67 using HiddenParam = bool (*)(ValueType);
68
82 const Signature& signature,
83 const Method* method,
84 CallbackHandler callback,
85 MemAddr data,
86 bool hidden
87 );
88
104 const Method& method,
105 CallbackHandler callback,
106 MemAddr data = nullptr,
107 HiddenParam hidden = &ValueUtils::IsHiddenParam
108 );
109
115 MemAddr GetFunction() const noexcept;
116
123 MemAddr GetUserData() const noexcept;
124
129 std::string_view GetError() noexcept;
130
137 JitCallback& operator=(const JitCallback& other) = delete;
138
146 JitCallback& operator=(JitCallback&& other) noexcept;
147
148 PLUGIFY_ACCESS : struct Impl;
149 PLUGIFY_NO_DLL_EXPORT_WARNING(std::unique_ptr<Impl> _impl;)
150 };
151
155 template <typename T>
156 concept SlotStorable = std::is_trivially_copyable_v<T> && sizeof(T) <= sizeof(uint64_t);
157
167 public:
168 using slot_type = uint64_t;
169
175 ParametersSpan(slot_type* data, size_t count) noexcept
176 : _data(data)
177 , _count(count) {
178 }
179
186 template <typename T>
187 requires SlotStorable<T>
188 T Get(size_t index) const noexcept {
189 assert(index < _count && "Index out of bounds");
190 T result{};
191 std::memcpy(&result, &_data[index], sizeof(T));
192 return result;
193 }
194
201 template <typename T>
202 requires SlotStorable<T>
203 void Set(size_t index, const T& value) noexcept {
204 assert(index < _count && "Index out of bounds");
205 std::memcpy(&_data[index], &value, sizeof(T));
206 }
207
208 private:
209 slot_type* _data;
210 [[maybe_unused]] size_t _count;
211 };
212
222 public:
223 using type = void;
224
230 explicit ReturnSlot(type* data, size_t size) noexcept
231 : _data(data)
232 , _size(size) {
233 }
234
240 template <typename T>
241 requires std::is_trivially_copyable_v<T>
242 void Set(const T& value) noexcept {
243 assert(sizeof(T) <= _size && "Return value too large");
244 std::memcpy(_data, &value, sizeof(T));
245 }
246
252 template <typename T>
253 requires std::is_trivially_copyable_v<T>
254 T Get() const noexcept {
255 assert(sizeof(T) <= _size && "Return type too large");
256 T result{};
257 std::memcpy(&result, _data, sizeof(T));
258 return result;
259 }
260
267 template <typename T, typename... Args>
268 // requires std::is_trivially_destructible_v<T>
269 void Construct(Args&&... args) noexcept(noexcept(T(std::forward<Args>(args)...))) {
270 assert(sizeof(T) <= _size && "Type too large");
271 std::construct_at(reinterpret_cast<T*>(_data), std::forward<Args>(args)...);
272 }
273
274 private:
275 type* _data;
276 [[maybe_unused]] size_t _size;
277 };
278} // namespace plugify
Class to create callback objects, that can be passed to functions as callback function pointers....
Definition callback.hpp:24
MemAddr GetFunction() const noexcept
Get a dynamically created function.
MemAddr GetJitFunc(const Method &method, CallbackHandler callback, MemAddr data=nullptr, HiddenParam hidden=&ValueUtils::IsHiddenParam)
Generates a callback function matching the specified method signature.
MemAddr GetJitFunc(const Signature &signature, const Method *method, CallbackHandler callback, MemAddr data, bool hidden)
Generates a JIT-compiled callback function based on the provided raw signature.
JitCallback()
Constructor.
~JitCallback()
Destructor.
JitCallback(JitCallback &&other) noexcept
Move constructor.
JitCallback(const JitCallback &other)=delete
Copy constructor.
void(*)(const Method *method, MemAddr data, uint64_t *params, size_t count, void *ret) CallbackHandler
CallbackHandler is a function pointer type for generic callback invocation. The params argument can b...
Definition callback.hpp:61
bool(*)(ValueType) HiddenParam
HiddenParam is a predicate function pointer to determine if a ValueType should be passed as a hidden ...
Definition callback.hpp:67
A wrapper class for memory addresses, providing utility functions for pointer manipulation.
Definition mem_addr.hpp:12
Provides access to a span of parameters stored in register-sized slots.
Definition callback.hpp:166
ParametersSpan(slot_type *data, size_t count) noexcept
Constructs a ParametersSpan.
Definition callback.hpp:175
void Set(size_t index, const T &value) noexcept
Set a value in the parameter span.
Definition callback.hpp:203
T Get(size_t index) const noexcept
Get a value from the parameter span.
Definition callback.hpp:188
Provides a storage area for return values in JIT/ABI contexts.
Definition callback.hpp:221
ReturnSlot(type *data, size_t size) noexcept
Construct from byte storage.
Definition callback.hpp:230
T Get() const noexcept
Get the return value.
Definition callback.hpp:254
void Set(const T &value) noexcept
Set the return value.
Definition callback.hpp:242
void Construct(Args &&... args) noexcept(noexcept(T(std::forward< Args >(args)...)))
Construct an object in-place.
Definition callback.hpp:269
Concept for types that can be stored directly in a register slot.
Definition callback.hpp:156
Replacement for asmjit::FuncSignature using ValueType.
Definition signarure.hpp:44