11 #define DECLARE_SNIPPET(TYPE, NUM_PARAMS) \ 13 static TYPE *s_Instance; \ 15 static const TYPE *getInstance() \ 17 if(s_Instance == NULL) \ 19 s_Instance = new TYPE; \ 23 typedef Snippet::ValueBase<NUM_PARAMS> ParamValues; \ 26 #define IMPLEMENT_SNIPPET(TYPE) TYPE *TYPE::s_Instance = NULL 28 #define SET_PARAM_NAMES(...) virtual StringVec getParamNames() const override{ return __VA_ARGS__; } 29 #define SET_DERIVED_PARAMS(...) virtual DerivedParamVec getDerivedParams() const override{ return __VA_ARGS__; } 38 template<
size_t NumVars>
44 template<
typename... T>
45 ValueBase(T&&... vals) : m_Values(
std::vector<double>{{std::forward<const double>(vals)...}})
47 static_assert(
sizeof...(vals) == NumVars,
"Wrong number of values");
71 std::vector<double> m_Values;
85 template<
typename... T>
88 static_assert(
sizeof...(vals) == 0,
"Wrong number of values");
118 typedef std::vector<std::pair<std::string, std::pair<std::string, double>>>
NameTypeValVec;
138 template<
typename SnippetBase>
142 Init(
const SnippetBase *snippet,
const std::vector<double> ¶ms)
143 : m_Snippet(snippet), m_Params(params)
151 const std::vector<double> &
getParams()
const{
return m_Params; }
156 auto derivedParams = m_Snippet->getDerivedParams();
159 m_DerivedParams.reserve(derivedParams.size());
162 for(
const auto &d : derivedParams) {
163 m_DerivedParams.push_back(d.second(m_Params, dt));
171 const SnippetBase *m_Snippet;
172 std::vector<double> m_Params;
173 std::vector<double> m_DerivedParams;
std::vector< std::pair< std::string, DerivedParamFunc > > DerivedParamVec
Definition: snippet.h:119
Definition: snippet.h:139
std::vector< std::pair< std::string, std::pair< std::string, double > > > NameTypeValVec
Definition: snippet.h:118
Init(const SnippetBase *snippet, const std::vector< double > ¶ms)
Definition: snippet.h:142
ValueBase(T &&... vals)
Definition: snippet.h:86
std::vector< double > getValues() const
Gets values as a vector of doubles.
Definition: snippet.h:95
std::function< double(const std::vector< double > &, double)> DerivedParamFunc
Definition: snippet.h:115
ValueBase(T &&... vals)
Definition: snippet.h:45
void initDerivedParams(double dt)
Definition: snippet.h:154
virtual ~Base()
Definition: snippet.h:108
double operator[](size_t pos) const
Definition: snippet.h:62
virtual StringVec getParamNames() const
Gets names of of (independent) model parameters.
Definition: snippet.h:125
const SnippetBase * getSnippet() const
Definition: snippet.h:150
const std::vector< double > & getParams() const
Definition: snippet.h:151
std::vector< std::string > StringVec
Definition: snippet.h:116
Base class for all code snippets.
Definition: snippet.h:105
const std::vector< double > & getValues() const
Gets values as a vector of doubles.
Definition: snippet.h:54
std::vector< std::pair< std::string, std::string > > StringPairVec
Definition: snippet.h:117
virtual DerivedParamVec getDerivedParams() const
Definition: snippet.h:129
const std::vector< double > & getDerivedParams() const
Definition: snippet.h:152