Page MenuHomec4science

factory_multiscale.cc
No OneTemporary

File Metadata

Created
Mon, Jul 22, 17:43

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/>.
*
*/
#define TIMER
#include "factory_multiscale.hh"
#include "action_manager.hh"
#include "filter_manager.hh"
#include "lm_common.hh"
#include "lm_parser.hh"
#include <typeinfo>
/* -------------------------------------------------------------------------- */
__BEGIN_LIBMULTISCALE__
template <typename T>
// template <typename IT>
void FactoryMultiScale<T>::createObject(std::stringstream &line) {
std::string mot;
DUMP(line.str() << " tellg: " << line.tellg(), DBG_DETAIL);
Parser::parse(mot, line);
if (mot == "INPUT") {
std::string id;
DUMP(line.str() << " tellg: " << line.tellg(), DBG_DETAIL);
Parser::parse(id, line);
DUMP(line.str() << " tellg: " << line.tellg(), DBG_DETAIL);
try {
DomainInterface *input = DomainMultiScale::getManager().getObject(id);
// input->connect(*static_cast<IT *>(this));
input->connect(*this);
} catch (LibMultiScaleException &e) {
try {
FilterInterface *input = FilterManager::getManager().getObject(id);
// input->connect(*static_cast<IT *>(this));
input->connect(*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);
DUMP(line.str() << " tellg: " << line.tellg(), DBG_DETAIL);
UInt pos = line.tellg();
std::string buffer = "";
if (pos < line.str().length())
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();
this->getObject(obj_id_being_created)->init();
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>
FactoryMultiScale<T>::FactoryMultiScale(UInt Dim)
: T(std::string("Manager:") + typeid(T).name()),
IDManager<T>(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> void FactoryMultiScale<T>::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);
STARTTIMER(actionID);
ptr->action();
STOPTIMER(actionID);
}
}
++this->action_step;
}
/* -------------------------------------------------------------------------- */
template <typename T> void FactoryMultiScale<T>::init() {
LM_THROW("deprecated method should not be called anymore");
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>
UInt FactoryMultiScale<T>::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 <>
IDManager<ActionInterface> *IDManager<ActionInterface>::static_pointer = NULL;
template <>
IDManager<FilterInterface> *IDManager<FilterInterface>::static_pointer = NULL;
/* -------------------------------------------------------------------------- */
template class FactoryMultiScale<ActionInterface>;
template class FactoryMultiScale<FilterInterface>;
/* -------------------------------------------------------------------------- */
__END_LIBMULTISCALE__

Event Timeline