趣味で作ってるロボット用ソフトウェア
 All Classes Files Functions Enumerations Enumerator Friends Pages
Scripted_state_machine.hpp
Go to the documentation of this file.
1 #ifndef HRK_SCRIPTED_STATE_MACHINE_HPP
2 #define HRK_SCRIPTED_STATE_MACHINE_HPP
3 
11 #include <iostream>
12 #include <luabind/luabind.hpp>
13 
14 
15 namespace hrk
16 {
18  template <class T>
20  {
21  public:
26  Scripted_state_machine(T* owner, lua_State* lua)
27  : owner_(owner), lua_(lua)
28  {
29  }
30 
31 
37  void set_current_state(const std::string& state_name)
38  {
39  luabind::object global = luabind::globals(lua_);
40  luabind::object state = global[state_name];
41 
42  check_state_is_table(state, state_name);
43  previous_state_ = current_state_;
44  current_state_ = state;
45  current_state_name_ = state_name;
46  }
47 
48 
52  luabind::object change_to_previous(void) const
53  {
54  return previous_state_;
55  }
56 
57 
59  bool update(void)
60  {
61  if (!current_state_.is_valid()) {
62  return false;
63  }
64 
65  if (luabind::type(current_state_["Execute"]) != LUA_TFUNCTION) {
66  std::string state_name = current_state_name_ + "['Execute']";
67  std::cerr << "Not found function: " << state_name << std::endl;
68  return false;
69  }
70  current_state_["Execute"](owner_);
71 
72  return true;
73  }
74 
75 
77  bool change_to(const std::string& next_state_name)
78  {
79  if (current_state_.is_valid()) {
80  if (luabind::type(current_state_["Exit"]) != LUA_TFUNCTION) {
81  std::cerr << "Not found function: "
82  << current_state_name_ << "['Exit']"
83  << std::endl;
84  return false;
85  }
86  current_state_["Exit"](owner_);
87  }
88 
89  // 直前の状態を保持しておく
90  previous_state_ = current_state_;
91  current_state_name_ = next_state_name;
92 
93  luabind::object global = luabind::globals(lua_);
94  luabind::object next_state = global[next_state_name];
95  check_state_is_table(next_state, next_state_name);
96  current_state_ = next_state;
97  if (luabind::type(current_state_["Enter"]) != LUA_TFUNCTION) {
98  std::cerr << "Not found function: "
99  << next_state_name << "['Enter']" << std::endl;
100  return false;
101  }
102  current_state_["Enter"](owner_);
103 
104  return true;
105  }
106 
107 
108  private:
109  void check_state_is_table(const luabind::object& state,
110  const std::string& state_name)
111  {
112  if (luabind::type(state) != LUA_TTABLE) {
113  std::cerr << "'" << state_name
114  << "' variable is not table." << std::endl;
115  if (luabind::type(state) == LUA_TNIL) {
116  std::cerr << "'" << state_name << "' is nil." << std::endl;
117  }
118  }
119  }
120 
121 
122  T* owner_;
123  lua_State* lua_;
124  luabind::object current_state_;
125  luabind::object previous_state_;
126  std::string current_state_name_;
127  };
128 }
129 
130 #endif
void set_current_state(const std::string &state_name)
現在の状態を設定する
Definition: Scripted_state_machine.hpp:37
Scripted_state_machine(T *owner, lua_State *lua)
Definition: Scripted_state_machine.hpp:26
bool update(void)
状態を更新する
Definition: Scripted_state_machine.hpp:59
bool change_to(const std::string &next_state_name)
次の状態に変更する
Definition: Scripted_state_machine.hpp:77
luabind::object change_to_previous(void) const
直前の状態を取得する
Definition: Scripted_state_machine.hpp:52
状態遷移を実現するためのテンプレート
Definition: Scripted_state_machine.hpp:19