Array.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*************************************************************************/
  2. /* Array.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #ifndef ARRAY_H
  31. #define ARRAY_H
  32. #include <gdnative/array.h>
  33. #include "String.hpp"
  34. namespace godot {
  35. namespace helpers {
  36. template <typename T, typename ValueT>
  37. T append_all(T appendable, ValueT value) {
  38. appendable.append(value);
  39. return appendable;
  40. }
  41. template <typename T, typename ValueT, typename... Args>
  42. T append_all(T appendable, ValueT value, Args... args) {
  43. appendable.append(value);
  44. return append_all(appendable, args...);
  45. }
  46. template <typename T>
  47. T append_all(T appendable) {
  48. return appendable;
  49. }
  50. template <typename KV, typename KeyT, typename ValueT>
  51. KV add_all(KV kv, KeyT key, ValueT value) {
  52. kv[key] = value;
  53. return kv;
  54. }
  55. template <typename KV, typename KeyT, typename ValueT, typename... Args>
  56. KV add_all(KV kv, KeyT key, ValueT value, Args... args) {
  57. kv[key] = value;
  58. return add_all(kv, args...);
  59. }
  60. template <typename KV>
  61. KV add_all(KV kv) {
  62. return kv;
  63. }
  64. } // namespace helpers
  65. class Variant;
  66. class PoolByteArray;
  67. class PoolIntArray;
  68. class PoolRealArray;
  69. class PoolStringArray;
  70. class PoolVector2Array;
  71. class PoolVector3Array;
  72. class PoolColorArray;
  73. class Object;
  74. class Array {
  75. godot_array _godot_array;
  76. friend class Variant;
  77. friend class Dictionary;
  78. friend class String;
  79. inline explicit Array(const godot_array &other) {
  80. _godot_array = other;
  81. }
  82. public:
  83. Array();
  84. Array(const Array &other);
  85. Array &operator=(const Array &other);
  86. Array(const PoolByteArray &a);
  87. Array(const PoolIntArray &a);
  88. Array(const PoolRealArray &a);
  89. Array(const PoolStringArray &a);
  90. Array(const PoolVector2Array &a);
  91. Array(const PoolVector3Array &a);
  92. Array(const PoolColorArray &a);
  93. template <class... Args>
  94. static Array make(Args... args) {
  95. return helpers::append_all(Array(), args...);
  96. }
  97. Variant &operator[](const int idx);
  98. const Variant &operator[](const int idx) const;
  99. void append(const Variant &v);
  100. void clear();
  101. int count(const Variant &v);
  102. bool empty() const;
  103. void erase(const Variant &v);
  104. Variant front() const;
  105. Variant back() const;
  106. int find(const Variant &what, const int from = 0) const;
  107. int find_last(const Variant &what) const;
  108. bool has(const Variant &what) const;
  109. uint32_t hash() const;
  110. void insert(const int pos, const Variant &value);
  111. void invert();
  112. bool is_shared() const;
  113. Variant pop_back();
  114. Variant pop_front();
  115. void push_back(const Variant &v);
  116. void push_front(const Variant &v);
  117. void remove(const int idx);
  118. int size() const;
  119. void resize(const int size);
  120. int rfind(const Variant &what, const int from = -1) const;
  121. void sort();
  122. void sort_custom(Object *obj, const String &func);
  123. int bsearch(const Variant &value, const bool before = true);
  124. int bsearch_custom(const Variant &value, const Object *obj,
  125. const String &func, const bool before = true);
  126. Array duplicate(const bool deep = false) const;
  127. Variant max() const;
  128. Variant min() const;
  129. void shuffle();
  130. ~Array();
  131. };
  132. } // namespace godot
  133. #endif // ARRAY_H