variant_op.h 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. /**************************************************************************/
  2. /* variant_op.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef VARIANT_OP_H
  31. #define VARIANT_OP_H
  32. #include "variant.h"
  33. #include "core/debugger/engine_debugger.h"
  34. #include "core/object/class_db.h"
  35. template <typename R, typename A, typename B>
  36. class OperatorEvaluatorAdd {
  37. public:
  38. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  39. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  40. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  41. *r_ret = a + b;
  42. r_valid = true;
  43. }
  44. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  45. *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) + *VariantGetInternalPtr<B>::get_ptr(right);
  46. }
  47. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  48. PtrToArg<R>::encode(PtrToArg<A>::convert(left) + PtrToArg<B>::convert(right), r_ret);
  49. }
  50. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  51. };
  52. template <typename R, typename A, typename B>
  53. class OperatorEvaluatorSub {
  54. public:
  55. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  56. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  57. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  58. *r_ret = a - b;
  59. r_valid = true;
  60. }
  61. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  62. *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) - *VariantGetInternalPtr<B>::get_ptr(right);
  63. }
  64. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  65. PtrToArg<R>::encode(PtrToArg<A>::convert(left) - PtrToArg<B>::convert(right), r_ret);
  66. }
  67. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  68. };
  69. template <typename R, typename A, typename B>
  70. class OperatorEvaluatorMul {
  71. public:
  72. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  73. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  74. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  75. *r_ret = a * b;
  76. r_valid = true;
  77. }
  78. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  79. *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) * *VariantGetInternalPtr<B>::get_ptr(right);
  80. }
  81. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  82. PtrToArg<R>::encode(PtrToArg<A>::convert(left) * PtrToArg<B>::convert(right), r_ret);
  83. }
  84. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  85. };
  86. template <typename R, typename A, typename B>
  87. class OperatorEvaluatorPow {
  88. public:
  89. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  90. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  91. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  92. *r_ret = R(Math::pow((double)a, (double)b));
  93. r_valid = true;
  94. }
  95. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  96. *VariantGetInternalPtr<R>::get_ptr(r_ret) = R(Math::pow((double)*VariantGetInternalPtr<A>::get_ptr(left), (double)*VariantGetInternalPtr<B>::get_ptr(right)));
  97. }
  98. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  99. PtrToArg<R>::encode(R(Math::pow((double)PtrToArg<A>::convert(left), (double)PtrToArg<B>::convert(right))), r_ret);
  100. }
  101. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  102. };
  103. template <typename R, typename A, typename B>
  104. class OperatorEvaluatorXForm {
  105. public:
  106. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  107. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  108. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  109. *r_ret = a.xform(b);
  110. r_valid = true;
  111. }
  112. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  113. *VariantGetInternalPtr<R>::get_ptr(r_ret) = VariantGetInternalPtr<A>::get_ptr(left)->xform(*VariantGetInternalPtr<B>::get_ptr(right));
  114. }
  115. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  116. PtrToArg<R>::encode(PtrToArg<A>::convert(left).xform(PtrToArg<B>::convert(right)), r_ret);
  117. }
  118. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  119. };
  120. template <typename R, typename A, typename B>
  121. class OperatorEvaluatorXFormInv {
  122. public:
  123. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  124. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  125. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  126. *r_ret = b.xform_inv(a);
  127. r_valid = true;
  128. }
  129. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  130. *VariantGetInternalPtr<R>::get_ptr(r_ret) = VariantGetInternalPtr<B>::get_ptr(right)->xform_inv(*VariantGetInternalPtr<A>::get_ptr(left));
  131. }
  132. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  133. PtrToArg<R>::encode(PtrToArg<B>::convert(right).xform_inv(PtrToArg<A>::convert(left)), r_ret);
  134. }
  135. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  136. };
  137. template <typename R, typename A, typename B>
  138. class OperatorEvaluatorDiv {
  139. public:
  140. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  141. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  142. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  143. *r_ret = a / b;
  144. r_valid = true;
  145. }
  146. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  147. *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) / *VariantGetInternalPtr<B>::get_ptr(right);
  148. }
  149. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  150. PtrToArg<R>::encode(PtrToArg<A>::convert(left) / PtrToArg<B>::convert(right), r_ret);
  151. }
  152. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  153. };
  154. template <typename R, typename A, typename B>
  155. class OperatorEvaluatorDivNZ {
  156. public:
  157. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  158. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  159. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  160. if (b == 0) {
  161. r_valid = false;
  162. *r_ret = "Division by zero error";
  163. return;
  164. }
  165. *r_ret = a / b;
  166. r_valid = true;
  167. }
  168. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  169. *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) / *VariantGetInternalPtr<B>::get_ptr(right);
  170. }
  171. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  172. PtrToArg<R>::encode(PtrToArg<A>::convert(left) / PtrToArg<B>::convert(right), r_ret);
  173. }
  174. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  175. };
  176. template <>
  177. class OperatorEvaluatorDivNZ<Vector2i, Vector2i, Vector2i> {
  178. public:
  179. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  180. const Vector2i &a = *VariantGetInternalPtr<Vector2i>::get_ptr(&p_left);
  181. const Vector2i &b = *VariantGetInternalPtr<Vector2i>::get_ptr(&p_right);
  182. if (unlikely(b.x == 0 || b.y == 0)) {
  183. r_valid = false;
  184. *r_ret = "Division by zero error";
  185. return;
  186. }
  187. *r_ret = a / b;
  188. r_valid = true;
  189. }
  190. static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  191. VariantTypeChanger<Vector2i>::change(r_ret);
  192. *VariantGetInternalPtr<Vector2i>::get_ptr(r_ret) = *VariantGetInternalPtr<Vector2i>::get_ptr(left) / *VariantGetInternalPtr<Vector2i>::get_ptr(right);
  193. }
  194. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  195. PtrToArg<Vector2i>::encode(PtrToArg<Vector2i>::convert(left) / PtrToArg<Vector2i>::convert(right), r_ret);
  196. }
  197. static Variant::Type get_return_type() { return GetTypeInfo<Vector2i>::VARIANT_TYPE; }
  198. };
  199. template <>
  200. class OperatorEvaluatorDivNZ<Vector3i, Vector3i, Vector3i> {
  201. public:
  202. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  203. const Vector3i &a = *VariantGetInternalPtr<Vector3i>::get_ptr(&p_left);
  204. const Vector3i &b = *VariantGetInternalPtr<Vector3i>::get_ptr(&p_right);
  205. if (unlikely(b.x == 0 || b.y == 0 || b.z == 0)) {
  206. r_valid = false;
  207. *r_ret = "Division by zero error";
  208. return;
  209. }
  210. *r_ret = a / b;
  211. r_valid = true;
  212. }
  213. static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  214. VariantTypeChanger<Vector3i>::change(r_ret);
  215. *VariantGetInternalPtr<Vector3i>::get_ptr(r_ret) = *VariantGetInternalPtr<Vector3i>::get_ptr(left) / *VariantGetInternalPtr<Vector3i>::get_ptr(right);
  216. }
  217. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  218. PtrToArg<Vector3i>::encode(PtrToArg<Vector3i>::convert(left) / PtrToArg<Vector3i>::convert(right), r_ret);
  219. }
  220. static Variant::Type get_return_type() { return GetTypeInfo<Vector3i>::VARIANT_TYPE; }
  221. };
  222. template <>
  223. class OperatorEvaluatorDivNZ<Vector4i, Vector4i, Vector4i> {
  224. public:
  225. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  226. const Vector4i &a = *VariantGetInternalPtr<Vector4i>::get_ptr(&p_left);
  227. const Vector4i &b = *VariantGetInternalPtr<Vector4i>::get_ptr(&p_right);
  228. if (unlikely(b.x == 0 || b.y == 0 || b.z == 0 || b.w == 0)) {
  229. r_valid = false;
  230. *r_ret = "Division by zero error";
  231. return;
  232. }
  233. *r_ret = a / b;
  234. r_valid = true;
  235. }
  236. static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  237. VariantTypeChanger<Vector4i>::change(r_ret);
  238. *VariantGetInternalPtr<Vector4i>::get_ptr(r_ret) = *VariantGetInternalPtr<Vector4i>::get_ptr(left) / *VariantGetInternalPtr<Vector4i>::get_ptr(right);
  239. }
  240. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  241. PtrToArg<Vector4i>::encode(PtrToArg<Vector4i>::convert(left) / PtrToArg<Vector4i>::convert(right), r_ret);
  242. }
  243. static Variant::Type get_return_type() { return GetTypeInfo<Vector4i>::VARIANT_TYPE; }
  244. };
  245. template <typename R, typename A, typename B>
  246. class OperatorEvaluatorMod {
  247. public:
  248. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  249. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  250. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  251. *r_ret = a % b;
  252. r_valid = true;
  253. }
  254. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  255. *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) % *VariantGetInternalPtr<B>::get_ptr(right);
  256. }
  257. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  258. PtrToArg<R>::encode(PtrToArg<A>::convert(left) % PtrToArg<B>::convert(right), r_ret);
  259. }
  260. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  261. };
  262. template <typename R, typename A, typename B>
  263. class OperatorEvaluatorModNZ {
  264. public:
  265. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  266. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  267. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  268. if (b == 0) {
  269. r_valid = false;
  270. *r_ret = "Modulo by zero error";
  271. return;
  272. }
  273. *r_ret = a % b;
  274. r_valid = true;
  275. }
  276. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  277. *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) % *VariantGetInternalPtr<B>::get_ptr(right);
  278. }
  279. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  280. PtrToArg<R>::encode(PtrToArg<A>::convert(left) % PtrToArg<B>::convert(right), r_ret);
  281. }
  282. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  283. };
  284. template <>
  285. class OperatorEvaluatorModNZ<Vector2i, Vector2i, Vector2i> {
  286. public:
  287. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  288. const Vector2i &a = *VariantGetInternalPtr<Vector2i>::get_ptr(&p_left);
  289. const Vector2i &b = *VariantGetInternalPtr<Vector2i>::get_ptr(&p_right);
  290. if (unlikely(b.x == 0 || b.y == 0)) {
  291. r_valid = false;
  292. *r_ret = "Modulo by zero error";
  293. return;
  294. }
  295. *r_ret = a % b;
  296. r_valid = true;
  297. }
  298. static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  299. VariantTypeChanger<Vector2i>::change(r_ret);
  300. *VariantGetInternalPtr<Vector2i>::get_ptr(r_ret) = *VariantGetInternalPtr<Vector2i>::get_ptr(left) % *VariantGetInternalPtr<Vector2i>::get_ptr(right);
  301. }
  302. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  303. PtrToArg<Vector2i>::encode(PtrToArg<Vector2i>::convert(left) / PtrToArg<Vector2i>::convert(right), r_ret);
  304. }
  305. static Variant::Type get_return_type() { return GetTypeInfo<Vector2i>::VARIANT_TYPE; }
  306. };
  307. template <>
  308. class OperatorEvaluatorModNZ<Vector3i, Vector3i, Vector3i> {
  309. public:
  310. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  311. const Vector3i &a = *VariantGetInternalPtr<Vector3i>::get_ptr(&p_left);
  312. const Vector3i &b = *VariantGetInternalPtr<Vector3i>::get_ptr(&p_right);
  313. if (unlikely(b.x == 0 || b.y == 0 || b.z == 0)) {
  314. r_valid = false;
  315. *r_ret = "Modulo by zero error";
  316. return;
  317. }
  318. *r_ret = a % b;
  319. r_valid = true;
  320. }
  321. static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  322. VariantTypeChanger<Vector3i>::change(r_ret);
  323. *VariantGetInternalPtr<Vector3i>::get_ptr(r_ret) = *VariantGetInternalPtr<Vector3i>::get_ptr(left) % *VariantGetInternalPtr<Vector3i>::get_ptr(right);
  324. }
  325. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  326. PtrToArg<Vector3i>::encode(PtrToArg<Vector3i>::convert(left) % PtrToArg<Vector3i>::convert(right), r_ret);
  327. }
  328. static Variant::Type get_return_type() { return GetTypeInfo<Vector3i>::VARIANT_TYPE; }
  329. };
  330. template <>
  331. class OperatorEvaluatorModNZ<Vector4i, Vector4i, Vector4i> {
  332. public:
  333. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  334. const Vector4i &a = *VariantGetInternalPtr<Vector4i>::get_ptr(&p_left);
  335. const Vector4i &b = *VariantGetInternalPtr<Vector4i>::get_ptr(&p_right);
  336. if (unlikely(b.x == 0 || b.y == 0 || b.z == 0 || b.w == 0)) {
  337. r_valid = false;
  338. *r_ret = "Modulo by zero error";
  339. return;
  340. }
  341. *r_ret = a % b;
  342. r_valid = true;
  343. }
  344. static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  345. VariantTypeChanger<Vector4i>::change(r_ret);
  346. *VariantGetInternalPtr<Vector4i>::get_ptr(r_ret) = *VariantGetInternalPtr<Vector4i>::get_ptr(left) % *VariantGetInternalPtr<Vector4i>::get_ptr(right);
  347. }
  348. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  349. PtrToArg<Vector4i>::encode(PtrToArg<Vector4i>::convert(left) % PtrToArg<Vector4i>::convert(right), r_ret);
  350. }
  351. static Variant::Type get_return_type() { return GetTypeInfo<Vector4i>::VARIANT_TYPE; }
  352. };
  353. template <typename R, typename A>
  354. class OperatorEvaluatorNeg {
  355. public:
  356. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  357. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  358. *r_ret = -a;
  359. r_valid = true;
  360. }
  361. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  362. *VariantGetInternalPtr<R>::get_ptr(r_ret) = -*VariantGetInternalPtr<A>::get_ptr(left);
  363. }
  364. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  365. PtrToArg<R>::encode(-PtrToArg<A>::convert(left), r_ret);
  366. }
  367. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  368. };
  369. template <typename R, typename A>
  370. class OperatorEvaluatorPos {
  371. public:
  372. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  373. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  374. *r_ret = a;
  375. r_valid = true;
  376. }
  377. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  378. *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left);
  379. }
  380. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  381. PtrToArg<R>::encode(PtrToArg<A>::convert(left), r_ret);
  382. }
  383. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  384. };
  385. template <typename R, typename A, typename B>
  386. class OperatorEvaluatorShiftLeft {
  387. public:
  388. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  389. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  390. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  391. #if defined(DEBUG_ENABLED)
  392. if (b < 0 || a < 0) {
  393. *r_ret = "Invalid operands for bit shifting. Only positive operands are supported.";
  394. r_valid = false;
  395. return;
  396. }
  397. #endif
  398. *r_ret = a << b;
  399. r_valid = true;
  400. }
  401. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  402. *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) << *VariantGetInternalPtr<B>::get_ptr(right);
  403. }
  404. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  405. PtrToArg<R>::encode(PtrToArg<A>::convert(left) << PtrToArg<B>::convert(right), r_ret);
  406. }
  407. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  408. };
  409. template <typename R, typename A, typename B>
  410. class OperatorEvaluatorShiftRight {
  411. public:
  412. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  413. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  414. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  415. #if defined(DEBUG_ENABLED)
  416. if (b < 0 || a < 0) {
  417. *r_ret = "Invalid operands for bit shifting. Only positive operands are supported.";
  418. r_valid = false;
  419. return;
  420. }
  421. #endif
  422. *r_ret = a >> b;
  423. r_valid = true;
  424. }
  425. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  426. *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) >> *VariantGetInternalPtr<B>::get_ptr(right);
  427. }
  428. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  429. PtrToArg<R>::encode(PtrToArg<A>::convert(left) >> PtrToArg<B>::convert(right), r_ret);
  430. }
  431. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  432. };
  433. template <typename R, typename A, typename B>
  434. class OperatorEvaluatorBitOr {
  435. public:
  436. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  437. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  438. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  439. *r_ret = a | b;
  440. r_valid = true;
  441. }
  442. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  443. *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) | *VariantGetInternalPtr<B>::get_ptr(right);
  444. }
  445. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  446. PtrToArg<R>::encode(PtrToArg<A>::convert(left) | PtrToArg<B>::convert(right), r_ret);
  447. }
  448. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  449. };
  450. template <typename R, typename A, typename B>
  451. class OperatorEvaluatorBitAnd {
  452. public:
  453. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  454. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  455. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  456. *r_ret = a & b;
  457. r_valid = true;
  458. }
  459. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  460. *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) & *VariantGetInternalPtr<B>::get_ptr(right);
  461. }
  462. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  463. PtrToArg<R>::encode(PtrToArg<A>::convert(left) & PtrToArg<B>::convert(right), r_ret);
  464. }
  465. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  466. };
  467. template <typename R, typename A, typename B>
  468. class OperatorEvaluatorBitXor {
  469. public:
  470. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  471. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  472. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  473. *r_ret = a ^ b;
  474. r_valid = true;
  475. }
  476. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  477. *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) ^ *VariantGetInternalPtr<B>::get_ptr(right);
  478. }
  479. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  480. PtrToArg<R>::encode(PtrToArg<A>::convert(left) ^ PtrToArg<B>::convert(right), r_ret);
  481. }
  482. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  483. };
  484. template <typename R, typename A>
  485. class OperatorEvaluatorBitNeg {
  486. public:
  487. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  488. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  489. *r_ret = ~a;
  490. r_valid = true;
  491. }
  492. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  493. *VariantGetInternalPtr<R>::get_ptr(r_ret) = ~*VariantGetInternalPtr<A>::get_ptr(left);
  494. }
  495. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  496. PtrToArg<R>::encode(~PtrToArg<A>::convert(left), r_ret);
  497. }
  498. static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
  499. };
  500. template <typename A, typename B>
  501. class OperatorEvaluatorEqual {
  502. public:
  503. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  504. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  505. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  506. *r_ret = a == b;
  507. r_valid = true;
  508. }
  509. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  510. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) == *VariantGetInternalPtr<B>::get_ptr(right);
  511. }
  512. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  513. PtrToArg<bool>::encode(PtrToArg<A>::convert(left) == PtrToArg<B>::convert(right), r_ret);
  514. }
  515. static Variant::Type get_return_type() { return Variant::BOOL; }
  516. };
  517. class OperatorEvaluatorEqualObject {
  518. public:
  519. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  520. const Object *a = p_left.get_validated_object();
  521. const Object *b = p_right.get_validated_object();
  522. *r_ret = a == b;
  523. r_valid = true;
  524. }
  525. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  526. const Object *a = left->get_validated_object();
  527. const Object *b = right->get_validated_object();
  528. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = a == b;
  529. }
  530. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  531. PtrToArg<bool>::encode(PtrToArg<Object *>::convert(left) == PtrToArg<Object *>::convert(right), r_ret);
  532. }
  533. static Variant::Type get_return_type() { return Variant::BOOL; }
  534. };
  535. class OperatorEvaluatorEqualObjectNil {
  536. public:
  537. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  538. const Object *a = p_left.get_validated_object();
  539. *r_ret = a == nullptr;
  540. r_valid = true;
  541. }
  542. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  543. const Object *a = left->get_validated_object();
  544. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = a == nullptr;
  545. }
  546. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  547. PtrToArg<bool>::encode(PtrToArg<Object *>::convert(left) == nullptr, r_ret);
  548. }
  549. static Variant::Type get_return_type() { return Variant::BOOL; }
  550. };
  551. class OperatorEvaluatorEqualNilObject {
  552. public:
  553. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  554. const Object *b = p_right.get_validated_object();
  555. *r_ret = nullptr == b;
  556. r_valid = true;
  557. }
  558. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  559. const Object *b = right->get_validated_object();
  560. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = nullptr == b;
  561. }
  562. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  563. PtrToArg<bool>::encode(nullptr == PtrToArg<Object *>::convert(right), r_ret);
  564. }
  565. static Variant::Type get_return_type() { return Variant::BOOL; }
  566. };
  567. template <typename A, typename B>
  568. class OperatorEvaluatorNotEqual {
  569. public:
  570. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  571. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  572. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  573. *r_ret = a != b;
  574. r_valid = true;
  575. }
  576. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  577. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) != *VariantGetInternalPtr<B>::get_ptr(right);
  578. }
  579. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  580. PtrToArg<bool>::encode(PtrToArg<A>::convert(left) != PtrToArg<B>::convert(right), r_ret);
  581. }
  582. static Variant::Type get_return_type() { return Variant::BOOL; }
  583. };
  584. class OperatorEvaluatorNotEqualObject {
  585. public:
  586. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  587. Object *a = p_left.get_validated_object();
  588. Object *b = p_right.get_validated_object();
  589. *r_ret = a != b;
  590. r_valid = true;
  591. }
  592. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  593. Object *a = left->get_validated_object();
  594. Object *b = right->get_validated_object();
  595. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = a != b;
  596. }
  597. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  598. PtrToArg<bool>::encode(PtrToArg<Object *>::convert(left) != PtrToArg<Object *>::convert(right), r_ret);
  599. }
  600. static Variant::Type get_return_type() { return Variant::BOOL; }
  601. };
  602. class OperatorEvaluatorNotEqualObjectNil {
  603. public:
  604. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  605. Object *a = p_left.get_validated_object();
  606. *r_ret = a != nullptr;
  607. r_valid = true;
  608. }
  609. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  610. Object *a = left->get_validated_object();
  611. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = a != nullptr;
  612. }
  613. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  614. PtrToArg<bool>::encode(PtrToArg<Object *>::convert(left) != nullptr, r_ret);
  615. }
  616. static Variant::Type get_return_type() { return Variant::BOOL; }
  617. };
  618. class OperatorEvaluatorNotEqualNilObject {
  619. public:
  620. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  621. Object *b = p_right.get_validated_object();
  622. *r_ret = nullptr != b;
  623. r_valid = true;
  624. }
  625. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  626. Object *b = right->get_validated_object();
  627. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = nullptr != b;
  628. }
  629. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  630. PtrToArg<bool>::encode(nullptr != PtrToArg<Object *>::convert(right), r_ret);
  631. }
  632. static Variant::Type get_return_type() { return Variant::BOOL; }
  633. };
  634. template <typename A, typename B>
  635. class OperatorEvaluatorLess {
  636. public:
  637. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  638. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  639. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  640. *r_ret = a < b;
  641. r_valid = true;
  642. }
  643. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  644. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) < *VariantGetInternalPtr<B>::get_ptr(right);
  645. }
  646. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  647. PtrToArg<bool>::encode(PtrToArg<A>::convert(left) < PtrToArg<B>::convert(right), r_ret);
  648. }
  649. static Variant::Type get_return_type() { return Variant::BOOL; }
  650. };
  651. template <typename A, typename B>
  652. class OperatorEvaluatorLessEqual {
  653. public:
  654. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  655. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  656. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  657. *r_ret = a <= b;
  658. r_valid = true;
  659. }
  660. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  661. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) <= *VariantGetInternalPtr<B>::get_ptr(right);
  662. }
  663. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  664. PtrToArg<bool>::encode(PtrToArg<A>::convert(left) <= PtrToArg<B>::convert(right), r_ret);
  665. }
  666. static Variant::Type get_return_type() { return Variant::BOOL; }
  667. };
  668. template <typename A, typename B>
  669. class OperatorEvaluatorGreater {
  670. public:
  671. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  672. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  673. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  674. *r_ret = a > b;
  675. r_valid = true;
  676. }
  677. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  678. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) > *VariantGetInternalPtr<B>::get_ptr(right);
  679. }
  680. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  681. PtrToArg<bool>::encode(PtrToArg<A>::convert(left) > PtrToArg<B>::convert(right), r_ret);
  682. }
  683. static Variant::Type get_return_type() { return Variant::BOOL; }
  684. };
  685. template <typename A, typename B>
  686. class OperatorEvaluatorGreaterEqual {
  687. public:
  688. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  689. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  690. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  691. *r_ret = a >= b;
  692. r_valid = true;
  693. }
  694. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  695. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) >= *VariantGetInternalPtr<B>::get_ptr(right);
  696. }
  697. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  698. PtrToArg<bool>::encode(PtrToArg<A>::convert(left) >= PtrToArg<B>::convert(right), r_ret);
  699. }
  700. static Variant::Type get_return_type() { return Variant::BOOL; }
  701. };
  702. template <typename A, typename B>
  703. class OperatorEvaluatorAnd {
  704. public:
  705. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  706. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  707. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  708. *r_ret = a && b;
  709. r_valid = true;
  710. }
  711. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  712. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) && *VariantGetInternalPtr<B>::get_ptr(right);
  713. }
  714. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  715. PtrToArg<bool>::encode(PtrToArg<A>::convert(left) && PtrToArg<B>::convert(right), r_ret);
  716. }
  717. static Variant::Type get_return_type() { return Variant::BOOL; }
  718. };
  719. template <typename A, typename B>
  720. class OperatorEvaluatorOr {
  721. public:
  722. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  723. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  724. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  725. *r_ret = a || b;
  726. r_valid = true;
  727. }
  728. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  729. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) || *VariantGetInternalPtr<B>::get_ptr(right);
  730. }
  731. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  732. PtrToArg<bool>::encode(PtrToArg<A>::convert(left) || PtrToArg<B>::convert(right), r_ret);
  733. }
  734. static Variant::Type get_return_type() { return Variant::BOOL; }
  735. };
  736. #define XOR_OP(m_a, m_b) (((m_a) || (m_b)) && !((m_a) && (m_b)))
  737. template <typename A, typename B>
  738. class OperatorEvaluatorXor {
  739. public:
  740. _FORCE_INLINE_ static bool xor_op(const A &a, const B &b) {
  741. return ((a) || (b)) && !((a) && (b));
  742. }
  743. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  744. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  745. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  746. *r_ret = xor_op(a, b);
  747. r_valid = true;
  748. }
  749. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  750. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = xor_op(*VariantGetInternalPtr<A>::get_ptr(left), *VariantGetInternalPtr<B>::get_ptr(right));
  751. }
  752. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  753. PtrToArg<bool>::encode(xor_op(PtrToArg<A>::convert(left), PtrToArg<B>::convert(right)), r_ret);
  754. }
  755. static Variant::Type get_return_type() { return Variant::BOOL; }
  756. };
  757. template <typename A>
  758. class OperatorEvaluatorNot {
  759. public:
  760. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  761. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  762. *r_ret = a == A();
  763. r_valid = true;
  764. }
  765. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  766. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) == A();
  767. }
  768. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  769. PtrToArg<bool>::encode(PtrToArg<A>::convert(left) == A(), r_ret);
  770. }
  771. static Variant::Type get_return_type() { return Variant::BOOL; }
  772. };
  773. //// CUSTOM ////
  774. class OperatorEvaluatorAddArray {
  775. public:
  776. _FORCE_INLINE_ static void _add_arrays(Array &sum, const Array &array_a, const Array &array_b) {
  777. int asize = array_a.size();
  778. int bsize = array_b.size();
  779. if (array_a.is_typed() && array_a.is_same_typed(array_b)) {
  780. sum.set_typed(array_a.get_typed_builtin(), array_a.get_typed_class_name(), array_a.get_typed_script());
  781. }
  782. sum.resize(asize + bsize);
  783. for (int i = 0; i < asize; i++) {
  784. sum[i] = array_a[i];
  785. }
  786. for (int i = 0; i < bsize; i++) {
  787. sum[i + asize] = array_b[i];
  788. }
  789. }
  790. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  791. const Array &array_a = *VariantGetInternalPtr<Array>::get_ptr(&p_left);
  792. const Array &array_b = *VariantGetInternalPtr<Array>::get_ptr(&p_right);
  793. Array sum;
  794. _add_arrays(sum, array_a, array_b);
  795. *r_ret = sum;
  796. r_valid = true;
  797. }
  798. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  799. *r_ret = Array();
  800. _add_arrays(*VariantGetInternalPtr<Array>::get_ptr(r_ret), *VariantGetInternalPtr<Array>::get_ptr(left), *VariantGetInternalPtr<Array>::get_ptr(right));
  801. }
  802. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  803. Array ret;
  804. _add_arrays(ret, PtrToArg<Array>::convert(left), PtrToArg<Array>::convert(right));
  805. PtrToArg<Array>::encode(ret, r_ret);
  806. }
  807. static Variant::Type get_return_type() { return Variant::ARRAY; }
  808. };
  809. template <typename T>
  810. class OperatorEvaluatorAppendArray {
  811. public:
  812. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  813. const Vector<T> &array_a = *VariantGetInternalPtr<Vector<T>>::get_ptr(&p_left);
  814. const Vector<T> &array_b = *VariantGetInternalPtr<Vector<T>>::get_ptr(&p_right);
  815. Vector<T> sum = array_a;
  816. sum.append_array(array_b);
  817. *r_ret = sum;
  818. r_valid = true;
  819. }
  820. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  821. *VariantGetInternalPtr<Vector<T>>::get_ptr(r_ret) = *VariantGetInternalPtr<Vector<T>>::get_ptr(left);
  822. VariantGetInternalPtr<Vector<T>>::get_ptr(r_ret)->append_array(*VariantGetInternalPtr<Vector<T>>::get_ptr(right));
  823. }
  824. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  825. Vector<T> sum = PtrToArg<Vector<T>>::convert(left);
  826. sum.append_array(PtrToArg<Vector<T>>::convert(right));
  827. PtrToArg<Vector<T>>::encode(sum, r_ret);
  828. }
  829. static Variant::Type get_return_type() { return GetTypeInfo<Vector<T>>::VARIANT_TYPE; }
  830. };
  831. template <typename Left, typename Right>
  832. class OperatorEvaluatorStringConcat {
  833. public:
  834. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  835. const String a(*VariantGetInternalPtr<Left>::get_ptr(&p_left));
  836. const String b(*VariantGetInternalPtr<Right>::get_ptr(&p_right));
  837. *r_ret = a + b;
  838. r_valid = true;
  839. }
  840. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  841. const String a(*VariantGetInternalPtr<Left>::get_ptr(left));
  842. const String b(*VariantGetInternalPtr<Right>::get_ptr(right));
  843. *VariantGetInternalPtr<String>::get_ptr(r_ret) = a + b;
  844. }
  845. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  846. const String a(PtrToArg<Left>::convert(left));
  847. const String b(PtrToArg<Right>::convert(right));
  848. PtrToArg<String>::encode(a + b, r_ret);
  849. }
  850. static Variant::Type get_return_type() { return Variant::STRING; }
  851. };
  852. template <typename S, typename T>
  853. class OperatorEvaluatorStringFormat;
  854. template <typename S>
  855. class OperatorEvaluatorStringFormat<S, void> {
  856. public:
  857. _FORCE_INLINE_ static String do_mod(const String &s, bool *r_valid) {
  858. Array values;
  859. values.push_back(Variant());
  860. String a = s.sprintf(values, r_valid);
  861. if (r_valid) {
  862. *r_valid = !*r_valid;
  863. }
  864. return a;
  865. }
  866. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  867. *r_ret = do_mod(*VariantGetInternalPtr<S>::get_ptr(&p_left), &r_valid);
  868. }
  869. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  870. bool valid = true;
  871. String result = do_mod(*VariantGetInternalPtr<S>::get_ptr(left), &valid);
  872. ERR_FAIL_COND_MSG(!valid, result);
  873. *VariantGetInternalPtr<String>::get_ptr(r_ret) = result;
  874. }
  875. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  876. PtrToArg<String>::encode(do_mod(PtrToArg<S>::convert(left), nullptr), r_ret);
  877. }
  878. static Variant::Type get_return_type() { return Variant::STRING; }
  879. };
  880. template <typename S>
  881. class OperatorEvaluatorStringFormat<S, Array> {
  882. public:
  883. _FORCE_INLINE_ static String do_mod(const String &s, const Array &p_values, bool *r_valid) {
  884. String a = s.sprintf(p_values, r_valid);
  885. if (r_valid) {
  886. *r_valid = !*r_valid;
  887. }
  888. return a;
  889. }
  890. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  891. *r_ret = do_mod(*VariantGetInternalPtr<S>::get_ptr(&p_left), *VariantGetInternalPtr<Array>::get_ptr(&p_right), &r_valid);
  892. }
  893. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  894. bool valid = true;
  895. String result = do_mod(*VariantGetInternalPtr<S>::get_ptr(left), *VariantGetInternalPtr<Array>::get_ptr(right), &valid);
  896. ERR_FAIL_COND_MSG(!valid, result);
  897. *VariantGetInternalPtr<String>::get_ptr(r_ret) = result;
  898. }
  899. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  900. PtrToArg<String>::encode(do_mod(PtrToArg<S>::convert(left), PtrToArg<Array>::convert(right), nullptr), r_ret);
  901. }
  902. static Variant::Type get_return_type() { return Variant::STRING; }
  903. };
  904. template <typename S>
  905. class OperatorEvaluatorStringFormat<S, Object> {
  906. public:
  907. _FORCE_INLINE_ static String do_mod(const String &s, const Object *p_object, bool *r_valid) {
  908. Array values;
  909. values.push_back(p_object);
  910. String a = s.sprintf(values, r_valid);
  911. if (r_valid) {
  912. *r_valid = !*r_valid;
  913. }
  914. return a;
  915. }
  916. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  917. *r_ret = do_mod(*VariantGetInternalPtr<S>::get_ptr(&p_left), p_right.get_validated_object(), &r_valid);
  918. }
  919. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  920. bool valid = true;
  921. String result = do_mod(*VariantGetInternalPtr<S>::get_ptr(left), right->get_validated_object(), &valid);
  922. ERR_FAIL_COND_MSG(!valid, result);
  923. *VariantGetInternalPtr<String>::get_ptr(r_ret) = result;
  924. }
  925. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  926. PtrToArg<String>::encode(do_mod(PtrToArg<S>::convert(left), PtrToArg<Object *>::convert(right), nullptr), r_ret);
  927. }
  928. static Variant::Type get_return_type() { return Variant::STRING; }
  929. };
  930. template <typename S, typename T>
  931. class OperatorEvaluatorStringFormat {
  932. public:
  933. _FORCE_INLINE_ static String do_mod(const String &s, const T &p_value, bool *r_valid) {
  934. Array values;
  935. values.push_back(p_value);
  936. String a = s.sprintf(values, r_valid);
  937. if (r_valid) {
  938. *r_valid = !*r_valid;
  939. }
  940. return a;
  941. }
  942. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  943. *r_ret = do_mod(*VariantGetInternalPtr<S>::get_ptr(&p_left), *VariantGetInternalPtr<T>::get_ptr(&p_right), &r_valid);
  944. }
  945. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  946. bool valid = true;
  947. String result = do_mod(*VariantGetInternalPtr<S>::get_ptr(left), *VariantGetInternalPtr<T>::get_ptr(right), &valid);
  948. ERR_FAIL_COND_MSG(!valid, result);
  949. *VariantGetInternalPtr<String>::get_ptr(r_ret) = result;
  950. }
  951. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  952. PtrToArg<String>::encode(do_mod(PtrToArg<S>::convert(left), PtrToArg<T>::convert(right), nullptr), r_ret);
  953. }
  954. static Variant::Type get_return_type() { return Variant::STRING; }
  955. };
  956. template <Variant::Operator op, Variant::Type type_left, Variant::Type type_right>
  957. class OperatorEvaluatorAlwaysTrue {
  958. public:
  959. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  960. *r_ret = true;
  961. r_valid = true;
  962. }
  963. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  964. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = true;
  965. }
  966. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  967. PtrToArg<bool>::encode(true, r_ret);
  968. }
  969. static Variant::Type get_return_type() { return Variant::BOOL; }
  970. };
  971. template <Variant::Operator op, Variant::Type type_left, Variant::Type type_right>
  972. class OperatorEvaluatorAlwaysFalse {
  973. public:
  974. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  975. *r_ret = false;
  976. r_valid = true;
  977. }
  978. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  979. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = false;
  980. }
  981. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  982. PtrToArg<bool>::encode(false, r_ret);
  983. }
  984. static Variant::Type get_return_type() { return Variant::BOOL; }
  985. };
  986. ///// OR ///////
  987. _FORCE_INLINE_ static bool _operate_or(bool p_left, bool p_right) {
  988. return p_left || p_right;
  989. }
  990. _FORCE_INLINE_ static bool _operate_and(bool p_left, bool p_right) {
  991. return p_left && p_right;
  992. }
  993. _FORCE_INLINE_ static bool _operate_xor(bool p_left, bool p_right) {
  994. return (p_left || p_right) && !(p_left && p_right);
  995. }
  996. _FORCE_INLINE_ static bool _operate_get_nil(const Variant *p_ptr) {
  997. return p_ptr->get_validated_object() != nullptr;
  998. }
  999. _FORCE_INLINE_ static bool _operate_get_bool(const Variant *p_ptr) {
  1000. return *VariantGetInternalPtr<bool>::get_ptr(p_ptr);
  1001. }
  1002. _FORCE_INLINE_ static bool _operate_get_int(const Variant *p_ptr) {
  1003. return *VariantGetInternalPtr<int64_t>::get_ptr(p_ptr) != 0;
  1004. }
  1005. _FORCE_INLINE_ static bool _operate_get_float(const Variant *p_ptr) {
  1006. return *VariantGetInternalPtr<double>::get_ptr(p_ptr) != 0.0;
  1007. }
  1008. _FORCE_INLINE_ static bool _operate_get_object(const Variant *p_ptr) {
  1009. return p_ptr->get_validated_object() != nullptr;
  1010. }
  1011. _FORCE_INLINE_ static bool _operate_get_ptr_nil(const void *p_ptr) {
  1012. return false;
  1013. }
  1014. _FORCE_INLINE_ static bool _operate_get_ptr_bool(const void *p_ptr) {
  1015. return PtrToArg<bool>::convert(p_ptr);
  1016. }
  1017. _FORCE_INLINE_ static bool _operate_get_ptr_int(const void *p_ptr) {
  1018. return PtrToArg<int64_t>::convert(p_ptr) != 0;
  1019. }
  1020. _FORCE_INLINE_ static bool _operate_get_ptr_float(const void *p_ptr) {
  1021. return PtrToArg<double>::convert(p_ptr) != 0.0;
  1022. }
  1023. _FORCE_INLINE_ static bool _operate_get_ptr_object(const void *p_ptr) {
  1024. return PtrToArg<Object *>::convert(p_ptr) != nullptr;
  1025. }
  1026. #define OP_EVALUATOR(m_class_name, m_left, m_right, m_op) \
  1027. class m_class_name { \
  1028. public: \
  1029. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) { \
  1030. *r_ret = m_op(_operate_get_##m_left(&p_left), _operate_get_##m_right(&p_right)); \
  1031. r_valid = true; \
  1032. } \
  1033. \
  1034. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { \
  1035. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = m_op(_operate_get_##m_left(left), _operate_get_##m_right(right)); \
  1036. } \
  1037. \
  1038. static void ptr_evaluate(const void *left, const void *right, void *r_ret) { \
  1039. PtrToArg<bool>::encode(m_op(_operate_get_ptr_##m_left(left), _operate_get_ptr_##m_right(right)), r_ret); \
  1040. } \
  1041. \
  1042. static Variant::Type get_return_type() { \
  1043. return Variant::BOOL; \
  1044. } \
  1045. };
  1046. // OR
  1047. // nil
  1048. OP_EVALUATOR(OperatorEvaluatorNilXBoolOr, nil, bool, _operate_or)
  1049. OP_EVALUATOR(OperatorEvaluatorBoolXNilOr, bool, nil, _operate_or)
  1050. OP_EVALUATOR(OperatorEvaluatorNilXIntOr, nil, int, _operate_or)
  1051. OP_EVALUATOR(OperatorEvaluatorIntXNilOr, int, nil, _operate_or)
  1052. OP_EVALUATOR(OperatorEvaluatorNilXFloatOr, nil, float, _operate_or)
  1053. OP_EVALUATOR(OperatorEvaluatorFloatXNilOr, float, nil, _operate_or)
  1054. OP_EVALUATOR(OperatorEvaluatorObjectXNilOr, object, nil, _operate_or)
  1055. OP_EVALUATOR(OperatorEvaluatorNilXObjectOr, nil, object, _operate_or)
  1056. // bool
  1057. OP_EVALUATOR(OperatorEvaluatorBoolXBoolOr, bool, bool, _operate_or)
  1058. OP_EVALUATOR(OperatorEvaluatorBoolXIntOr, bool, int, _operate_or)
  1059. OP_EVALUATOR(OperatorEvaluatorIntXBoolOr, int, bool, _operate_or)
  1060. OP_EVALUATOR(OperatorEvaluatorBoolXFloatOr, bool, float, _operate_or)
  1061. OP_EVALUATOR(OperatorEvaluatorFloatXBoolOr, float, bool, _operate_or)
  1062. OP_EVALUATOR(OperatorEvaluatorBoolXObjectOr, bool, object, _operate_or)
  1063. OP_EVALUATOR(OperatorEvaluatorObjectXBoolOr, object, bool, _operate_or)
  1064. // int
  1065. OP_EVALUATOR(OperatorEvaluatorIntXIntOr, int, int, _operate_or)
  1066. OP_EVALUATOR(OperatorEvaluatorIntXFloatOr, int, float, _operate_or)
  1067. OP_EVALUATOR(OperatorEvaluatorFloatXIntOr, float, int, _operate_or)
  1068. OP_EVALUATOR(OperatorEvaluatorIntXObjectOr, int, object, _operate_or)
  1069. OP_EVALUATOR(OperatorEvaluatorObjectXIntOr, object, int, _operate_or)
  1070. // float
  1071. OP_EVALUATOR(OperatorEvaluatorFloatXFloatOr, float, float, _operate_or)
  1072. OP_EVALUATOR(OperatorEvaluatorFloatXObjectOr, float, object, _operate_or)
  1073. OP_EVALUATOR(OperatorEvaluatorObjectXFloatOr, object, float, _operate_or)
  1074. // object
  1075. OP_EVALUATOR(OperatorEvaluatorObjectXObjectOr, object, object, _operate_or)
  1076. // AND
  1077. // nil
  1078. OP_EVALUATOR(OperatorEvaluatorNilXBoolAnd, nil, bool, _operate_and)
  1079. OP_EVALUATOR(OperatorEvaluatorBoolXNilAnd, bool, nil, _operate_and)
  1080. OP_EVALUATOR(OperatorEvaluatorNilXIntAnd, nil, int, _operate_and)
  1081. OP_EVALUATOR(OperatorEvaluatorIntXNilAnd, int, nil, _operate_and)
  1082. OP_EVALUATOR(OperatorEvaluatorNilXFloatAnd, nil, float, _operate_and)
  1083. OP_EVALUATOR(OperatorEvaluatorFloatXNilAnd, float, nil, _operate_and)
  1084. OP_EVALUATOR(OperatorEvaluatorObjectXNilAnd, object, nil, _operate_and)
  1085. OP_EVALUATOR(OperatorEvaluatorNilXObjectAnd, nil, object, _operate_and)
  1086. // bool
  1087. OP_EVALUATOR(OperatorEvaluatorBoolXBoolAnd, bool, bool, _operate_and)
  1088. OP_EVALUATOR(OperatorEvaluatorBoolXIntAnd, bool, int, _operate_and)
  1089. OP_EVALUATOR(OperatorEvaluatorIntXBoolAnd, int, bool, _operate_and)
  1090. OP_EVALUATOR(OperatorEvaluatorBoolXFloatAnd, bool, float, _operate_and)
  1091. OP_EVALUATOR(OperatorEvaluatorFloatXBoolAnd, float, bool, _operate_and)
  1092. OP_EVALUATOR(OperatorEvaluatorBoolXObjectAnd, bool, object, _operate_and)
  1093. OP_EVALUATOR(OperatorEvaluatorObjectXBoolAnd, object, bool, _operate_and)
  1094. // int
  1095. OP_EVALUATOR(OperatorEvaluatorIntXIntAnd, int, int, _operate_and)
  1096. OP_EVALUATOR(OperatorEvaluatorIntXFloatAnd, int, float, _operate_and)
  1097. OP_EVALUATOR(OperatorEvaluatorFloatXIntAnd, float, int, _operate_and)
  1098. OP_EVALUATOR(OperatorEvaluatorIntXObjectAnd, int, object, _operate_and)
  1099. OP_EVALUATOR(OperatorEvaluatorObjectXIntAnd, object, int, _operate_and)
  1100. // float
  1101. OP_EVALUATOR(OperatorEvaluatorFloatXFloatAnd, float, float, _operate_and)
  1102. OP_EVALUATOR(OperatorEvaluatorFloatXObjectAnd, float, object, _operate_and)
  1103. OP_EVALUATOR(OperatorEvaluatorObjectXFloatAnd, object, float, _operate_and)
  1104. // object
  1105. OP_EVALUATOR(OperatorEvaluatorObjectXObjectAnd, object, object, _operate_and)
  1106. // XOR
  1107. // nil
  1108. OP_EVALUATOR(OperatorEvaluatorNilXBoolXor, nil, bool, _operate_xor)
  1109. OP_EVALUATOR(OperatorEvaluatorBoolXNilXor, bool, nil, _operate_xor)
  1110. OP_EVALUATOR(OperatorEvaluatorNilXIntXor, nil, int, _operate_xor)
  1111. OP_EVALUATOR(OperatorEvaluatorIntXNilXor, int, nil, _operate_xor)
  1112. OP_EVALUATOR(OperatorEvaluatorNilXFloatXor, nil, float, _operate_xor)
  1113. OP_EVALUATOR(OperatorEvaluatorFloatXNilXor, float, nil, _operate_xor)
  1114. OP_EVALUATOR(OperatorEvaluatorObjectXNilXor, object, nil, _operate_xor)
  1115. OP_EVALUATOR(OperatorEvaluatorNilXObjectXor, nil, object, _operate_xor)
  1116. // bool
  1117. OP_EVALUATOR(OperatorEvaluatorBoolXBoolXor, bool, bool, _operate_xor)
  1118. OP_EVALUATOR(OperatorEvaluatorBoolXIntXor, bool, int, _operate_xor)
  1119. OP_EVALUATOR(OperatorEvaluatorIntXBoolXor, int, bool, _operate_xor)
  1120. OP_EVALUATOR(OperatorEvaluatorBoolXFloatXor, bool, float, _operate_xor)
  1121. OP_EVALUATOR(OperatorEvaluatorFloatXBoolXor, float, bool, _operate_xor)
  1122. OP_EVALUATOR(OperatorEvaluatorBoolXObjectXor, bool, object, _operate_xor)
  1123. OP_EVALUATOR(OperatorEvaluatorObjectXBoolXor, object, bool, _operate_xor)
  1124. // int
  1125. OP_EVALUATOR(OperatorEvaluatorIntXIntXor, int, int, _operate_xor)
  1126. OP_EVALUATOR(OperatorEvaluatorIntXFloatXor, int, float, _operate_xor)
  1127. OP_EVALUATOR(OperatorEvaluatorFloatXIntXor, float, int, _operate_xor)
  1128. OP_EVALUATOR(OperatorEvaluatorIntXObjectXor, int, object, _operate_xor)
  1129. OP_EVALUATOR(OperatorEvaluatorObjectXIntXor, object, int, _operate_xor)
  1130. // float
  1131. OP_EVALUATOR(OperatorEvaluatorFloatXFloatXor, float, float, _operate_xor)
  1132. OP_EVALUATOR(OperatorEvaluatorFloatXObjectXor, float, object, _operate_xor)
  1133. OP_EVALUATOR(OperatorEvaluatorObjectXFloatXor, object, float, _operate_xor)
  1134. // object
  1135. OP_EVALUATOR(OperatorEvaluatorObjectXObjectXor, object, object, _operate_xor)
  1136. class OperatorEvaluatorNotBool {
  1137. public:
  1138. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  1139. *r_ret = !*VariantGetInternalPtr<bool>::get_ptr(&p_left);
  1140. r_valid = true;
  1141. }
  1142. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  1143. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = !*VariantGetInternalPtr<bool>::get_ptr(left);
  1144. }
  1145. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  1146. PtrToArg<bool>::encode(!PtrToArg<bool>::convert(left), r_ret);
  1147. }
  1148. static Variant::Type get_return_type() { return Variant::BOOL; }
  1149. };
  1150. class OperatorEvaluatorNotInt {
  1151. public:
  1152. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  1153. *r_ret = !*VariantGetInternalPtr<int64_t>::get_ptr(&p_left);
  1154. r_valid = true;
  1155. }
  1156. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  1157. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = !*VariantGetInternalPtr<int64_t>::get_ptr(left);
  1158. }
  1159. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  1160. PtrToArg<bool>::encode(!PtrToArg<int64_t>::convert(left), r_ret);
  1161. }
  1162. static Variant::Type get_return_type() { return Variant::BOOL; }
  1163. };
  1164. class OperatorEvaluatorNotFloat {
  1165. public:
  1166. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  1167. *r_ret = !*VariantGetInternalPtr<double>::get_ptr(&p_left);
  1168. r_valid = true;
  1169. }
  1170. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  1171. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = !*VariantGetInternalPtr<double>::get_ptr(left);
  1172. }
  1173. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  1174. PtrToArg<bool>::encode(!PtrToArg<double>::convert(left), r_ret);
  1175. }
  1176. static Variant::Type get_return_type() { return Variant::BOOL; }
  1177. };
  1178. class OperatorEvaluatorNotObject {
  1179. public:
  1180. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  1181. *r_ret = p_left.get_validated_object() == nullptr;
  1182. r_valid = true;
  1183. }
  1184. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  1185. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = left->get_validated_object() == nullptr;
  1186. }
  1187. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  1188. PtrToArg<bool>::encode(PtrToArg<Object *>::convert(left) == nullptr, r_ret);
  1189. }
  1190. static Variant::Type get_return_type() { return Variant::BOOL; }
  1191. };
  1192. ////
  1193. template <typename Left, typename Right>
  1194. class OperatorEvaluatorInStringFind;
  1195. template <typename Left>
  1196. class OperatorEvaluatorInStringFind<Left, String> {
  1197. public:
  1198. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  1199. const Left &str_a = *VariantGetInternalPtr<Left>::get_ptr(&p_left);
  1200. const String &str_b = *VariantGetInternalPtr<String>::get_ptr(&p_right);
  1201. *r_ret = str_b.find(str_a) != -1;
  1202. r_valid = true;
  1203. }
  1204. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  1205. const Left &str_a = *VariantGetInternalPtr<Left>::get_ptr(left);
  1206. const String &str_b = *VariantGetInternalPtr<String>::get_ptr(right);
  1207. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = str_b.find(str_a) != -1;
  1208. }
  1209. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  1210. PtrToArg<bool>::encode(PtrToArg<String>::convert(right).find(PtrToArg<Left>::convert(left)) != -1, r_ret);
  1211. }
  1212. static Variant::Type get_return_type() { return Variant::BOOL; }
  1213. };
  1214. template <typename Left>
  1215. class OperatorEvaluatorInStringFind<Left, StringName> {
  1216. public:
  1217. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  1218. const Left &str_a = *VariantGetInternalPtr<Left>::get_ptr(&p_left);
  1219. const String str_b = VariantGetInternalPtr<StringName>::get_ptr(&p_right)->operator String();
  1220. *r_ret = str_b.find(str_a) != -1;
  1221. r_valid = true;
  1222. }
  1223. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  1224. const Left &str_a = *VariantGetInternalPtr<Left>::get_ptr(left);
  1225. const String str_b = VariantGetInternalPtr<StringName>::get_ptr(right)->operator String();
  1226. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = str_b.find(str_a) != -1;
  1227. }
  1228. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  1229. PtrToArg<bool>::encode(PtrToArg<StringName>::convert(right).operator String().find(PtrToArg<Left>::convert(left)) != -1, r_ret);
  1230. }
  1231. static Variant::Type get_return_type() { return Variant::BOOL; }
  1232. };
  1233. template <typename A, typename B>
  1234. class OperatorEvaluatorInArrayFind {
  1235. public:
  1236. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  1237. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  1238. const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
  1239. *r_ret = b.find(a) != -1;
  1240. r_valid = true;
  1241. }
  1242. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  1243. const A &a = *VariantGetInternalPtr<A>::get_ptr(left);
  1244. const B &b = *VariantGetInternalPtr<B>::get_ptr(right);
  1245. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = b.find(a) != -1;
  1246. }
  1247. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  1248. PtrToArg<bool>::encode(PtrToArg<B>::convert(right).find(PtrToArg<A>::convert(left)) != -1, r_ret);
  1249. }
  1250. static Variant::Type get_return_type() { return Variant::BOOL; }
  1251. };
  1252. class OperatorEvaluatorInArrayFindNil {
  1253. public:
  1254. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  1255. const Array &b = *VariantGetInternalPtr<Array>::get_ptr(&p_right);
  1256. *r_ret = b.find(Variant()) != -1;
  1257. r_valid = true;
  1258. }
  1259. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  1260. const Array &b = *VariantGetInternalPtr<Array>::get_ptr(right);
  1261. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = b.find(Variant()) != -1;
  1262. }
  1263. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  1264. PtrToArg<bool>::encode(PtrToArg<Array>::convert(right).find(Variant()) != -1, r_ret);
  1265. }
  1266. static Variant::Type get_return_type() { return Variant::BOOL; }
  1267. };
  1268. class OperatorEvaluatorInArrayFindObject {
  1269. public:
  1270. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  1271. const Array &b = *VariantGetInternalPtr<Array>::get_ptr(&p_right);
  1272. *r_ret = b.find(p_left) != -1;
  1273. r_valid = true;
  1274. }
  1275. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  1276. const Array &b = *VariantGetInternalPtr<Array>::get_ptr(right);
  1277. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = b.find(*left) != -1;
  1278. }
  1279. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  1280. PtrToArg<bool>::encode(PtrToArg<Array>::convert(right).find(PtrToArg<Object *>::convert(left)) != -1, r_ret);
  1281. }
  1282. static Variant::Type get_return_type() { return Variant::BOOL; }
  1283. };
  1284. template <typename A>
  1285. class OperatorEvaluatorInDictionaryHas {
  1286. public:
  1287. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  1288. const Dictionary &b = *VariantGetInternalPtr<Dictionary>::get_ptr(&p_right);
  1289. const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
  1290. *r_ret = b.has(a);
  1291. r_valid = true;
  1292. }
  1293. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  1294. const Dictionary &b = *VariantGetInternalPtr<Dictionary>::get_ptr(right);
  1295. const A &a = *VariantGetInternalPtr<A>::get_ptr(left);
  1296. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = b.has(a);
  1297. }
  1298. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  1299. PtrToArg<bool>::encode(PtrToArg<Dictionary>::convert(right).has(PtrToArg<A>::convert(left)), r_ret);
  1300. }
  1301. static Variant::Type get_return_type() { return Variant::BOOL; }
  1302. };
  1303. class OperatorEvaluatorInDictionaryHasNil {
  1304. public:
  1305. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  1306. const Dictionary &b = *VariantGetInternalPtr<Dictionary>::get_ptr(&p_right);
  1307. *r_ret = b.has(Variant());
  1308. r_valid = true;
  1309. }
  1310. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  1311. const Dictionary &b = *VariantGetInternalPtr<Dictionary>::get_ptr(right);
  1312. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = b.has(Variant());
  1313. }
  1314. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  1315. PtrToArg<bool>::encode(PtrToArg<Dictionary>::convert(right).has(Variant()), r_ret);
  1316. }
  1317. static Variant::Type get_return_type() { return Variant::BOOL; }
  1318. };
  1319. class OperatorEvaluatorInDictionaryHasObject {
  1320. public:
  1321. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  1322. const Dictionary &b = *VariantGetInternalPtr<Dictionary>::get_ptr(&p_right);
  1323. *r_ret = b.has(p_left);
  1324. r_valid = true;
  1325. }
  1326. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  1327. const Dictionary &b = *VariantGetInternalPtr<Dictionary>::get_ptr(right);
  1328. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = b.has(*left);
  1329. }
  1330. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  1331. PtrToArg<bool>::encode(PtrToArg<Dictionary>::convert(right).has(PtrToArg<Object *>::convert(left)), r_ret);
  1332. }
  1333. static Variant::Type get_return_type() { return Variant::BOOL; }
  1334. };
  1335. class OperatorEvaluatorObjectHasPropertyString {
  1336. public:
  1337. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  1338. Object *b = p_right.get_validated_object();
  1339. if (!b) {
  1340. *r_ret = "Invalid base object for 'in'";
  1341. r_valid = false;
  1342. return;
  1343. }
  1344. const String &a = *VariantGetInternalPtr<String>::get_ptr(&p_left);
  1345. bool exist;
  1346. b->get(a, &exist);
  1347. *r_ret = exist;
  1348. r_valid = true;
  1349. }
  1350. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  1351. Object *l = right->get_validated_object();
  1352. ERR_FAIL_NULL(l);
  1353. const String &a = *VariantGetInternalPtr<String>::get_ptr(left);
  1354. bool valid;
  1355. l->get(a, &valid);
  1356. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = valid;
  1357. }
  1358. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  1359. bool valid;
  1360. PtrToArg<Object *>::convert(right)->get(PtrToArg<String>::convert(left), &valid);
  1361. PtrToArg<bool>::encode(valid, r_ret);
  1362. }
  1363. static Variant::Type get_return_type() { return Variant::BOOL; }
  1364. };
  1365. class OperatorEvaluatorObjectHasPropertyStringName {
  1366. public:
  1367. static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
  1368. Object *b = p_right.get_validated_object();
  1369. if (!b) {
  1370. *r_ret = "Invalid base object for 'in'";
  1371. r_valid = false;
  1372. return;
  1373. }
  1374. const StringName &a = *VariantGetInternalPtr<StringName>::get_ptr(&p_left);
  1375. bool exist;
  1376. b->get(a, &exist);
  1377. *r_ret = exist;
  1378. r_valid = true;
  1379. }
  1380. static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
  1381. Object *l = right->get_validated_object();
  1382. ERR_FAIL_NULL(l);
  1383. const StringName &a = *VariantGetInternalPtr<StringName>::get_ptr(left);
  1384. bool valid;
  1385. l->get(a, &valid);
  1386. *VariantGetInternalPtr<bool>::get_ptr(r_ret) = valid;
  1387. }
  1388. static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
  1389. bool valid;
  1390. PtrToArg<Object *>::convert(right)->get(PtrToArg<StringName>::convert(left), &valid);
  1391. PtrToArg<bool>::encode(valid, r_ret);
  1392. }
  1393. static Variant::Type get_return_type() { return Variant::BOOL; }
  1394. };
  1395. #endif // VARIANT_OP_H