forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapHelper.h
More file actions
91 lines (73 loc) · 2.45 KB
/
MapHelper.h
File metadata and controls
91 lines (73 loc) · 2.45 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
//
// 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::unordered_map parameters or return types, to automatically convert them to / from JS objects.
* 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_MAP_HELPER_H
#define JSMATERIALX_MAP_HELPER_H
#ifdef __EMSCRIPTEN__
#include <emscripten/bind.h>
#include <memory>
#include <unordered_map>
namespace emscripten {
namespace internal {
template <typename T>
std::unordered_map<std::string, T> unorderedMapFromJSObject(const val& m) {
val keys = val::global("Object").call<val>("entries", m);
size_t length = keys["length"].as<size_t>();
std::unordered_map<std::string, T> rm;
for (size_t i = 0; i < length; ++i) {
rm.set(m[i][0].as<T>(), m[i][1].as<T>());
}
return rm;
}
template<typename T>
struct TypeID<std::unordered_map<std::string, T>> {
static constexpr TYPEID get() {
return LightTypeID<val>::get();
}
};
template<typename T>
struct TypeID<const std::unordered_map<std::string, T>> {
static constexpr TYPEID get() {
return LightTypeID<val>::get();
}
};
template<typename T>
struct TypeID<std::unordered_map<std::string, T>&> {
static constexpr TYPEID get() {
return LightTypeID<val>::get();
}
};
template<typename T>
struct TypeID<const std::unordered_map<std::string, T>&> {
static constexpr TYPEID get() {
return LightTypeID<val>::get();
}
};
template<typename T>
struct BindingType<std::unordered_map<std::string, T>> {
using ValBinding = BindingType<val>;
using WireType = ValBinding::WireType;
static WireType toWireType(const std::unordered_map<std::string, T> &map) {
val obj = val::object();
for (std::pair<std::string, T> element : map)
{
obj.set(element.first, element.second);
}
return ValBinding::toWireType(obj);
}
static std::unordered_map<std::string, T> fromWireType(WireType value) {
return unorderedMapFromJSObject<T>(ValBinding::fromWireType(value));
}
};
} // namespace internal
} // namespace emscripten
#endif // __EMSCRIPTEN__
#endif // JSMATERIALX_MAP_HELPER_H