core/CMsgIdPool.h
author Borja Ferrer <faluco@alliedmods.net>
Fri Feb 16 18:47:16 2007 +0000 (2007-02-16)
changeset 505 9fb8a1ccd4ec
permissions -rw-r--r--
initial user message implementation with its natives and such
     1 /**
     2 * vim: set ts=4 :
     3 * ===============================================================
     4 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5 * ===============================================================
     6 *
     7 * This file is not open source and may not be copied without explicit
     8 * written permission of AlliedModders LLC.  This file may not be redistributed 
     9 * in whole or significant part.
    10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11 *
    12 * Version: $Id$
    13 */
    14 
    15 #ifndef _INCLUDE_SOURCEMOD_CMSGIDPOOL_H_
    16 #define _INCLUDE_SOURCEMOD_CMSGIDPOOL_H_
    17 
    18 #include "sm_trie.h"
    19 #include "sourcemm_api.h"
    20 
    21 #define INVALID_MESSAGE_ID -1
    22 
    23 class CMessageIdPool
    24 {
    25 public:
    26 	CMessageIdPool()
    27 	{
    28 		m_Names = sm_trie_create();
    29 	}
    30 	~CMessageIdPool()
    31 	{
    32 		sm_trie_destroy(m_Names);
    33 	}
    34 public:
    35 	int GetMessageId(const char *name)
    36 	{
    37 		int msgid;
    38 		if (!sm_trie_retrieve(m_Names, name, reinterpret_cast<void **>(&msgid)))
    39 		{
    40 			char buf[255];
    41 			int dummy;
    42 			msgid = 0;
    43 
    44 			while (gamedll->GetUserMessageInfo(msgid, buf, sizeof(buf), dummy))
    45 			{
    46 				if (strcmp(name, buf) == 0)
    47 				{
    48 					sm_trie_insert(m_Names, name, reinterpret_cast<void *>(msgid));
    49 					return msgid;
    50 				}
    51 				msgid++;
    52 			}
    53 
    54 			return INVALID_MESSAGE_ID;
    55 		}
    56 
    57 		return msgid;
    58 	}
    59 private:
    60 	Trie *m_Names;
    61 };
    62 
    63 #endif //_INCLUDE_SOURCEMOD_CMSGIDPOOL_H_