2010-07-22 01:46:14 -05:00
|
|
|
//========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
|
|
|
|
//
|
|
|
|
// Purpose:
|
|
|
|
//
|
|
|
|
// $NoKeywords: $
|
|
|
|
//=============================================================================//
|
|
|
|
|
|
|
|
#ifndef STRINGPOOL_H
|
|
|
|
#define STRINGPOOL_H
|
|
|
|
|
|
|
|
#if defined( _WIN32 )
|
|
|
|
#pragma once
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "utlrbtree.h"
|
|
|
|
#include "utlvector.h"
|
|
|
|
#include "utlbuffer.h"
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: Allocates memory for strings, checking for duplicates first,
|
|
|
|
// reusing exising strings if duplicate found.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
enum StringPoolCase_t
|
|
|
|
{
|
|
|
|
StringPoolCaseInsensitive,
|
|
|
|
StringPoolCaseSensitive
|
|
|
|
};
|
|
|
|
|
|
|
|
class CStringPool
|
|
|
|
{
|
|
|
|
public:
|
2024-03-18 22:06:03 +03:00
|
|
|
DLL_CLASS_IMPORT CStringPool( StringPoolCase_t caseSensitivity = StringPoolCaseInsensitive );
|
|
|
|
DLL_CLASS_IMPORT ~CStringPool();
|
2010-07-22 01:46:14 -05:00
|
|
|
|
2024-03-18 22:06:03 +03:00
|
|
|
DLL_CLASS_IMPORT unsigned int Count() const;
|
2010-07-22 01:46:14 -05:00
|
|
|
|
2024-03-18 22:06:03 +03:00
|
|
|
DLL_CLASS_IMPORT const char * Allocate( const char *pszValue );
|
|
|
|
DLL_CLASS_IMPORT void FreeAll();
|
2010-07-22 01:46:14 -05:00
|
|
|
|
|
|
|
// searches for a string already in the pool
|
2024-03-18 22:06:03 +03:00
|
|
|
DLL_CLASS_IMPORT const char * Find( const char *pszValue );
|
2010-07-22 01:46:14 -05:00
|
|
|
|
|
|
|
protected:
|
|
|
|
typedef CUtlRBTree<const char *, unsigned short> CStrSet;
|
|
|
|
|
2024-03-18 22:06:03 +03:00
|
|
|
CThreadFastMutex m_Mutex;
|
2010-07-22 01:46:14 -05:00
|
|
|
CStrSet m_Strings;
|
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: A reference counted string pool.
|
|
|
|
//
|
|
|
|
// Elements are stored more efficiently than in the conventional string pool,
|
|
|
|
// quicker to look up, and storage is tracked via reference counts.
|
|
|
|
//
|
|
|
|
// At some point this should replace CStringPool
|
|
|
|
//-----------------------------------------------------------------------------
|
2024-05-31 05:57:39 +03:00
|
|
|
class CCountedStringPool_CI
|
2010-07-22 01:46:14 -05:00
|
|
|
{
|
|
|
|
public: // HACK, hash_item_t structure should not be public.
|
|
|
|
|
|
|
|
struct hash_item_t
|
|
|
|
{
|
|
|
|
char* pString;
|
|
|
|
unsigned short nNextElement;
|
|
|
|
unsigned char nReferenceCount;
|
|
|
|
unsigned char pad;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
INVALID_ELEMENT = 0,
|
|
|
|
MAX_REFERENCE = 0xFF,
|
|
|
|
HASH_TABLE_SIZE = 1024
|
|
|
|
};
|
|
|
|
|
|
|
|
CUtlVector<unsigned short> m_HashTable; // Points to each element
|
|
|
|
CUtlVector<hash_item_t> m_Elements;
|
|
|
|
unsigned short m_FreeListStart;
|
|
|
|
StringPoolCase_t m_caseSensitivity;
|
|
|
|
|
|
|
|
public:
|
2024-05-31 05:57:39 +03:00
|
|
|
DLL_CLASS_IMPORT CCountedStringPool_CI();
|
|
|
|
DLL_CLASS_IMPORT virtual ~CCountedStringPool_CI();
|
2010-07-22 01:46:14 -05:00
|
|
|
|
2024-03-18 22:06:03 +03:00
|
|
|
DLL_CLASS_IMPORT void FreeAll();
|
2010-07-22 01:46:14 -05:00
|
|
|
|
2024-05-31 05:57:39 +03:00
|
|
|
DLL_CLASS_IMPORT const char *FindString( const char* pIntrinsic ) const;
|
|
|
|
DLL_CLASS_IMPORT const char *ReferenceString( const char* pIntrinsic );
|
2024-03-18 22:06:03 +03:00
|
|
|
DLL_CLASS_IMPORT void DereferenceString( const char* pIntrinsic );
|
2010-07-22 01:46:14 -05:00
|
|
|
|
|
|
|
// These are only reliable if there are less than 64k strings in your string pool
|
2024-05-31 05:57:39 +03:00
|
|
|
DLL_CLASS_IMPORT unsigned short FindStringHandle( const char* pIntrinsic ) const;
|
2024-03-18 22:06:03 +03:00
|
|
|
DLL_CLASS_IMPORT unsigned short ReferenceStringHandle( const char* pIntrinsic );
|
2024-05-31 05:57:39 +03:00
|
|
|
DLL_CLASS_IMPORT const char *HandleToString( unsigned short handle ) const;
|
|
|
|
DLL_CLASS_IMPORT void SpewStrings() const;
|
|
|
|
DLL_CLASS_IMPORT unsigned int Hash( const char *pszKey ) const;
|
2024-03-18 22:06:03 +03:00
|
|
|
|
2024-05-31 05:57:39 +03:00
|
|
|
DLL_CLASS_IMPORT bool SaveToBuffer( CUtlBuffer &buffer ) const;
|
2024-03-18 22:06:03 +03:00
|
|
|
DLL_CLASS_IMPORT bool RestoreFromBuffer( CUtlBuffer &buffer );
|
|
|
|
};
|
2010-07-22 01:46:14 -05:00
|
|
|
|
|
|
|
#endif // STRINGPOOL_H
|