Make duplicate script compatible with Python 3
[wdl/wdl-ol.git] / WDL / stringpool.h
blob31c8c170fa63045eecd63afc392b357f293b9a4a
1 #ifndef _WDL_STRINGPOOL_H_
2 #define _WDL_STRINGPOOL_H_
5 #include "wdlstring.h"
6 #include "assocarray.h"
7 #include "mutex.h"
9 class WDL_StringPool
11 friend class WDL_PooledString;
12 public:
13 WDL_StringPool(bool wantMutex) : m_strings(true) { m_mutex = wantMutex ? new WDL_Mutex : NULL; };
14 ~WDL_StringPool() { delete m_mutex; }
16 private:
17 WDL_StringKeyedArray<int> m_strings;
18 protected:
19 WDL_Mutex *m_mutex;
23 class WDL_PooledString
25 public:
26 WDL_PooledString(WDL_StringPool *pool, const char *value=NULL) { m_pool = pool; m_val = ""; Set(value); }
27 ~WDL_PooledString() { Set(""); }
29 const char *Get() { return m_val; }
31 void Set(const char *value)
33 if (!value) value="";
34 if (strcmp(value,m_val))
36 WDL_MutexLock(m_pool->m_mutex); // may or may not actually be a mutex
38 const char *oldval = m_val;
40 if (*value)
42 // set to new value
43 const char *keyptr=NULL;
44 int * ref = m_pool->m_strings.GetPtr(value,&keyptr);
45 if (ref)
47 ++*ref;
48 m_val=keyptr;
50 else m_pool->m_strings.Insert(value,1,&m_val);
52 else m_val="";
54 if (oldval[0])
56 int *oldref = m_pool->m_strings.GetPtr(oldval);
57 if (oldref && --*oldref<=0) m_pool->m_strings.Delete(oldval);
62 // utility for compat with WDL_String
63 void Append(const char *value)
65 if (value&&*value)
67 WDL_String tmp(Get());
68 tmp.Append(value);
69 Set(tmp.Get());
72 void Insert(const char *value, int pos)
74 WDL_String tmp(Get());
75 tmp.Insert(value,pos);
76 Set(tmp.Get());
78 void DeleteSub(int pos, int len)
80 WDL_String tmp(Get());
81 tmp.DeleteSub(pos,len);
82 Set(tmp.Get());
85 private:
86 const char *m_val;
87 WDL_StringPool *m_pool;
92 #endif _WDL_STRINGPOOL_H_