/[svn]/linuxsampler/trunk/src/common/ResourceManager.h
ViewVC logotype

Annotation of /linuxsampler/trunk/src/common/ResourceManager.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 947 - (hide annotations) (download) (as text)
Mon Nov 27 21:34:55 2006 UTC (17 years, 5 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 21660 byte(s)
* implemented MIDI instrument mapping according to latest LSCP draft

1 schoenebeck 53 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 56 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 925 * Copyright (C) 2005, 2006 Christian Schoenebeck *
7 schoenebeck 53 * *
8     * This program is free software; you can redistribute it and/or modify *
9     * it under the terms of the GNU General Public License as published by *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13     * This program is distributed in the hope that it will be useful, *
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19     * along with this program; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24     #ifndef __RESOURCE_MANAGER__
25     #define __RESOURCE_MANAGER__
26    
27     #include <set>
28     #include <map>
29 schoenebeck 947 #include <vector>
30 schoenebeck 53
31 schoenebeck 947 #include "Exception.h"
32    
33     namespace LinuxSampler {
34    
35     /** @brief Interface class for Resource Consumers.
36     *
37 schoenebeck 53 * Interface class for consumer classes which use a resource managed
38 schoenebeck 947 * by the ResourceManager class. All classes which use the ResourceManager
39     * to aquire resources have to derive from this interface class and
40     * implement the abstract methods.
41 schoenebeck 53 */
42     template<class T_res>
43     class ResourceConsumer {
44     public:
45     /**
46     * Will be called by the ResourceManager to inform the
47     * consumer that a resource currently used by him is going
48     * to be updated. The consumer can then react by stopping
49     * usage until resource is updated. The ResourceManager will
50     * not update the resource until this method returns. This
51     * method needs to be implemented by the consumer.
52     *
53     * @param pResource - resource going to be updated
54     * @param pUpdateArg - pointer the consumer might use to store
55     * informations he might need when update
56     * process was completed
57     */
58     virtual void ResourceToBeUpdated(T_res* pResource, void*& pUpdateArg) = 0;
59    
60     /**
61     * Will be called by the ResourceManager to inform the
62     * consumer that resource update was completed. This method
63     * needs to be implemented by the consumer.
64     *
65     * @param pOldResource - (now invalid) pointer to the old
66     * resource
67     * @param pNewResource - (valid) pointer to the updated
68     * resource
69     * @param pUpdateArg - pointer the consumer might have used when
70     * ResourceToBeUpdated() was called
71     */
72     virtual void ResourceUpdated(T_res* pOldResource, T_res* pNewResource, void* pUpdateArg) = 0;
73 schoenebeck 517
74     /**
75     * Might be called by the ResourceManager periodically during an
76     * update / creation of a resource to inform the consumer about the
77     * current progress of that process. This method needs to be
78     * implemented by the consumer.
79     *
80     * @param fProgress - current progress as value between 0.0 and 1.0
81     */
82     virtual void OnResourceProgress(float fProgress) = 0;
83 schoenebeck 53 };
84    
85 schoenebeck 947 /** @brief Manager for sharing resources.
86     *
87 schoenebeck 53 * Abstract base class for sharing resources between multiple consumers.
88     * A consumer can borrow a resource from the ResourceManager, if the
89     * resource doesn't exist yet it will be created. Other consumers will
90     * just be given the same pointer to the resource then. When all consumers
91 schoenebeck 947 * gave back their pointer to the resource, the resource will (by default)
92     * be destroyed.
93     *
94 schoenebeck 53 * Descendants of this base class have to implement the (protected)
95     * Create() and Destroy() methods to create and destroy a resource.
96     */
97     template<class T_key, class T_res>
98     class ResourceManager {
99 schoenebeck 947 public:
100     /**
101     * Defines life-time strategy for resources.
102     */
103     enum mode_t {
104     ON_DEMAND = 0, ///< Create resource when needed, free it once not needed anymore (default behavior).
105     ON_DEMAND_HOLD = 1, ///< Create resource when needed and keep it even if not needed anymore.
106     PERSISTENT = 2 ///< Immediately create resource and keep it.
107     };
108    
109 schoenebeck 53 private:
110     typedef std::set<ResourceConsumer<T_res>*> ConsumerSet;
111     struct resource_entry_t {
112     T_key key;
113     T_res* resource; ///< pointer to the resource
114 schoenebeck 947 mode_t mode; ///< When should the resource be created? When should it be destroyed?
115 schoenebeck 53 ConsumerSet consumers; ///< list of all consumers who currently use the resource
116 schoenebeck 947 void* lifearg; ///< optional pointer the descendant might use to store informations about a created resource
117     void* entryarg; ///< optional pointer the descendant might use to store informations about an entry
118 schoenebeck 53 };
119     typedef std::map<T_key, resource_entry_t> ResourceMap;
120     ResourceMap ResourceEntries;
121    
122     public:
123     /**
124 schoenebeck 947 * Returns (the keys of) all current entries of this
125     * ResourceManager instance.
126     */
127     std::vector<T_key> Entries() {
128     std::vector<T_key> result;
129     for (typename ResourceMap::iterator iter = ResourceEntries.begin();
130     iter != ResourceEntries.end(); iter++)
131     {
132     result.push_back(iter->first);
133     }
134     return result;
135     }
136    
137     /**
138 schoenebeck 53 * Borrow a resource identified by \a Key. The ResourceManager will
139     * mark the resource as in usage by the consumer given with
140     * \a pConsumer. If the Resource doesn't exist yet it will be
141     * created.
142     *
143     * @param Key - resource identifier
144     * @param pConsumer - identifier of the consumer who borrows it
145     * @returns pointer to resource
146     */
147     T_res* Borrow(T_key Key, ResourceConsumer<T_res>* pConsumer) {
148 schoenebeck 947 // search for an entry for this resource
149 schoenebeck 53 typename ResourceMap::iterator iterEntry = ResourceEntries.find(Key);
150 schoenebeck 947 if (iterEntry == ResourceEntries.end()) { // entry doesn't exist yet
151 schoenebeck 517 // already create an entry for the resource
152 schoenebeck 53 resource_entry_t entry;
153     entry.key = Key;
154 schoenebeck 517 entry.resource = NULL;
155 schoenebeck 947 entry.mode = ON_DEMAND; // default mode
156     entry.lifearg = NULL;
157     entry.entryarg = NULL;
158 schoenebeck 517 entry.consumers.insert(pConsumer);
159     ResourceEntries[Key] = entry;
160 schoenebeck 925 try {
161     // actually create the resource
162 schoenebeck 947 entry.resource = Create(Key, pConsumer, entry.lifearg);
163 schoenebeck 925 } catch (...) {
164     // creating the resource failed, so remove the entry
165     ResourceEntries.erase(Key);
166     // rethrow the same exception
167     throw;
168     }
169 schoenebeck 517 // now update the entry with the created resource
170     ResourceEntries[Key] = entry;
171 schoenebeck 947 OnBorrow(entry.resource, pConsumer, entry.lifearg);
172 schoenebeck 53 return entry.resource;
173 schoenebeck 947 } else { // entry already exists
174     resource_entry_t& entry = iterEntry->second;
175     if (!entry.resource) { // create resource if not created already
176     try {
177     entry.resource = Create(Key, pConsumer, entry.lifearg);
178     } catch (...) {
179     entry.resource = NULL;
180     throw; // rethrow the same exception
181     }
182     }
183     entry.consumers.insert(pConsumer);
184     OnBorrow(entry.resource, pConsumer, entry.lifearg);
185     return entry.resource;
186 schoenebeck 53 }
187     }
188    
189     /**
190     * Give back a resource. This tells the ResourceManager that the
191     * consumer given by \a pConsumer doesn't need the resource anymore.
192 schoenebeck 947 * If the resource is not needed by any consumer anymore and the
193     * resource has a life-time strategy of ON_DEMAND (which is the
194     * default setting) then the resource will be destroyed.
195 schoenebeck 53 *
196     * @param pResource - pointer to resource
197     * @param pConsumer - identifier of the consumer who borrowed the
198     * resource
199     */
200     void HandBack(T_res* pResource, ResourceConsumer<T_res>* pConsumer) {
201 schoenebeck 947 // search for the entry associated with the given resource
202 schoenebeck 53 typename ResourceMap::iterator iter = ResourceEntries.begin();
203     typename ResourceMap::iterator end = ResourceEntries.end();
204     for (; iter != end; iter++) {
205 schoenebeck 947 if (iter->second.resource == pResource) { // found entry for resource
206 schoenebeck 53 resource_entry_t& entry = iter->second;
207     entry.consumers.erase(pConsumer);
208 schoenebeck 947 // remove entry if necessary
209     if (entry.mode == ON_DEMAND && !entry.entryarg && entry.consumers.empty()) {
210 persson 835 T_res* resource = entry.resource;
211 schoenebeck 947 void* arg = entry.lifearg;
212 schoenebeck 53 ResourceEntries.erase(iter);
213 schoenebeck 947 // destroy resource if necessary
214     if (resource) Destroy(resource, arg);
215 schoenebeck 53 }
216     return;
217     }
218     }
219     }
220    
221     /**
222 schoenebeck 947 * Request update of a resource. All consumers will be informed
223     * about the pending update of the resource so they can safely react
224     * by stopping its usage first, then the resource will be recreated
225     * and finally the consumers will be informed once the update was
226     * completed, so they can continue to use the resource.
227 schoenebeck 53 *
228     * @param pResource - resource to be updated
229     * @param pConsumer - consumer who requested the update
230     */
231     void Update(T_res* pResource, ResourceConsumer<T_res>* pConsumer) {
232     typename ResourceMap::iterator iter = ResourceEntries.begin();
233     typename ResourceMap::iterator end = ResourceEntries.end();
234     for (; iter != end; iter++) {
235 schoenebeck 351 if (iter->second.resource == pResource) {
236 schoenebeck 53 resource_entry_t& entry = iter->second;
237     // inform all consumers about pending update
238     std::map<ResourceConsumer<T_res>*,void*> updateargs;
239     typename ConsumerSet::iterator iterCons = entry.consumers.begin();
240     typename ConsumerSet::iterator endCons = entry.consumers.end();
241     for (; iterCons != endCons; iterCons++) {
242     if (*iterCons == pConsumer) continue;
243     void* updatearg = NULL;
244     (*iterCons)->ResourceToBeUpdated(entry.resource, updatearg);
245     if (updatearg) updateargs[*iterCons] = updatearg;
246     }
247     // update resource
248     T_res* pOldResource = entry.resource;
249 schoenebeck 947 Destroy(entry.resource, entry.lifearg);
250     entry.resource = Create(entry.key, pConsumer, entry.lifearg);
251 schoenebeck 53 // inform all consumers about update completed
252     iterCons = entry.consumers.begin();
253     endCons = entry.consumers.end();
254     for (; iterCons != endCons; iterCons++) {
255     if (*iterCons == pConsumer) continue;
256     typename std::map<ResourceConsumer<T_res>*,void*>::iterator iterArg = updateargs.find(*iterCons);
257     void* updatearg = (iterArg != updateargs.end()) ? iterArg->second : NULL;
258     (*iterCons)->ResourceUpdated(pOldResource, entry.resource, updatearg);
259     }
260     return;
261     }
262     }
263     }
264    
265 schoenebeck 947 /**
266     * Returns the life-time strategy of the given resource.
267     *
268     * @param Key - ID of the resource
269     */
270     mode_t AvailabilityMode(T_key Key) {
271     typename ResourceMap::iterator iterEntry = ResourceEntries.find(Key);
272     if (iterEntry == ResourceEntries.end())
273     return ON_DEMAND; // resource entry doesn't exist, so we return the default mode
274     resource_entry_t& entry = iterEntry->second;
275     return entry.mode;
276     }
277    
278     /**
279     * Change life-time strategy of the given resource. If a life-time
280     * strategy of PERSISTENT was given and the resource was not created
281     * yet, it will immediately be created and this method will block
282     * until the resource creation was completed.
283     *
284     * @param Key - ID of the resource
285     * @param Mode - life-time strategy of resource to be set
286     * @throws Exception in case an invalid Mode was given
287     */
288     void SetAvailabilityMode(T_key Key, mode_t Mode) throw (Exception) {
289     if (Mode != ON_DEMAND && Mode != ON_DEMAND_HOLD && Mode != PERSISTENT)
290     throw Exception("ResourceManager::SetAvailabilityMode(): invalid mode");
291    
292     // search for an entry for this resource
293     typename ResourceMap::iterator iterEntry = ResourceEntries.find(Key);
294     resource_entry_t* pEntry = NULL;
295     if (iterEntry == ResourceEntries.end()) { // resource entry doesn't exist
296     if (Mode == ON_DEMAND) return; // we don't create an entry for the default value
297     // create an entry for the resource
298     pEntry = &ResourceEntries[Key];
299     pEntry->key = Key;
300     pEntry->resource = NULL;
301     pEntry->mode = Mode;
302     pEntry->lifearg = NULL;
303     pEntry->entryarg = NULL;
304     } else { // resource entry exists
305     pEntry = &iterEntry->second;
306     // remove entry if necessary
307     if (Mode == ON_DEMAND && !pEntry->entryarg && pEntry->consumers.empty()) {
308     T_res* resource = pEntry->resource;
309     void* arg = pEntry->lifearg;
310     ResourceEntries.erase(iterEntry);
311     // destroy resource if necessary
312     if (resource) Destroy(resource, arg);
313     return; // done
314     }
315     pEntry->mode = Mode; // apply new mode
316     }
317    
318     // already create the resource if necessary
319     if (pEntry->mode == PERSISTENT && !pEntry->resource) {
320     try {
321     // actually create the resource
322     pEntry->resource = Create(Key, NULL /*no consumer yet*/, pEntry->lifearg);
323     } catch (...) {
324     // creating the resource failed, so skip it for now
325     pEntry->resource = NULL;
326     // rethrow the same exception
327     throw;
328     }
329     }
330     }
331    
332     /**
333     * Returns custom data sticked to the given resource previously by
334     * a SetCustomData() call.
335     *
336     * @param Key - ID of resource
337     */
338     void* CustomData(T_key Key) {
339     typename ResourceMap::iterator iterEntry = ResourceEntries.find(Key);
340     if (iterEntry == ResourceEntries.end()) return NULL; // resource entry doesn't exist, so we return the default mode
341     resource_entry_t& entry = iterEntry->second;
342     return entry.entryarg;
343     }
344    
345     /**
346     * This method can be used to stick custom data to an resource
347     * entry. In case the custom data is not needed anymore, you should
348     * call this method again and set \a pData to NULL, so the
349     * ResourceManager might safe space by removing the respective
350     * entry if not needed anymore.
351     *
352     * @param Key - ID of resource
353     * @param pData - pointer to custom data, or NULL if not needed anymore
354     */
355     void SetCustomData(T_key Key, void* pData) {
356     typename ResourceMap::iterator iterEntry = ResourceEntries.find(Key);
357     if (pData) {
358     if (iterEntry == ResourceEntries.end()) { // entry doesnt exist, so create one
359     resource_entry_t* pEntry = &ResourceEntries[Key];
360     pEntry->key = Key;
361     pEntry->resource = NULL;
362     pEntry->mode = ON_DEMAND;
363     pEntry->lifearg = NULL;
364     pEntry->entryarg = pData; // set custom data
365     } else { // entry exists, so just update its custom data
366     iterEntry->second.entryarg = pData;
367     }
368     } else { // !pData
369     if (iterEntry == ResourceEntries.end()) return; // entry doesnt exist, so nothing to do
370     // entry exists, remove it if necessary
371     resource_entry_t* pEntry = &iterEntry->second;
372     if (pEntry->mode == ON_DEMAND && pEntry->consumers.empty()) {
373     ResourceEntries.erase(iterEntry);
374     } else iterEntry->second.entryarg = NULL;
375     }
376     }
377    
378     virtual ~ResourceManager() {} // due to C++'s nature we cannot destroy created resources here
379    
380 schoenebeck 53 protected:
381     /**
382     * Has to be implemented by the descendant to create (allocate) a
383     * resource identified by \a Key.
384     *
385     * @param Key - identifier of the resource
386     * @param pConsumer - identifier of the consumer who borrows the
387     * resource
388     * @param pArg - pointer the descendant can use to store
389     * informations he might need for destruction of
390     * the resource
391     * @returns pointer to new resource
392     */
393     virtual T_res* Create(T_key Key, ResourceConsumer<T_res>* pConsumer, void*& pArg) = 0;
394    
395     /**
396     * Has to be implemented by the descendant to destroy (free) a
397     * resource pointed by \a pResource.
398     *
399     * @param pResource - pointer to the resource
400     * @param pArg - pointer the descendant might have used when
401     * Create() was called to store informations
402     * about the resource
403     */
404     virtual void Destroy(T_res* pResource, void* pArg) = 0;
405    
406     /**
407     * Has to be implemented by the descendant to react when a consumer
408     * borrows a resource (no matter if freshly created or an already
409     * created one). Of course reacting is optional, but the descendant
410     * at least has to provide a method with empty body.
411     *
412     * @param pResource - pointer to the resource
413     * @param pConsumer - identifier of the consumer who borrows the
414     * resource
415     * @param pArg - pointer the descendant might have used when
416     * Create() was called to store informations
417     * about the resource, this information can be
418     * updated by the descendant here
419     */
420     virtual void OnBorrow(T_res* pResource, ResourceConsumer<T_res>* pConsumer, void*& pArg) = 0;
421 schoenebeck 517
422     /**
423     * Dispatcher method which should be periodically called by the
424     * descendant during update or creation of the resource associated
425     * with \a Key. This method will inform all associated consumers
426     * of the given resource about the current progress.
427     *
428     * @param Key - unique identifier of the resource which is
429     * currently creating or updating
430     * @param fProgress - current progress of that creation / update
431     * process as value between 0.0 and 1.0
432     */
433     void DispatchResourceProgressEvent(T_key Key, float fProgress) {
434     typename ResourceMap::iterator iterEntry = ResourceEntries.find(Key);
435     if (iterEntry != ResourceEntries.end()) {
436     resource_entry_t& entry = iterEntry->second;
437     // inform all consumers of that resource about current progress
438     typename ConsumerSet::iterator iterCons = entry.consumers.begin();
439     typename ConsumerSet::iterator endCons = entry.consumers.end();
440     for (; iterCons != endCons; iterCons++) {
441     (*iterCons)->OnResourceProgress(fProgress);
442     }
443     }
444     }
445 schoenebeck 53 };
446    
447 schoenebeck 947 } // namespace LinuxSampler
448    
449 schoenebeck 53 #endif // __RESOURCE_MANAGER__

  ViewVC Help
Powered by ViewVC