/[svn]/linuxsampler/trunk/src/drivers/DeviceParameterFactory.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/drivers/DeviceParameterFactory.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1248 - (show annotations) (download) (as text)
Fri Jun 22 10:10:06 2007 UTC (12 years, 11 months ago) by persson
File MIME type: text/x-c++hdr
File size: 5595 byte(s)
* fixed some minor memory leaks

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * Copyright (C) 2005 - 2007 Christian Schoenebeck *
7 * *
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 __LS_DEVICE_PARAMETER_FACTORY_H__
25 #define __LS_DEVICE_PARAMETER_FACTORY_H__
26
27 #include <map>
28 #include <vector>
29
30 #include "../common/global.h"
31 #include "../common/optional.h"
32 #include "../common/Exception.h"
33 #include "DeviceParameter.h"
34
35 namespace LinuxSampler {
36
37 class DeviceParameterFactory {
38 public:
39 typedef std::map<String,String> StringMap; // nasty workaround for a GCC bug (see GCC bug #15980, #57)
40
41 class InnerFactory {
42 public:
43 InnerFactory(DeviceParameterFactory* pParent) { this->pParent = pParent; }
44 virtual DeviceCreationParameter* Create(std::map<String,String> Parameters = StringMap()) = 0;
45 virtual DeviceCreationParameter* Create(String val) = 0;
46 protected:
47 DeviceParameterFactory* pParent;
48 };
49
50 template <class Parameter_T>
51 class InnerFactoryTemplate : public InnerFactory {
52 public:
53 InnerFactoryTemplate(DeviceParameterFactory* pParent) : InnerFactory(pParent) {}
54
55 /// Create parameter with respective value in map or use its default value.
56 virtual DeviceCreationParameter* Create(std::map<String,String> Parameters = StringMap()) {
57 const String paramName = Parameter_T::Name();
58 if (Parameters.count(paramName)) {
59 // parameter with this name was specified, so use that given value
60 return new Parameter_T(Parameters[paramName]);
61 }
62 // parameter value not given, use its default value ...
63 // ... but first resolve its dependencies to other parameters
64 Parameter_T param;
65 std::map<String,DeviceCreationParameter*> dependencies = param.DependsAsParameters();
66 std::map<String,String> dependencysParams;
67 for (std::map<String,DeviceCreationParameter*>::iterator iter = dependencies.begin(); iter != dependencies.end(); iter++) {
68 if (Parameters.count(iter->first)) {
69 // value for this dependency parameter already given
70 dependencysParams[iter->first] = Parameters[iter->first];
71 } else {
72 // no value provided for this dependency parameter, use its default value
73 // (FIXME: no sanity check for cyclic dependencies here yet)
74 DeviceCreationParameter* pDependencyParam = pParent->Create(iter->first, Parameters);
75 if (pDependencyParam) {
76 dependencysParams[iter->first] = pDependencyParam->Value();
77 delete pDependencyParam;
78 }
79 }
80 }
81 // now that we resolved all dependencies, we can finally determine parameter's default value
82 optional<String> defaultValue = param.Default(dependencysParams);
83 return (defaultValue) ? new Parameter_T(*defaultValue) : new Parameter_T();
84 }
85 /// Create parameter with given value.
86 virtual DeviceCreationParameter* Create(String val) { return new Parameter_T(val); }
87 };
88
89 template <class Parameter_T>
90 static void Register(DeviceParameterFactory* factory) {
91 factory->InnerFactories[Parameter_T::Name()] = new InnerFactoryTemplate<Parameter_T>(factory);
92 }
93
94 template <class Parameter_T>
95 static void Unregister(DeviceParameterFactory* factory) {
96 std::map<String, InnerFactory*>::iterator iter = factory->InnerFactories.find(Parameter_T::Name());
97 delete iter->second;
98 factory->InnerFactories.erase(Parameter_T::Name());
99 }
100
101 std::map<String,DeviceCreationParameter*> CreateAllParams ( std::map<String,String> Parameters );
102 std::map<String,DeviceCreationParameter*> CreateAllParams ();
103
104 DeviceCreationParameter* Create(String ParameterName, std::map<String,String> Parameters = StringMap()) throw (Exception);
105 DeviceCreationParameter* Create(String ParameterName, String val) throw (Exception);
106
107 protected:
108 std::map<String, InnerFactory*> InnerFactories;
109 };
110
111 } // namespace LinuxSampler
112
113 #endif // __LS_DEVICE_PARAMETER_FACTORY_H__

  ViewVC Help
Powered by ViewVC