/[svn]/linuxsampler/trunk/src/engines/EngineChannelFactory.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/engines/EngineChannelFactory.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2427 - (hide annotations) (download)
Sat Mar 2 07:03:04 2013 UTC (11 years ago) by persson
File size: 6077 byte(s)
* code refactoring: added a lock guard class for exception safe mutex
  handling and used it everywhere appropriate

1 schoenebeck 411 /***************************************************************************
2     * *
3 persson 2427 * Copyright (C) 2005 - 2013 Christian Schoenebeck *
4 schoenebeck 411 * *
5     * This program is free software; you can redistribute it and/or modify *
6     * it under the terms of the GNU General Public License as published by *
7     * the Free Software Foundation; either version 2 of the License, or *
8     * (at your option) any later version. *
9     * *
10     * This program is distributed in the hope that it will be useful, *
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13     * GNU General Public License for more details. *
14     * *
15     * You should have received a copy of the GNU General Public License *
16     * along with this program; if not, write to the Free Software *
17     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
18     * MA 02111-1307 USA *
19     ***************************************************************************/
20    
21     #include "EngineChannelFactory.h"
22    
23     #include <strings.h>
24    
25     #include "gig/EngineChannel.h"
26    
27 iliev 2012 #if HAVE_SF2
28     #include "sf2/EngineChannel.h"
29     #endif
30    
31     #include "sfz/EngineChannel.h"
32    
33 schoenebeck 411 namespace LinuxSampler {
34 iliev 1826 class LockedChannel {
35     public:
36     const EngineChannel* pChannel;
37     bool bDestroyed;
38 schoenebeck 411
39 iliev 1826 LockedChannel(const EngineChannel* pChannel) {
40     this->pChannel = pChannel;
41     bDestroyed = false;
42     }
43     };
44    
45     class LockedChannelList {
46     public:
47     void Add(const EngineChannel* pChannel) {
48     vChannelList.push_back(LockedChannel(pChannel));
49     }
50    
51     bool IsDestroyed(const EngineChannel* pChannel) {
52     LockedChannel* pLockedChannel = get(pChannel);
53     if (pLockedChannel == NULL) return false;
54     return pLockedChannel->bDestroyed;
55     }
56    
57     void SetDestroyed(const EngineChannel* pChannel, bool bDestroyed = true) {
58     LockedChannel* pLockedChannel = get(pChannel);
59     if (pLockedChannel == NULL) return;
60     pLockedChannel->bDestroyed = bDestroyed;
61     }
62    
63     void Remove(const EngineChannel* pChannel) {
64     std::vector<LockedChannel>::iterator it = vChannelList.begin();
65     for (; it != vChannelList.end(); it++) {
66     if ((*it).pChannel == pChannel) {
67     vChannelList.erase(it);
68     return;
69     }
70     }
71     }
72    
73     bool Contains(const EngineChannel* pChannel) {
74     return get(pChannel) != NULL;
75     }
76    
77     private:
78     std::vector<LockedChannel> vChannelList;
79    
80     LockedChannel* get(const EngineChannel* pChannel) {
81     for (int i = 0; i < vChannelList.size(); i++) {
82     if (vChannelList[i].pChannel == pChannel) {
83     return &vChannelList[i];
84     }
85     }
86    
87     return NULL;
88     }
89     } lockedChannels;
90    
91     Mutex EngineChannelFactory::LockedChannelsMutex;
92 persson 2324 Mutex EngineChannelFactory::EngineChannelsMutex;
93 iliev 1826
94     void EngineChannelFactory::SetDeleteEnabled(const EngineChannel* pEngineChannel, bool enable) {
95     if (!enable) {
96 persson 2427 LockGuard lock(LockedChannelsMutex);
97 iliev 1826 if (!lockedChannels.Contains(pEngineChannel)) lockedChannels.Add(pEngineChannel);
98     } else {
99 persson 2427 bool b;
100     {
101     LockGuard lock(LockedChannelsMutex);
102     b = lockedChannels.IsDestroyed(pEngineChannel);
103     lockedChannels.Remove(pEngineChannel);
104     }
105 iliev 1826 if (b) delete pEngineChannel;
106     }
107     }
108    
109 schoenebeck 660 // all currently existing engine channel instances
110     static std::set<LinuxSampler::EngineChannel*> engineChannels;
111    
112 schoenebeck 880 LinuxSampler::EngineChannel* EngineChannelFactory::Create(String EngineType) throw (Exception) {
113 persson 2324 LinuxSampler::EngineChannel* pEngineChannel;
114 schoenebeck 411 if (!strcasecmp(EngineType.c_str(),"GigEngine") || !strcasecmp(EngineType.c_str(),"gig")) {
115 persson 2324 pEngineChannel = new gig::EngineChannel;
116 iliev 2012 } else if (!strcasecmp(EngineType.c_str(),"sf2")) {
117     #if HAVE_SF2
118 persson 2324 pEngineChannel = new sf2::EngineChannel;
119 iliev 2012 #else
120     throw Exception("LinuxSampler is not compiled with SF2 support");
121     #endif
122     } else if (!strcasecmp(EngineType.c_str(),"sfz")) {
123 persson 2324 pEngineChannel = new sfz::EngineChannel;
124     } else {
125     throw Exception("Unknown engine type");
126 schoenebeck 411 }
127 persson 2427 LockGuard lock(EngineChannelsMutex);
128 persson 2324 engineChannels.insert(pEngineChannel);
129     return pEngineChannel;
130 schoenebeck 411 }
131    
132 schoenebeck 660 void EngineChannelFactory::Destroy(LinuxSampler::EngineChannel* pEngineChannel) {
133 iliev 1130 pEngineChannel->RemoveAllFxSendCountListeners();
134 persson 2427 {
135     LockGuard lock(EngineChannelsMutex);
136     engineChannels.erase(pEngineChannel);
137     }
138 iliev 1826
139     // Postpone the deletion of the specified EngineChannel if needed (bug #113)
140 persson 2427 {
141     LockGuard lock(LockedChannelsMutex);
142     if (lockedChannels.Contains(pEngineChannel)) {
143     lockedChannels.SetDestroyed(pEngineChannel);
144     pEngineChannel->SetSamplerChannel(NULL);
145     return;
146     }
147 iliev 1826 }
148     ///////
149    
150 persson 1248 delete pEngineChannel;
151 schoenebeck 660 }
152    
153 persson 840 const std::set<LinuxSampler::EngineChannel*>& EngineChannelFactory::EngineChannelInstances() {
154 schoenebeck 660 return engineChannels;
155     }
156    
157 persson 840 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC