/** * @file parsable_tmpl.hh * * @author Nicolas Richart * * @date Mon Aug 5 15:27:35 2013 * * @brief implementation of the templated part of ParsableParam Parsable and * ParsableParamTyped * * @section LICENSE * * Copyright (©) 2010-2011 EPFL (Ecole Polytechnique Fédérale de Lausanne) * Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides) * * Akantu 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. * * Akantu 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 Akantu. If not, see . * */ /* -------------------------------------------------------------------------- */ __BEGIN_AKANTU__ /* -------------------------------------------------------------------------- */ template const ParsableParamTyped & ParsableParam::getParsableParamTyped() const { try { const ParsableParamTyped & tmp = dynamic_cast &>(*this); return tmp; } catch (...) { AKANTU_EXCEPTION("The parameter named " << name << " is of type " << debug::demangle(typeid(T).name()) <<"."); } } /* -------------------------------------------------------------------------- */ template ParsableParamTyped & ParsableParam::getParsableParamTyped() { try { ParsableParamTyped & tmp = dynamic_cast &>(*this); return tmp; } catch (...) { AKANTU_EXCEPTION("The parameter named " << name << " is of type " << debug::demangle(typeid(T).name()) <<"."); } } /* ------------------------------------------------------------------------ */ template void ParsableParam::set(const V & value) { ParsableParamTyped & typed_param = getParsableParamTyped(); if(!(isWritable())) AKANTU_EXCEPTION("The parameter named " << name << " is not writable."); typed_param.setTyped(value); } /* -------------------------------------------------------------------------- */ template const T & ParsableParam::get() const { const ParsableParamTyped & typed_param = getParsableParamTyped(); if(!(isReadable())) AKANTU_EXCEPTION("The parameter named " << name << " is not readable."); return typed_param.getTyped(); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ template ParsableParamTyped::ParsableParamTyped(std::string name, std::string description, ParamAccessType param_type, T & param) : ParsableParam(name, description, param_type), param(param) {} /* -------------------------------------------------------------------------- */ template template void ParsableParamTyped::setTyped(const V & value) { param = value; } /* -------------------------------------------------------------------------- */ template const T & ParsableParamTyped::getTyped() const { return param; } /* -------------------------------------------------------------------------- */ template inline void ParsableParamTyped::parseParam(const ParserParameter & in_param) { ParsableParam::parseParam(in_param); param = static_cast(in_param); } /* -------------------------------------------------------------------------- */ template<> inline void ParsableParamTyped< Vector >::parseParam(const ParserParameter & in_param) { ParsableParam::parseParam(in_param); Vector tmp = in_param; for (UInt i = 0; i < param.size(); ++i) { param(i) = tmp(i); } } /* -------------------------------------------------------------------------- */ template<> inline void ParsableParamTyped< Matrix >::parseParam(const ParserParameter & in_param) { ParsableParam::parseParam(in_param); Matrix tmp = in_param; for (UInt i = 0; i < param.cols(); ++i) { for (UInt j = 0; j < param.rows(); ++j) { param(i, j) = tmp(i, j); } } } /* -------------------------------------------------------------------------- */ template<> inline void ParsableParamTyped::parseParam(const ParserParameter & in_param) { ParsableParam::parseParam(in_param); param = in_param.getValue(); } /* -------------------------------------------------------------------------- */ template inline void ParsableParamTyped::printself(std::ostream & stream) const { ParsableParam::printself(stream); stream << param << std::endl; } /* -------------------------------------------------------------------------- */ template<> inline void ParsableParamTyped::printself(std::ostream & stream) const { ParsableParam::printself(stream); stream << std::boolalpha << param << std::endl; } /* -------------------------------------------------------------------------- */ template void Parsable::registerParam(std::string name, T & variable, ParamAccessType type, const std::string description) { std::map::iterator it = params.find(name); if(it != params.end()) AKANTU_EXCEPTION("Parameter named " << name << " already registered."); ParsableParamTyped * param = new ParsableParamTyped(name, description, type, variable); params[name] = param; } /* -------------------------------------------------------------------------- */ template void Parsable::registerParam(std::string name, T & variable, T default_value, ParamAccessType type, const std::string description) { variable = default_value; registerParam(name, variable, type, description); } /* -------------------------------------------------------------------------- */ template void Parsable::setMixed(const std::string & name, const V & value) { std::map::iterator it = params.find(name); if(it == params.end()) AKANTU_EXCEPTION("No parameter named " << name << " in the material."); ParsableParam & param = *(it->second); param.set(value); } /* -------------------------------------------------------------------------- */ template void Parsable::set(const std::string & name, const T & value) { this->template setMixed(name, value); } /* -------------------------------------------------------------------------- */ template const T & Parsable::get(const std::string & name) const { std::map::const_iterator it = params.find(name); if(it == params.end()) AKANTU_EXCEPTION("No parameter named " << name << " in the material."); const ParsableParam & param = *(it->second); return param.get(); } __END_AKANTU__