Page MenuHomec4science

lm_parameter.hh
No OneTemporary

File Metadata

Created
Mon, Oct 21, 00:17

lm_parameter.hh

/**
* @file lm_parameter.hh
*
* @author Guillaume Anciaux <guillaume.anciaux@epfl.ch>
*
* @date Mon Nov 25 17:17:19 2013
*
* @brief Encapsulate a parameter for the parsing system
*
* @section LICENSE
*
* Copyright (©) 2010-2011 EPFL (Ecole Polytechnique Fédérale de Lausanne)
* Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides)
*
* LibMultiScale is free software: you can redistribute it and/or modify it
* under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* LibMultiScale is distributed in the hope that it will be useful, but
* WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with LibMultiScale. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef __LIBMULTISCALE_LM_PARAMETERS_HH__
#define __LIBMULTISCALE_LM_PARAMETERS_HH__
/* -------------------------------------------------------------------------- */
#include "lm_parser.hh"
#include <typeinfo>
/* -------------------------------------------------------------------------- */
__BEGIN_LIBMULTISCALE__
/* -------------------------------------------------------------------------- */
class Parsable::Parameter {
public:
Parameter() { this->is_set = false; };
virtual ~Parameter(){};
template <typename T> ParameterTyped<T> &getParameterTyped() {
try {
ParameterTyped<T> &tmp = dynamic_cast<ParameterTyped<T> &>(*this);
return tmp;
} catch (...) {
LM_FATAL("The parameter named "
<< name << " is of type " << typeid(*this).name()
<< " and not of type " << typeid(T).name());
}
};
virtual Parameter *createCopy() { LM_TOIMPLEMENT; };
virtual ParseResult setParam(const std::string &variable,
std::stringstream &line) = 0;
template <typename T>
void setParam(const std::string &variable, const T &value);
template <typename T> void getParam(const std::string &variable, T &value);
template <typename T>
void changeDefault(const std::string &variable, const T &value);
bool isSet() { return is_set; };
protected:
std::string name;
bool is_set;
};
/* -------------------------------------------------------------------------- */
template <typename T>
void Parsable::Parameter::setParam(const std::string &variable,
const T &value) {
this->getParameterTyped<T>().setParam(variable, value);
}
/* -------------------------------------------------------------------------- */
template <typename T>
void Parsable::Parameter::getParam(const std::string &variable, T &value) {
this->getParameterTyped<T>().getParam(variable, value);
}
/* -------------------------------------------------------------------------- */
template <typename T>
void Parsable::Parameter::changeDefault(const std::string &variable,
const T &value) {
this->getParameterTyped<T>().changeDefault(variable, value);
}
/* -------------------------------------------------------------------------- */
template <typename T> class ParameterTypedScalar : public Parsable::Parameter {
public:
ParameterTypedScalar(const std::string n, T &v, const default_val<T> def)
: values(v) {
this->name = n;
this->is_set = def.setDefault(this->values);
DUMPFILE(Parser::fout, "declare " << n << " 1 x " << typeid(T).name());
};
virtual ~ParameterTypedScalar(){};
virtual void setParam(const std::string &variable, const T &value) {
this->values = value;
this->is_set = true;
}
virtual void getParam(const std::string &variable, T &value) {
value = values;
}
virtual void changeDefault(const std::string &variable, const T &value) {
this->values = value;
}
virtual ParseResult setParam(const std::string &variable,
std::stringstream &line) {
LM_ASSERT(variable == name, "internal keyword problem");
UInt word_count = 0;
word_count += Parser::parse(values, line);
this->is_set = true;
return ParseResult(true, word_count);
};
virtual Parameter *createCopy() { LM_TOIMPLEMENT; };
protected:
T &values;
};
/* -------------------------------------------------------------------------- */
template <typename T>
class Parsable::ParameterTyped : public ParameterTypedScalar<T> {
public:
ParameterTyped(const std::string n, T &v, const default_val<T> def)
: ParameterTypedScalar<T>(n, v, def){};
virtual ParseResult setParam(const std::string &variable,
std::stringstream &line) {
ParseResult res = ParameterTypedScalar<T>::setParam(variable, line);
DUMPFILE(Parser::fout, "parsing " << variable << " with value "
<< this->values);
return res;
};
virtual void setParam(const std::string &variable, const T &value) {
ParameterTypedScalar<T>::setParam(variable, value);
}
};
/* -------------------------------------------------------------------------- */
template <>
class Parsable::ParameterTyped<bool> : public ParameterTypedScalar<bool> {
public:
ParameterTyped(const std::string n, bool &v, const default_val<bool> def)
: ParameterTypedScalar<bool>(n, v, def){};
virtual ParseResult setParam(const std::string &variable,
std::stringstream &line) {
if (this->values)
this->values = false;
else
this->values = true;
DUMPFILE(Parser::fout, "switch " << variable << " to " << this->values);
return ParseResult(true, 0);
};
virtual void setParam(const std::string &variable, const bool &value) {
ParameterTypedScalar<bool>::setParam(variable, value);
}
};
/* -------------------------------------------------------------------------- */
template <typename T, typename Tscalar, UInt nb>
class ParameterTypedStaticVector : public Parsable::Parameter {
public:
ParameterTypedStaticVector(const std::string n, T &v,
const default_val<T> def)
: values(v) {
this->name = n;
this->is_set = def.setDefault(values);
DUMPFILE(Parser::fout, "declare " << n << " " << nb << " x "
<< typeid(Tscalar).name()
<< " within a type " << typeid(T).name());
};
virtual ~ParameterTypedStaticVector(){};
virtual void setParam(const std::string &variable, const T &values) {
for (UInt i = 0; i < nb; ++i) {
this->values[i] = values[i];
}
this->is_set = true;
}
virtual void getParam(const std::string &variable, T &value) {
for (UInt i = 0; i < nb; ++i)
value[i] = values[i];
}
virtual void changeDefault(const std::string &variable, const T &values) {
for (UInt i = 0; i < nb; ++i) {
this->values[i] = values[i];
}
}
virtual ParseResult setParam(const std::string &variable,
std::stringstream &line) {
LM_ASSERT(variable == name, "internal keyword problem");
UInt word_count = 0;
word_count += Parser::parse(values, line);
for (UInt i = 0; i < nb; ++i)
DUMPFILE(Parser::fout, "parsing " << variable << " with value "
<< values);
this->is_set = true;
return ParseResult(true, word_count);
};
protected:
T &values;
};
/* -------------------------------------------------------------------------- */
template <typename T, UInt nb>
class Parsable::ParameterTyped<T[nb]>
: public ParameterTypedStaticVector<T[nb], T, nb> {
public:
ParameterTyped(const std::string n, T (&v)[nb], const default_val<T[nb]> def)
: ParameterTypedStaticVector<T[nb], T, nb>(n, v, def){};
protected:
virtual Parameter *createCopy() {
return new Parsable::ParameterTyped<T[nb]>(*this);
};
};
/* -------------------------------------------------------------------------- */
template <PhysicalQuantity q, UInt nb, typename T>
class Parsable::ParameterTyped<Quantity<q, nb, T>>
: public ParameterTypedStaticVector<Quantity<q, nb, T>, T, nb> {
public:
ParameterTyped(const std::string n, Quantity<q, nb, T> &v,
const default_val<Quantity<q, nb, T>> def)
: ParameterTypedStaticVector<Quantity<q, nb, T>, T, nb>(n, v, def){};
virtual Parameter *createCopy() {
return new Parsable::ParameterTyped<Quantity<q, nb, T>>(*this);
};
};
/* -------------------------------------------------------------------------- */
template <typename T>
class Parsable::ParameterTyped<std::vector<T>>
: public ParameterTypedScalar<std::vector<T>> {
public:
ParameterTyped(const std::string n, std::vector<T> &v,
const default_val<std::vector<T>> def)
: ParameterTypedScalar<std::vector<T>>(n, v, def){};
virtual ParseResult setParam(const std::string &variable,
std::stringstream &line) {
LM_ASSERT(variable == this->name, "internal keyword problem");
T val;
UInt word_count = 0;
word_count += Parser::parse(val, line);
this->values.push_back(val);
DUMPFILE(Parser::fout, "parsing " << variable << " with value " << val);
this->is_set = true;
return ParseResult(true, word_count);
};
// virtual Parameter * createCopy(){return new
// Parsable::ParameterTyped<std::vector<T> >(*this);};
virtual Parameter *createCopy() { LM_TOIMPLEMENT; };
};
/* -------------------------------------------------------------------------- */
template <typename T1, typename T2>
class Parsable::ParameterTyped<std::map<T1, T2>>
: public ParameterTypedScalar<std::map<T1, T2>> {
public:
ParameterTyped(const std::string n, std::map<T1, T2> &v,
const default_val<std::map<T1, T2>> def)
: ParameterTypedScalar<std::map<T1, T2>>(n, v, def){};
virtual ParseResult setParam(const std::string &variable,
std::stringstream &line) {
LM_ASSERT(variable == this->name, "internal keyword problem");
T1 val1;
T2 val2;
UInt word_count = 0;
word_count += Parser::parse(val1, line);
word_count += Parser::parse(val2, line);
this->values[val1] = val2;
DUMPFILE(Parser::fout, "parsing " << variable << " with value " << val1
<< " -> " << val2);
this->is_set = true;
return ParseResult(true, word_count);
};
virtual Parameter *createCopy() { LM_TOIMPLEMENT; };
// virtual Parameter * createCopy(){return new
// Parsable::ParameterTyped<std::map<T1,T2> >(*this);};
};
/* -------------------------------------------------------------------------- */
__END_LIBMULTISCALE__
#endif /* __LIBMULTISCALE_LM_PARAMETERS_HH__ */

Event Timeline