marshalls.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/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)
  94. *p_data = 0;
  95. return len + 1;
  96. }
  97. static inline uint16_t decode_uint16(const uint8_t *p_arr) {
  98. uint16_t u = 0;
  99. for (int i = 0; i < 2; i++) {
  100. uint16_t b = *p_arr;
  101. b <<= (i * 8);
  102. u |= b;
  103. p_arr++;
  104. }
  105. return u;
  106. }
  107. static inline uint32_t decode_uint32(const uint8_t *p_arr) {
  108. uint32_t u = 0;
  109. for (int i = 0; i < 4; i++) {
  110. uint32_t b = *p_arr;
  111. b <<= (i * 8);
  112. u |= b;
  113. p_arr++;
  114. }
  115. return u;
  116. }
  117. static inline float decode_float(const uint8_t *p_arr) {
  118. MarshallFloat mf;
  119. mf.i = decode_uint32(p_arr);
  120. return mf.f;
  121. }
  122. static inline uint64_t decode_uint64(const uint8_t *p_arr) {
  123. uint64_t u = 0;
  124. for (int i = 0; i < 8; i++) {
  125. uint64_t b = (*p_arr) & 0xFF;
  126. b <<= (i * 8);
  127. u |= b;
  128. p_arr++;
  129. }
  130. return u;
  131. }
  132. static inline double decode_double(const uint8_t *p_arr) {
  133. MarshallDouble md;
  134. md.l = decode_uint64(p_arr);
  135. return md.d;
  136. }
  137. class EncodedObjectAsID : public Reference {
  138. GDCLASS(EncodedObjectAsID, Reference);
  139. ObjectID id;
  140. protected:
  141. static void _bind_methods();
  142. public:
  143. void set_object_id(ObjectID p_id);
  144. ObjectID get_object_id() const;
  145. EncodedObjectAsID();
  146. };
  147. Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = nullptr, bool p_allow_objects = false);
  148. Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects = false);
  149. #endif // MARSHALLS_H