summary refs log tree commit diff stats
path: root/generator/godot_variant.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'generator/godot_variant.cpp')
-rw-r--r--generator/godot_variant.cpp69
1 files changed, 69 insertions, 0 deletions
diff --git a/generator/godot_variant.cpp b/generator/godot_variant.cpp new file mode 100644 index 0000000..dd1ad33 --- /dev/null +++ b/generator/godot_variant.cpp
@@ -0,0 +1,69 @@
1#include "godot_variant.h"
2
3#include <stdexcept>
4
5bool GodotVariant::operator<(const GodotVariant& rhs) const {
6 if (value.index() != rhs.value.index()) {
7 return value.index() < rhs.value.index();
8 }
9
10 if (const int32_t* pval = std::get_if<int32_t>(&value)) {
11 return *pval < std::get<int32_t>(rhs.value);
12 } else if (const std::string* pval = std::get_if<std::string>(&value)) {
13 return *pval < std::get<std::string>(rhs.value);
14 } else if (const std::map<GodotVariant, GodotVariant>* pval =
15 std::get_if<std::map<GodotVariant, GodotVariant>>(&value)) {
16 return *pval < std::get<std::map<GodotVariant, GodotVariant>>(rhs.value);
17 } else if (const std::vector<GodotVariant>* pval =
18 std::get_if<std::vector<GodotVariant>>(&value)) {
19 return *pval < std::get<std::vector<GodotVariant>>(rhs.value);
20 }
21
22 throw std::invalid_argument("Incomparable variants.");
23}
24
25void GodotVariant::Serialize(std::basic_ostream<char>& output) const {
26 if (const int32_t* pval = std::get_if<int32_t>(&value)) {
27 uint32_t typebits = 2;
28 output.write(reinterpret_cast<char*>(&typebits), 4);
29 output.write(reinterpret_cast<const char*>(pval), 4);
30 } else if (const std::string* pval = std::get_if<std::string>(&value)) {
31 uint32_t typebits = 4;
32 output.write(reinterpret_cast<char*>(&typebits), 4);
33
34 uint32_t lengthbits = pval->length();
35 output.write(reinterpret_cast<char*>(&lengthbits), 4);
36
37 output.write(pval->data(), pval->length());
38
39 if (pval->length() % 4 != 0) {
40 int padding = 4 - (pval->length() % 4);
41 for (int i = 0; i < padding; i++) {
42 output.put(0);
43 }
44 }
45 } else if (const std::map<GodotVariant, GodotVariant>* pval =
46 std::get_if<std::map<GodotVariant, GodotVariant>>(&value)) {
47 uint32_t typebits = 18;
48 output.write(reinterpret_cast<char*>(&typebits), 4);
49
50 uint32_t lengthbits = pval->size() & 0x7fffffff;
51 output.write(reinterpret_cast<char*>(&lengthbits), 4);
52
53 for (const auto& mapping : *pval) {
54 mapping.first.Serialize(output);
55 mapping.second.Serialize(output);
56 }
57 } else if (const std::vector<GodotVariant>* pval =
58 std::get_if<std::vector<GodotVariant>>(&value)) {
59 uint32_t typebits = 19;
60 output.write(reinterpret_cast<char*>(&typebits), 4);
61
62 uint32_t lengthbits = pval->size() & 0x7fffffff;
63 output.write(reinterpret_cast<char*>(&lengthbits), 4);
64
65 for (const auto& entry : *pval) {
66 entry.Serialize(output);
67 }
68 }
69}