diff options
| author | Joe Ludwig <[email protected]> | 2013-06-26 15:22:04 -0700 |
|---|---|---|
| committer | Joe Ludwig <[email protected]> | 2013-06-26 15:22:04 -0700 |
| commit | 39ed87570bdb2f86969d4be821c94b722dc71179 (patch) | |
| tree | abc53757f75f40c80278e87650ea92808274aa59 /mp/src/game/shared/entitydatainstantiator.h | |
| download | source-sdk-2013-39ed87570bdb2f86969d4be821c94b722dc71179.tar.xz source-sdk-2013-39ed87570bdb2f86969d4be821c94b722dc71179.zip | |
First version of the SOurce SDK 2013
Diffstat (limited to 'mp/src/game/shared/entitydatainstantiator.h')
| -rw-r--r-- | mp/src/game/shared/entitydatainstantiator.h | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/mp/src/game/shared/entitydatainstantiator.h b/mp/src/game/shared/entitydatainstantiator.h new file mode 100644 index 00000000..128e52d1 --- /dev/null +++ b/mp/src/game/shared/entitydatainstantiator.h @@ -0,0 +1,126 @@ +//========= Copyright Valve Corporation, All rights reserved. ============//
+//
+// Purpose:
+//
+//=============================================================================//
+
+#ifndef ENTITYDATAINSTANTIATOR_H
+#define ENTITYDATAINSTANTIATOR_H
+#ifdef _WIN32
+#pragma once
+#endif
+
+#include "utlhash.h"
+
+#include "tier0/memdbgon.h"
+
+// This is the hash key type, but it could just as easily be and int or void *
+class CBaseEntity;
+
+//-----------------------------------------------------------------------------
+// Purpose:
+//-----------------------------------------------------------------------------
+abstract_class IEntityDataInstantiator
+{
+public:
+ virtual ~IEntityDataInstantiator() {};
+
+ virtual void *GetDataObject( const CBaseEntity *instance ) = 0;
+ virtual void *CreateDataObject( const CBaseEntity *instance ) = 0;
+ virtual void DestroyDataObject( const CBaseEntity *instance ) = 0;
+};
+
+//-----------------------------------------------------------------------------
+// Purpose:
+//-----------------------------------------------------------------------------
+template <class T>
+class CEntityDataInstantiator : public IEntityDataInstantiator
+{
+public:
+ CEntityDataInstantiator() :
+ m_HashTable( 64, 0, 0, CompareFunc, KeyFunc )
+ {
+ }
+
+ virtual void *GetDataObject( const CBaseEntity *instance )
+ {
+ UtlHashHandle_t handle;
+ HashEntry entry;
+ entry.key = instance;
+ handle = m_HashTable.Find( entry );
+
+ if ( handle != m_HashTable.InvalidHandle() )
+ {
+ return (void *)m_HashTable[ handle ].data;
+ }
+
+ return NULL;
+ }
+
+ virtual void *CreateDataObject( const CBaseEntity *instance )
+ {
+ UtlHashHandle_t handle;
+ HashEntry entry;
+ entry.key = instance;
+ handle = m_HashTable.Find( entry );
+
+ // Create it if not already present
+ if ( handle == m_HashTable.InvalidHandle() )
+ {
+ handle = m_HashTable.Insert( entry );
+ Assert( handle != m_HashTable.InvalidHandle() );
+ m_HashTable[ handle ].data = new T;
+
+ // FIXME: We'll have to remove this if any objects we instance have vtables!!!
+ Q_memset( m_HashTable[ handle ].data, 0, sizeof( T ) );
+ }
+
+ return (void *)m_HashTable[ handle ].data;
+ }
+
+ virtual void DestroyDataObject( const CBaseEntity *instance )
+ {
+ UtlHashHandle_t handle;
+ HashEntry entry;
+ entry.key = instance;
+ handle = m_HashTable.Find( entry );
+
+ if ( handle != m_HashTable.InvalidHandle() )
+ {
+ delete m_HashTable[ handle ].data;
+ m_HashTable.Remove( handle );
+ }
+ }
+
+private:
+
+ struct HashEntry
+ {
+ HashEntry()
+ {
+ key = NULL;
+ data = NULL;
+ }
+
+ const CBaseEntity *key;
+ T *data;
+ };
+
+ static bool CompareFunc( const HashEntry &src1, const HashEntry &src2 )
+ {
+ return ( src1.key == src2.key );
+ }
+
+
+ static unsigned int KeyFunc( const HashEntry &src )
+ {
+ // Shift right to get rid of alignment bits and border the struct on a 16 byte boundary
+ return (unsigned int)src.key;
+ }
+
+ CUtlHash< HashEntry > m_HashTable;
+};
+
+#include "tier0/memdbgoff.h"
+
+#endif // ENTITYDATAINSTANTIATOR_H
|