Debian Codegraph @

core/io/marshalls.h

raw symbols SCIP
1 /**************************************************************************/
2 /* marshalls.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/math/math_defs.h"
34 #include "core/object/ref_counted.h"
35 #include "core/typedefs.h"
36 #include "core/variant/variant.h"
37
38 // uintr_t is only for pairing with real_t, and we only need it in here.
39 #ifdef REAL_T_IS_DOUBLE
40 typedef uint64_t uintr_t;
41 #else
42 typedef uint32_t uintr_t;
43 #endif
44
45 /**
46 * Miscellaneous helpers for marshaling data types, and encoding
47 * in an endian independent way
48 */
49
50 union MarshallFloat {
51 uint32_t i; ///< int
52 float f; ///< float
53 };
54
55 union MarshallDouble {
56 uint64_t l; ///< long long
57 double d; ///< double
58 };
59
60 // Behaves like one of the above, depending on compilation setting.
61 union MarshallReal {
62 uintr_t i;
63 real_t r;
64 };
65
66 static inline unsigned int encode_uint16(uint16_t p_uint, uint8_t *p_arr) {
67 for (int i = 0; i < 2; i++) {
68 *p_arr = p_uint & 0xFF;
69 p_arr++;
70 p_uint >>= 8;
71 }
72
73 return sizeof(uint16_t);
74 }
75
76 static inline unsigned int encode_uint32(uint32_t p_uint, uint8_t *p_arr) {
77 for (int i = 0; i < 4; i++) {
78 *p_arr = p_uint & 0xFF;
79 p_arr++;
80 p_uint >>= 8;
81 }
82
83 return sizeof(uint32_t);
84 }
85
86 static inline unsigned int encode_half(float p_float, uint8_t *p_arr) {
87 encode_uint16(Math::make_half_float(p_float), p_arr);
88
89 return sizeof(uint16_t);
90 }
91
92 static inline unsigned int encode_float(float p_float, uint8_t *p_arr) {
93 MarshallFloat mf;
94 mf.f = p_float;
95 encode_uint32(mf.i, p_arr);
96
97 return sizeof(uint32_t);
98 }
99
100 static inline unsigned int encode_uint64(uint64_t p_uint, uint8_t *p_arr) {
101 for (int i = 0; i < 8; i++) {
102 *p_arr = p_uint & 0xFF;
103 p_arr++;
104 p_uint >>= 8;
105 }
106
107 return sizeof(uint64_t);
108 }
109
110 static inline unsigned int encode_double(double p_double, uint8_t *p_arr) {
111 MarshallDouble md;
112 md.d = p_double;
113 encode_uint64(md.l, p_arr);
114
115 return sizeof(uint64_t);
116 }
117
118 static inline unsigned int encode_uintr(uintr_t p_uint, uint8_t *p_arr) {
119 for (size_t i = 0; i < sizeof(uintr_t); i++) {
120 *p_arr = p_uint & 0xFF;
121 p_arr++;
122 p_uint >>= 8;
123 }
124
125 return sizeof(uintr_t);
126 }
127
128 static inline unsigned int encode_real(real_t p_real, uint8_t *p_arr) {
129 MarshallReal mr;
130 mr.r = p_real;
131 encode_uintr(mr.i, p_arr);
132
133 return sizeof(uintr_t);
134 }
135
136 static inline int encode_cstring(const char *p_string, uint8_t *p_data) {
137 int len = 0;
138
139 while (*p_string) {
140 if (p_data) {
141 *p_data = (uint8_t)*p_string;
142 p_data++;
143 }
144 p_string++;
145 len++;
146 }
147
148 if (p_data) {
149 *p_data = 0;
150 }
151 return len + 1;
152 }
153
154 static inline uint16_t decode_uint16(const uint8_t *p_arr) {
155 uint16_t u = 0;
156
157 for (int i = 0; i < 2; i++) {
158 uint16_t b = *p_arr;
159 b <<= (i * 8);
160 u |= b;
161 p_arr++;
162 }
163
164 return u;
165 }
166
167 static inline uint32_t decode_uint32(const uint8_t *p_arr) {
168 uint32_t u = 0;
169
170 for (int i = 0; i < 4; i++) {
171 uint32_t b = *p_arr;
172 b <<= (i * 8);
173 u |= b;
174 p_arr++;
175 }
176
177 return u;
178 }
179
180 static inline float decode_half(const uint8_t *p_arr) {
181 return Math::half_to_float(decode_uint16(p_arr));
182 }
183
184 static inline float decode_float(const uint8_t *p_arr) {
185 MarshallFloat mf;
186 mf.i = decode_uint32(p_arr);
187 return mf.f;
188 }
189
190 static inline uint64_t decode_uint64(const uint8_t *p_arr) {
191 uint64_t u = 0;
192
193 for (int i = 0; i < 8; i++) {
194 uint64_t b = (*p_arr) & 0xFF;
195 b <<= (i * 8);
196 u |= b;
197 p_arr++;
198 }
199
200 return u;
201 }
202
203 static inline double decode_double(const uint8_t *p_arr) {
204 MarshallDouble md;
205 md.l = decode_uint64(p_arr);
206 return md.d;
207 }
208
209 class EncodedObjectAsID : public RefCounted {
210 GDCLASS(EncodedObjectAsID, RefCounted);
211
212 ObjectID id;
213
214 protected:
215 static void _bind_methods();
216
217 public:
218 void set_object_id(ObjectID p_id);
219 ObjectID get_object_id() const;
220 };
221
222 Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = nullptr, bool p_allow_objects = false, int p_depth = 0);
223 Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects = false, int p_depth = 0);
224
225 Vector<float> vector3_to_float32_array(const Vector3 *vecs, size_t count);