forked from HeliumProject/Foundation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Name.h
145 lines (119 loc) · 3.86 KB
/
Name.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#pragma once
#include "Foundation/API.h"
#include "Platform/Trace.h"
#include "Platform/Locks.h"
#include "Foundation/String.h"
#include "Foundation/StringConverter.h"
#include "Foundation/HashFunctions.h"
namespace Helium
{
/// Null name constant.
enum ENullName
{
NULL_NAME
};
/// Base support for string table entry types.
template< typename TableType >
class NameBase
{
public:
/// Character type.
typedef typename TableType::CharType CharType;
/// Number of name hash table buckets (prime numbers are recommended).
static const size_t TABLE_BUCKET_COUNT = 37;
/// Name stack memory heap block size.
static const size_t STACK_HEAP_BLOCK_SIZE = sizeof( CharType ) * 8192;
/// Name hash table bucket.
class TableBucket
{
public:
/// @name Access
//@{
const CharType* Find( const CharType* pString, size_t& rEntryCount );
const CharType* Add( const CharType* pString, size_t previousEntryCount );
//@}
private:
/// Array of name pointers.
DynamicArray< const CharType* > m_entries;
/// Read-write lock for synchronizing access.
ReadWriteLock m_lock;
};
/// @name Construction/Destruction
//@{
NameBase();
NameBase( ENullName );
explicit NameBase( const CharType* pString );
explicit NameBase( const StringBase< CharType >& rString );
//@}
/// @name Name Access
//@{
const CharType* Get() const;
const CharType* GetDirect() const;
void Set( const CharType* pString );
void Set( const StringBase< CharType >& rString );
bool IsEmpty() const;
void Clear();
//@}
/// @name Overloaded Operators
//@{
const CharType* operator*() const;
bool operator<( const NameBase& rName ) const;
bool operator>( const NameBase& rName ) const;
bool operator<=( const NameBase& rName ) const;
bool operator>=( const NameBase& rName ) const;
bool operator==( const NameBase& rName ) const;
bool operator!=( const NameBase& rName ) const;
//@}
/// @name Static Initialization
//@{
static void Shutdown();
//@}
private:
/// Name entry.
const CharType* m_pEntry;
};
/// CharString name table.
class HELIUM_FOUNDATION_API CharNameTable
{
friend class NameBase< CharNameTable >;
public:
/// Character type.
typedef char CharType;
private:
/// Name hash table.
static NameBase< CharNameTable >::TableBucket* sm_pTable;
/// Stack-based memory heap for name entry allocations.
static StackMemoryHeap<>* sm_pNameMemoryHeap;
/// Empty name string.
static char sm_emptyString[ 1 ];
};
/// CharString name table entry.
typedef NameBase< CharNameTable > CharName;
/// WideString name table.
class HELIUM_FOUNDATION_API WideNameTable
{
friend class NameBase< WideNameTable >;
public:
/// Character type.
typedef wchar_t CharType;
private:
/// Name hash table.
static NameBase< WideNameTable >::TableBucket* sm_pTable;
/// Stack-based memory heap for name entry allocations.
static StackMemoryHeap<>* sm_pNameMemoryHeap;
/// Empty name string.
static wchar_t sm_emptyString[ 1 ];
};
/// WideString name table entry.
typedef NameBase< WideNameTable > WideName;
/// String table entry.
typedef CharName Name;
/// Default Name hash.
template< typename TableType >
class Hash< NameBase< TableType > >
{
public:
inline size_t operator()( const NameBase< TableType >& rKey ) const;
};
}
#include "Foundation/Name.inl"