variant_setget.cpp 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  1. /**************************************************************************/
  2. /* variant_setget.cpp */
  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. #include "variant_setget.h"
  31. struct VariantSetterGetterInfo {
  32. void (*setter)(Variant *base, const Variant *value, bool &valid);
  33. void (*getter)(const Variant *base, Variant *value);
  34. Variant::ValidatedSetter validated_setter;
  35. Variant::ValidatedGetter validated_getter;
  36. Variant::PTRSetter ptr_setter;
  37. Variant::PTRGetter ptr_getter;
  38. Variant::Type member_type;
  39. };
  40. static LocalVector<VariantSetterGetterInfo> variant_setters_getters[Variant::VARIANT_MAX];
  41. static LocalVector<StringName> variant_setters_getters_names[Variant::VARIANT_MAX]; //one next to another to make it cache friendly
  42. template <class T>
  43. static void register_member(Variant::Type p_type, const StringName &p_member) {
  44. VariantSetterGetterInfo sgi;
  45. sgi.setter = T::set;
  46. sgi.validated_setter = T::validated_set;
  47. sgi.ptr_setter = T::ptr_set;
  48. sgi.getter = T::get;
  49. sgi.validated_getter = T::validated_get;
  50. sgi.ptr_getter = T::ptr_get;
  51. sgi.member_type = T::get_type();
  52. variant_setters_getters[p_type].push_back(sgi);
  53. variant_setters_getters_names[p_type].push_back(p_member);
  54. }
  55. void register_named_setters_getters() {
  56. #define REGISTER_MEMBER(m_base_type, m_member) register_member<VariantSetGet_##m_base_type##_##m_member>(GetTypeInfo<m_base_type>::VARIANT_TYPE, #m_member)
  57. REGISTER_MEMBER(Vector2, x);
  58. REGISTER_MEMBER(Vector2, y);
  59. REGISTER_MEMBER(Vector2i, x);
  60. REGISTER_MEMBER(Vector2i, y);
  61. REGISTER_MEMBER(Vector3, x);
  62. REGISTER_MEMBER(Vector3, y);
  63. REGISTER_MEMBER(Vector3, z);
  64. REGISTER_MEMBER(Vector3i, x);
  65. REGISTER_MEMBER(Vector3i, y);
  66. REGISTER_MEMBER(Vector3i, z);
  67. REGISTER_MEMBER(Vector4, x);
  68. REGISTER_MEMBER(Vector4, y);
  69. REGISTER_MEMBER(Vector4, z);
  70. REGISTER_MEMBER(Vector4, w);
  71. REGISTER_MEMBER(Vector4i, x);
  72. REGISTER_MEMBER(Vector4i, y);
  73. REGISTER_MEMBER(Vector4i, z);
  74. REGISTER_MEMBER(Vector4i, w);
  75. REGISTER_MEMBER(Rect2, position);
  76. REGISTER_MEMBER(Rect2, size);
  77. REGISTER_MEMBER(Rect2, end);
  78. REGISTER_MEMBER(Rect2i, position);
  79. REGISTER_MEMBER(Rect2i, size);
  80. REGISTER_MEMBER(Rect2i, end);
  81. REGISTER_MEMBER(AABB, position);
  82. REGISTER_MEMBER(AABB, size);
  83. REGISTER_MEMBER(AABB, end);
  84. REGISTER_MEMBER(Transform2D, x);
  85. REGISTER_MEMBER(Transform2D, y);
  86. REGISTER_MEMBER(Transform2D, origin);
  87. REGISTER_MEMBER(Plane, x);
  88. REGISTER_MEMBER(Plane, y);
  89. REGISTER_MEMBER(Plane, z);
  90. REGISTER_MEMBER(Plane, d);
  91. REGISTER_MEMBER(Plane, normal);
  92. REGISTER_MEMBER(Quaternion, x);
  93. REGISTER_MEMBER(Quaternion, y);
  94. REGISTER_MEMBER(Quaternion, z);
  95. REGISTER_MEMBER(Quaternion, w);
  96. REGISTER_MEMBER(Basis, x);
  97. REGISTER_MEMBER(Basis, y);
  98. REGISTER_MEMBER(Basis, z);
  99. REGISTER_MEMBER(Transform3D, basis);
  100. REGISTER_MEMBER(Transform3D, origin);
  101. REGISTER_MEMBER(Projection, x);
  102. REGISTER_MEMBER(Projection, y);
  103. REGISTER_MEMBER(Projection, z);
  104. REGISTER_MEMBER(Projection, w);
  105. REGISTER_MEMBER(Color, r);
  106. REGISTER_MEMBER(Color, g);
  107. REGISTER_MEMBER(Color, b);
  108. REGISTER_MEMBER(Color, a);
  109. REGISTER_MEMBER(Color, r8);
  110. REGISTER_MEMBER(Color, g8);
  111. REGISTER_MEMBER(Color, b8);
  112. REGISTER_MEMBER(Color, a8);
  113. REGISTER_MEMBER(Color, h);
  114. REGISTER_MEMBER(Color, s);
  115. REGISTER_MEMBER(Color, v);
  116. }
  117. void unregister_named_setters_getters() {
  118. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  119. variant_setters_getters[i].clear();
  120. variant_setters_getters_names[i].clear();
  121. }
  122. }
  123. bool Variant::has_member(Variant::Type p_type, const StringName &p_member) {
  124. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, false);
  125. for (const StringName &member : variant_setters_getters_names[p_type]) {
  126. if (member == p_member) {
  127. return true;
  128. }
  129. }
  130. return false;
  131. }
  132. Variant::Type Variant::get_member_type(Variant::Type p_type, const StringName &p_member) {
  133. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, Variant::VARIANT_MAX);
  134. for (uint32_t i = 0; i < variant_setters_getters_names[p_type].size(); i++) {
  135. if (variant_setters_getters_names[p_type][i] == p_member) {
  136. return variant_setters_getters[p_type][i].member_type;
  137. }
  138. }
  139. return Variant::NIL;
  140. }
  141. void Variant::get_member_list(Variant::Type p_type, List<StringName> *r_members) {
  142. for (const StringName &member : variant_setters_getters_names[p_type]) {
  143. r_members->push_back(member);
  144. }
  145. }
  146. int Variant::get_member_count(Type p_type) {
  147. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, -1);
  148. return variant_setters_getters_names[p_type].size();
  149. }
  150. Variant::ValidatedSetter Variant::get_member_validated_setter(Variant::Type p_type, const StringName &p_member) {
  151. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr);
  152. for (uint32_t i = 0; i < variant_setters_getters_names[p_type].size(); i++) {
  153. if (variant_setters_getters_names[p_type][i] == p_member) {
  154. return variant_setters_getters[p_type][i].validated_setter;
  155. }
  156. }
  157. return nullptr;
  158. }
  159. Variant::ValidatedGetter Variant::get_member_validated_getter(Variant::Type p_type, const StringName &p_member) {
  160. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr);
  161. for (uint32_t i = 0; i < variant_setters_getters_names[p_type].size(); i++) {
  162. if (variant_setters_getters_names[p_type][i] == p_member) {
  163. return variant_setters_getters[p_type][i].validated_getter;
  164. }
  165. }
  166. return nullptr;
  167. }
  168. Variant::PTRSetter Variant::get_member_ptr_setter(Variant::Type p_type, const StringName &p_member) {
  169. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr);
  170. for (uint32_t i = 0; i < variant_setters_getters_names[p_type].size(); i++) {
  171. if (variant_setters_getters_names[p_type][i] == p_member) {
  172. return variant_setters_getters[p_type][i].ptr_setter;
  173. }
  174. }
  175. return nullptr;
  176. }
  177. Variant::PTRGetter Variant::get_member_ptr_getter(Variant::Type p_type, const StringName &p_member) {
  178. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr);
  179. for (uint32_t i = 0; i < variant_setters_getters_names[p_type].size(); i++) {
  180. if (variant_setters_getters_names[p_type][i] == p_member) {
  181. return variant_setters_getters[p_type][i].ptr_getter;
  182. }
  183. }
  184. return nullptr;
  185. }
  186. void Variant::set_named(const StringName &p_member, const Variant &p_value, bool &r_valid) {
  187. uint32_t s = variant_setters_getters[type].size();
  188. if (s) {
  189. for (uint32_t i = 0; i < s; i++) {
  190. if (variant_setters_getters_names[type][i] == p_member) {
  191. variant_setters_getters[type][i].setter(this, &p_value, r_valid);
  192. return;
  193. }
  194. }
  195. r_valid = false;
  196. } else if (type == Variant::OBJECT) {
  197. Object *obj = get_validated_object();
  198. if (!obj) {
  199. r_valid = false;
  200. } else {
  201. obj->set(p_member, p_value, &r_valid);
  202. return;
  203. }
  204. } else if (type == Variant::DICTIONARY) {
  205. Variant *v = VariantGetInternalPtr<Dictionary>::get_ptr(this)->getptr(p_member);
  206. if (v) {
  207. *v = p_value;
  208. r_valid = true;
  209. } else {
  210. VariantGetInternalPtr<Dictionary>::get_ptr(this)->operator[](p_member) = p_value;
  211. r_valid = true;
  212. }
  213. } else {
  214. r_valid = false;
  215. }
  216. }
  217. Variant Variant::get_named(const StringName &p_member, bool &r_valid) const {
  218. Variant ret;
  219. uint32_t s = variant_setters_getters[type].size();
  220. if (s) {
  221. for (uint32_t i = 0; i < s; i++) {
  222. if (variant_setters_getters_names[type][i] == p_member) {
  223. variant_setters_getters[type][i].getter(this, &ret);
  224. r_valid = true;
  225. return ret;
  226. }
  227. }
  228. r_valid = false;
  229. } else if (type == Variant::OBJECT) {
  230. Object *obj = get_validated_object();
  231. if (!obj) {
  232. r_valid = false;
  233. return "Instance base is null.";
  234. } else {
  235. return obj->get(p_member, &r_valid);
  236. }
  237. } else if (type == Variant::DICTIONARY) {
  238. const Variant *v = VariantGetInternalPtr<Dictionary>::get_ptr(this)->getptr(p_member);
  239. if (v) {
  240. r_valid = true;
  241. return *v;
  242. } else {
  243. r_valid = false;
  244. }
  245. } else {
  246. r_valid = false;
  247. }
  248. return ret;
  249. }
  250. /**** INDEXED SETTERS AND GETTERS ****/
  251. #ifdef DEBUG_ENABLED
  252. #define OOB_TEST(m_idx, m_v) \
  253. ERR_FAIL_INDEX(m_idx, m_v)
  254. #else
  255. #define OOB_TEST(m_idx, m_v)
  256. #endif
  257. #ifdef DEBUG_ENABLED
  258. #define NULL_TEST(m_key) \
  259. ERR_FAIL_COND(!m_key)
  260. #else
  261. #define NULL_TEST(m_key)
  262. #endif
  263. #define INDEXED_SETGET_STRUCT_TYPED(m_base_type, m_elem_type) \
  264. struct VariantIndexedSetGet_##m_base_type { \
  265. static void get(const Variant *base, int64_t index, Variant *value, bool *oob) { \
  266. int64_t size = VariantGetInternalPtr<m_base_type>::get_ptr(base)->size(); \
  267. if (index < 0) { \
  268. index += size; \
  269. } \
  270. if (index < 0 || index >= size) { \
  271. *oob = true; \
  272. return; \
  273. } \
  274. VariantTypeAdjust<m_elem_type>::adjust(value); \
  275. *VariantGetInternalPtr<m_elem_type>::get_ptr(value) = (*VariantGetInternalPtr<m_base_type>::get_ptr(base))[index]; \
  276. *oob = false; \
  277. } \
  278. static void ptr_get(const void *base, int64_t index, void *member) { \
  279. /* avoid ptrconvert for performance*/ \
  280. const m_base_type &v = *reinterpret_cast<const m_base_type *>(base); \
  281. if (index < 0) \
  282. index += v.size(); \
  283. OOB_TEST(index, v.size()); \
  284. PtrToArg<m_elem_type>::encode(v[index], member); \
  285. } \
  286. static void set(Variant *base, int64_t index, const Variant *value, bool *valid, bool *oob) { \
  287. if (value->get_type() != GetTypeInfo<m_elem_type>::VARIANT_TYPE) { \
  288. *oob = false; \
  289. *valid = false; \
  290. return; \
  291. } \
  292. int64_t size = VariantGetInternalPtr<m_base_type>::get_ptr(base)->size(); \
  293. if (index < 0) { \
  294. index += size; \
  295. } \
  296. if (index < 0 || index >= size) { \
  297. *oob = true; \
  298. *valid = false; \
  299. return; \
  300. } \
  301. (*VariantGetInternalPtr<m_base_type>::get_ptr(base)).write[index] = *VariantGetInternalPtr<m_elem_type>::get_ptr(value); \
  302. *oob = false; \
  303. *valid = true; \
  304. } \
  305. static void validated_set(Variant *base, int64_t index, const Variant *value, bool *oob) { \
  306. int64_t size = VariantGetInternalPtr<m_base_type>::get_ptr(base)->size(); \
  307. if (index < 0) { \
  308. index += size; \
  309. } \
  310. if (index < 0 || index >= size) { \
  311. *oob = true; \
  312. return; \
  313. } \
  314. (*VariantGetInternalPtr<m_base_type>::get_ptr(base)).write[index] = *VariantGetInternalPtr<m_elem_type>::get_ptr(value); \
  315. *oob = false; \
  316. } \
  317. static void ptr_set(void *base, int64_t index, const void *member) { \
  318. /* avoid ptrconvert for performance*/ \
  319. m_base_type &v = *reinterpret_cast<m_base_type *>(base); \
  320. if (index < 0) \
  321. index += v.size(); \
  322. OOB_TEST(index, v.size()); \
  323. v.write[index] = PtrToArg<m_elem_type>::convert(member); \
  324. } \
  325. static Variant::Type get_index_type() { return GetTypeInfo<m_elem_type>::VARIANT_TYPE; } \
  326. static uint32_t get_index_usage() { return GetTypeInfo<m_elem_type>::get_class_info().usage; } \
  327. static uint64_t get_indexed_size(const Variant *base) { return VariantGetInternalPtr<m_base_type>::get_ptr(base)->size(); } \
  328. };
  329. #define INDEXED_SETGET_STRUCT_TYPED_NUMERIC(m_base_type, m_elem_type, m_assign_type) \
  330. struct VariantIndexedSetGet_##m_base_type { \
  331. static void get(const Variant *base, int64_t index, Variant *value, bool *oob) { \
  332. int64_t size = VariantGetInternalPtr<m_base_type>::get_ptr(base)->size(); \
  333. if (index < 0) { \
  334. index += size; \
  335. } \
  336. if (index < 0 || index >= size) { \
  337. *oob = true; \
  338. return; \
  339. } \
  340. VariantTypeAdjust<m_elem_type>::adjust(value); \
  341. *VariantGetInternalPtr<m_elem_type>::get_ptr(value) = (*VariantGetInternalPtr<m_base_type>::get_ptr(base))[index]; \
  342. *oob = false; \
  343. } \
  344. static void ptr_get(const void *base, int64_t index, void *member) { \
  345. /* avoid ptrconvert for performance*/ \
  346. const m_base_type &v = *reinterpret_cast<const m_base_type *>(base); \
  347. if (index < 0) \
  348. index += v.size(); \
  349. OOB_TEST(index, v.size()); \
  350. PtrToArg<m_elem_type>::encode(v[index], member); \
  351. } \
  352. static void set(Variant *base, int64_t index, const Variant *value, bool *valid, bool *oob) { \
  353. int64_t size = VariantGetInternalPtr<m_base_type>::get_ptr(base)->size(); \
  354. if (index < 0) { \
  355. index += size; \
  356. } \
  357. if (index < 0 || index >= size) { \
  358. *oob = true; \
  359. *valid = false; \
  360. return; \
  361. } \
  362. m_assign_type num; \
  363. if (value->get_type() == Variant::INT) { \
  364. num = (m_assign_type)*VariantGetInternalPtr<int64_t>::get_ptr(value); \
  365. } else if (value->get_type() == Variant::FLOAT) { \
  366. num = (m_assign_type)*VariantGetInternalPtr<double>::get_ptr(value); \
  367. } else { \
  368. *oob = false; \
  369. *valid = false; \
  370. return; \
  371. } \
  372. (*VariantGetInternalPtr<m_base_type>::get_ptr(base)).write[index] = num; \
  373. *oob = false; \
  374. *valid = true; \
  375. } \
  376. static void validated_set(Variant *base, int64_t index, const Variant *value, bool *oob) { \
  377. int64_t size = VariantGetInternalPtr<m_base_type>::get_ptr(base)->size(); \
  378. if (index < 0) { \
  379. index += size; \
  380. } \
  381. if (index < 0 || index >= size) { \
  382. *oob = true; \
  383. return; \
  384. } \
  385. (*VariantGetInternalPtr<m_base_type>::get_ptr(base)).write[index] = *VariantGetInternalPtr<m_elem_type>::get_ptr(value); \
  386. *oob = false; \
  387. } \
  388. static void ptr_set(void *base, int64_t index, const void *member) { \
  389. /* avoid ptrconvert for performance*/ \
  390. m_base_type &v = *reinterpret_cast<m_base_type *>(base); \
  391. if (index < 0) \
  392. index += v.size(); \
  393. OOB_TEST(index, v.size()); \
  394. v.write[index] = PtrToArg<m_elem_type>::convert(member); \
  395. } \
  396. static Variant::Type get_index_type() { return GetTypeInfo<m_elem_type>::VARIANT_TYPE; } \
  397. static uint32_t get_index_usage() { return GetTypeInfo<m_elem_type>::get_class_info().usage; } \
  398. static uint64_t get_indexed_size(const Variant *base) { return VariantGetInternalPtr<m_base_type>::get_ptr(base)->size(); } \
  399. };
  400. #define INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(m_base_type, m_elem_type, m_assign_type, m_max) \
  401. struct VariantIndexedSetGet_##m_base_type { \
  402. static void get(const Variant *base, int64_t index, Variant *value, bool *oob) { \
  403. if (index < 0 || index >= m_max) { \
  404. *oob = true; \
  405. return; \
  406. } \
  407. VariantTypeAdjust<m_elem_type>::adjust(value); \
  408. *VariantGetInternalPtr<m_elem_type>::get_ptr(value) = (*VariantGetInternalPtr<m_base_type>::get_ptr(base))[index]; \
  409. *oob = false; \
  410. } \
  411. static void ptr_get(const void *base, int64_t index, void *member) { \
  412. /* avoid ptrconvert for performance*/ \
  413. const m_base_type &v = *reinterpret_cast<const m_base_type *>(base); \
  414. OOB_TEST(index, m_max); \
  415. PtrToArg<m_elem_type>::encode(v[index], member); \
  416. } \
  417. static void set(Variant *base, int64_t index, const Variant *value, bool *valid, bool *oob) { \
  418. if (index < 0 || index >= m_max) { \
  419. *oob = true; \
  420. *valid = false; \
  421. return; \
  422. } \
  423. m_assign_type num; \
  424. if (value->get_type() == Variant::INT) { \
  425. num = (m_assign_type)*VariantGetInternalPtr<int64_t>::get_ptr(value); \
  426. } else if (value->get_type() == Variant::FLOAT) { \
  427. num = (m_assign_type)*VariantGetInternalPtr<double>::get_ptr(value); \
  428. } else { \
  429. *oob = false; \
  430. *valid = false; \
  431. return; \
  432. } \
  433. (*VariantGetInternalPtr<m_base_type>::get_ptr(base))[index] = num; \
  434. *oob = false; \
  435. *valid = true; \
  436. } \
  437. static void validated_set(Variant *base, int64_t index, const Variant *value, bool *oob) { \
  438. if (index < 0 || index >= m_max) { \
  439. *oob = true; \
  440. return; \
  441. } \
  442. (*VariantGetInternalPtr<m_base_type>::get_ptr(base))[index] = *VariantGetInternalPtr<m_elem_type>::get_ptr(value); \
  443. *oob = false; \
  444. } \
  445. static void ptr_set(void *base, int64_t index, const void *member) { \
  446. /* avoid ptrconvert for performance*/ \
  447. m_base_type &v = *reinterpret_cast<m_base_type *>(base); \
  448. OOB_TEST(index, m_max); \
  449. v[index] = PtrToArg<m_elem_type>::convert(member); \
  450. } \
  451. static Variant::Type get_index_type() { return GetTypeInfo<m_elem_type>::VARIANT_TYPE; } \
  452. static uint32_t get_index_usage() { return GetTypeInfo<m_elem_type>::get_class_info().usage; } \
  453. static uint64_t get_indexed_size(const Variant *base) { return m_max; } \
  454. };
  455. #define INDEXED_SETGET_STRUCT_BULTIN_ACCESSOR(m_base_type, m_elem_type, m_accessor, m_max) \
  456. struct VariantIndexedSetGet_##m_base_type { \
  457. static void get(const Variant *base, int64_t index, Variant *value, bool *oob) { \
  458. if (index < 0 || index >= m_max) { \
  459. *oob = true; \
  460. return; \
  461. } \
  462. VariantTypeAdjust<m_elem_type>::adjust(value); \
  463. *VariantGetInternalPtr<m_elem_type>::get_ptr(value) = (*VariantGetInternalPtr<m_base_type>::get_ptr(base))m_accessor[index]; \
  464. *oob = false; \
  465. } \
  466. static void ptr_get(const void *base, int64_t index, void *member) { \
  467. /* avoid ptrconvert for performance*/ \
  468. const m_base_type &v = *reinterpret_cast<const m_base_type *>(base); \
  469. OOB_TEST(index, m_max); \
  470. PtrToArg<m_elem_type>::encode(v m_accessor[index], member); \
  471. } \
  472. static void set(Variant *base, int64_t index, const Variant *value, bool *valid, bool *oob) { \
  473. if (value->get_type() != GetTypeInfo<m_elem_type>::VARIANT_TYPE) { \
  474. *oob = false; \
  475. *valid = false; \
  476. } \
  477. if (index < 0 || index >= m_max) { \
  478. *oob = true; \
  479. *valid = false; \
  480. return; \
  481. } \
  482. (*VariantGetInternalPtr<m_base_type>::get_ptr(base)) m_accessor[index] = *VariantGetInternalPtr<m_elem_type>::get_ptr(value); \
  483. *oob = false; \
  484. *valid = true; \
  485. } \
  486. static void validated_set(Variant *base, int64_t index, const Variant *value, bool *oob) { \
  487. if (index < 0 || index >= m_max) { \
  488. *oob = true; \
  489. return; \
  490. } \
  491. (*VariantGetInternalPtr<m_base_type>::get_ptr(base)) m_accessor[index] = *VariantGetInternalPtr<m_elem_type>::get_ptr(value); \
  492. *oob = false; \
  493. } \
  494. static void ptr_set(void *base, int64_t index, const void *member) { \
  495. /* avoid ptrconvert for performance*/ \
  496. m_base_type &v = *reinterpret_cast<m_base_type *>(base); \
  497. OOB_TEST(index, m_max); \
  498. v m_accessor[index] = PtrToArg<m_elem_type>::convert(member); \
  499. } \
  500. static Variant::Type get_index_type() { return GetTypeInfo<m_elem_type>::VARIANT_TYPE; } \
  501. static uint32_t get_index_usage() { return GetTypeInfo<m_elem_type>::get_class_info().usage; } \
  502. static uint64_t get_indexed_size(const Variant *base) { return m_max; } \
  503. };
  504. #define INDEXED_SETGET_STRUCT_BULTIN_FUNC(m_base_type, m_elem_type, m_set, m_get, m_max) \
  505. struct VariantIndexedSetGet_##m_base_type { \
  506. static void get(const Variant *base, int64_t index, Variant *value, bool *oob) { \
  507. if (index < 0 || index >= m_max) { \
  508. *oob = true; \
  509. return; \
  510. } \
  511. VariantTypeAdjust<m_elem_type>::adjust(value); \
  512. *VariantGetInternalPtr<m_elem_type>::get_ptr(value) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_get(index); \
  513. *oob = false; \
  514. } \
  515. static void ptr_get(const void *base, int64_t index, void *member) { \
  516. /* avoid ptrconvert for performance*/ \
  517. const m_base_type &v = *reinterpret_cast<const m_base_type *>(base); \
  518. OOB_TEST(index, m_max); \
  519. PtrToArg<m_elem_type>::encode(v.m_get(index), member); \
  520. } \
  521. static void set(Variant *base, int64_t index, const Variant *value, bool *valid, bool *oob) { \
  522. if (value->get_type() != GetTypeInfo<m_elem_type>::VARIANT_TYPE) { \
  523. *oob = false; \
  524. *valid = false; \
  525. } \
  526. if (index < 0 || index >= m_max) { \
  527. *oob = true; \
  528. *valid = false; \
  529. return; \
  530. } \
  531. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_set(index, *VariantGetInternalPtr<m_elem_type>::get_ptr(value)); \
  532. *oob = false; \
  533. *valid = true; \
  534. } \
  535. static void validated_set(Variant *base, int64_t index, const Variant *value, bool *oob) { \
  536. if (index < 0 || index >= m_max) { \
  537. *oob = true; \
  538. return; \
  539. } \
  540. VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_set(index, *VariantGetInternalPtr<m_elem_type>::get_ptr(value)); \
  541. *oob = false; \
  542. } \
  543. static void ptr_set(void *base, int64_t index, const void *member) { \
  544. /* avoid ptrconvert for performance*/ \
  545. m_base_type &v = *reinterpret_cast<m_base_type *>(base); \
  546. OOB_TEST(index, m_max); \
  547. v.m_set(index, PtrToArg<m_elem_type>::convert(member)); \
  548. } \
  549. static Variant::Type get_index_type() { return GetTypeInfo<m_elem_type>::VARIANT_TYPE; } \
  550. static uint32_t get_index_usage() { return GetTypeInfo<m_elem_type>::get_class_info().usage; } \
  551. static uint64_t get_indexed_size(const Variant *base) { return m_max; } \
  552. };
  553. struct VariantIndexedSetGet_Array {
  554. static void get(const Variant *base, int64_t index, Variant *value, bool *oob) {
  555. int64_t size = VariantGetInternalPtr<Array>::get_ptr(base)->size();
  556. if (index < 0) {
  557. index += size;
  558. }
  559. if (index < 0 || index >= size) {
  560. *oob = true;
  561. return;
  562. }
  563. *value = (*VariantGetInternalPtr<Array>::get_ptr(base))[index];
  564. *oob = false;
  565. }
  566. static void ptr_get(const void *base, int64_t index, void *member) {
  567. /* avoid ptrconvert for performance*/
  568. const Array &v = *reinterpret_cast<const Array *>(base);
  569. if (index < 0) {
  570. index += v.size();
  571. }
  572. OOB_TEST(index, v.size());
  573. PtrToArg<Variant>::encode(v[index], member);
  574. }
  575. static void set(Variant *base, int64_t index, const Variant *value, bool *valid, bool *oob) {
  576. if (VariantGetInternalPtr<Array>::get_ptr(base)->is_read_only()) {
  577. *valid = false;
  578. *oob = true;
  579. return;
  580. }
  581. int64_t size = VariantGetInternalPtr<Array>::get_ptr(base)->size();
  582. if (index < 0) {
  583. index += size;
  584. }
  585. if (index < 0 || index >= size) {
  586. *oob = true;
  587. *valid = false;
  588. return;
  589. }
  590. VariantGetInternalPtr<Array>::get_ptr(base)->set(index, *value);
  591. *oob = false;
  592. *valid = true;
  593. }
  594. static void validated_set(Variant *base, int64_t index, const Variant *value, bool *oob) {
  595. if (VariantGetInternalPtr<Array>::get_ptr(base)->is_read_only()) {
  596. *oob = true;
  597. return;
  598. }
  599. int64_t size = VariantGetInternalPtr<Array>::get_ptr(base)->size();
  600. if (index < 0) {
  601. index += size;
  602. }
  603. if (index < 0 || index >= size) {
  604. *oob = true;
  605. return;
  606. }
  607. VariantGetInternalPtr<Array>::get_ptr(base)->set(index, *value);
  608. *oob = false;
  609. }
  610. static void ptr_set(void *base, int64_t index, const void *member) {
  611. /* avoid ptrconvert for performance*/
  612. Array &v = *reinterpret_cast<Array *>(base);
  613. if (index < 0) {
  614. index += v.size();
  615. }
  616. OOB_TEST(index, v.size());
  617. v.set(index, PtrToArg<Variant>::convert(member));
  618. }
  619. static Variant::Type get_index_type() { return Variant::NIL; }
  620. static uint32_t get_index_usage() { return PROPERTY_USAGE_NIL_IS_VARIANT; }
  621. static uint64_t get_indexed_size(const Variant *base) { return 0; }
  622. };
  623. struct VariantIndexedSetGet_String {
  624. static void get(const Variant *base, int64_t index, Variant *value, bool *oob) {
  625. int64_t length = VariantGetInternalPtr<String>::get_ptr(base)->length();
  626. if (index < 0) {
  627. index += length;
  628. }
  629. if (index < 0 || index >= length) {
  630. *oob = true;
  631. return;
  632. }
  633. char32_t result = (*VariantGetInternalPtr<String>::get_ptr(base))[index];
  634. *value = String(&result, 1);
  635. *oob = false;
  636. }
  637. static void ptr_get(const void *base, int64_t index, void *member) {
  638. /* avoid ptrconvert for performance*/
  639. const String &v = *reinterpret_cast<const String *>(base);
  640. if (index < 0) {
  641. index += v.length();
  642. }
  643. OOB_TEST(index, v.length());
  644. char32_t c = v[index];
  645. PtrToArg<String>::encode(String(&c, 1), member);
  646. }
  647. static void set(Variant *base, int64_t index, const Variant *value, bool *valid, bool *oob) {
  648. if (value->get_type() != Variant::STRING) {
  649. *oob = false;
  650. *valid = false;
  651. return;
  652. }
  653. int64_t length = VariantGetInternalPtr<String>::get_ptr(base)->length();
  654. if (index < 0) {
  655. index += length;
  656. }
  657. if (index < 0 || index >= length) {
  658. *oob = true;
  659. *valid = false;
  660. return;
  661. }
  662. String *b = VariantGetInternalPtr<String>::get_ptr(base);
  663. const String *v = VariantInternal::get_string(value);
  664. if (v->length() == 0) {
  665. b->remove_at(index);
  666. } else {
  667. b->set(index, v->get(0));
  668. }
  669. *oob = false;
  670. *valid = true;
  671. }
  672. static void validated_set(Variant *base, int64_t index, const Variant *value, bool *oob) {
  673. int64_t length = VariantGetInternalPtr<String>::get_ptr(base)->length();
  674. if (index < 0) {
  675. index += length;
  676. }
  677. if (index < 0 || index >= length) {
  678. *oob = true;
  679. return;
  680. }
  681. String *b = VariantGetInternalPtr<String>::get_ptr(base);
  682. const String *v = VariantInternal::get_string(value);
  683. if (v->length() == 0) {
  684. b->remove_at(index);
  685. } else {
  686. b->set(index, v->get(0));
  687. }
  688. *oob = false;
  689. }
  690. static void ptr_set(void *base, int64_t index, const void *member) {
  691. /* avoid ptrconvert for performance*/
  692. String &v = *reinterpret_cast<String *>(base);
  693. if (index < 0) {
  694. index += v.length();
  695. }
  696. OOB_TEST(index, v.length());
  697. const String &m = *reinterpret_cast<const String *>(member);
  698. if (unlikely(m.length() == 0)) {
  699. v.remove_at(index);
  700. } else {
  701. v.set(index, m.unicode_at(0));
  702. }
  703. }
  704. static Variant::Type get_index_type() { return Variant::STRING; }
  705. static uint32_t get_index_usage() { return PROPERTY_USAGE_DEFAULT; }
  706. static uint64_t get_indexed_size(const Variant *base) { return VariantInternal::get_string(base)->length(); }
  707. };
  708. #define INDEXED_SETGET_STRUCT_DICT(m_base_type) \
  709. struct VariantIndexedSetGet_##m_base_type { \
  710. static void get(const Variant *base, int64_t index, Variant *value, bool *oob) { \
  711. const Variant *ptr = VariantGetInternalPtr<m_base_type>::get_ptr(base)->getptr(index); \
  712. if (!ptr) { \
  713. *oob = true; \
  714. return; \
  715. } \
  716. *value = *ptr; \
  717. *oob = false; \
  718. } \
  719. static void ptr_get(const void *base, int64_t index, void *member) { \
  720. /* avoid ptrconvert for performance*/ \
  721. const m_base_type &v = *reinterpret_cast<const m_base_type *>(base); \
  722. const Variant *ptr = v.getptr(index); \
  723. NULL_TEST(ptr); \
  724. PtrToArg<Variant>::encode(*ptr, member); \
  725. } \
  726. static void set(Variant *base, int64_t index, const Variant *value, bool *valid, bool *oob) { \
  727. if (VariantGetInternalPtr<m_base_type>::get_ptr(base)->is_read_only()) { \
  728. *valid = false; \
  729. *oob = true; \
  730. return; \
  731. } \
  732. (*VariantGetInternalPtr<m_base_type>::get_ptr(base))[index] = *value; \
  733. *oob = false; \
  734. *valid = true; \
  735. } \
  736. static void validated_set(Variant *base, int64_t index, const Variant *value, bool *oob) { \
  737. if (VariantGetInternalPtr<m_base_type>::get_ptr(base)->is_read_only()) { \
  738. *oob = true; \
  739. return; \
  740. } \
  741. (*VariantGetInternalPtr<m_base_type>::get_ptr(base))[index] = *value; \
  742. *oob = false; \
  743. } \
  744. static void ptr_set(void *base, int64_t index, const void *member) { \
  745. m_base_type &v = *reinterpret_cast<m_base_type *>(base); \
  746. v[index] = PtrToArg<Variant>::convert(member); \
  747. } \
  748. static Variant::Type get_index_type() { return Variant::NIL; } \
  749. static uint32_t get_index_usage() { return PROPERTY_USAGE_DEFAULT; } \
  750. static uint64_t get_indexed_size(const Variant *base) { return VariantGetInternalPtr<m_base_type>::get_ptr(base)->size(); } \
  751. };
  752. INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Vector2, double, real_t, 2)
  753. INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Vector2i, int64_t, int32_t, 2)
  754. INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Vector3, double, real_t, 3)
  755. INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Vector3i, int64_t, int32_t, 3)
  756. INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Vector4, double, real_t, 4)
  757. INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Vector4i, int64_t, int32_t, 4)
  758. INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Quaternion, double, real_t, 4)
  759. INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Color, double, float, 4)
  760. INDEXED_SETGET_STRUCT_BULTIN_ACCESSOR(Transform2D, Vector2, .columns, 3)
  761. INDEXED_SETGET_STRUCT_BULTIN_FUNC(Basis, Vector3, set_column, get_column, 3)
  762. INDEXED_SETGET_STRUCT_BULTIN_ACCESSOR(Projection, Vector4, .columns, 4)
  763. INDEXED_SETGET_STRUCT_TYPED_NUMERIC(PackedByteArray, int64_t, uint8_t)
  764. INDEXED_SETGET_STRUCT_TYPED_NUMERIC(PackedInt32Array, int64_t, int32_t)
  765. INDEXED_SETGET_STRUCT_TYPED_NUMERIC(PackedInt64Array, int64_t, int64_t)
  766. INDEXED_SETGET_STRUCT_TYPED_NUMERIC(PackedFloat32Array, double, float)
  767. INDEXED_SETGET_STRUCT_TYPED_NUMERIC(PackedFloat64Array, double, double)
  768. INDEXED_SETGET_STRUCT_TYPED(PackedVector2Array, Vector2)
  769. INDEXED_SETGET_STRUCT_TYPED(PackedVector3Array, Vector3)
  770. INDEXED_SETGET_STRUCT_TYPED(PackedStringArray, String)
  771. INDEXED_SETGET_STRUCT_TYPED(PackedColorArray, Color)
  772. INDEXED_SETGET_STRUCT_DICT(Dictionary)
  773. struct VariantIndexedSetterGetterInfo {
  774. void (*setter)(Variant *base, int64_t index, const Variant *value, bool *valid, bool *oob) = nullptr;
  775. void (*getter)(const Variant *base, int64_t index, Variant *value, bool *oob) = nullptr;
  776. Variant::ValidatedIndexedSetter validated_setter = nullptr;
  777. Variant::ValidatedIndexedGetter validated_getter = nullptr;
  778. Variant::PTRIndexedSetter ptr_setter = nullptr;
  779. Variant::PTRIndexedGetter ptr_getter = nullptr;
  780. uint64_t (*get_indexed_size)(const Variant *base) = nullptr;
  781. Variant::Type index_type = Variant::NIL;
  782. uint32_t index_usage = PROPERTY_USAGE_DEFAULT;
  783. bool valid = false;
  784. };
  785. static VariantIndexedSetterGetterInfo variant_indexed_setters_getters[Variant::VARIANT_MAX];
  786. template <class T>
  787. static void register_indexed_member(Variant::Type p_type) {
  788. VariantIndexedSetterGetterInfo &sgi = variant_indexed_setters_getters[p_type];
  789. sgi.setter = T::set;
  790. sgi.validated_setter = T::validated_set;
  791. sgi.ptr_setter = T::ptr_set;
  792. sgi.getter = T::get;
  793. sgi.validated_getter = T::get;
  794. sgi.ptr_getter = T::ptr_get;
  795. sgi.index_type = T::get_index_type();
  796. sgi.index_usage = T::get_index_usage();
  797. sgi.get_indexed_size = T::get_indexed_size;
  798. sgi.valid = true;
  799. }
  800. void register_indexed_setters_getters() {
  801. #define REGISTER_INDEXED_MEMBER(m_base_type) register_indexed_member<VariantIndexedSetGet_##m_base_type>(GetTypeInfo<m_base_type>::VARIANT_TYPE)
  802. REGISTER_INDEXED_MEMBER(String);
  803. REGISTER_INDEXED_MEMBER(Vector2);
  804. REGISTER_INDEXED_MEMBER(Vector2i);
  805. REGISTER_INDEXED_MEMBER(Vector3);
  806. REGISTER_INDEXED_MEMBER(Vector3i);
  807. REGISTER_INDEXED_MEMBER(Vector4);
  808. REGISTER_INDEXED_MEMBER(Vector4i);
  809. REGISTER_INDEXED_MEMBER(Quaternion);
  810. REGISTER_INDEXED_MEMBER(Color);
  811. REGISTER_INDEXED_MEMBER(Transform2D);
  812. REGISTER_INDEXED_MEMBER(Basis);
  813. REGISTER_INDEXED_MEMBER(Projection);
  814. REGISTER_INDEXED_MEMBER(PackedByteArray);
  815. REGISTER_INDEXED_MEMBER(PackedInt32Array);
  816. REGISTER_INDEXED_MEMBER(PackedInt64Array);
  817. REGISTER_INDEXED_MEMBER(PackedFloat32Array);
  818. REGISTER_INDEXED_MEMBER(PackedFloat64Array);
  819. REGISTER_INDEXED_MEMBER(PackedVector2Array);
  820. REGISTER_INDEXED_MEMBER(PackedVector3Array);
  821. REGISTER_INDEXED_MEMBER(PackedStringArray);
  822. REGISTER_INDEXED_MEMBER(PackedColorArray);
  823. REGISTER_INDEXED_MEMBER(Array);
  824. REGISTER_INDEXED_MEMBER(Dictionary);
  825. }
  826. static void unregister_indexed_setters_getters() {
  827. }
  828. bool Variant::has_indexing(Variant::Type p_type) {
  829. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, false);
  830. return variant_indexed_setters_getters[p_type].valid;
  831. }
  832. Variant::Type Variant::get_indexed_element_type(Variant::Type p_type) {
  833. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, Variant::VARIANT_MAX);
  834. return variant_indexed_setters_getters[p_type].index_type;
  835. }
  836. uint32_t Variant::get_indexed_element_usage(Variant::Type p_type) {
  837. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, PROPERTY_USAGE_DEFAULT);
  838. return variant_indexed_setters_getters[p_type].index_usage;
  839. }
  840. Variant::ValidatedIndexedSetter Variant::get_member_validated_indexed_setter(Variant::Type p_type) {
  841. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr);
  842. return variant_indexed_setters_getters[p_type].validated_setter;
  843. }
  844. Variant::ValidatedIndexedGetter Variant::get_member_validated_indexed_getter(Variant::Type p_type) {
  845. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr);
  846. return variant_indexed_setters_getters[p_type].validated_getter;
  847. }
  848. Variant::PTRIndexedSetter Variant::get_member_ptr_indexed_setter(Variant::Type p_type) {
  849. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr);
  850. return variant_indexed_setters_getters[p_type].ptr_setter;
  851. }
  852. Variant::PTRIndexedGetter Variant::get_member_ptr_indexed_getter(Variant::Type p_type) {
  853. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr);
  854. return variant_indexed_setters_getters[p_type].ptr_getter;
  855. }
  856. void Variant::set_indexed(int64_t p_index, const Variant &p_value, bool &r_valid, bool &r_oob) {
  857. if (likely(variant_indexed_setters_getters[type].valid)) {
  858. variant_indexed_setters_getters[type].setter(this, p_index, &p_value, &r_valid, &r_oob);
  859. } else {
  860. r_valid = false;
  861. r_oob = false;
  862. }
  863. }
  864. Variant Variant::get_indexed(int64_t p_index, bool &r_valid, bool &r_oob) const {
  865. if (likely(variant_indexed_setters_getters[type].valid)) {
  866. Variant ret;
  867. variant_indexed_setters_getters[type].getter(this, p_index, &ret, &r_oob);
  868. r_valid = !r_oob;
  869. return ret;
  870. } else {
  871. r_valid = false;
  872. r_oob = false;
  873. return Variant();
  874. }
  875. }
  876. uint64_t Variant::get_indexed_size() const {
  877. if (likely(variant_indexed_setters_getters[type].valid && variant_indexed_setters_getters[type].get_indexed_size)) {
  878. return variant_indexed_setters_getters[type].get_indexed_size(this);
  879. } else {
  880. return 0;
  881. }
  882. }
  883. struct VariantKeyedSetGetDictionary {
  884. static void get(const Variant *base, const Variant *key, Variant *value, bool *r_valid) {
  885. const Variant *ptr = VariantGetInternalPtr<Dictionary>::get_ptr(base)->getptr(*key);
  886. if (!ptr) {
  887. *r_valid = false;
  888. return;
  889. }
  890. *value = *ptr;
  891. *r_valid = true;
  892. }
  893. static void ptr_get(const void *base, const void *key, void *value) {
  894. /* avoid ptrconvert for performance*/
  895. const Dictionary &v = *reinterpret_cast<const Dictionary *>(base);
  896. const Variant *ptr = v.getptr(PtrToArg<Variant>::convert(key));
  897. NULL_TEST(ptr);
  898. PtrToArg<Variant>::encode(*ptr, value);
  899. }
  900. static void set(Variant *base, const Variant *key, const Variant *value, bool *r_valid) {
  901. if (VariantGetInternalPtr<Dictionary>::get_ptr(base)->is_read_only()) {
  902. *r_valid = false;
  903. return;
  904. }
  905. (*VariantGetInternalPtr<Dictionary>::get_ptr(base))[*key] = *value;
  906. *r_valid = true;
  907. }
  908. static void ptr_set(void *base, const void *key, const void *value) {
  909. Dictionary &v = *reinterpret_cast<Dictionary *>(base);
  910. v[PtrToArg<Variant>::convert(key)] = PtrToArg<Variant>::convert(value);
  911. }
  912. static bool has(const Variant *base, const Variant *key, bool *r_valid) {
  913. *r_valid = true;
  914. return VariantGetInternalPtr<Dictionary>::get_ptr(base)->has(*key);
  915. }
  916. static uint32_t ptr_has(const void *base, const void *key) {
  917. /* avoid ptrconvert for performance*/
  918. const Dictionary &v = *reinterpret_cast<const Dictionary *>(base);
  919. return v.has(PtrToArg<Variant>::convert(key));
  920. }
  921. };
  922. struct VariantKeyedSetGetObject {
  923. static void get(const Variant *base, const Variant *key, Variant *value, bool *r_valid) {
  924. Object *obj = base->get_validated_object();
  925. if (!obj) {
  926. *r_valid = false;
  927. *value = Variant();
  928. return;
  929. }
  930. *value = obj->getvar(*key, r_valid);
  931. }
  932. static void ptr_get(const void *base, const void *key, void *value) {
  933. const Object *obj = PtrToArg<Object *>::convert(base);
  934. NULL_TEST(obj);
  935. Variant v = obj->getvar(PtrToArg<Variant>::convert(key));
  936. PtrToArg<Variant>::encode(v, value);
  937. }
  938. static void set(Variant *base, const Variant *key, const Variant *value, bool *r_valid) {
  939. Object *obj = base->get_validated_object();
  940. if (!obj) {
  941. *r_valid = false;
  942. return;
  943. }
  944. obj->setvar(*key, *value, r_valid);
  945. }
  946. static void ptr_set(void *base, const void *key, const void *value) {
  947. Object *obj = PtrToArg<Object *>::convert(base);
  948. NULL_TEST(obj);
  949. obj->setvar(PtrToArg<Variant>::convert(key), PtrToArg<Variant>::convert(value));
  950. }
  951. static bool has(const Variant *base, const Variant *key, bool *r_valid) {
  952. Object *obj = base->get_validated_object();
  953. if (!obj) {
  954. *r_valid = false;
  955. return false;
  956. }
  957. *r_valid = true;
  958. bool exists;
  959. obj->getvar(*key, &exists);
  960. return exists;
  961. }
  962. static uint32_t ptr_has(const void *base, const void *key) {
  963. const Object *obj = PtrToArg<Object *>::convert(base);
  964. ERR_FAIL_COND_V(!obj, false);
  965. bool valid;
  966. obj->getvar(PtrToArg<Variant>::convert(key), &valid);
  967. return valid;
  968. }
  969. };
  970. struct VariantKeyedSetterGetterInfo {
  971. Variant::ValidatedKeyedSetter validated_setter = nullptr;
  972. Variant::ValidatedKeyedGetter validated_getter = nullptr;
  973. Variant::ValidatedKeyedChecker validated_checker = nullptr;
  974. Variant::PTRKeyedSetter ptr_setter = nullptr;
  975. Variant::PTRKeyedGetter ptr_getter = nullptr;
  976. Variant::PTRKeyedChecker ptr_checker = nullptr;
  977. bool valid = false;
  978. };
  979. static VariantKeyedSetterGetterInfo variant_keyed_setters_getters[Variant::VARIANT_MAX];
  980. template <class T>
  981. static void register_keyed_member(Variant::Type p_type) {
  982. VariantKeyedSetterGetterInfo &sgi = variant_keyed_setters_getters[p_type];
  983. sgi.validated_setter = T::set;
  984. sgi.ptr_setter = T::ptr_set;
  985. sgi.validated_getter = T::get;
  986. sgi.ptr_getter = T::ptr_get;
  987. sgi.validated_checker = T::has;
  988. sgi.ptr_checker = T::ptr_has;
  989. sgi.valid = true;
  990. }
  991. static void register_keyed_setters_getters() {
  992. register_keyed_member<VariantKeyedSetGetDictionary>(Variant::DICTIONARY);
  993. register_keyed_member<VariantKeyedSetGetObject>(Variant::OBJECT);
  994. }
  995. bool Variant::is_keyed(Variant::Type p_type) {
  996. ERR_FAIL_INDEX_V(p_type, VARIANT_MAX, false);
  997. return variant_keyed_setters_getters[p_type].valid;
  998. }
  999. Variant::ValidatedKeyedSetter Variant::get_member_validated_keyed_setter(Variant::Type p_type) {
  1000. ERR_FAIL_INDEX_V(p_type, VARIANT_MAX, nullptr);
  1001. return variant_keyed_setters_getters[p_type].validated_setter;
  1002. }
  1003. Variant::ValidatedKeyedGetter Variant::get_member_validated_keyed_getter(Variant::Type p_type) {
  1004. ERR_FAIL_INDEX_V(p_type, VARIANT_MAX, nullptr);
  1005. return variant_keyed_setters_getters[p_type].validated_getter;
  1006. }
  1007. Variant::ValidatedKeyedChecker Variant::get_member_validated_keyed_checker(Variant::Type p_type) {
  1008. ERR_FAIL_INDEX_V(p_type, VARIANT_MAX, nullptr);
  1009. return variant_keyed_setters_getters[p_type].validated_checker;
  1010. }
  1011. Variant::PTRKeyedSetter Variant::get_member_ptr_keyed_setter(Variant::Type p_type) {
  1012. ERR_FAIL_INDEX_V(p_type, VARIANT_MAX, nullptr);
  1013. return variant_keyed_setters_getters[p_type].ptr_setter;
  1014. }
  1015. Variant::PTRKeyedGetter Variant::get_member_ptr_keyed_getter(Variant::Type p_type) {
  1016. ERR_FAIL_INDEX_V(p_type, VARIANT_MAX, nullptr);
  1017. return variant_keyed_setters_getters[p_type].ptr_getter;
  1018. }
  1019. Variant::PTRKeyedChecker Variant::get_member_ptr_keyed_checker(Variant::Type p_type) {
  1020. ERR_FAIL_INDEX_V(p_type, VARIANT_MAX, nullptr);
  1021. return variant_keyed_setters_getters[p_type].ptr_checker;
  1022. }
  1023. void Variant::set_keyed(const Variant &p_key, const Variant &p_value, bool &r_valid) {
  1024. if (likely(variant_keyed_setters_getters[type].valid)) {
  1025. variant_keyed_setters_getters[type].validated_setter(this, &p_key, &p_value, &r_valid);
  1026. } else {
  1027. r_valid = false;
  1028. }
  1029. }
  1030. Variant Variant::get_keyed(const Variant &p_key, bool &r_valid) const {
  1031. if (likely(variant_keyed_setters_getters[type].valid)) {
  1032. Variant ret;
  1033. variant_keyed_setters_getters[type].validated_getter(this, &p_key, &ret, &r_valid);
  1034. return ret;
  1035. } else {
  1036. r_valid = false;
  1037. return Variant();
  1038. }
  1039. }
  1040. bool Variant::has_key(const Variant &p_key, bool &r_valid) const {
  1041. if (likely(variant_keyed_setters_getters[type].valid)) {
  1042. return variant_keyed_setters_getters[type].validated_checker(this, &p_key, &r_valid);
  1043. } else {
  1044. r_valid = false;
  1045. return false;
  1046. }
  1047. }
  1048. void Variant::set(const Variant &p_index, const Variant &p_value, bool *r_valid) {
  1049. if (type == DICTIONARY || type == OBJECT) {
  1050. bool valid;
  1051. set_keyed(p_index, p_value, valid);
  1052. if (r_valid) {
  1053. *r_valid = valid;
  1054. }
  1055. } else {
  1056. bool valid = false;
  1057. if (p_index.get_type() == STRING_NAME) {
  1058. set_named(*VariantGetInternalPtr<StringName>::get_ptr(&p_index), p_value, valid);
  1059. } else if (p_index.get_type() == INT) {
  1060. bool obb;
  1061. set_indexed(*VariantGetInternalPtr<int64_t>::get_ptr(&p_index), p_value, valid, obb);
  1062. if (obb) {
  1063. valid = false;
  1064. }
  1065. } else if (p_index.get_type() == STRING) { // less efficient version of named
  1066. set_named(*VariantGetInternalPtr<String>::get_ptr(&p_index), p_value, valid);
  1067. } else if (p_index.get_type() == FLOAT) { // less efficient version of indexed
  1068. bool obb;
  1069. set_indexed(*VariantGetInternalPtr<double>::get_ptr(&p_index), p_value, valid, obb);
  1070. if (obb) {
  1071. valid = false;
  1072. }
  1073. }
  1074. if (r_valid) {
  1075. *r_valid = valid;
  1076. }
  1077. }
  1078. }
  1079. Variant Variant::get(const Variant &p_index, bool *r_valid) const {
  1080. Variant ret;
  1081. if (type == DICTIONARY || type == OBJECT) {
  1082. bool valid;
  1083. ret = get_keyed(p_index, valid);
  1084. if (r_valid) {
  1085. *r_valid = valid;
  1086. }
  1087. } else {
  1088. bool valid = false;
  1089. if (p_index.get_type() == STRING_NAME) {
  1090. ret = get_named(*VariantGetInternalPtr<StringName>::get_ptr(&p_index), valid);
  1091. } else if (p_index.get_type() == INT) {
  1092. bool obb;
  1093. ret = get_indexed(*VariantGetInternalPtr<int64_t>::get_ptr(&p_index), valid, obb);
  1094. if (obb) {
  1095. valid = false;
  1096. }
  1097. } else if (p_index.get_type() == STRING) { // less efficient version of named
  1098. ret = get_named(*VariantGetInternalPtr<String>::get_ptr(&p_index), valid);
  1099. } else if (p_index.get_type() == FLOAT) { // less efficient version of indexed
  1100. bool obb;
  1101. ret = get_indexed(*VariantGetInternalPtr<double>::get_ptr(&p_index), valid, obb);
  1102. if (obb) {
  1103. valid = false;
  1104. }
  1105. }
  1106. if (r_valid) {
  1107. *r_valid = valid;
  1108. }
  1109. }
  1110. return ret;
  1111. }
  1112. void Variant::get_property_list(List<PropertyInfo> *p_list) const {
  1113. if (type == DICTIONARY) {
  1114. const Dictionary *dic = reinterpret_cast<const Dictionary *>(_data._mem);
  1115. List<Variant> keys;
  1116. dic->get_key_list(&keys);
  1117. for (const Variant &E : keys) {
  1118. if (E.get_type() == Variant::STRING) {
  1119. p_list->push_back(PropertyInfo(Variant::STRING, E));
  1120. }
  1121. }
  1122. } else if (type == OBJECT) {
  1123. Object *obj = get_validated_object();
  1124. ERR_FAIL_COND(!obj);
  1125. obj->get_property_list(p_list);
  1126. } else {
  1127. List<StringName> members;
  1128. get_member_list(type, &members);
  1129. for (const StringName &E : members) {
  1130. PropertyInfo pi;
  1131. pi.name = E;
  1132. pi.type = get_member_type(type, E);
  1133. p_list->push_back(pi);
  1134. }
  1135. }
  1136. }
  1137. bool Variant::iter_init(Variant &r_iter, bool &valid) const {
  1138. valid = true;
  1139. switch (type) {
  1140. case INT: {
  1141. r_iter = 0;
  1142. return _data._int > 0;
  1143. } break;
  1144. case FLOAT: {
  1145. r_iter = 0.0;
  1146. return _data._float > 0.0;
  1147. } break;
  1148. case VECTOR2: {
  1149. double from = reinterpret_cast<const Vector2 *>(_data._mem)->x;
  1150. double to = reinterpret_cast<const Vector2 *>(_data._mem)->y;
  1151. r_iter = from;
  1152. return from < to;
  1153. } break;
  1154. case VECTOR2I: {
  1155. int64_t from = reinterpret_cast<const Vector2i *>(_data._mem)->x;
  1156. int64_t to = reinterpret_cast<const Vector2i *>(_data._mem)->y;
  1157. r_iter = from;
  1158. return from < to;
  1159. } break;
  1160. case VECTOR3: {
  1161. double from = reinterpret_cast<const Vector3 *>(_data._mem)->x;
  1162. double to = reinterpret_cast<const Vector3 *>(_data._mem)->y;
  1163. double step = reinterpret_cast<const Vector3 *>(_data._mem)->z;
  1164. r_iter = from;
  1165. if (from == to) {
  1166. return false;
  1167. } else if (from < to) {
  1168. return step > 0;
  1169. }
  1170. return step < 0;
  1171. } break;
  1172. case VECTOR3I: {
  1173. int64_t from = reinterpret_cast<const Vector3i *>(_data._mem)->x;
  1174. int64_t to = reinterpret_cast<const Vector3i *>(_data._mem)->y;
  1175. int64_t step = reinterpret_cast<const Vector3i *>(_data._mem)->z;
  1176. r_iter = from;
  1177. if (from == to) {
  1178. return false;
  1179. } else if (from < to) {
  1180. return step > 0;
  1181. }
  1182. return step < 0;
  1183. } break;
  1184. case OBJECT: {
  1185. if (!_get_obj().obj) {
  1186. valid = false;
  1187. return false;
  1188. }
  1189. #ifdef DEBUG_ENABLED
  1190. if (EngineDebugger::is_active() && !_get_obj().id.is_ref_counted() && ObjectDB::get_instance(_get_obj().id) == nullptr) {
  1191. valid = false;
  1192. return false;
  1193. }
  1194. #endif
  1195. Callable::CallError ce;
  1196. ce.error = Callable::CallError::CALL_OK;
  1197. Array ref;
  1198. ref.push_back(r_iter);
  1199. Variant vref = ref;
  1200. const Variant *refp[] = { &vref };
  1201. Variant ret = _get_obj().obj->callp(CoreStringNames::get_singleton()->_iter_init, refp, 1, ce);
  1202. if (ref.size() != 1 || ce.error != Callable::CallError::CALL_OK) {
  1203. valid = false;
  1204. return false;
  1205. }
  1206. r_iter = ref[0];
  1207. return ret;
  1208. } break;
  1209. case STRING: {
  1210. const String *str = reinterpret_cast<const String *>(_data._mem);
  1211. if (str->is_empty()) {
  1212. return false;
  1213. }
  1214. r_iter = 0;
  1215. return true;
  1216. } break;
  1217. case DICTIONARY: {
  1218. const Dictionary *dic = reinterpret_cast<const Dictionary *>(_data._mem);
  1219. if (dic->is_empty()) {
  1220. return false;
  1221. }
  1222. const Variant *next = dic->next(nullptr);
  1223. r_iter = *next;
  1224. return true;
  1225. } break;
  1226. case ARRAY: {
  1227. const Array *arr = reinterpret_cast<const Array *>(_data._mem);
  1228. if (arr->is_empty()) {
  1229. return false;
  1230. }
  1231. r_iter = 0;
  1232. return true;
  1233. } break;
  1234. case PACKED_BYTE_ARRAY: {
  1235. const Vector<uint8_t> *arr = &PackedArrayRef<uint8_t>::get_array(_data.packed_array);
  1236. if (arr->size() == 0) {
  1237. return false;
  1238. }
  1239. r_iter = 0;
  1240. return true;
  1241. } break;
  1242. case PACKED_INT32_ARRAY: {
  1243. const Vector<int32_t> *arr = &PackedArrayRef<int32_t>::get_array(_data.packed_array);
  1244. if (arr->size() == 0) {
  1245. return false;
  1246. }
  1247. r_iter = 0;
  1248. return true;
  1249. } break;
  1250. case PACKED_INT64_ARRAY: {
  1251. const Vector<int64_t> *arr = &PackedArrayRef<int64_t>::get_array(_data.packed_array);
  1252. if (arr->size() == 0) {
  1253. return false;
  1254. }
  1255. r_iter = 0;
  1256. return true;
  1257. } break;
  1258. case PACKED_FLOAT32_ARRAY: {
  1259. const Vector<float> *arr = &PackedArrayRef<float>::get_array(_data.packed_array);
  1260. if (arr->size() == 0) {
  1261. return false;
  1262. }
  1263. r_iter = 0;
  1264. return true;
  1265. } break;
  1266. case PACKED_FLOAT64_ARRAY: {
  1267. const Vector<double> *arr = &PackedArrayRef<double>::get_array(_data.packed_array);
  1268. if (arr->size() == 0) {
  1269. return false;
  1270. }
  1271. r_iter = 0;
  1272. return true;
  1273. } break;
  1274. case PACKED_STRING_ARRAY: {
  1275. const Vector<String> *arr = &PackedArrayRef<String>::get_array(_data.packed_array);
  1276. if (arr->size() == 0) {
  1277. return false;
  1278. }
  1279. r_iter = 0;
  1280. return true;
  1281. } break;
  1282. case PACKED_VECTOR2_ARRAY: {
  1283. const Vector<Vector2> *arr = &PackedArrayRef<Vector2>::get_array(_data.packed_array);
  1284. if (arr->size() == 0) {
  1285. return false;
  1286. }
  1287. r_iter = 0;
  1288. return true;
  1289. } break;
  1290. case PACKED_VECTOR3_ARRAY: {
  1291. const Vector<Vector3> *arr = &PackedArrayRef<Vector3>::get_array(_data.packed_array);
  1292. if (arr->size() == 0) {
  1293. return false;
  1294. }
  1295. r_iter = 0;
  1296. return true;
  1297. } break;
  1298. case PACKED_COLOR_ARRAY: {
  1299. const Vector<Color> *arr = &PackedArrayRef<Color>::get_array(_data.packed_array);
  1300. if (arr->size() == 0) {
  1301. return false;
  1302. }
  1303. r_iter = 0;
  1304. return true;
  1305. } break;
  1306. default: {
  1307. }
  1308. }
  1309. valid = false;
  1310. return false;
  1311. }
  1312. bool Variant::iter_next(Variant &r_iter, bool &valid) const {
  1313. valid = true;
  1314. switch (type) {
  1315. case INT: {
  1316. int64_t idx = r_iter;
  1317. idx++;
  1318. if (idx >= _data._int) {
  1319. return false;
  1320. }
  1321. r_iter = idx;
  1322. return true;
  1323. } break;
  1324. case FLOAT: {
  1325. double idx = r_iter;
  1326. idx++;
  1327. if (idx >= _data._float) {
  1328. return false;
  1329. }
  1330. r_iter = idx;
  1331. return true;
  1332. } break;
  1333. case VECTOR2: {
  1334. double to = reinterpret_cast<const Vector2 *>(_data._mem)->y;
  1335. double idx = r_iter;
  1336. idx++;
  1337. if (idx >= to) {
  1338. return false;
  1339. }
  1340. r_iter = idx;
  1341. return true;
  1342. } break;
  1343. case VECTOR2I: {
  1344. int64_t to = reinterpret_cast<const Vector2i *>(_data._mem)->y;
  1345. int64_t idx = r_iter;
  1346. idx++;
  1347. if (idx >= to) {
  1348. return false;
  1349. }
  1350. r_iter = idx;
  1351. return true;
  1352. } break;
  1353. case VECTOR3: {
  1354. double to = reinterpret_cast<const Vector3 *>(_data._mem)->y;
  1355. double step = reinterpret_cast<const Vector3 *>(_data._mem)->z;
  1356. double idx = r_iter;
  1357. idx += step;
  1358. if (step < 0 && idx <= to) {
  1359. return false;
  1360. }
  1361. if (step > 0 && idx >= to) {
  1362. return false;
  1363. }
  1364. r_iter = idx;
  1365. return true;
  1366. } break;
  1367. case VECTOR3I: {
  1368. int64_t to = reinterpret_cast<const Vector3i *>(_data._mem)->y;
  1369. int64_t step = reinterpret_cast<const Vector3i *>(_data._mem)->z;
  1370. int64_t idx = r_iter;
  1371. idx += step;
  1372. if (step < 0 && idx <= to) {
  1373. return false;
  1374. }
  1375. if (step > 0 && idx >= to) {
  1376. return false;
  1377. }
  1378. r_iter = idx;
  1379. return true;
  1380. } break;
  1381. case OBJECT: {
  1382. if (!_get_obj().obj) {
  1383. valid = false;
  1384. return false;
  1385. }
  1386. #ifdef DEBUG_ENABLED
  1387. if (EngineDebugger::is_active() && !_get_obj().id.is_ref_counted() && ObjectDB::get_instance(_get_obj().id) == nullptr) {
  1388. valid = false;
  1389. return false;
  1390. }
  1391. #endif
  1392. Callable::CallError ce;
  1393. ce.error = Callable::CallError::CALL_OK;
  1394. Array ref;
  1395. ref.push_back(r_iter);
  1396. Variant vref = ref;
  1397. const Variant *refp[] = { &vref };
  1398. Variant ret = _get_obj().obj->callp(CoreStringNames::get_singleton()->_iter_next, refp, 1, ce);
  1399. if (ref.size() != 1 || ce.error != Callable::CallError::CALL_OK) {
  1400. valid = false;
  1401. return false;
  1402. }
  1403. r_iter = ref[0];
  1404. return ret;
  1405. } break;
  1406. case STRING: {
  1407. const String *str = reinterpret_cast<const String *>(_data._mem);
  1408. int idx = r_iter;
  1409. idx++;
  1410. if (idx >= str->length()) {
  1411. return false;
  1412. }
  1413. r_iter = idx;
  1414. return true;
  1415. } break;
  1416. case DICTIONARY: {
  1417. const Dictionary *dic = reinterpret_cast<const Dictionary *>(_data._mem);
  1418. const Variant *next = dic->next(&r_iter);
  1419. if (!next) {
  1420. return false;
  1421. }
  1422. r_iter = *next;
  1423. return true;
  1424. } break;
  1425. case ARRAY: {
  1426. const Array *arr = reinterpret_cast<const Array *>(_data._mem);
  1427. int idx = r_iter;
  1428. idx++;
  1429. if (idx >= arr->size()) {
  1430. return false;
  1431. }
  1432. r_iter = idx;
  1433. return true;
  1434. } break;
  1435. case PACKED_BYTE_ARRAY: {
  1436. const Vector<uint8_t> *arr = &PackedArrayRef<uint8_t>::get_array(_data.packed_array);
  1437. int idx = r_iter;
  1438. idx++;
  1439. if (idx >= arr->size()) {
  1440. return false;
  1441. }
  1442. r_iter = idx;
  1443. return true;
  1444. } break;
  1445. case PACKED_INT32_ARRAY: {
  1446. const Vector<int32_t> *arr = &PackedArrayRef<int32_t>::get_array(_data.packed_array);
  1447. int32_t idx = r_iter;
  1448. idx++;
  1449. if (idx >= arr->size()) {
  1450. return false;
  1451. }
  1452. r_iter = idx;
  1453. return true;
  1454. } break;
  1455. case PACKED_INT64_ARRAY: {
  1456. const Vector<int64_t> *arr = &PackedArrayRef<int64_t>::get_array(_data.packed_array);
  1457. int64_t idx = r_iter;
  1458. idx++;
  1459. if (idx >= arr->size()) {
  1460. return false;
  1461. }
  1462. r_iter = idx;
  1463. return true;
  1464. } break;
  1465. case PACKED_FLOAT32_ARRAY: {
  1466. const Vector<float> *arr = &PackedArrayRef<float>::get_array(_data.packed_array);
  1467. int idx = r_iter;
  1468. idx++;
  1469. if (idx >= arr->size()) {
  1470. return false;
  1471. }
  1472. r_iter = idx;
  1473. return true;
  1474. } break;
  1475. case PACKED_FLOAT64_ARRAY: {
  1476. const Vector<double> *arr = &PackedArrayRef<double>::get_array(_data.packed_array);
  1477. int idx = r_iter;
  1478. idx++;
  1479. if (idx >= arr->size()) {
  1480. return false;
  1481. }
  1482. r_iter = idx;
  1483. return true;
  1484. } break;
  1485. case PACKED_STRING_ARRAY: {
  1486. const Vector<String> *arr = &PackedArrayRef<String>::get_array(_data.packed_array);
  1487. int idx = r_iter;
  1488. idx++;
  1489. if (idx >= arr->size()) {
  1490. return false;
  1491. }
  1492. r_iter = idx;
  1493. return true;
  1494. } break;
  1495. case PACKED_VECTOR2_ARRAY: {
  1496. const Vector<Vector2> *arr = &PackedArrayRef<Vector2>::get_array(_data.packed_array);
  1497. int idx = r_iter;
  1498. idx++;
  1499. if (idx >= arr->size()) {
  1500. return false;
  1501. }
  1502. r_iter = idx;
  1503. return true;
  1504. } break;
  1505. case PACKED_VECTOR3_ARRAY: {
  1506. const Vector<Vector3> *arr = &PackedArrayRef<Vector3>::get_array(_data.packed_array);
  1507. int idx = r_iter;
  1508. idx++;
  1509. if (idx >= arr->size()) {
  1510. return false;
  1511. }
  1512. r_iter = idx;
  1513. return true;
  1514. } break;
  1515. case PACKED_COLOR_ARRAY: {
  1516. const Vector<Color> *arr = &PackedArrayRef<Color>::get_array(_data.packed_array);
  1517. int idx = r_iter;
  1518. idx++;
  1519. if (idx >= arr->size()) {
  1520. return false;
  1521. }
  1522. r_iter = idx;
  1523. return true;
  1524. } break;
  1525. default: {
  1526. }
  1527. }
  1528. valid = false;
  1529. return false;
  1530. }
  1531. Variant Variant::iter_get(const Variant &r_iter, bool &r_valid) const {
  1532. r_valid = true;
  1533. switch (type) {
  1534. case INT: {
  1535. return r_iter;
  1536. } break;
  1537. case FLOAT: {
  1538. return r_iter;
  1539. } break;
  1540. case VECTOR2: {
  1541. return r_iter;
  1542. } break;
  1543. case VECTOR2I: {
  1544. return r_iter;
  1545. } break;
  1546. case VECTOR3: {
  1547. return r_iter;
  1548. } break;
  1549. case VECTOR3I: {
  1550. return r_iter;
  1551. } break;
  1552. case OBJECT: {
  1553. if (!_get_obj().obj) {
  1554. r_valid = false;
  1555. return Variant();
  1556. }
  1557. #ifdef DEBUG_ENABLED
  1558. if (EngineDebugger::is_active() && !_get_obj().id.is_ref_counted() && ObjectDB::get_instance(_get_obj().id) == nullptr) {
  1559. r_valid = false;
  1560. return Variant();
  1561. }
  1562. #endif
  1563. Callable::CallError ce;
  1564. ce.error = Callable::CallError::CALL_OK;
  1565. const Variant *refp[] = { &r_iter };
  1566. Variant ret = _get_obj().obj->callp(CoreStringNames::get_singleton()->_iter_get, refp, 1, ce);
  1567. if (ce.error != Callable::CallError::CALL_OK) {
  1568. r_valid = false;
  1569. return Variant();
  1570. }
  1571. //r_iter=ref[0];
  1572. return ret;
  1573. } break;
  1574. case STRING: {
  1575. const String *str = reinterpret_cast<const String *>(_data._mem);
  1576. return str->substr(r_iter, 1);
  1577. } break;
  1578. case DICTIONARY: {
  1579. return r_iter; //iterator is the same as the key
  1580. } break;
  1581. case ARRAY: {
  1582. const Array *arr = reinterpret_cast<const Array *>(_data._mem);
  1583. int idx = r_iter;
  1584. #ifdef DEBUG_ENABLED
  1585. if (idx < 0 || idx >= arr->size()) {
  1586. r_valid = false;
  1587. return Variant();
  1588. }
  1589. #endif
  1590. return arr->get(idx);
  1591. } break;
  1592. case PACKED_BYTE_ARRAY: {
  1593. const Vector<uint8_t> *arr = &PackedArrayRef<uint8_t>::get_array(_data.packed_array);
  1594. int idx = r_iter;
  1595. #ifdef DEBUG_ENABLED
  1596. if (idx < 0 || idx >= arr->size()) {
  1597. r_valid = false;
  1598. return Variant();
  1599. }
  1600. #endif
  1601. return arr->get(idx);
  1602. } break;
  1603. case PACKED_INT32_ARRAY: {
  1604. const Vector<int32_t> *arr = &PackedArrayRef<int32_t>::get_array(_data.packed_array);
  1605. int32_t idx = r_iter;
  1606. #ifdef DEBUG_ENABLED
  1607. if (idx < 0 || idx >= arr->size()) {
  1608. r_valid = false;
  1609. return Variant();
  1610. }
  1611. #endif
  1612. return arr->get(idx);
  1613. } break;
  1614. case PACKED_INT64_ARRAY: {
  1615. const Vector<int64_t> *arr = &PackedArrayRef<int64_t>::get_array(_data.packed_array);
  1616. int64_t idx = r_iter;
  1617. #ifdef DEBUG_ENABLED
  1618. if (idx < 0 || idx >= arr->size()) {
  1619. r_valid = false;
  1620. return Variant();
  1621. }
  1622. #endif
  1623. return arr->get(idx);
  1624. } break;
  1625. case PACKED_FLOAT32_ARRAY: {
  1626. const Vector<float> *arr = &PackedArrayRef<float>::get_array(_data.packed_array);
  1627. int idx = r_iter;
  1628. #ifdef DEBUG_ENABLED
  1629. if (idx < 0 || idx >= arr->size()) {
  1630. r_valid = false;
  1631. return Variant();
  1632. }
  1633. #endif
  1634. return arr->get(idx);
  1635. } break;
  1636. case PACKED_FLOAT64_ARRAY: {
  1637. const Vector<double> *arr = &PackedArrayRef<double>::get_array(_data.packed_array);
  1638. int idx = r_iter;
  1639. #ifdef DEBUG_ENABLED
  1640. if (idx < 0 || idx >= arr->size()) {
  1641. r_valid = false;
  1642. return Variant();
  1643. }
  1644. #endif
  1645. return arr->get(idx);
  1646. } break;
  1647. case PACKED_STRING_ARRAY: {
  1648. const Vector<String> *arr = &PackedArrayRef<String>::get_array(_data.packed_array);
  1649. int idx = r_iter;
  1650. #ifdef DEBUG_ENABLED
  1651. if (idx < 0 || idx >= arr->size()) {
  1652. r_valid = false;
  1653. return Variant();
  1654. }
  1655. #endif
  1656. return arr->get(idx);
  1657. } break;
  1658. case PACKED_VECTOR2_ARRAY: {
  1659. const Vector<Vector2> *arr = &PackedArrayRef<Vector2>::get_array(_data.packed_array);
  1660. int idx = r_iter;
  1661. #ifdef DEBUG_ENABLED
  1662. if (idx < 0 || idx >= arr->size()) {
  1663. r_valid = false;
  1664. return Variant();
  1665. }
  1666. #endif
  1667. return arr->get(idx);
  1668. } break;
  1669. case PACKED_VECTOR3_ARRAY: {
  1670. const Vector<Vector3> *arr = &PackedArrayRef<Vector3>::get_array(_data.packed_array);
  1671. int idx = r_iter;
  1672. #ifdef DEBUG_ENABLED
  1673. if (idx < 0 || idx >= arr->size()) {
  1674. r_valid = false;
  1675. return Variant();
  1676. }
  1677. #endif
  1678. return arr->get(idx);
  1679. } break;
  1680. case PACKED_COLOR_ARRAY: {
  1681. const Vector<Color> *arr = &PackedArrayRef<Color>::get_array(_data.packed_array);
  1682. int idx = r_iter;
  1683. #ifdef DEBUG_ENABLED
  1684. if (idx < 0 || idx >= arr->size()) {
  1685. r_valid = false;
  1686. return Variant();
  1687. }
  1688. #endif
  1689. return arr->get(idx);
  1690. } break;
  1691. default: {
  1692. }
  1693. }
  1694. r_valid = false;
  1695. return Variant();
  1696. }
  1697. Variant Variant::duplicate(bool p_deep) const {
  1698. return recursive_duplicate(p_deep, 0);
  1699. }
  1700. Variant Variant::recursive_duplicate(bool p_deep, int recursion_count) const {
  1701. switch (type) {
  1702. case OBJECT: {
  1703. /* breaks stuff :(
  1704. if (p_deep && !_get_obj().ref.is_null()) {
  1705. Ref<Resource> resource = _get_obj().ref;
  1706. if (resource.is_valid()) {
  1707. return resource->duplicate(true);
  1708. }
  1709. }
  1710. */
  1711. return *this;
  1712. } break;
  1713. case DICTIONARY:
  1714. return operator Dictionary().recursive_duplicate(p_deep, recursion_count);
  1715. case ARRAY:
  1716. return operator Array().recursive_duplicate(p_deep, recursion_count);
  1717. case PACKED_BYTE_ARRAY:
  1718. return operator Vector<uint8_t>().duplicate();
  1719. case PACKED_INT32_ARRAY:
  1720. return operator Vector<int32_t>().duplicate();
  1721. case PACKED_INT64_ARRAY:
  1722. return operator Vector<int64_t>().duplicate();
  1723. case PACKED_FLOAT32_ARRAY:
  1724. return operator Vector<float>().duplicate();
  1725. case PACKED_FLOAT64_ARRAY:
  1726. return operator Vector<double>().duplicate();
  1727. case PACKED_STRING_ARRAY:
  1728. return operator Vector<String>().duplicate();
  1729. case PACKED_VECTOR2_ARRAY:
  1730. return operator Vector<Vector2>().duplicate();
  1731. case PACKED_VECTOR3_ARRAY:
  1732. return operator Vector<Vector3>().duplicate();
  1733. case PACKED_COLOR_ARRAY:
  1734. return operator Vector<Color>().duplicate();
  1735. default:
  1736. return *this;
  1737. }
  1738. }
  1739. void Variant::_register_variant_setters_getters() {
  1740. register_named_setters_getters();
  1741. register_indexed_setters_getters();
  1742. register_keyed_setters_getters();
  1743. }
  1744. void Variant::_unregister_variant_setters_getters() {
  1745. unregister_named_setters_getters();
  1746. unregister_indexed_setters_getters();
  1747. }