/[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 2505 - (show annotations) (download) (as text)
Sat Jan 11 21:47:57 2014 UTC (10 years, 3 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 5260 byte(s)
- Forgot to commit file with previous commit.

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

  ViewVC Help
Powered by ViewVC