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
+ //https://github.com/godotengine/godot/blob/1f787b63a54b74a5238653883fe1a531200b675e/core/io/marshalls.h#L167
32
+
33
+ #pragma once
34
+
35
+ #include <classes/ref_counted.hpp>
36
+ #include <variant/variant.hpp>
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_float (float p_float , uint8_t * p_arr ) {
87
+ MarshallFloat mf ;
88
+ mf .f = p_float ;
89
+ encode_uint32 (mf .i , p_arr );
90
+
91
+ return sizeof (uint32_t );
92
+ }
93
+
94
+ static inline unsigned int encode_uint64 (uint64_t p_uint , uint8_t * p_arr ) {
95
+ for (int i = 0 ; i < 8 ; i ++ ) {
96
+ * p_arr = p_uint & 0xFF ;
97
+ p_arr ++ ;
98
+ p_uint >>= 8 ;
99
+ }
100
+
101
+ return sizeof (uint64_t );
102
+ }
103
+
104
+ static inline unsigned int encode_double (double p_double , uint8_t * p_arr ) {
105
+ MarshallDouble md ;
106
+ md .d = p_double ;
107
+ encode_uint64 (md .l , p_arr );
108
+
109
+ return sizeof (uint64_t );
110
+ }
111
+
112
+ static inline unsigned int encode_uintr (uintr_t p_uint , uint8_t * p_arr ) {
113
+ for (size_t i = 0 ; i < sizeof (uintr_t ); i ++ ) {
114
+ * p_arr = p_uint & 0xFF ;
115
+ p_arr ++ ;
116
+ p_uint >>= 8 ;
117
+ }
118
+
119
+ return sizeof (uintr_t );
120
+ }
121
+
122
+ static inline unsigned int encode_real (real_t p_real , uint8_t * p_arr ) {
123
+ MarshallReal mr ;
124
+ mr .r = p_real ;
125
+ encode_uintr (mr .i , p_arr );
126
+
127
+ return sizeof (uintr_t );
128
+ }
129
+
130
+ static inline int encode_cstring (const char * p_string , uint8_t * p_data ) {
131
+ int len = 0 ;
132
+
133
+ while (* p_string ) {
134
+ if (p_data ) {
135
+ * p_data = (uint8_t )* p_string ;
136
+ p_data ++ ;
137
+ }
138
+ p_string ++ ;
139
+ len ++ ;
140
+ }
141
+
142
+ if (p_data ) {
143
+ * p_data = 0 ;
144
+ }
145
+ return len + 1 ;
146
+ }
147
+
148
+ static inline uint16_t decode_uint16 (const uint8_t * p_arr ) {
149
+ uint16_t u = 0 ;
150
+
151
+ for (int i = 0 ; i < 2 ; i ++ ) {
152
+ uint16_t b = * p_arr ;
153
+ b <<= (i * 8 );
154
+ u |= b ;
155
+ p_arr ++ ;
156
+ }
157
+
158
+ return u ;
159
+ }
160
+
161
+ static inline uint32_t decode_uint32 (const uint8_t * p_arr ) {
162
+ uint32_t u = 0 ;
163
+
164
+ for (int i = 0 ; i < 4 ; i ++ ) {
165
+ uint32_t b = * p_arr ;
166
+ b <<= (i * 8 );
167
+ u |= b ;
168
+ p_arr ++ ;
169
+ }
170
+
171
+ return u ;
172
+ }
173
+
174
+ static inline float decode_float (const uint8_t * p_arr ) {
175
+ MarshallFloat mf ;
176
+ mf .i = decode_uint32 (p_arr );
177
+ return mf .f ;
178
+ }
179
+
180
+ static inline uint64_t decode_uint64 (const uint8_t * p_arr ) {
181
+ uint64_t u = 0 ;
182
+
183
+ for (int i = 0 ; i < 8 ; i ++ ) {
184
+ uint64_t b = (* p_arr ) & 0xFF ;
185
+ b <<= (i * 8 );
186
+ u |= b ;
187
+ p_arr ++ ;
188
+ }
189
+
190
+ return u ;
191
+ }
192
+
193
+ static inline double decode_double (const uint8_t * p_arr ) {
194
+ MarshallDouble md ;
195
+ md .l = decode_uint64 (p_arr );
196
+ return md .d ;
197
+ }
0 commit comments