arrayObject_ScriptBinding.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. // adopted from original code by Daniel Neilsen - 14/10/03
  23. // [email protected]
  24. ConsoleMethodWithDocs( Array, getIndexFromValue, S32, 3, 3, "(string value)"
  25. "Search array from current position for the first matching value")
  26. {
  27. StringTableEntry value = StringTable->insert(argv[2]);
  28. return (S32)object->getIndexFromValue(value);
  29. }
  30. ConsoleMethodWithDocs( Array, getIndexFromKey, S32, 3, 3, "(string key)"
  31. "Search array from current position for the first matching key")
  32. {
  33. StringTableEntry value = StringTable->insert(argv[2]);
  34. return (S32)object->getIndexFromKey(value);
  35. }
  36. ConsoleMethodWithDocs( Array, getValue, const char*, 3, 3, "(int index)"
  37. "Get the value of the array element at the submitted index")
  38. {
  39. StringTableEntry ret = object->getValueFromIndex(dAtoi(argv[2]));
  40. return ret; // oxe 20081103 - no need to use the return buffer mechanism because StringTableEntries are write-once read-many.
  41. }
  42. ConsoleMethodWithDocs( Array, getKey, const char*, 3, 3, "(int index)"
  43. "Get the key of the array element at the submitted index")
  44. {
  45. StringTableEntry ret = object->getKeyFromIndex(dAtoi(argv[2]));
  46. return ret; // oxe 20081103 - no need to use the return buffer mechanism because StringTableEntries are write-once read-many.
  47. }
  48. ConsoleMethodWithDocs( Array, setKey, void, 4, 4, "(string key, int index)"
  49. "Set the key at the given index")
  50. {
  51. StringTableEntry key = StringTable->insert(argv[2]);
  52. S32 index = dAtoi(argv[3]);
  53. object->setKey(key, index);
  54. }
  55. ConsoleMethodWithDocs( Array, setValue, void, 4, 4, "(string key, int index)"
  56. "Set the value at the given index")
  57. {
  58. StringTableEntry value = StringTable->insert(argv[2]);
  59. S32 index = dAtoi(argv[3]);
  60. object->setValue(value, index);
  61. }
  62. ConsoleMethodWithDocs( Array, count, S32, 2, 2, "Get the number of elements in the array")
  63. {
  64. return (S32)object->count();
  65. }
  66. ConsoleMethodWithDocs( Array, countValue, S32, 3, 3, "(string value)"
  67. "Get the number of times a particular value is found in the array")
  68. {
  69. StringTableEntry value = StringTable->insert(argv[2]);
  70. return (S32)object->countValue(value);
  71. }
  72. ConsoleMethodWithDocs( Array, countKey, S32, 3, 3, "(string key)"
  73. "Get the number of times a particular key is found in the array")
  74. {
  75. StringTableEntry value = StringTable->insert(argv[2]);
  76. return (S32)object->countKey(value);
  77. }
  78. ConsoleMethodWithDocs( Array, add, void, 4, 4, "(string key, string value)"
  79. "Adds a new element to the end of an array")
  80. {
  81. StringTableEntry key = StringTable->insert(argv[2]);
  82. StringTableEntry value = StringTable->insert(argv[3]);
  83. object->push_back(key, value);
  84. }
  85. ConsoleMethodWithDocs( Array, push_back, void, 4, 4, "(string key, string value)"
  86. "Adds a new element to the end of an array")
  87. {
  88. StringTableEntry key = StringTable->insert(argv[2]);
  89. StringTableEntry value = StringTable->insert(argv[3]);
  90. object->push_back(key, value);
  91. }
  92. ConsoleMethodWithDocs( Array, push_front, void, 4, 4, "(string key, string value)"
  93. "Adds a new element to the front of an array")
  94. {
  95. StringTableEntry key = StringTable->insert(argv[2]);
  96. StringTableEntry value = StringTable->insert(argv[3]);
  97. object->push_front(key, value);
  98. }
  99. ConsoleMethodWithDocs( Array, insert, void, 5, 5, "(string key, string value, int index)"
  100. "Adds a new element to a specified position in the array")
  101. {
  102. StringTableEntry key = StringTable->insert(argv[2]);
  103. StringTableEntry value = StringTable->insert(argv[3]);
  104. object->insert(key, value, dAtoi(argv[4]));
  105. }
  106. ConsoleMethodWithDocs( Array, pop_back, void, 2, 2, "Removes the last element from the array")
  107. {
  108. object->pop_back();
  109. }
  110. ConsoleMethodWithDocs( Array, pop_front, void, 2, 2, "Removes the first element from the array")
  111. {
  112. object->pop_front();
  113. }
  114. ConsoleMethodWithDocs( Array, erase, void, 3, 3, "(int index)"
  115. "Removes an element at a specific position from the array")
  116. {
  117. object->erase(dAtoi(argv[2]));
  118. }
  119. ConsoleMethodWithDocs( Array, empty, void, 2, 2, "Emptys all elements from an array")
  120. {
  121. object->empty();
  122. }
  123. ConsoleMethodWithDocs( Array, uniqueValue, void, 2, 2, "Removes any elements that have duplicated values (leaving the first instance)")
  124. {
  125. object->uniqueValue();
  126. }
  127. ConsoleMethodWithDocs( Array, uniqueKey, void, 2, 2, "Removes any elements that have duplicated keys (leaving the first instance)")
  128. {
  129. object->uniqueKey();
  130. }
  131. ConsoleMethodWithDocs( Array, duplicate, bool, 3, 3, "(Array target)"
  132. "Alters array into an exact duplicate of the target array")
  133. {
  134. Array *target;
  135. if (Sim::findObject(argv[2],target))
  136. {
  137. object->duplicate(target);
  138. return true;
  139. }
  140. return false;
  141. }
  142. ConsoleMethodWithDocs( Array, crop, bool, 3, 3, "(Array target)"
  143. "Removes elements with matching keys from array")
  144. {
  145. Array *target;
  146. if (Sim::findObject(argv[2],target))
  147. {
  148. object->crop(target);
  149. return true;
  150. }
  151. return false;
  152. }
  153. ConsoleMethodWithDocs( Array, append, bool, 3, 3, "(Array target)"
  154. "Appends the target array to the array object")
  155. {
  156. Array *target;
  157. if (Sim::findObject(argv[2],target))
  158. {
  159. object->append(target);
  160. return true;
  161. }
  162. return false;
  163. }
  164. ConsoleMethodWithDocs( Array, sort, void, 2, 3, "(bool desc)"
  165. "Alpha sorts the array by value (default ascending sort)")
  166. {
  167. bool descending = argc == 3 ? dAtob(argv[2]) : false;
  168. object->sort(true,descending,false);
  169. }
  170. ConsoleMethodWithDocs( Array, sorta, void, 2, 2, "Alpha sorts the array by value in ascending order")
  171. {
  172. object->sort(true,false,false);
  173. }
  174. ConsoleMethodWithDocs( Array, sortd, void, 2, 2, "Alpha sorts the array by value in descending order")
  175. {
  176. object->sort(true,true,false);
  177. }
  178. ConsoleMethodWithDocs( Array, sortk, void, 2, 3, "(bool desc)"
  179. "Alpha sorts the array by key (default ascending sort)")
  180. {
  181. bool descending = argc == 3 ? dAtob(argv[2]) : false;
  182. object->sort(false,descending,false);
  183. }
  184. ConsoleMethodWithDocs( Array, sortka, void, 2, 2, "Alpha sorts the array by key in ascending order")
  185. {
  186. object->sort(false,false,false);
  187. }
  188. ConsoleMethodWithDocs( Array, sortkd, void, 2, 2, "Alpha sorts the array by key in descending order")
  189. {
  190. object->sort(false,true,false);
  191. }
  192. ConsoleMethodWithDocs( Array, sortn, void, 2, 3, "(bool desc)"
  193. "Numerically sorts the array by value (default ascending sort)")
  194. {
  195. bool descending = argc == 3 ? dAtob(argv[2]) : false;
  196. object->sort(true,descending,true);
  197. }
  198. ConsoleMethodWithDocs( Array, sortna, void, 2, 2, "Numerically sorts the array by value in ascending order")
  199. {
  200. object->sort(true,false,true);
  201. }
  202. ConsoleMethodWithDocs( Array, sortnd, void, 2, 2, "Numerically sorts the array by value in descending order")
  203. {
  204. object->sort(true,true,true);
  205. }
  206. ConsoleMethodWithDocs( Array, sortnk, void, 2, 3, "(bool desc)"
  207. "Numerically sorts the array by key (default ascending sort)")
  208. {
  209. bool descending = argc == 3 ? dAtob(argv[2]) : false;
  210. object->sort(false,descending,true);
  211. }
  212. ConsoleMethodWithDocs( Array, sortnka, void, 2, 2, "Numerical sorts the array by key in ascending order")
  213. {
  214. object->sort(false,false,true);
  215. }
  216. ConsoleMethodWithDocs( Array, sortnkd, void, 2, 2, "Numerical sorts the array by key in descending order")
  217. {
  218. object->sort(false,true,true);
  219. }
  220. ConsoleMethodWithDocs( Array, moveFirst, S32, 2, 2, "Moves array pointer to start of array")
  221. {
  222. return object->moveFirst();
  223. }
  224. ConsoleMethodWithDocs( Array, moveLast, S32, 2, 2, "Moves array pointer to end of array")
  225. {
  226. return object->moveLast();
  227. }
  228. ConsoleMethodWithDocs( Array, moveNext, S32, 2, 2, "Moves array pointer to next position (returns -1 if cannot move)")
  229. {
  230. return object->moveNext();
  231. }
  232. ConsoleMethodWithDocs( Array, movePrev, S32, 2, 2, "Moves array pointer to prev position (returns -1 if cannot move)")
  233. {
  234. return object->movePrev();
  235. }
  236. ConsoleMethodWithDocs( Array, getCurrent, S32, 2, 2, "Gets the current pointer index")
  237. {
  238. return object->getCurrent();
  239. }
  240. ConsoleMethodWithDocs( Array, echo, void, 2, 2, "Echos the array in the console")
  241. {
  242. object->echo();
  243. }