00001 /* 00002 * Copyright (C) 2011 The Board of Trustees of The Leland Stanford Junior University. All rights reserved. 00003 * 00004 * Author: Roland Philippsen 00005 * http://cs.stanford.edu/group/manips/ 00006 * 00007 * This program is free software: you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License 00009 * as published by the Free Software Foundation, either version 3 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, but 00013 * WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this program. If not, see 00019 * <http://www.gnu.org/licenses/> 00020 */ 00021 00022 #ifndef OPSPACE_FACTORY_HPP 00023 #define OPSPACE_FACTORY_HPP 00024 00025 #include <jspace/Status.hpp> 00026 #include <boost/shared_ptr.hpp> 00027 #include <vector> 00028 #include <map> 00029 00030 00031 namespace opspace { 00032 00033 using jspace::Status; 00034 00035 class Task; 00036 class Skill; 00037 class ReflectionRegistry; 00038 00039 00040 template<typename base_type> 00041 struct ShopAPI { 00042 virtual ~ShopAPI() {} 00043 virtual base_type * create(std::string const & name) = 0; 00044 }; 00045 00046 template<typename task_subtype> 00047 struct TaskShop : public ShopAPI<Task> { 00048 virtual Task * create(std::string const & name) { return new task_subtype(name); } 00049 }; 00050 00051 template<typename skill_subtype> 00052 struct SkillShop : public ShopAPI<Skill> { 00053 virtual Skill * create(std::string const & name) { return new skill_subtype(name); } 00054 }; 00055 00056 00113 class Factory 00114 { 00115 public: 00116 typedef std::vector<boost::shared_ptr<Task> > task_table_t; 00117 typedef std::vector<boost::shared_ptr<Skill> > skill_table_t; 00118 00119 static void setDebugStream(std::ostream * dbg); 00120 00121 template<typename task_subtype> 00122 static void addTaskType(std::string const & type) { 00123 task_shop__[type].reset(new TaskShop<task_subtype>()); 00124 } 00125 00126 template<typename skill_subtype> 00127 static void addSkillType(std::string const & type) { 00128 skill_shop__[type].reset(new SkillShop<skill_subtype>()); 00129 } 00130 00131 static Task * createTask(std::string const & type, std::string const & name); 00132 00133 static Skill * createSkill(std::string const & type, std::string const & name); 00134 00139 Status parseString(std::string const & yaml_string); 00140 00145 Status parseFile(std::string const & yaml_filename); 00146 00151 Status parseStream(std::istream & yaml_istream); 00152 00158 task_table_t const & getTaskTable() const; 00159 00165 skill_table_t const & getSkillTable() const; 00166 00167 boost::shared_ptr<Task> findTask(std::string const & name) const; 00168 boost::shared_ptr<Skill> findSkill(std::string const & name) const; 00169 00174 ReflectionRegistry * createRegistry(); 00175 00180 void dump(std::ostream & os, 00181 std::string const & title, 00182 std::string const & prefix) const; 00183 00184 protected: 00185 typedef std::map<std::string, boost::shared_ptr<ShopAPI<Task> > > task_shop_t; 00186 typedef std::map<std::string, boost::shared_ptr<ShopAPI<Skill> > > skill_shop_t; 00187 00188 static std::ostream * dbg__; 00189 static task_shop_t task_shop__; 00190 static skill_shop_t skill_shop__; 00191 00192 task_table_t task_table_; 00193 skill_table_t skill_table_; 00194 }; 00195 00196 } 00197 00198 #endif // OPSPACE_FACTORY_HPP