2
0

marshalls.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*************************************************************************/
  2. /* marshalls.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef MARSHALLS_H
  31. #define MARSHALLS_H
  32. #include "core/object/reference.h"
  33. #include "core/typedefs.h"
  34. #include "core/variant/variant.h"
  35. /**
  36. * Miscellaneous helpers for marshalling data types, and encoding
  37. * in an endian independent way
  38. */
  39. union MarshallFloat {
  40. uint32_t i; ///< int
  41. float f; ///< float
  42. };
  43. union MarshallDouble {
  44. uint64_t l; ///< long long
  45. double d; ///< double
  46. };
  47. static inline unsigned int encode_uint16(uint16_t p_uint, uint8_t *p_arr) {
  48. for (int i = 0; i < 2; i++) {
  49. *p_arr = p_uint & 0xFF;
  50. p_arr++;
  51. p_uint >>= 8;
  52. }
  53. return sizeof(uint16_t);
  54. }
  55. static inline unsigned int encode_uint32(uint32_t p_uint, uint8_t *p_arr) {
  56. for (int i = 0; i < 4; i++) {
  57. *p_arr = p_uint & 0xFF;
  58. p_arr++;
  59. p_uint >>= 8;
  60. }
  61. return sizeof(uint32_t);
  62. }
  63. static inline unsigned int encode_float(float p_float, uint8_t *p_arr) {
  64. MarshallFloat mf;
  65. mf.f = p_float;
  66. encode_uint32(mf.i, p_arr);
  67. return sizeof(uint32_t);
  68. }
  69. static inline unsigned int encode_uint64(uint64_t p_uint, uint8_t *p_arr) {
  70. for (int i = 0; i < 8; i++) {
  71. *p_arr = p_uint & 0xFF;
  72. p_arr++;
  73. p_uint >>= 8;
  74. }
  75. return sizeof(uint64_t);
  76. }
  77. static inline unsigned int encode_double(double p_double, uint8_t *p_arr) {
  78. MarshallDouble md;
  79. md.d = p_double;
  80. encode_uint64(md.l, p_arr);
  81. return sizeof(uint64_t);
  82. }
  83. static inline int encode_cstring(const char *p_string, uint8_t *p_data) {
  84. int len = 0;
  85. while (*p_string) {
  86. if (p_data) {
  87. *p_data = (uint8_t)*p_string;
  88. p_data++;
  89. }
  90. p_string++;
  91. len++;
  92. }
  93. if (p_data) {
  94. *p_data = 0;
  95. }
  96. return len + 1;
  97. }
  98. static inline uint16_t decode_uint16(const uint8_t *p_arr) {
  99. uint16_t u = 0;
  100. for (int i = 0; i < 2; i++) {
  101. uint16_t b = *p_arr;
  102. b <<= (i * 8);
  103. u |= b;
  104. p_arr++;
  105. }
  106. return u;
  107. }
  108. static inline uint32_t decode_uint32(const uint8_t *p_arr) {
  109. uint32_t u = 0;
  110. for (int i = 0; i < 4; i++) {
  111. uint32_t b = *p_arr;
  112. b <<= (i * 8);
  113. u |= b;
  114. p_arr++;
  115. }
  116. return u;
  117. }
  118. static inline float decode_float(const uint8_t *p_arr) {
  119. MarshallFloat mf;
  120. mf.i = decode_uint32(p_arr);
  121. return mf.f;
  122. }
  123. static inline uint64_t decode_uint64(const uint8_t *p_arr) {
  124. uint64_t u = 0;
  125. for (int i = 0; i < 8; i++) {
  126. uint64_t b = (*p_arr) & 0xFF;
  127. b <<= (i * 8);
  128. u |= b;
  129. p_arr++;
  130. }
  131. return u;
  132. }
  133. static inline double decode_double(const uint8_t *p_arr) {
  134. MarshallDouble md;
  135. md.l = decode_uint64(p_arr);
  136. return md.d;
  137. }
  138. class EncodedObjectAsID : public Reference {
  139. GDCLASS(EncodedObjectAsID, Reference);
  140. ObjectID id;
  141. protected:
  142. static void _bind_methods();
  143. public:
  144. void set_object_id(ObjectID p_id);
  145. ObjectID get_object_id() const;
  146. EncodedObjectAsID() {}
  147. };
  148. Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = nullptr, bool p_allow_objects = false);
  149. Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects = false);
  150. #endif // MARSHALLS_H