3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005, 2006 Christian Schoenebeck * |
* Copyright (C) 2005 - 2007 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
36 |
|
|
37 |
class DeviceParameterFactory { |
class DeviceParameterFactory { |
38 |
public: |
public: |
39 |
|
typedef std::map<String,String> StringMap; // nasty workaround for a GCC bug (see GCC bug #15980, #57) |
40 |
|
|
41 |
class InnerFactory { |
class InnerFactory { |
42 |
public: |
public: |
43 |
virtual DeviceCreationParameter* Create() = 0; |
InnerFactory(DeviceParameterFactory* pParent) { this->pParent = pParent; } |
44 |
|
virtual DeviceCreationParameter* Create(std::map<String,String> Parameters = StringMap()) = 0; |
45 |
virtual DeviceCreationParameter* Create(String val) = 0; |
virtual DeviceCreationParameter* Create(String val) = 0; |
46 |
|
protected: |
47 |
|
DeviceParameterFactory* pParent; |
48 |
}; |
}; |
49 |
|
|
50 |
template <class Parameter_T> |
template <class Parameter_T> |
51 |
class InnerFactoryTemplate : public InnerFactory { |
class InnerFactoryTemplate : public InnerFactory { |
52 |
public: |
public: |
53 |
virtual DeviceCreationParameter* Create() { return new Parameter_T(); } |
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); } |
virtual DeviceCreationParameter* Create(String val) { return new Parameter_T(val); } |
87 |
}; |
}; |
88 |
|
|
89 |
template <class Parameter_T> |
template <class Parameter_T> |
90 |
static void Register(DeviceParameterFactory* factory) { |
static void Register(DeviceParameterFactory* factory) { |
91 |
factory->InnerFactories[Parameter_T::Name()] = new InnerFactoryTemplate<Parameter_T>; |
factory->InnerFactories[Parameter_T::Name()] = new InnerFactoryTemplate<Parameter_T>(factory); |
92 |
} |
} |
93 |
|
|
94 |
std::map<String,DeviceCreationParameter*> CreateAllParams ( std::map<String,String> Parameters ); |
std::map<String,DeviceCreationParameter*> CreateAllParams ( std::map<String,String> Parameters ); |
95 |
std::map<String,DeviceCreationParameter*> CreateAllParams (); |
std::map<String,DeviceCreationParameter*> CreateAllParams (); |
96 |
|
|
97 |
DeviceCreationParameter* Create(String ParameterName) throw (Exception); |
DeviceCreationParameter* Create(String ParameterName, std::map<String,String> Parameters = StringMap()) throw (Exception); |
98 |
DeviceCreationParameter* Create(String ParameterName, String val) throw (Exception); |
DeviceCreationParameter* Create(String ParameterName, String val) throw (Exception); |
99 |
|
|
100 |
protected: |
protected: |