simDictionary.h
Engine/source/console/simDictionary.h
Classes:
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 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 24#ifndef _SIMDICTIONARY_H_ 25#define _SIMDICTIONARY_H_ 26#ifndef _PLATFORM_H_ 27#include "platform/platform.h" 28#endif 29#ifndef _STRINGTABLE_H_ 30#include "core/stringTable.h" 31#endif 32 33#ifndef _PLATFORMMUTEX_H_ 34#include "platform/threads/mutex.h" 35#endif 36 37#include "torqueConfig.h" 38 39class SimObject; 40 41#ifdef USE_NEW_SIMDICTIONARY 42#include <string> 43#include <unordered_map> 44 45#ifndef _SIM_H_ 46#include "console/sim.h" 47#endif 48 49struct StringTableEntryHash 50{ 51 inline size_t operator()(StringTableEntry val) const 52 { 53 return (size_t)val; 54 } 55}; 56 57struct StringTableEntryEq 58{ 59 inline bool operator()(StringTableEntry s1, StringTableEntry s2) const 60 { 61 return s1 == s2; 62 } 63}; 64 65typedef std::unordered_map<StringTableEntry, SimObject*, StringTableEntryHash, StringTableEntryEq> StringDictDef; 66typedef std::unordered_map<SimObjectId, SimObject*> SimObjectIdDictDef; 67#endif 68 69//---------------------------------------------------------------------------- 70/// Map of names to SimObjects 71/// 72/// Provides fast lookup for name->object and 73/// for fast removal of an object given object* 74class SimNameDictionary 75{ 76#ifndef USE_NEW_SIMDICTIONARY 77 enum 78 { 79 DefaultTableSize = 29 80 }; 81 82 SimObject **hashTable; // hash the pointers of the names... 83 S32 hashTableSize; 84 S32 hashEntryCount; 85#else 86 StringDictDef root; 87#endif 88 89 void *mutex; 90 91public: 92 void insert(SimObject* obj); 93 void remove(SimObject* obj); 94 SimObject* find(StringTableEntry name); 95 96 SimNameDictionary(); 97 ~SimNameDictionary(); 98}; 99 100class SimManagerNameDictionary 101{ 102#ifndef USE_NEW_SIMDICTIONARY 103 enum 104 { 105 DefaultTableSize = 29 106 }; 107 108 SimObject **hashTable; // hash the pointers of the names... 109 S32 hashTableSize; 110 S32 hashEntryCount; 111#else 112 StringDictDef root; 113#endif 114 115 void *mutex; 116 117public: 118 void insert(SimObject* obj); 119 void remove(SimObject* obj); 120 SimObject* find(StringTableEntry name); 121 122 SimManagerNameDictionary(); 123 ~SimManagerNameDictionary(); 124}; 125 126//---------------------------------------------------------------------------- 127/// Map of ID's to SimObjects. 128/// 129/// Provides fast lookup for ID->object and 130/// for fast removal of an object given object* 131class SimIdDictionary 132{ 133#ifndef USE_NEW_SIMDICTIONARY 134 enum 135 { 136 DefaultTableSize = 4096, 137 TableBitMask = 4095 138 }; 139 SimObject *table[DefaultTableSize]; 140#else 141 SimObjectIdDictDef root; 142#endif 143 144 void *mutex; 145 146public: 147 void insert(SimObject* obj); 148 void remove(SimObject* obj); 149 SimObject* find(S32 id); 150 151 SimIdDictionary(); 152 ~SimIdDictionary(); 153}; 154 155#endif //_SIMDICTIONARY_H_ 156