variant_setget.cpp 87 KB

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