variant_setget.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /**************************************************************************/
  2. /* variant_setget.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. #ifndef VARIANT_SETGET_H
  31. #define VARIANT_SETGET_H
  32. #include "variant.h"
  33. #include "core/debugger/engine_debugger.h"
  34. #include "core/object/class_db.h"
  35. #include "core/templates/local_vector.h"
  36. #include "core/variant/variant_internal.h"
  37. /**** NAMED SETTERS AND GETTERS ****/
  38. #define SETGET_STRUCT(m_base_type, m_member_type, m_member) \
  39. struct VariantSetGet_##m_base_type##_##m_member { \
  40. static void get(const Variant *base, Variant *member) { \
  41. VariantTypeAdjust<m_member_type>::adjust(member); \
  42. *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member; \
  43. } \
  44. static inline void validated_get(const Variant *base, Variant *member) { \
  45. *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member; \
  46. } \
  47. static void ptr_get(const void *base, void *member) { \
  48. PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_member, member); \
  49. } \
  50. static void set(Variant *base, const Variant *value, bool &valid) { \
  51. if (value->get_type() == GetTypeInfo<m_member_type>::VARIANT_TYPE) { \
  52. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
  53. valid = true; \
  54. } else { \
  55. valid = false; \
  56. } \
  57. } \
  58. static inline void validated_set(Variant *base, const Variant *value) { \
  59. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
  60. } \
  61. static void ptr_set(void *base, const void *member) { \
  62. m_base_type b = PtrToArg<m_base_type>::convert(base); \
  63. b.m_member = PtrToArg<m_member_type>::convert(member); \
  64. PtrToArg<m_base_type>::encode(b, base); \
  65. } \
  66. static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
  67. };
  68. #define SETGET_NUMBER_STRUCT(m_base_type, m_member_type, m_member) \
  69. struct VariantSetGet_##m_base_type##_##m_member { \
  70. static void get(const Variant *base, Variant *member) { \
  71. VariantTypeAdjust<m_member_type>::adjust(member); \
  72. *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member; \
  73. } \
  74. static inline void validated_get(const Variant *base, Variant *member) { \
  75. *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member; \
  76. } \
  77. static void ptr_get(const void *base, void *member) { \
  78. PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_member, member); \
  79. } \
  80. static void set(Variant *base, const Variant *value, bool &valid) { \
  81. if (value->get_type() == Variant::FLOAT) { \
  82. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<double>::get_ptr(value); \
  83. valid = true; \
  84. } else if (value->get_type() == Variant::INT) { \
  85. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<int64_t>::get_ptr(value); \
  86. valid = true; \
  87. } else { \
  88. valid = false; \
  89. } \
  90. } \
  91. static inline void validated_set(Variant *base, const Variant *value) { \
  92. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
  93. } \
  94. static void ptr_set(void *base, const void *member) { \
  95. m_base_type b = PtrToArg<m_base_type>::convert(base); \
  96. b.m_member = PtrToArg<m_member_type>::convert(member); \
  97. PtrToArg<m_base_type>::encode(b, base); \
  98. } \
  99. static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
  100. };
  101. #define SETGET_STRUCT_CUSTOM(m_base_type, m_member_type, m_member, m_custom) \
  102. struct VariantSetGet_##m_base_type##_##m_member { \
  103. static void get(const Variant *base, Variant *member) { \
  104. VariantTypeAdjust<m_member_type>::adjust(member); \
  105. *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom; \
  106. } \
  107. static inline void validated_get(const Variant *base, Variant *member) { \
  108. *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom; \
  109. } \
  110. static void ptr_get(const void *base, void *member) { \
  111. PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_custom, member); \
  112. } \
  113. static void set(Variant *base, const Variant *value, bool &valid) { \
  114. if (value->get_type() == GetTypeInfo<m_member_type>::VARIANT_TYPE) { \
  115. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
  116. valid = true; \
  117. } else { \
  118. valid = false; \
  119. } \
  120. } \
  121. static inline void validated_set(Variant *base, const Variant *value) { \
  122. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
  123. } \
  124. static void ptr_set(void *base, const void *member) { \
  125. m_base_type b = PtrToArg<m_base_type>::convert(base); \
  126. b.m_custom = PtrToArg<m_member_type>::convert(member); \
  127. PtrToArg<m_base_type>::encode(b, base); \
  128. } \
  129. static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
  130. };
  131. #define SETGET_NUMBER_STRUCT_CUSTOM(m_base_type, m_member_type, m_member, m_custom) \
  132. struct VariantSetGet_##m_base_type##_##m_member { \
  133. static void get(const Variant *base, Variant *member) { \
  134. VariantTypeAdjust<m_member_type>::adjust(member); \
  135. *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom; \
  136. } \
  137. static inline void validated_get(const Variant *base, Variant *member) { \
  138. *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom; \
  139. } \
  140. static void ptr_get(const void *base, void *member) { \
  141. PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_custom, member); \
  142. } \
  143. static void set(Variant *base, const Variant *value, bool &valid) { \
  144. if (value->get_type() == Variant::FLOAT) { \
  145. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<double>::get_ptr(value); \
  146. valid = true; \
  147. } else if (value->get_type() == Variant::INT) { \
  148. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<int64_t>::get_ptr(value); \
  149. valid = true; \
  150. } else { \
  151. valid = false; \
  152. } \
  153. } \
  154. static inline void validated_set(Variant *base, const Variant *value) { \
  155. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
  156. } \
  157. static void ptr_set(void *base, const void *member) { \
  158. m_base_type b = PtrToArg<m_base_type>::convert(base); \
  159. b.m_custom = PtrToArg<m_member_type>::convert(member); \
  160. PtrToArg<m_base_type>::encode(b, base); \
  161. } \
  162. static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
  163. };
  164. #define SETGET_STRUCT_FUNC(m_base_type, m_member_type, m_member, m_setter, m_getter) \
  165. struct VariantSetGet_##m_base_type##_##m_member { \
  166. static void get(const Variant *base, Variant *member) { \
  167. VariantTypeAdjust<m_member_type>::adjust(member); \
  168. *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(); \
  169. } \
  170. static inline void validated_get(const Variant *base, Variant *member) { \
  171. *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(); \
  172. } \
  173. static void ptr_get(const void *base, void *member) { \
  174. PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_getter(), member); \
  175. } \
  176. static void set(Variant *base, const Variant *value, bool &valid) { \
  177. if (value->get_type() == GetTypeInfo<m_member_type>::VARIANT_TYPE) { \
  178. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
  179. valid = true; \
  180. } else { \
  181. valid = false; \
  182. } \
  183. } \
  184. static inline void validated_set(Variant *base, const Variant *value) { \
  185. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
  186. } \
  187. static void ptr_set(void *base, const void *member) { \
  188. m_base_type b = PtrToArg<m_base_type>::convert(base); \
  189. b.m_setter(PtrToArg<m_member_type>::convert(member)); \
  190. PtrToArg<m_base_type>::encode(b, base); \
  191. } \
  192. static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
  193. };
  194. #define SETGET_NUMBER_STRUCT_FUNC(m_base_type, m_member_type, m_member, m_setter, m_getter) \
  195. struct VariantSetGet_##m_base_type##_##m_member { \
  196. static void get(const Variant *base, Variant *member) { \
  197. VariantTypeAdjust<m_member_type>::adjust(member); \
  198. *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(); \
  199. } \
  200. static inline void validated_get(const Variant *base, Variant *member) { \
  201. *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(); \
  202. } \
  203. static void ptr_get(const void *base, void *member) { \
  204. PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_getter(), member); \
  205. } \
  206. static void set(Variant *base, const Variant *value, bool &valid) { \
  207. if (value->get_type() == Variant::FLOAT) { \
  208. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<double>::get_ptr(value)); \
  209. valid = true; \
  210. } else if (value->get_type() == Variant::INT) { \
  211. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<int64_t>::get_ptr(value)); \
  212. valid = true; \
  213. } else { \
  214. valid = false; \
  215. } \
  216. } \
  217. static inline void validated_set(Variant *base, const Variant *value) { \
  218. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
  219. } \
  220. static void ptr_set(void *base, const void *member) { \
  221. m_base_type b = PtrToArg<m_base_type>::convert(base); \
  222. b.m_setter(PtrToArg<m_member_type>::convert(member)); \
  223. PtrToArg<m_base_type>::encode(b, base); \
  224. } \
  225. static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
  226. };
  227. #define SETGET_STRUCT_FUNC_INDEX(m_base_type, m_member_type, m_member, m_setter, m_getter, m_index) \
  228. struct VariantSetGet_##m_base_type##_##m_member { \
  229. static void get(const Variant *base, Variant *member) { \
  230. VariantTypeAdjust<m_member_type>::adjust(member); \
  231. *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(m_index); \
  232. } \
  233. static inline void validated_get(const Variant *base, Variant *member) { \
  234. *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(m_index); \
  235. } \
  236. static void ptr_get(const void *base, void *member) { \
  237. PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_getter(m_index), member); \
  238. } \
  239. static void set(Variant *base, const Variant *value, bool &valid) { \
  240. if (value->get_type() == GetTypeInfo<m_member_type>::VARIANT_TYPE) { \
  241. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(m_index, *VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
  242. valid = true; \
  243. } else { \
  244. valid = false; \
  245. } \
  246. } \
  247. static inline void validated_set(Variant *base, const Variant *value) { \
  248. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(m_index, *VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
  249. } \
  250. static void ptr_set(void *base, const void *member) { \
  251. m_base_type b = PtrToArg<m_base_type>::convert(base); \
  252. b.m_setter(m_index, PtrToArg<m_member_type>::convert(member)); \
  253. PtrToArg<m_base_type>::encode(b, base); \
  254. } \
  255. static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
  256. };
  257. SETGET_NUMBER_STRUCT(Vector2, double, x)
  258. SETGET_NUMBER_STRUCT(Vector2, double, y)
  259. SETGET_NUMBER_STRUCT(Vector2i, int64_t, x)
  260. SETGET_NUMBER_STRUCT(Vector2i, int64_t, y)
  261. SETGET_NUMBER_STRUCT(Vector3, double, x)
  262. SETGET_NUMBER_STRUCT(Vector3, double, y)
  263. SETGET_NUMBER_STRUCT(Vector3, double, z)
  264. SETGET_NUMBER_STRUCT(Vector3i, int64_t, x)
  265. SETGET_NUMBER_STRUCT(Vector3i, int64_t, y)
  266. SETGET_NUMBER_STRUCT(Vector3i, int64_t, z)
  267. SETGET_NUMBER_STRUCT(Vector4, double, x)
  268. SETGET_NUMBER_STRUCT(Vector4, double, y)
  269. SETGET_NUMBER_STRUCT(Vector4, double, z)
  270. SETGET_NUMBER_STRUCT(Vector4, double, w)
  271. SETGET_NUMBER_STRUCT(Vector4i, int64_t, x)
  272. SETGET_NUMBER_STRUCT(Vector4i, int64_t, y)
  273. SETGET_NUMBER_STRUCT(Vector4i, int64_t, z)
  274. SETGET_NUMBER_STRUCT(Vector4i, int64_t, w)
  275. SETGET_STRUCT(Rect2, Vector2, position)
  276. SETGET_STRUCT(Rect2, Vector2, size)
  277. SETGET_STRUCT_FUNC(Rect2, Vector2, end, set_end, get_end)
  278. SETGET_STRUCT(Rect2i, Vector2i, position)
  279. SETGET_STRUCT(Rect2i, Vector2i, size)
  280. SETGET_STRUCT_FUNC(Rect2i, Vector2i, end, set_end, get_end)
  281. SETGET_STRUCT(AABB, Vector3, position)
  282. SETGET_STRUCT(AABB, Vector3, size)
  283. SETGET_STRUCT_FUNC(AABB, Vector3, end, set_end, get_end)
  284. SETGET_STRUCT_CUSTOM(Transform2D, Vector2, x, columns[0])
  285. SETGET_STRUCT_CUSTOM(Transform2D, Vector2, y, columns[1])
  286. SETGET_STRUCT_CUSTOM(Transform2D, Vector2, origin, columns[2])
  287. SETGET_NUMBER_STRUCT_CUSTOM(Plane, double, x, normal.x)
  288. SETGET_NUMBER_STRUCT_CUSTOM(Plane, double, y, normal.y)
  289. SETGET_NUMBER_STRUCT_CUSTOM(Plane, double, z, normal.z)
  290. SETGET_STRUCT(Plane, Vector3, normal)
  291. SETGET_NUMBER_STRUCT(Plane, double, d)
  292. SETGET_NUMBER_STRUCT(Quaternion, double, x)
  293. SETGET_NUMBER_STRUCT(Quaternion, double, y)
  294. SETGET_NUMBER_STRUCT(Quaternion, double, z)
  295. SETGET_NUMBER_STRUCT(Quaternion, double, w)
  296. SETGET_STRUCT_FUNC_INDEX(Basis, Vector3, x, set_column, get_column, 0)
  297. SETGET_STRUCT_FUNC_INDEX(Basis, Vector3, y, set_column, get_column, 1)
  298. SETGET_STRUCT_FUNC_INDEX(Basis, Vector3, z, set_column, get_column, 2)
  299. SETGET_STRUCT(Transform3D, Basis, basis)
  300. SETGET_STRUCT(Transform3D, Vector3, origin)
  301. SETGET_STRUCT_CUSTOM(Projection, Vector4, x, columns[0])
  302. SETGET_STRUCT_CUSTOM(Projection, Vector4, y, columns[1])
  303. SETGET_STRUCT_CUSTOM(Projection, Vector4, z, columns[2])
  304. SETGET_STRUCT_CUSTOM(Projection, Vector4, w, columns[3])
  305. SETGET_NUMBER_STRUCT(Color, double, r)
  306. SETGET_NUMBER_STRUCT(Color, double, g)
  307. SETGET_NUMBER_STRUCT(Color, double, b)
  308. SETGET_NUMBER_STRUCT(Color, double, a)
  309. SETGET_NUMBER_STRUCT_FUNC(Color, int64_t, r8, set_r8, get_r8)
  310. SETGET_NUMBER_STRUCT_FUNC(Color, int64_t, g8, set_g8, get_g8)
  311. SETGET_NUMBER_STRUCT_FUNC(Color, int64_t, b8, set_b8, get_b8)
  312. SETGET_NUMBER_STRUCT_FUNC(Color, int64_t, a8, set_a8, get_a8)
  313. SETGET_NUMBER_STRUCT_FUNC(Color, double, h, set_h, get_h)
  314. SETGET_NUMBER_STRUCT_FUNC(Color, double, s, set_s, get_s)
  315. SETGET_NUMBER_STRUCT_FUNC(Color, double, v, set_v, get_v)
  316. SETGET_NUMBER_STRUCT_FUNC(Color, double, ok_hsl_h, set_ok_hsl_h, get_ok_hsl_h)
  317. SETGET_NUMBER_STRUCT_FUNC(Color, double, ok_hsl_s, set_ok_hsl_s, get_ok_hsl_s)
  318. SETGET_NUMBER_STRUCT_FUNC(Color, double, ok_hsl_l, set_ok_hsl_l, get_ok_hsl_l)
  319. #endif // VARIANT_SETGET_H