marshalls.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*************************************************************************/
  2. /* marshalls.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/reference.h"
  33. #include "core/typedefs.h"
  34. #include "core/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) *p_data = 0;
  94. return len + 1;
  95. }
  96. static inline uint16_t decode_uint16(const uint8_t *p_arr) {
  97. uint16_t u = 0;
  98. for (int i = 0; i < 2; i++) {
  99. uint16_t b = *p_arr;
  100. b <<= (i * 8);
  101. u |= b;
  102. p_arr++;
  103. }
  104. return u;
  105. }
  106. static inline uint32_t decode_uint32(const uint8_t *p_arr) {
  107. uint32_t u = 0;
  108. for (int i = 0; i < 4; i++) {
  109. uint32_t b = *p_arr;
  110. b <<= (i * 8);
  111. u |= b;
  112. p_arr++;
  113. }
  114. return u;
  115. }
  116. static inline float decode_float(const uint8_t *p_arr) {
  117. MarshallFloat mf;
  118. mf.i = decode_uint32(p_arr);
  119. return mf.f;
  120. }
  121. static inline uint64_t decode_uint64(const uint8_t *p_arr) {
  122. uint64_t u = 0;
  123. for (int i = 0; i < 8; i++) {
  124. uint64_t b = (*p_arr) & 0xFF;
  125. b <<= (i * 8);
  126. u |= b;
  127. p_arr++;
  128. }
  129. return u;
  130. }
  131. static inline double decode_double(const uint8_t *p_arr) {
  132. MarshallDouble md;
  133. md.l = decode_uint64(p_arr);
  134. return md.d;
  135. }
  136. class EncodedObjectAsID : public Reference {
  137. GDCLASS(EncodedObjectAsID, Reference);
  138. ObjectID id;
  139. protected:
  140. static void _bind_methods();
  141. public:
  142. void set_object_id(ObjectID p_id);
  143. ObjectID get_object_id() const;
  144. EncodedObjectAsID();
  145. };
  146. Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = NULL, bool p_allow_objects = false);
  147. Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects = false);
  148. #endif