extensions/sdktools/util.h
author Matt Woodrow <pred@alliedmods.net>
Wed Mar 12 00:43:35 2008 +0000 (2008-03-12)
changeset 1922 a8cff6b8ef64
child 1924 ca49b1412e01
permissions -rw-r--r--
Added sm_dump_classes command
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod SDKTools Extension
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id: vcallbuilder.h 1566 2007-10-14 22:12:46Z faluco $
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_UTIL_H_
    33 #define _INCLUDE_SOURCEMOD_UTIL_H_
    34 
    35 #include "utldict.h"
    36 
    37 abstract_class IEntityFactory
    38 {
    39 public:
    40 	virtual IServerNetworkable *Create( const char *pClassName ) = 0;
    41 	virtual void Destroy( IServerNetworkable *pNetworkable ) = 0;
    42 	virtual size_t GetEntitySize() = 0;
    43 };
    44 
    45 template <class T>
    46 class CEntityFactory : public IEntityFactory
    47 {
    48 public:
    49 	CEntityFactory( const char *pClassName )
    50 	{
    51 		EntityFactoryDictionary()->InstallFactory( this, pClassName );
    52 	}
    53 
    54 	IServerNetworkable *Create( const char *pClassName )
    55 	{
    56 		T* pEnt = _CreateEntityTemplate((T*)NULL, pClassName);
    57 		return pEnt->NetworkProp();
    58 	}
    59 
    60 	void Destroy( IServerNetworkable *pNetworkable )
    61 	{
    62 		if ( pNetworkable )
    63 		{
    64 			pNetworkable->Release();
    65 		}
    66 	}
    67 
    68 	virtual size_t GetEntitySize()
    69 	{
    70 		return sizeof(T);
    71 	}
    72 };
    73 
    74 abstract_class IEntityFactoryDictionary
    75 {
    76 public:
    77 	virtual void InstallFactory( IEntityFactory *pFactory, const char *pClassName ) = 0;
    78 	virtual IServerNetworkable *Create( const char *pClassName ) = 0;
    79 	virtual void Destroy( const char *pClassName, IServerNetworkable *pNetworkable ) = 0;
    80 	virtual IEntityFactory *FindFactory( const char *pClassName ) = 0;
    81 	virtual const char *GetCannonicalName( const char *pClassName ) = 0;
    82 };
    83 
    84 
    85 class CEntityFactoryDictionary : public IEntityFactoryDictionary
    86 {
    87 public:
    88 	CEntityFactoryDictionary();
    89 
    90 	virtual void InstallFactory( IEntityFactory *pFactory, const char *pClassName );
    91 	virtual IServerNetworkable *Create( const char *pClassName );
    92 	virtual void Destroy( const char *pClassName, IServerNetworkable *pNetworkable );
    93 	virtual const char *GetCannonicalName( const char *pClassName );
    94 	void ReportEntitySizes();
    95 
    96 private:
    97 	IEntityFactory *FindFactory( const char *pClassName );
    98 public:
    99 	CUtlDict< IEntityFactory *, unsigned short > m_Factories;
   100 };
   101 
   102 #endif //_INCLUDE_SOURCEMOD_UTIL_H_