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