00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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