forked from blueyeti/jit.realsense
-
Notifications
You must be signed in to change notification settings - Fork 1
/
max_utils.hpp
128 lines (113 loc) · 4.13 KB
/
max_utils.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#pragma once
#include <jit.common.h>
#include <vector>
#include <array>
#include <cstdint>
template <typename T, typename U>
intptr_t get_offset(T U::*member)
{
return reinterpret_cast<intptr_t>(&(((U*) nullptr)->*member));
}
template <typename T, typename U>
intptr_t get_offset(T U::*member, int N)
{
return reinterpret_cast<intptr_t>(&((((U*) nullptr)->*member)[N]));
}
template <typename Type, typename T>
static void add_attribute_flags(std::string name, T Type::*member, long flags)
{
jit_class_addattr(Type::max_class,
(t_jit_object*) jit_object_new(
_jit_sym_jit_attr_offset,
name.c_str(),
_jit_sym_long,
flags,
(method)nullptr,
(method)nullptr,
get_offset(member)));
}
template <typename Type, typename T>
static void add_attribute(std::string name, T Type::*member)
{
add_attribute_flags(name, member, JIT_ATTR_GET_DEFER_LOW | JIT_ATTR_SET_USURP_LOW);
}
template <typename Type, typename T, typename Gadget>
static void add_output_attribute(std::string name, int num, T Gadget::*member)
{
const auto flags = JIT_ATTR_GET_DEFER_LOW | JIT_ATTR_SET_USURP_LOW;
const auto outputs_offset = get_offset(&Type::outputs, num);
jit_class_addattr(Type::max_class,
(t_jit_object*) jit_object_new(
_jit_sym_jit_attr_offset,
name.c_str(),
_jit_sym_long,
flags,
(method)nullptr,
(method)nullptr,
outputs_offset + get_offset(member)));
}
template <typename Type, typename T, typename Gadget>
static void add_array_output_attribute(std::string name, int num, T Gadget::*member)
{
const auto flags = JIT_ATTR_GET_DEFER_LOW | JIT_ATTR_SET_USURP_LOW;
const auto outputs_offset = get_offset(&Type::outputs, num);
jit_class_addattr(Type::max_class,
(t_jit_object*) jit_object_new(
_jit_sym_jit_attr_offset_array,
name.c_str(),
_jit_sym_long,
2,
flags,
(method)nullptr,
(method)nullptr,
outputs_offset + get_offset(member) - sizeof(long),
outputs_offset + get_offset(member)));
}
template <typename Type, typename T, typename Gadget>
static void add_dynamic_attribute(t_object *ob, std::string name, int num, int size, T Gadget::*member)
{
const auto outputs_offset = get_offset(&Type::outputs, num);
t_object *attr = nullptr;
if(size == 1) {
attr = attr_offset_new(name.c_str(), _jit_sym_long, 0, (method)nullptr, (method)nullptr, outputs_offset + get_offset(member));
}
else {
attr = attr_offset_array_new(name.c_str(), _jit_sym_long, size, 0, (method)nullptr, (method)nullptr, outputs_offset + get_offset(member) - sizeof(long), outputs_offset + get_offset(member));
}
if(attr) {
jit_object_addattr(ob, attr);
object_attr_addattr_parse(ob, name.c_str(), "dynamicattr", gensym("long"), 0, "1");
}
}
void class_attr_enumindex_rec(t_atom*)
{
}
template<typename Arg, typename... Args>
void class_attr_enumindex_rec(t_atom* aaa, Arg&& arg, Args&&... args)
{
atom_setsym(aaa, gensym_tr(arg));
class_attr_enumindex_rec(aaa + 1, std::forward<Args>(args)...);
}
template<typename... Args>
void class_attr_enumindex(t_class* theclass, std::string attrname, Args&&... args)
{
constexpr int num = sizeof...(Args);
t_atom aaa[num];
CLASS_ATTR_STYLE(theclass, attrname.c_str(), 0, "enumindex");
class_attr_enumindex_rec(aaa, std::forward<Args>(args)...);
CLASS_ATTR_ATTR_ATOMS(theclass, attrname.c_str(), "enumvals", USESYM(atom), 1, num, aaa);
}
template<typename T, typename... Args>
T* jit_new(t_class* cls, Args&&... args)
{
auto obj = jit_object_alloc(cls);
if(obj)
{
t_object tmp;
memcpy(&tmp, obj, sizeof(t_object));
auto x = new(obj) T{std::forward<Args>(args)...};
memcpy(x, &tmp, sizeof(t_object));
return x;
}
return nullptr;
}