variant_setget.h 32 KB

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