1
0
mirror of https://github.com/alliedmodders/hl2sdk.git synced 2025-09-19 20:16:10 +08:00
Files
hl2sdk/public/tier1/stringpool.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

105 lines
3.2 KiB
C
Raw Normal View History

//========= 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();
2024-03-18 22:06:03 +03:00
DLL_CLASS_IMPORT unsigned int Count() const;
2024-03-18 22:06:03 +03:00
DLL_CLASS_IMPORT const char * Allocate( const char *pszValue );
DLL_CLASS_IMPORT void FreeAll();
// 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 );
protected:
typedef CUtlRBTree<const char *, unsigned short> CStrSet;
2024-03-18 22:06:03 +03:00
CThreadFastMutex m_Mutex;
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
{
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();
2024-03-18 22:06:03 +03:00
DLL_CLASS_IMPORT void FreeAll();
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 );
// 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 );
};
#endif // STRINGPOOL_H