-
-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathstringpool.h
More file actions
92 lines (76 loc) · 1.8 KB
/
stringpool.h
File metadata and controls
92 lines (76 loc) · 1.8 KB
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
#ifndef _WDL_STRINGPOOL_H_
#define _WDL_STRINGPOOL_H_
#include "wdlstring.h"
#include "assocarray.h"
#include "mutex.h"
class WDL_StringPool
{
friend class WDL_PooledString;
public:
WDL_StringPool(bool wantMutex) : m_strings(true) { m_mutex = wantMutex ? new WDL_Mutex : NULL; };
~WDL_StringPool() { delete m_mutex; }
private:
WDL_StringKeyedArray<int> m_strings;
protected:
WDL_Mutex *m_mutex;
};
class WDL_PooledString
{
public:
WDL_PooledString(WDL_StringPool *pool, const char *value=NULL) { m_pool = pool; m_val = ""; Set(value); }
~WDL_PooledString() { Set(""); }
const char *Get() { return m_val; }
void Set(const char *value)
{
if (!value) value="";
if (strcmp(value,m_val))
{
WDL_MutexLock(m_pool->m_mutex); // may or may not actually be a mutex
const char *oldval = m_val;
if (*value)
{
// set to new value
const char *keyptr=NULL;
int * ref = m_pool->m_strings.GetPtr(value,&keyptr);
if (ref)
{
++*ref;
m_val=keyptr;
}
else m_pool->m_strings.Insert(value,1,&m_val);
}
else m_val="";
if (oldval[0])
{
int *oldref = m_pool->m_strings.GetPtr(oldval);
if (oldref && --*oldref<=0) m_pool->m_strings.Delete(oldval);
}
}
}
// utility for compat with WDL_String
void Append(const char *value)
{
if (value&&*value)
{
WDL_String tmp(Get());
tmp.Append(value);
Set(tmp.Get());
}
}
void Insert(const char *value, int pos)
{
WDL_String tmp(Get());
tmp.Insert(value,pos);
Set(tmp.Get());
}
void DeleteSub(int pos, int len)
{
WDL_String tmp(Get());
tmp.DeleteSub(pos,len);
Set(tmp.Get());
}
private:
const char *m_val;
WDL_StringPool *m_pool;
};
#endif //_WDL_STRINGPOOL_H_