00001 // g2o - General Graph Optimization 00002 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard 00003 // 00004 // g2o is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU Lesser General Public License as published 00006 // by the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // g2o is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU Lesser General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU Lesser General Public License 00015 // along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 00017 #include "solver_factory.h" 00018 00019 #include <iostream> 00020 #include <typeinfo> 00021 #include <cassert> 00022 00023 using namespace std; 00024 00025 namespace g2o { 00026 00027 AbstractSolverCreator::AbstractSolverCreator(const SolverProperty& p) : 00028 _property(p) 00029 { 00030 } 00031 00032 SolverFactory* SolverFactory::factoryInstance = 0; 00033 00034 SolverFactory::SolverFactory() 00035 { 00036 } 00037 00038 SolverFactory::~SolverFactory() 00039 { 00040 for (CreatorList::iterator it = _creator.begin(); it != _creator.end(); ++it) 00041 delete *it; 00042 } 00043 00044 SolverFactory* SolverFactory::instance() 00045 { 00046 if (factoryInstance == 0) { 00047 factoryInstance = new SolverFactory(); 00048 } 00049 return factoryInstance; 00050 } 00051 00052 void SolverFactory::registerSolver(AbstractSolverCreator* c) 00053 { 00054 const string& name = c->property().name; 00055 CreatorList::iterator foundIt = findSolver(name); 00056 if (foundIt != _creator.end()) { 00057 _creator.erase(foundIt); 00058 cerr << "SOLVER FACTORY WARNING: Overwriting Solver _creator " << name << endl; 00059 assert(0); 00060 } 00061 _creator.push_back(c); 00062 } 00063 00064 Solver* SolverFactory::construct(const std::string& name, SparseOptimizer* optimizer, SolverProperty& solverProperty) const 00065 { 00066 CreatorList::const_iterator foundIt = findSolver(name); 00067 if (foundIt != _creator.end()) { 00068 solverProperty = (*foundIt)->property(); 00069 return (*foundIt)->construct(optimizer); 00070 } 00071 return 0; 00072 } 00073 00074 void SolverFactory::destroy() 00075 { 00076 delete factoryInstance; 00077 factoryInstance = 0; 00078 } 00079 00080 void SolverFactory::listSolvers(std::ostream& os) const 00081 { 00082 for (CreatorList::const_iterator it = _creator.begin(); it != _creator.end(); ++it) { 00083 const SolverProperty& sp = (*it)->property(); 00084 os << sp.name << "\t " << sp.desc << endl; 00085 } 00086 } 00087 00088 SolverFactory::CreatorList::const_iterator SolverFactory::findSolver(const std::string& name) const 00089 { 00090 for (CreatorList::const_iterator it = _creator.begin(); it != _creator.end(); ++it) { 00091 const SolverProperty& sp = (*it)->property(); 00092 if (sp.name == name) 00093 return it; 00094 } 00095 return _creator.end(); 00096 } 00097 00098 SolverFactory::CreatorList::iterator SolverFactory::findSolver(const std::string& name) 00099 { 00100 for (CreatorList::iterator it = _creator.begin(); it != _creator.end(); ++it) { 00101 const SolverProperty& sp = (*it)->property(); 00102 if (sp.name == name) 00103 return it; 00104 } 00105 return _creator.end(); 00106 } 00107 00108 } // end namespace