VDataTable.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //-----------------------------------------------------------------------------
  2. // Verve
  3. // Copyright (C) 2014 - Violent Tulip
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #include "Verve/Core/VDataTable.h"
  24. #include "console/script.h"
  25. #include "console/simObject.h"
  26. //-----------------------------------------------------------------------------
  27. // Implement the DataType enum list.
  28. ImplementEnumType( VDataTableDataType, "" )
  29. { VDataTable::k_TypeExpression, "EXPRESSION" },
  30. { VDataTable::k_TypeStatic, "STATIC" },
  31. { VDataTable::k_TypeVariable, "VARIABLE" },
  32. EndImplementEnumType;
  33. VDataTable::eDataType VDataTable::getDataTypeEnum( const char *pLabel )
  34. {
  35. VDataTable::eDataType out;
  36. if ( !castConsoleTypeFromString( out, pLabel ) )
  37. {
  38. // Bah!
  39. return VDataTable::k_TypeInvalid;
  40. }
  41. // Return.
  42. return out;
  43. }
  44. const char *VDataTable::getDataTypeDescription( const VDataTable::eDataType pEnum )
  45. {
  46. // Return.
  47. return castConsoleTypeToString( pEnum );
  48. }
  49. //-----------------------------------------------------------------------------
  50. VDataTable::VDataTable( void )
  51. {
  52. mDataMap.clear();
  53. }
  54. VDataTable::~VDataTable( void )
  55. {
  56. mDataMap.clear();
  57. }
  58. //-----------------------------------------------------------------------------
  59. //-----------------------------------------------------------------------------
  60. //
  61. // VDataTable::insert( pType, pFieldName );
  62. //
  63. // Add a DataTable entry, referencing the field name and assign it the given
  64. // data type.
  65. //
  66. // For a full list of possible data types, see the 'eDataType' declaration in
  67. // VDataTable.h.
  68. //
  69. //-----------------------------------------------------------------------------
  70. void VDataTable::insert( eDataType pType, const String &pFieldName )
  71. {
  72. if ( mDataMap.contains( pFieldName ) )
  73. {
  74. // Change Field Type.
  75. mDataMap.find( pFieldName )->value.Type = pType;
  76. // Return.
  77. return;
  78. }
  79. // Insert Item.
  80. mDataMap.insert( pFieldName, sDataItem( pType, pFieldName ) );
  81. }
  82. //-----------------------------------------------------------------------------
  83. //
  84. // VDataTable::clear( pFieldName );
  85. //
  86. // Clear the DataTable entry with the given field name.
  87. //
  88. //-----------------------------------------------------------------------------
  89. void VDataTable::clear( const String &pFieldName )
  90. {
  91. // Clear Item.
  92. mDataMap.erase( pFieldName );
  93. }
  94. //-----------------------------------------------------------------------------
  95. //
  96. // VDataTable::clear();
  97. //
  98. // Clear the contents of the DataTable entirely.
  99. //
  100. //-----------------------------------------------------------------------------
  101. void VDataTable::clear( void )
  102. {
  103. // Clear.
  104. mDataMap.clear();
  105. }
  106. //-----------------------------------------------------------------------------
  107. //-----------------------------------------------------------------------------
  108. //
  109. // VDataTable::getCount();
  110. //
  111. // Return the number of DataTable entries.
  112. //
  113. //-----------------------------------------------------------------------------
  114. S32 VDataTable::getCount( void )
  115. {
  116. return mDataMap.size();
  117. }
  118. //-----------------------------------------------------------------------------
  119. //
  120. // VDataTable::getItem( pIndex, *pDataItem );
  121. //
  122. // Return the item with the given index. This method will return false if there
  123. // is no valid data entry with that index.
  124. //
  125. //-----------------------------------------------------------------------------
  126. bool VDataTable::getItem( const S32 &pIndex, sDataItem *pDataItem )
  127. {
  128. if ( pIndex < 0 || pIndex >= mDataMap.size() )
  129. {
  130. // Invalid Field.
  131. return false;
  132. }
  133. S32 index = 0;
  134. for ( VDataMap::Iterator itr = mDataMap.begin(); itr != mDataMap.end(); ++itr )
  135. {
  136. if ( index == pIndex )
  137. {
  138. if ( pDataItem )
  139. {
  140. // Store Reference.
  141. *pDataItem = ( itr->value );
  142. }
  143. // Valid Field.
  144. return true;
  145. }
  146. // Increment.
  147. ++index;
  148. }
  149. // Invalid Field.
  150. return false;
  151. }
  152. //-----------------------------------------------------------------------------
  153. //
  154. // VDataTable::getItem( pFieldName, *pDataItem );
  155. //
  156. // Return the item with the given field name. This method will return false if
  157. // there is no valid data entry with that name.
  158. //
  159. //-----------------------------------------------------------------------------
  160. bool VDataTable::getItem( const String &pFieldName, sDataItem *pDataItem )
  161. {
  162. if ( mDataMap.contains( pFieldName ) )
  163. {
  164. if ( pDataItem )
  165. {
  166. // Fetch Item
  167. *pDataItem = mDataMap.find( pFieldName )->value;
  168. }
  169. // Valid Field.
  170. return true;
  171. }
  172. // Invalid Field.
  173. return false;
  174. }
  175. //-----------------------------------------------------------------------------
  176. //
  177. // VDataTable::getValue( pObject, pFieldName, *pValue );
  178. //
  179. // Evaluate and return the expression provided in the data field.
  180. //
  181. //-----------------------------------------------------------------------------
  182. bool VDataTable::getValue( SimObject *pObject, const String &pFieldName, String &pValue )
  183. {
  184. if ( !pObject || pFieldName.isEmpty() )
  185. {
  186. // Sanity!
  187. return false;
  188. }
  189. // Fetch Data.
  190. sDataItem *data = &( mDataMap.find( pFieldName )->value );
  191. if ( !data )
  192. {
  193. // No Field.
  194. return false;
  195. }
  196. // Field Value.
  197. const char *fieldValue = pObject->getDataField( StringTable->insert( data->FieldName ), NULL );
  198. switch ( data->Type )
  199. {
  200. case VDataTable::k_TypeExpression :
  201. {
  202. // Evaluate.
  203. pValue = Con::evaluate( fieldValue, false ).value.getString();
  204. } break;
  205. case VDataTable::k_TypeStatic :
  206. {
  207. // Use Value.
  208. pValue = fieldValue;
  209. } break;
  210. case VDataTable::k_TypeVariable :
  211. {
  212. // Fetch Variable.
  213. pValue = Con::getVariable( fieldValue );
  214. } break;
  215. }
  216. // Valid Field.
  217. return true;
  218. }