VDataTable.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/simObject.h"
  25. //-----------------------------------------------------------------------------
  26. // Implement the DataType enum list.
  27. ImplementEnumType( VDataTableDataType, "" )
  28. { VDataTable::k_TypeExpression, "EXPRESSION" },
  29. { VDataTable::k_TypeStatic, "STATIC" },
  30. { VDataTable::k_TypeVariable, "VARIABLE" },
  31. EndImplementEnumType;
  32. VDataTable::eDataType VDataTable::getDataTypeEnum( const char *pLabel )
  33. {
  34. VDataTable::eDataType out;
  35. if ( !castConsoleTypeFromString( out, pLabel ) )
  36. {
  37. // Bah!
  38. return VDataTable::k_TypeInvalid;
  39. }
  40. // Return.
  41. return out;
  42. }
  43. const char *VDataTable::getDataTypeDescription( const VDataTable::eDataType pEnum )
  44. {
  45. // Return.
  46. return castConsoleTypeToString( pEnum );
  47. }
  48. //-----------------------------------------------------------------------------
  49. VDataTable::VDataTable( void )
  50. {
  51. mDataMap.clear();
  52. }
  53. VDataTable::~VDataTable( void )
  54. {
  55. mDataMap.clear();
  56. }
  57. //-----------------------------------------------------------------------------
  58. //-----------------------------------------------------------------------------
  59. //
  60. // VDataTable::insert( pType, pFieldName );
  61. //
  62. // Add a DataTable entry, referencing the field name and assign it the given
  63. // data type.
  64. //
  65. // For a full list of possible data types, see the 'eDataType' declaration in
  66. // VDataTable.h.
  67. //
  68. //-----------------------------------------------------------------------------
  69. void VDataTable::insert( eDataType pType, const String &pFieldName )
  70. {
  71. if ( mDataMap.contains( pFieldName ) )
  72. {
  73. // Change Field Type.
  74. mDataMap.find( pFieldName )->value.Type = pType;
  75. // Return.
  76. return;
  77. }
  78. // Insert Item.
  79. mDataMap.insert( pFieldName, sDataItem( pType, pFieldName ) );
  80. }
  81. //-----------------------------------------------------------------------------
  82. //
  83. // VDataTable::clear( pFieldName );
  84. //
  85. // Clear the DataTable entry with the given field name.
  86. //
  87. //-----------------------------------------------------------------------------
  88. void VDataTable::clear( const String &pFieldName )
  89. {
  90. // Clear Item.
  91. mDataMap.erase( pFieldName );
  92. }
  93. //-----------------------------------------------------------------------------
  94. //
  95. // VDataTable::clear();
  96. //
  97. // Clear the contents of the DataTable entirely.
  98. //
  99. //-----------------------------------------------------------------------------
  100. void VDataTable::clear( void )
  101. {
  102. // Clear.
  103. mDataMap.clear();
  104. }
  105. //-----------------------------------------------------------------------------
  106. //-----------------------------------------------------------------------------
  107. //
  108. // VDataTable::getCount();
  109. //
  110. // Return the number of DataTable entries.
  111. //
  112. //-----------------------------------------------------------------------------
  113. S32 VDataTable::getCount( void )
  114. {
  115. return mDataMap.size();
  116. }
  117. //-----------------------------------------------------------------------------
  118. //
  119. // VDataTable::getItem( pIndex, *pDataItem );
  120. //
  121. // Return the item with the given index. This method will return false if there
  122. // is no valid data entry with that index.
  123. //
  124. //-----------------------------------------------------------------------------
  125. bool VDataTable::getItem( const S32 &pIndex, sDataItem *pDataItem )
  126. {
  127. if ( pIndex < 0 || pIndex >= mDataMap.size() )
  128. {
  129. // Invalid Field.
  130. return false;
  131. }
  132. S32 index = 0;
  133. for ( VDataMap::Iterator itr = mDataMap.begin(); itr != mDataMap.end(); ++itr )
  134. {
  135. if ( index == pIndex )
  136. {
  137. if ( pDataItem )
  138. {
  139. // Store Reference.
  140. *pDataItem = ( itr->value );
  141. }
  142. // Valid Field.
  143. return true;
  144. }
  145. // Increment.
  146. ++index;
  147. }
  148. // Invalid Field.
  149. return false;
  150. }
  151. //-----------------------------------------------------------------------------
  152. //
  153. // VDataTable::getItem( pFieldName, *pDataItem );
  154. //
  155. // Return the item with the given field name. This method will return false if
  156. // there is no valid data entry with that name.
  157. //
  158. //-----------------------------------------------------------------------------
  159. bool VDataTable::getItem( const String &pFieldName, sDataItem *pDataItem )
  160. {
  161. if ( mDataMap.contains( pFieldName ) )
  162. {
  163. if ( pDataItem )
  164. {
  165. // Fetch Item
  166. *pDataItem = mDataMap.find( pFieldName )->value;
  167. }
  168. // Valid Field.
  169. return true;
  170. }
  171. // Invalid Field.
  172. return false;
  173. }
  174. //-----------------------------------------------------------------------------
  175. //
  176. // VDataTable::getValue( pObject, pFieldName, *pValue );
  177. //
  178. // Evaluate and return the expression provided in the data field.
  179. //
  180. //-----------------------------------------------------------------------------
  181. bool VDataTable::getValue( SimObject *pObject, const String &pFieldName, String &pValue )
  182. {
  183. if ( !pObject || pFieldName.isEmpty() )
  184. {
  185. // Sanity!
  186. return false;
  187. }
  188. // Fetch Data.
  189. sDataItem *data = &( mDataMap.find( pFieldName )->value );
  190. if ( !data )
  191. {
  192. // No Field.
  193. return false;
  194. }
  195. // Field Value.
  196. const char *fieldValue = pObject->getDataField( StringTable->insert( data->FieldName ), NULL );
  197. switch ( data->Type )
  198. {
  199. case VDataTable::k_TypeExpression :
  200. {
  201. // Evaluate.
  202. pValue = Con::evaluate( fieldValue, false ).getString();
  203. } break;
  204. case VDataTable::k_TypeStatic :
  205. {
  206. // Use Value.
  207. pValue = fieldValue;
  208. } break;
  209. case VDataTable::k_TypeVariable :
  210. {
  211. // Fetch Variable.
  212. pValue = Con::getVariable( fieldValue );
  213. } break;
  214. }
  215. // Valid Field.
  216. return true;
  217. }