update plists MACOSX_DEPLOYMENT_TARGET = 10.7.0
[wdl/wdl-ol.git] / WDL / poollist.h
bloba53a64043ffb57b043ab25995eceb590ae215900
1 /*
2 WDL - poollist.h
3 Copyright (C) 2006 and later, Cockos Incorporated
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
23 This file defines a template class for hosting lists of referenced count, string-identified objects.
25 We mostly use it with WDL_ResourcePool, but any class like this can use it:
28 class SomeClass
30 public:
31 SomeClass(char *identstr) { WDL_POOLLIST_identstr=identstr; WDL_POOLLIST_refcnt=0; }
32 ~SomeClass() {} // do NOT free or delete WDL_POOLLIST_identstr
35 void Clear() {} // will be called if ReleasePool(x,false) is called and refcnt gets to 0
36 int WDL_POOLLIST_refcnt;
37 char *WDL_POOLLIST_identstr;
46 #ifndef _WDL_POOLLIST_H_
47 #define _WDL_POOLLIST_H_
49 #include <stdlib.h>
51 #include "mutex.h"
53 template<class DATATYPE> class WDL_PoolList_NoFreeOnDestroy
55 public:
57 WDL_PoolList_NoFreeOnDestroy()
60 ~WDL_PoolList_NoFreeOnDestroy()
64 DATATYPE *Get(const char *filename, bool createIfExists=true)
66 WDL_MutexLock lock(&mutex);
68 DATATYPE *t = Find(filename,false);
69 if (t)
71 t->WDL_POOLLIST_refcnt++;
72 return t;
74 if (!createIfExists) return NULL;
76 t = new DATATYPE(strdup(filename));
77 t->WDL_POOLLIST_refcnt=1;
79 int x;
80 for(x=0;x<pool.GetSize();x++) if (stricmp(pool.Get(x)->WDL_POOLLIST_identstr,filename)>0) break;
82 pool.Insert(x,t);
84 return t;
87 DATATYPE *Find(const char *filename, bool lockMutex=true) // not threadsafe
89 if (lockMutex) mutex.Enter();
90 DATATYPE **_tmp=NULL;
91 if (pool.GetSize())
93 DATATYPE tmp((char *)filename),*t=&tmp;
94 _tmp = (DATATYPE**)bsearch(&t,pool.GetList(),pool.GetSize(),sizeof(void *),_sortfunc);
96 if (lockMutex) mutex.Leave();
97 return _tmp ? *_tmp : NULL;
100 int ReleaseByName(const char *filename, bool isFull=true)
102 WDL_MutexLock lock(&mutex);
103 return Release(Find(filename,false),isFull);
106 int Release(DATATYPE *tp, bool isFull=true)
108 if (!tp) return -1;
109 WDL_MutexLock lock(&mutex);
111 int refcnt;
112 if (!(refcnt=--tp->WDL_POOLLIST_refcnt))
114 if (!isFull)
116 tp->Clear();
118 else
120 int x;
121 for (x = 0; x < pool.GetSize() && pool.Get(x) != tp; x ++);
122 if (x<pool.GetSize())
124 pool.Delete(x);
126 free(tp->WDL_POOLLIST_identstr);
127 delete tp;
129 // remove from list
131 return refcnt;
134 void RemoveAll()
136 int x;
137 for (x = 0; x < pool.GetSize(); x ++)
139 DATATYPE *p = pool.Get(x);
140 free(p->WDL_POOLLIST_identstr);
141 delete p;
143 pool.Empty();
146 WDL_Mutex mutex;
147 WDL_PtrList< DATATYPE > pool;
149 private:
151 static int _sortfunc(const void *a, const void *b)
153 DATATYPE *ta = *(DATATYPE **)a;
154 DATATYPE *tb = *(DATATYPE **)b;
156 return stricmp(ta->WDL_POOLLIST_identstr,tb->WDL_POOLLIST_identstr);
160 template<class DATATYPE> class WDL_PoolList : public WDL_PoolList_NoFreeOnDestroy<DATATYPE>
162 public:
163 WDL_PoolList() { }
164 ~WDL_PoolList() { WDL_PoolList_NoFreeOnDestroy<DATATYPE>::RemoveAll(); }
169 #endif