19#ifndef RTC_STATEMACHINE_H
20#define RTC_STATEMACHINE_H
56 template <
class State>
261 template <
class State,
264 class Callback = void (
Listener::*)(const States& states)
287 : m_num(num_of_state),
289 m_entry(m_num, (Callback)nullptr),
290 m_predo(m_num, (Callback)nullptr),
291 m_do(m_num, (Callback)nullptr),
292 m_postdo(m_num, (Callback)nullptr),
293 m_exit(m_num, (Callback)nullptr),
302 : m_num(other.m_num),
303 m_listener(other.m_listener),
304 m_entry (other.m_entry),
305 m_predo (other.m_predo),
307 m_postdo(other.m_postdo),
308 m_exit (other.m_exit),
309 m_transit(other.m_transit),
310 m_states(other.m_states),
311 m_selftrans(other.m_selftrans)
324 std::swap(m_num, other.
m_num);
326 std::swap(m_entry, other.
m_entry);
327 std::swap(m_predo, other.
m_predo);
328 std::swap(m_do, other.
m_do);
329 std::swap(m_postdo, other.
m_postdo);
330 std::swap(m_exit, other.
m_exit);
332 std::swap(m_states, other.
m_states);
354 setNullFunc(m_entry, call_back);
355 setNullFunc(m_do, call_back);
356 setNullFunc(m_exit, call_back);
357 setNullFunc(m_predo, call_back);
358 setNullFunc(m_postdo, call_back);
359 m_transit = call_back;
381 assert(listener !=
nullptr);
382 m_listener = listener;
413 m_entry.at(state) = call_back;
450 m_predo.at(state) = call_back;
487 m_do.at(state) = call_back;
524 m_postdo.at(state) = call_back;
561 m_exit.at(state) = call_back;
596 m_transit = call_back;
619 m_states.curr = states.curr;
620 m_states.prev = states.prev;
621 m_states.next = states.next;
646 std::lock_guard<std::mutex> guard(m_mutex);
669 std::lock_guard<std::mutex> guard(m_mutex);
670 return m_states.curr;
696 std::lock_guard<std::mutex> guard(m_mutex);
697 return m_states.curr == state ? true :
false;
728 std::lock_guard<std::mutex> guard(m_mutex);
729 m_states.next = state;
730 if (m_states.curr == state)
759 if (state.curr == state.next)
762 if (m_predo[state.curr] != NULL)
763 (m_listener->*m_predo[state.curr])(state);
765 if (need_trans())
return;
768 if (m_do[state.curr] != NULL)
769 (m_listener->*m_do[state.curr])(state);
771 if (need_trans())
return;
774 if (m_postdo[state.curr] != NULL)
775 (m_listener->*m_postdo[state.curr])(state);
779 if (m_exit[state.curr] != NULL)
780 (m_listener->*m_exit[state.curr])(state);
784 if (state.curr != state.next)
786 state.curr = state.next;
787 if (m_entry[state.curr] != NULL)
788 (m_listener->*m_entry[state.curr])(state);
789 update_curr(state.curr);
805 if (state.curr == state.next)
807 if (m_predo[state.curr] !=
nullptr)
809 (m_listener->*m_predo[state.curr])(state);
815 if (m_exit[state.curr] !=
nullptr)
817 (m_listener->*m_exit[state.curr])(state);
820 if (state.curr != state.next)
822 state.curr = state.next;
823 if (m_entry[state.curr] !=
nullptr)
825 (m_listener->*m_entry[state.curr])(state);
827 update_curr(state.curr);
835 if (m_do[state.curr] !=
nullptr)
837 (m_listener->*m_do[state.curr])(state);
845 if (m_postdo[state.curr] !=
nullptr)
847 (m_listener->*m_postdo[state.curr])(state);
874 for (
size_t i(0); i < m_num; ++i)
876 s.emplace_back(nullfunc);
964 inline void sync(States& st)
966 std::lock_guard<std::mutex> guard(m_mutex);
970 inline bool need_trans()
972 std::lock_guard<std::mutex> guard(m_mutex);
973 return (m_states.curr != m_states.next);
976 inline void update_curr(
const State curr)
978 std::lock_guard<std::mutex> guard(m_mutex);
979 m_states.curr = curr;
984 template <
class T,
class U,
class V,
class W>
状態マシンクラス
Definition StateMachine.h:267
void setNOP(Callback call_back)
NOP関数を登録する
Definition StateMachine.h:352
State getState()
現在の状態を取得する
Definition StateMachine.h:667
bool setTransitionAction(Callback call_back)
State transition action 関数を登録する
Definition StateMachine.h:594
int m_num
状態数
Definition StateMachine.h:887
Listener * m_listener
コールバック関数用リスナー
Definition StateMachine.h:896
States m_states
現在の状態情報
Definition StateMachine.h:959
void worker_pre()
Definition StateMachine.h:801
void worker_post()
Definition StateMachine.h:841
void setNullFunc(std::vector< Callback > &s, Callback nullfunc)
NOP関数を設定
Definition StateMachine.h:871
bool setDoAction(State state, Callback call_back)
Do action 関数を登録する
Definition StateMachine.h:483
void goTo(State state)
状態を遷移
Definition StateMachine.h:726
void swap(StateMachine &other)
Definition StateMachine.h:322
std::vector< Callback > m_postdo
PostDo action 用コールバック関数
Definition StateMachine.h:932
void setStartState(States states)
初期状態をセットする
Definition StateMachine.h:617
std::mutex m_mutex
Definition StateMachine.h:961
bool setEntryAction(State state, Callback call_back)
Entry action 関数を登録する
Definition StateMachine.h:409
bool isIn(State state)
現在状態を確認
Definition StateMachine.h:694
std::vector< Callback > m_exit
Exit action 用コールバック関数
Definition StateMachine.h:941
std::vector< Callback > m_entry
Entry action 用コールバック関数
Definition StateMachine.h:905
bool setPreDoAction(State state, Callback call_back)
PreDo action 関数を登録する
Definition StateMachine.h:446
void worker_do()
Definition StateMachine.h:831
StateMachine(const StateMachine &other)
Definition StateMachine.h:301
bool m_selftrans
Definition StateMachine.h:960
Callback m_transit
State transition action 用コールバック関数
Definition StateMachine.h:950
std::vector< Callback > m_do
Do action 用コールバック関数
Definition StateMachine.h:923
bool setPostDoAction(State state, Callback call_back)
PostDo action 関数を登録する
Definition StateMachine.h:520
bool setExitAction(State state, Callback call_back)
Exit action 関数を登録する
Definition StateMachine.h:557
StateMachine(int num_of_state)
コンストラクタ
Definition StateMachine.h:286
std::vector< Callback > m_predo
PreDo action 用コールバック関数
Definition StateMachine.h:914
States getStates()
状態を取得する
Definition StateMachine.h:644
void worker()
駆動関数
Definition StateMachine.h:753
void setListener(Listener *listener)
Listener オブジェクトを登録する
Definition StateMachine.h:379
StateMachine & operator=(const StateMachine &other)
Definition StateMachine.h:315
状態保持用クラス
Definition StateMachine.h:58
State curr
Definition StateMachine.h:59
State prev
Definition StateMachine.h:60
State next
Definition StateMachine.h:61