Page MenuHomec4science

factory_multiscale.cc
No OneTemporary

File Metadata

Created
Sun, May 26, 18:34

factory_multiscale.cc

/**
* @file factory_multiscale.cc
*
* @author Guillaume Anciaux <guillaume.anciaux@epfl.ch>
*
* @date Mon May 05 17:54:16 2014
*
* @brief This is the central factory for all the objects created through the parser
*
* @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/>.
*
*/
#include "lm_common.hh"
#include "filter_manager.hh"
#include "action_manager.hh"
#include "factory_multiscale.hh"
#include "lm_parser.hh"
#include <typeinfo>
/* -------------------------------------------------------------------------- */
__BEGIN_LIBMULTISCALE__
template <typename T, typename IT>
void FactoryMultiScale<T,IT>::createObject(std::stringstream & line){
std::string mot;
Parser::parse(mot,line);
if (mot == "INPUT"){
std::string id;
Parser::parse(id,line);
try {
DomainInterface * input = DomainMultiScale::getManager().getObject(id);
input->connect(*static_cast<IT *>(this));
}
catch (LibMultiScaleException & e){
try {
FilterInterface * input = FilterManager::getManager().getObject(id);
input->connect(*static_cast<IT *>(this));
}
catch (LibMultiScaleException & e){
LM_THROW("id " << id << " is most probably not a valid input " << std::endl
<< e.what());
}
}
}
else LM_FATAL("Unknown keyword " << mot
<< " was not parsed !! " << std::endl
<< "check config file for a syntax problem for line\n\t"
<< Parser::getParserState() << std::endl
<< "\t\t\"" << line.str() << "\"" << std::endl);
UInt pos = line.tellg();
if (pos < line.str().length()){
std::string buffer = line.str().substr(pos);
DUMPFILE(Parser::fout,"Parsing Filter " << buffer);
ParseResult parsed = this->getObject(obj_id_being_created)->parseLine(buffer);
if (!parsed) LM_FATAL("Filter parameter " << line.str()
<< " was not parsed !! check config file for a syntax problem");
Parser::shiftLine(parsed,line);
this->getObject(obj_id_being_created)->checkAllKeywordsAreParsed();
}
DUMP("filter configuration parsed ... ",DBG_INFO);
}
/* -------------------------------------------------------------------------- */
// template <typename T, typename IT>
// FactoryMultiScale<T,IT>::~FactoryMultiScale(){
// typename std::map<ID,T *>::iterator it = this->objects.begin();
// typename std::map<ID,T *>::iterator end = this->objects.end();
// while (it != end){
// DUMP("deleting " << (*it).first,DBG_MESSAGE);
// delete (*it).second;
// it = this->objects.begin();
// end = this->objects.end();
// }
// }
// /* -------------------------------------------------------------------------- */
template <typename T, typename IT>
FactoryMultiScale<T,IT>::FactoryMultiScale(UInt Dim):
T(std::string("Manager:") + typeid(T).name()),
IDManager<T,IT>(typeid(T).name()),dim(Dim){
LM_ASSERT(!this->static_pointer,"There should not be more than one instance"
<< " of this object " << stringify_macro(T));
this->static_pointer = dynamic_cast<IT*>(this);
}
/* -------------------------------------------------------------------------- */
template <typename T, typename IT>
void FactoryMultiScale<T,IT>::action()
{
for (UInt i = 0; i < this->object_list.size(); ++i) {
ID actionID = this->object_list[i];
DUMP("testing action " << actionID << " at stage " << current_stage,
DBG_INFO);
ActionInterface * ptr = this->objects[actionID];
if (ptr == NULL) LM_FATAL("NULL pointer registered!");
if (ptr->shouldMakeAction()){
DUMP("calling action " << actionID << " at stage " << current_stage,
DBG_INFO);
ptr->action();
}
}
++this->action_step;
}
/* -------------------------------------------------------------------------- */
template <typename T, typename IT>
void FactoryMultiScale<T,IT>::init(){
typename std::map<ID,T *>::iterator it = this->objects.begin();
typename std::map<ID,T *>::iterator end = this->objects.end();
for ( ; it != end ; ++it){
(*it).second->init();
}
}
/* -------------------------------------------------------------------------- */
template <typename T, typename IT>
UInt FactoryMultiScale<T,IT>::parseLine(std::stringstream & line,ActionType atype){
std::string mot;
obj_type_being_created = "NULL";
obj_kind_being_created = atype;
Parser::parse(obj_id_being_created,line);
Parser::parse(obj_type_being_created,line);
try {
createObject(line);
}
catch (LibMultiScaleException e){
LM_FATAL(line.str()
<< " was not parsed !! " << std::endl
<< "\t\tcheck config file for a syntax problem" << std::endl
<< "\t\t" << e.what());
}
return true;
}
/* -------------------------------------------------------------------------- */
template <typename T, typename IT>
IT * FactoryMultiScale<T,IT>::getManager(UInt dim){
LM_ASSERT(!(IDManager<T,IT>::static_pointer),"manager already constructed");
IDManager<T,IT>::static_pointer = new IT(dim);
return IDManager<T,IT>::static_pointer;
}
/* -------------------------------------------------------------------------- */
template <typename T, typename IT>
IT & FactoryMultiScale<T,IT>::getManager(){
if (!IDManager<T,IT>::static_pointer)
LM_THROW("Manager " << typeid(T).name() << " was not created");
return *IDManager<T,IT>::static_pointer;
}
/* -------------------------------------------------------------------------- */
template class FactoryMultiScale<ActionInterface,ActionManager>;
template class FactoryMultiScale<FilterInterface,FilterManager>;
/* -------------------------------------------------------------------------- */
template <>
ActionManager * IDManager<ActionInterface,ActionManager>::static_pointer = NULL;
template <>
FilterManager * IDManager<FilterInterface,FilterManager>::static_pointer = NULL;
/* -------------------------------------------------------------------------- */
__END_LIBMULTISCALE__

Event Timeline