forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorHelper.h
More file actions
120 lines (99 loc) · 3.55 KB
/
VectorHelper.h
File metadata and controls
120 lines (99 loc) · 3.55 KB
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
//
// TM & (c) 2021 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
/**
* Include this in every file that defines Emscripten bindings for functions with
* std::vector parameters or return types, to automatically convert them to / from JS arrays.
* It actually doesn't hurt to include this in every binding file ;)
* Note that this only works for types that are known to Emscripten, i.e. primitive (built-in) types
* and types that have bindings defined.
*/
#ifndef JSMATERIALX_VECTOR_HELPERS_H
#define JSMATERIALX_VECTOR_HELPERS_H
#ifdef __EMSCRIPTEN__
#include <emscripten/bind.h>
#include <memory>
#include <vector>
namespace emscripten {
namespace internal {
template<typename T>
struct TypeID<std::vector<T>> {
static constexpr TYPEID get() {
return LightTypeID<val>::get();
}
};
template<typename T>
struct TypeID<const std::vector<T>> {
static constexpr TYPEID get() {
return LightTypeID<val>::get();
}
};
template<typename T>
struct TypeID<std::vector<T>&> {
static constexpr TYPEID get() {
return LightTypeID<val>::get();
}
};
template<typename T>
struct TypeID<const std::vector<T>&> {
static constexpr TYPEID get() {
return LightTypeID<val>::get();
}
};
template<typename T>
struct BindingType<std::vector<T>> {
using ValBinding = BindingType<val>;
using WireType = ValBinding::WireType;
static WireType toWireType(const std::vector<T> &vec) {
WireType result = ValBinding::toWireType(val::array(vec));
return result;
}
static std::vector<T> fromWireType(WireType value) {
return vecFromJSArray<T>(ValBinding::fromWireType(value));
}
};
// std<bool> are stored using bits, therefore the default emscripten
// implementation doesn't work for them and need to be specialized
template <>
struct BindingType<std::vector<bool>> {
using ValBinding = BindingType<val>;
using WireType = ValBinding::WireType;
static WireType toWireType(const std::vector<bool> &vec) {
val out = val::array();
for (auto i: vec) {
out.call<void>("push", i == 1 ? true : false);
}
WireType result = ValBinding::toWireType(out);
return result;
}
static std::vector<bool> fromWireType(WireType value) {
return vecFromJSArray<bool>(ValBinding::fromWireType(value));
}
};
/**
* Vectors of smart pointers need special treatment. The above generic toWireType definition uses val::array(vec),
* which constructs a val::array using val::array.call<void>("push", element). This leads to invalid (deleted) smart
* pointers on the JS side, since the generated code constructs the smart pointer object, pushes it into a JS array,
* and then deletes the smart pointer object (i.e. sets the ref count to 0). Using val::array.set() doesn't suffer from
* this issue.
*/
template<typename T>
struct BindingType<std::vector<std::shared_ptr<T>>> {
using ValBinding = BindingType<val>;
using WireType = ValBinding::WireType;
static WireType toWireType(const std::vector<std::shared_ptr<T>> &vec) {
auto arr = val::array();
for (int i = 0; i < vec.size(); ++i) {
arr.set(i, vec.at(i));
}
return ValBinding::toWireType(arr);
}
static std::vector<std::shared_ptr<T>> fromWireType(WireType value) {
return vecFromJSArray<std::shared_ptr<T>>(ValBinding::fromWireType(value));
}
};
} // namespace internal
} // namespace emscripten
#endif // __EMSCRIPTEN__
#endif // JSMATERIALX_VECTOR_HELPERS_H