opspace/include/opspace/Skill.hpp

Go to the documentation of this file.
00001 /*
00002  * Shared copyright notice and LGPLv3 license statement.
00003  *
00004  * Copyright (C) 2011 The Board of Trustees of The Leland Stanford Junior University. All rights reserved.
00005  * Copyright (C) 2011 University of Texas at Austin. All rights reserved.
00006  *
00007  * Authors: Roland Philippsen (Stanford) and Luis Sentis (UT Austin)
00008  *          http://cs.stanford.edu/group/manips/
00009  *          http://www.me.utexas.edu/~hcrl/
00010  *
00011  * This program is free software: you can redistribute it and/or
00012  * modify it under the terms of the GNU Lesser General Public License
00013  * as published by the Free Software Foundation, either version 3 of
00014  * the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with this program.  If not, see
00023  * <http://www.gnu.org/licenses/>
00024  */
00025 
00026 #ifndef OPSPACE_SKILL_HPP
00027 #define OPSPACE_SKILL_HPP
00028 
00029 #include <opspace/Task.hpp>
00030 #include <opspace/Parameter.hpp>
00031 #include <boost/shared_ptr.hpp>
00032 #include <vector>
00033 
00034 namespace opspace {
00035   
00036   
00037   typedef enum {
00038     TASK_SLOT_DEFAULT = 0,
00039     TASK_SLOT_OPTIONAL = 1
00040   } task_slot_flags_t;
00041   
00042   
00043   class TaskSlotAPI
00044   {
00045   public:
00046     std::string const name_;
00047     task_slot_flags_t const flags_;
00048     
00049     TaskSlotAPI(std::string const & name, task_slot_flags_t flags)
00050       : name_(name), flags_(flags) {}
00051     
00052     virtual ~TaskSlotAPI() {}
00053     
00054     virtual Status assign(boost::shared_ptr<Task> instance)
00055     { return Status(false, "type mismatch"); }
00056     
00057     virtual size_t getNInstances() const = 0;
00058     virtual boost::shared_ptr<Task> getInstance(size_t index) = 0;
00059     
00060     inline bool isOptional() const { return flags_ & TASK_SLOT_OPTIONAL; }
00061   };
00062   
00063   
00064   template<typename task_subtype>
00065   class TaskSlot
00066     : public TaskSlotAPI
00067   {
00068   public:
00069     TaskSlot(std::string const & name,
00070        task_subtype ** slot,
00071        task_slot_flags_t flags)
00072       : TaskSlotAPI(name, flags), slot_(slot) {}
00073     
00074     virtual Status assign(boost::shared_ptr<Task> instance) {
00075       Task * base(instance.get());
00076       if ( ! base) {
00077   return Status(false, "null instance");
00078       }
00079       task_subtype * raw(dynamic_cast<task_subtype *>(base));
00080       if ( ! raw) {
00081   return Status(false, "type mismatch");
00082       }
00083       if (slot_) {
00084   *slot_ = raw;
00085       }
00086       instances_.push_back(instance);
00087       return Status();
00088     }
00089 
00090     virtual size_t getNInstances() const { return instances_.size(); }
00091     virtual boost::shared_ptr<Task> getInstance(size_t index) { return instances_[index]; }
00092     
00093   protected:
00094     task_subtype ** slot_;
00095     std::vector<boost::shared_ptr<Task> > instances_;
00096   };
00097   
00098   
00099   class Skill
00100     : public ParameterReflection
00101   {
00102   public:
00103     typedef std::vector<Task *> task_table_t;
00104     
00105     virtual ~Skill();
00106     
00107     virtual Status update(Model const & model) = 0;
00108     virtual task_table_t const * getTaskTable() = 0;
00109     
00110     virtual Status init(Model const & model);
00111     virtual Status checkJStarSV(Task const * task, Vector const & sv) { Status ok; return ok; }
00112     
00113     inline std::string const & getName() const { return name_; }
00114     
00115     boost::shared_ptr<TaskSlotAPI> lookupSlot(std::string const & name);
00116     
00117     virtual void dump(std::ostream & os,
00118           std::string const & title,
00119           std::string const & prefix) const;
00120     
00121     virtual void dbg(std::ostream & os,
00122          std::string const & title,
00123          std::string const & prefix) const;
00124     
00125   protected:
00126     Skill(std::string const & name);
00127     
00128     template<typename task_subtype>
00129     boost::shared_ptr<TaskSlotAPI>
00130     declareSlot(std::string const & name,
00131     task_subtype ** slot = 0,
00132     task_slot_flags_t flags = TASK_SLOT_DEFAULT)
00133     {
00134       boost::shared_ptr<TaskSlotAPI>
00135   slot_api(new TaskSlot<task_subtype>(name, slot, flags));
00136       slot_map_[name] = slot_api;
00137       return slot_api;
00138     }
00139     
00140     std::string const name_;
00141     
00142   private:
00143     typedef std::map<std::string, boost::shared_ptr<TaskSlotAPI> > slot_map_t;
00144     slot_map_t slot_map_;
00145   };
00146   
00147 }
00148 
00149 #endif // OPSPACE_SKILL_HPP

Generated on Fri Aug 26 01:31:16 2011 for Stanford Whole-Body Control Framework by  doxygen 1.5.4