arrayObject.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #ifndef _ARRAYOBJECT_H_
  23. #define _ARRAYOBJECT_H_
  24. #ifndef _SIMBASE_H_
  25. #include "console/simBase.h"
  26. #endif
  27. // This class is based on original code by community
  28. // member Daniel Neilsen:
  29. //
  30. // http://www.garagegames.com/community/resources/view/4711
  31. /// A data structure holding indexed sequences of key/value pairs for script use.
  32. class ArrayObject : public SimObject
  33. {
  34. typedef SimObject Parent;
  35. protected:
  36. struct Element
  37. {
  38. String key;
  39. String value;
  40. Element() { }
  41. Element( const String& _key, const String& _value ) : key(_key), value(_value) { }
  42. };
  43. bool mCaseSensitive;
  44. S32 mCurrentIndex;
  45. /// The array of key/value pairs.
  46. Vector< Element > mArray;
  47. /// @name Sorting
  48. /// @{
  49. static bool smDecreasing;
  50. static bool smCaseSensitive;
  51. static const char* smCompareFunction;
  52. static S32 QSORT_CALLBACK _valueCompare( const void *a, const void *b );
  53. static S32 QSORT_CALLBACK _valueNumCompare( const void *a, const void *b );
  54. static S32 QSORT_CALLBACK _keyCompare( const void *a, const void *b );
  55. static S32 QSORT_CALLBACK _keyNumCompare( const void *a, const void *b );
  56. static S32 QSORT_CALLBACK _keyFunctionCompare( const void* a, const void* b );
  57. static S32 QSORT_CALLBACK _valueFunctionCompare( const void* a, const void* b );
  58. /// @}
  59. static bool _addKeyFromField( void *object, const char *index, const char *data );
  60. public:
  61. ArrayObject();
  62. /// @name Data Query
  63. /// @{
  64. /// Returns true if string handling by the array is case-sensitive.
  65. bool isCaseSensitive() const { return mCaseSensitive; }
  66. bool isEqual( const String &valA, const String &valB ) const
  67. {
  68. return valA.equal( valB, isCaseSensitive() ? String::Case : String::NoCase );
  69. }
  70. /// Searches the array for the first matching value from the
  71. /// current array position. It will return -1 if no matching
  72. /// index is found.
  73. S32 getIndexFromValue( const String &value ) const;
  74. /// Searches the array for the first matching key from the current
  75. /// array position. It will return -1 if no matching index found.
  76. S32 getIndexFromKey( const String &key ) const;
  77. /// Returns the key for a given index.
  78. /// Will return a null value for an invalid index
  79. const String& getKeyFromIndex( S32 index ) const;
  80. /// Returns the value for a given index.
  81. /// Will return a null value for an invalid index
  82. const String& getValueFromIndex( S32 index ) const;
  83. ///
  84. S32 getIndexFromKeyValue( const String &key, const String &value ) const;
  85. /// Counts the number of elements in the array
  86. S32 count() const { return mArray.size(); }
  87. /// Counts the number of instances of a particular value in the array
  88. S32 countValue( const String &value ) const;
  89. /// Counts the number of instances of a particular key in the array
  90. S32 countKey( const String &key ) const;
  91. /// @}
  92. /// @name Data Alteration
  93. /// @{
  94. /// Adds a new array item to the end of the array
  95. void push_back( const String &key, const String &value );
  96. /// Adds a new array item to the front of the array
  97. void push_front( const String &key, const String &value );
  98. /// Adds a new array item to a particular index of the array
  99. void insert( const String &key, const String &value, S32 index );
  100. /// Removes an array item from the end of the array
  101. void pop_back();
  102. /// Removes an array item from the end of the array
  103. void pop_front();
  104. /// Removes an array item from a particular index of the array
  105. void erase( S32 index );
  106. /// Clears an array
  107. void empty();
  108. /// Moves a key and value from one index location to another.
  109. void moveIndex( S32 prev, S32 index );
  110. /// @}
  111. /// @name Complex Data Alteration
  112. /// @{
  113. /// Removes any duplicate values from the array
  114. /// (keeps the first instance only)
  115. void uniqueValue();
  116. /// Removes any duplicate keys from the array
  117. /// (keeps the first instance only)
  118. void uniqueKey();
  119. /// Makes this array an exact duplicate of another array
  120. void duplicate( ArrayObject *obj );
  121. /// Crops the keys that exists in the target array from our current array
  122. void crop( ArrayObject *obj );
  123. /// Appends the target array to our current array
  124. void append( ArrayObject *obj );
  125. /// Sets the key at the given index
  126. void setKey( const String &key, S32 index );
  127. /// Sets the key at the given index
  128. void setValue( const String &value, S32 index );
  129. /// This sorts the array.
  130. /// @param valtest Determines whether sorting by value or key.
  131. /// @param asc Determines if sorting ascending or descending.
  132. /// @param numeric Determines if sorting alpha or numeric search.
  133. void sort( bool valtest, bool asc, bool numeric );
  134. /// This sorts the array using a script callback.
  135. /// @param valtest Determines whether sorting by value or key.
  136. /// @param asc Determines if sorting ascending or descending.
  137. /// @param callbackFunctionName Name of the script function.
  138. void sort( bool valtest, bool asc, const char* callbackFunctionName );
  139. /// @}
  140. /// @name Pointer Manipulation
  141. /// @{
  142. /// Moves pointer to arrays first position
  143. S32 moveFirst();
  144. /// Moves pointer to arrays last position
  145. S32 moveLast();
  146. /// Moves pointer to arrays next position
  147. /// If last position it returns -1 and no move occurs;
  148. S32 moveNext();
  149. /// Moves pointer to arrays prev position
  150. /// If first position it returns -1 and no move occurs;
  151. S32 movePrev();
  152. /// Returns current pointer index.
  153. S32 getCurrent() const { return mCurrentIndex; }
  154. ///
  155. void setCurrent( S32 idx );
  156. /// @}
  157. /// @name Data Listing
  158. /// @{
  159. /// Echos the content of the array to the console.
  160. void echo();
  161. /// @}
  162. // SimObject
  163. DECLARE_CONOBJECT( ArrayObject );
  164. DECLARE_CATEGORY( "Core" );
  165. DECLARE_DESCRIPTION( "An object storing an indexed sequence of key/value pairs." );
  166. static void initPersistFields();
  167. };
  168. #endif // _ARRAYOBJECT_H_