array.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*************************************************************************/
  2. /* array.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  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 <godot/array.h>
  31. #include "core/array.h"
  32. #include "core/os/memory.h"
  33. #include "core/color.h"
  34. #include "core/dvector.h"
  35. #include "core/variant.h"
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. void _array_api_anchor() {
  40. }
  41. void GDAPI godot_array_new(godot_array *r_dest) {
  42. Array *dest = (Array *)r_dest;
  43. memnew_placement(dest, Array);
  44. }
  45. void GDAPI godot_array_new_copy(godot_array *r_dest, const godot_array *p_src) {
  46. Array *dest = (Array *)r_dest;
  47. const Array *src = (const Array *)p_src;
  48. memnew_placement(dest, Array(*src));
  49. }
  50. void GDAPI godot_array_new_pool_color_array(godot_array *r_dest, const godot_pool_color_array *p_pca) {
  51. Array *dest = (Array *)r_dest;
  52. PoolVector<Color> *pca = (PoolVector<Color> *)p_pca;
  53. memnew_placement(dest, Array);
  54. dest->resize(pca->size());
  55. for (size_t i = 0; i < dest->size(); i++) {
  56. Variant v = pca->operator[](i);
  57. dest->operator[](i) = v;
  58. }
  59. }
  60. void GDAPI godot_array_new_pool_vector3_array(godot_array *r_dest, const godot_pool_vector3_array *p_pv3a) {
  61. Array *dest = (Array *)r_dest;
  62. PoolVector<Vector3> *pca = (PoolVector<Vector3> *)p_pv3a;
  63. memnew_placement(dest, Array);
  64. dest->resize(pca->size());
  65. for (size_t i = 0; i < dest->size(); i++) {
  66. Variant v = pca->operator[](i);
  67. dest->operator[](i) = v;
  68. }
  69. }
  70. void GDAPI godot_array_new_pool_vector2_array(godot_array *r_dest, const godot_pool_vector2_array *p_pv2a) {
  71. Array *dest = (Array *)r_dest;
  72. PoolVector<Vector2> *pca = (PoolVector<Vector2> *)p_pv2a;
  73. memnew_placement(dest, Array);
  74. dest->resize(pca->size());
  75. for (size_t i = 0; i < dest->size(); i++) {
  76. Variant v = pca->operator[](i);
  77. dest->operator[](i) = v;
  78. }
  79. }
  80. void GDAPI godot_array_new_pool_string_array(godot_array *r_dest, const godot_pool_string_array *p_psa) {
  81. Array *dest = (Array *)r_dest;
  82. PoolVector<String> *pca = (PoolVector<String> *)p_psa;
  83. memnew_placement(dest, Array);
  84. dest->resize(pca->size());
  85. for (size_t i = 0; i < dest->size(); i++) {
  86. Variant v = pca->operator[](i);
  87. dest->operator[](i) = v;
  88. }
  89. }
  90. void GDAPI godot_array_new_pool_real_array(godot_array *r_dest, const godot_pool_real_array *p_pra) {
  91. Array *dest = (Array *)r_dest;
  92. PoolVector<godot_real> *pca = (PoolVector<godot_real> *)p_pra;
  93. memnew_placement(dest, Array);
  94. dest->resize(pca->size());
  95. for (size_t i = 0; i < dest->size(); i++) {
  96. Variant v = pca->operator[](i);
  97. dest->operator[](i) = v;
  98. }
  99. }
  100. void GDAPI godot_array_new_pool_int_array(godot_array *r_dest, const godot_pool_int_array *p_pia) {
  101. Array *dest = (Array *)r_dest;
  102. PoolVector<godot_int> *pca = (PoolVector<godot_int> *)p_pia;
  103. memnew_placement(dest, Array);
  104. dest->resize(pca->size());
  105. for (size_t i = 0; i < dest->size(); i++) {
  106. Variant v = pca->operator[](i);
  107. dest->operator[](i) = v;
  108. }
  109. }
  110. void GDAPI godot_array_new_pool_byte_array(godot_array *r_dest, const godot_pool_byte_array *p_pba) {
  111. Array *dest = (Array *)r_dest;
  112. PoolVector<uint8_t> *pca = (PoolVector<uint8_t> *)p_pba;
  113. memnew_placement(dest, Array);
  114. dest->resize(pca->size());
  115. for (size_t i = 0; i < dest->size(); i++) {
  116. Variant v = pca->operator[](i);
  117. dest->operator[](i) = v;
  118. }
  119. }
  120. void GDAPI godot_array_set(godot_array *p_self, const godot_int p_idx, const godot_variant *p_value) {
  121. Array *self = (Array *)p_self;
  122. Variant *val = (Variant *)p_value;
  123. self->operator[](p_idx) = *val;
  124. }
  125. godot_variant GDAPI godot_array_get(const godot_array *p_self, const godot_int p_idx) {
  126. godot_variant raw_dest;
  127. Variant *dest = (Variant *)&raw_dest;
  128. const Array *self = (const Array *)p_self;
  129. memnew_placement(dest, Variant(self->operator[](p_idx)));
  130. return raw_dest;
  131. }
  132. godot_variant GDAPI *godot_array_operator_index(godot_array *p_self, const godot_int p_idx) {
  133. Array *self = (Array *)p_self;
  134. return (godot_variant *)&self->operator[](p_idx);
  135. }
  136. void GDAPI godot_array_append(godot_array *p_self, const godot_variant *p_value) {
  137. Array *self = (Array *)p_self;
  138. Variant *val = (Variant *)p_value;
  139. self->append(*val);
  140. }
  141. void GDAPI godot_array_clear(godot_array *p_self) {
  142. Array *self = (Array *)p_self;
  143. self->clear();
  144. }
  145. godot_int GDAPI godot_array_count(const godot_array *p_self, const godot_variant *p_value) {
  146. const Array *self = (const Array *)p_self;
  147. const Variant *val = (const Variant *)p_value;
  148. return self->count(*val);
  149. }
  150. godot_bool GDAPI godot_array_empty(const godot_array *p_self) {
  151. const Array *self = (const Array *)p_self;
  152. return self->empty();
  153. }
  154. void GDAPI godot_array_erase(godot_array *p_self, const godot_variant *p_value) {
  155. Array *self = (Array *)p_self;
  156. const Variant *val = (const Variant *)p_value;
  157. self->erase(*val);
  158. }
  159. godot_variant GDAPI godot_array_front(const godot_array *p_self) {
  160. const Array *self = (const Array *)p_self;
  161. godot_variant v;
  162. Variant *val = (Variant *)&v;
  163. memnew_placement(val, Variant);
  164. *val = self->front();
  165. return v;
  166. }
  167. godot_variant GDAPI godot_array_back(const godot_array *p_self) {
  168. const Array *self = (const Array *)p_self;
  169. godot_variant v;
  170. Variant *val = (Variant *)&v;
  171. memnew_placement(val, Variant);
  172. *val = self->back();
  173. return v;
  174. }
  175. godot_int GDAPI godot_array_find(const godot_array *p_self, const godot_variant *p_what, const godot_int p_from) {
  176. const Array *self = (const Array *)p_self;
  177. const Variant *val = (const Variant *)p_what;
  178. return self->find(*val, p_from);
  179. }
  180. godot_int GDAPI godot_array_find_last(const godot_array *p_self, const godot_variant *p_what) {
  181. const Array *self = (const Array *)p_self;
  182. const Variant *val = (const Variant *)p_what;
  183. return self->find_last(*val);
  184. }
  185. godot_bool GDAPI godot_array_has(const godot_array *p_self, const godot_variant *p_value) {
  186. const Array *self = (const Array *)p_self;
  187. const Variant *val = (const Variant *)p_value;
  188. return self->has(*val);
  189. }
  190. godot_int GDAPI godot_array_hash(const godot_array *p_self) {
  191. const Array *self = (const Array *)p_self;
  192. return self->hash();
  193. }
  194. void GDAPI godot_array_insert(godot_array *p_self, const godot_int p_pos, const godot_variant *p_value) {
  195. Array *self = (Array *)p_self;
  196. const Variant *val = (const Variant *)p_value;
  197. self->insert(p_pos, *val);
  198. }
  199. void GDAPI godot_array_invert(godot_array *p_self) {
  200. Array *self = (Array *)p_self;
  201. self->invert();
  202. }
  203. godot_variant GDAPI godot_array_pop_back(godot_array *p_self) {
  204. Array *self = (Array *)p_self;
  205. godot_variant v;
  206. Variant *val = (Variant *)&v;
  207. memnew_placement(val, Variant);
  208. *val = self->pop_back();
  209. return v;
  210. }
  211. godot_variant GDAPI godot_array_pop_front(godot_array *p_self) {
  212. Array *self = (Array *)p_self;
  213. godot_variant v;
  214. Variant *val = (Variant *)&v;
  215. memnew_placement(val, Variant);
  216. *val = self->pop_front();
  217. return v;
  218. }
  219. void GDAPI godot_array_push_back(godot_array *p_self, const godot_variant *p_value) {
  220. Array *self = (Array *)p_self;
  221. const Variant *val = (const Variant *)p_value;
  222. self->push_back(*val);
  223. }
  224. void GDAPI godot_array_push_front(godot_array *p_self, const godot_variant *p_value) {
  225. Array *self = (Array *)p_self;
  226. const Variant *val = (const Variant *)p_value;
  227. self->push_front(*val);
  228. }
  229. void GDAPI godot_array_remove(godot_array *p_self, const godot_int p_idx) {
  230. Array *self = (Array *)p_self;
  231. self->remove(p_idx);
  232. }
  233. void GDAPI godot_array_resize(godot_array *p_self, const godot_int p_size) {
  234. Array *self = (Array *)p_self;
  235. self->resize(p_size);
  236. }
  237. godot_int GDAPI godot_array_rfind(const godot_array *p_self, const godot_variant *p_what, const godot_int p_from) {
  238. const Array *self = (const Array *)p_self;
  239. const Variant *val = (const Variant *)p_what;
  240. return self->rfind(*val, p_from);
  241. }
  242. godot_int GDAPI godot_array_size(const godot_array *p_self) {
  243. const Array *self = (const Array *)p_self;
  244. return self->size();
  245. }
  246. void GDAPI godot_array_sort(godot_array *p_self) {
  247. Array *self = (Array *)p_self;
  248. self->sort();
  249. }
  250. void GDAPI godot_array_sort_custom(godot_array *p_self, godot_object *p_obj, const godot_string *p_func) {
  251. Array *self = (Array *)p_self;
  252. const String *func = (const String *)p_func;
  253. self->sort_custom((Object *)p_obj, *func);
  254. }
  255. void GDAPI godot_array_destroy(godot_array *p_self) {
  256. ((Array *)p_self)->~Array();
  257. }
  258. #ifdef __cplusplus
  259. }
  260. #endif