Skip to content

Commit

Permalink
enable streaming events input such as m << begin{};
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Oct 1, 2021
1 parent 262f0cb commit 03cbaef
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
22 changes: 16 additions & 6 deletions include/fsm_cxx/fsm-sm.hh
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ namespace fsm_cxx {
namespace fsm_cxx {
// struct dummy_event {};

namespace detail{
namespace detail {
inline std::string shorten(std::string const &s) {
auto pos = s.rfind("::");
return pos == std::string::npos ? s : s.substr(pos + 2);
}
}
} // namespace detail

struct event_t {
virtual ~event_t() {}
virtual std::string to_string() const { return ""; }
Expand Down Expand Up @@ -112,13 +112,13 @@ namespace fsm_cxx {
friend std::ostream &operator<<(std::ostream &os, payload_t const &o) { return os << o.to_string(); }
bool _ok;
};

template<typename T>
struct payload_type : public payload_t {
virtual ~payload_type() {}
std::string to_string() const { return detail::shorten(std::string(debug::type_name<T>())); }
};

} // namespace fsm_cxx

#define FSM_DEFINE_EVENT(n) \
Expand Down Expand Up @@ -541,7 +541,7 @@ namespace fsm_cxx {
_ctx.reset(_initial);
return (*this);
}

machine_t &on_transition(OnAction &&fn) {
_on_action = fn;
return (*this);
Expand Down Expand Up @@ -767,6 +767,16 @@ namespace fsm_cxx {
return is;
}

public:
template<typename Evt,
std::enable_if_t<std::is_base_of<Event, std::decay_t<Evt>>::value &&
!std::is_same<Evt, std::string>::value,
bool> = true>
machine_t &operator<<(Evt const &ev) {
step_by(ev);
return (*this);
}

private:
ContextT _ctx{};
StateT _initial{}, _terminated{}, _error{};
Expand Down
6 changes: 1 addition & 5 deletions tests/basic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,7 @@ namespace fsm_cxx { namespace test {

// processing

m.step_by(begin{});
m.step_by(open{});
m.step_by(close{});
m.step_by(open{});
m.step_by(end{});
m << begin{} << open{} << close{} << open{} << end{};

std::printf("---- END OF test_state_meta_2()\n\n\n");
}
Expand Down

0 comments on commit 03cbaef

Please sign in to comment.