Skip to content

Commit 03cbaef

Browse files
committed
enable streaming events input such as m << begin{};
1 parent 262f0cb commit 03cbaef

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

include/fsm_cxx/fsm-sm.hh

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ namespace fsm_cxx {
6565
namespace fsm_cxx {
6666
// struct dummy_event {};
6767

68-
namespace detail{
68+
namespace detail {
6969
inline std::string shorten(std::string const &s) {
7070
auto pos = s.rfind("::");
7171
return pos == std::string::npos ? s : s.substr(pos + 2);
7272
}
73-
}
74-
73+
} // namespace detail
74+
7575
struct event_t {
7676
virtual ~event_t() {}
7777
virtual std::string to_string() const { return ""; }
@@ -112,13 +112,13 @@ namespace fsm_cxx {
112112
friend std::ostream &operator<<(std::ostream &os, payload_t const &o) { return os << o.to_string(); }
113113
bool _ok;
114114
};
115-
115+
116116
template<typename T>
117117
struct payload_type : public payload_t {
118118
virtual ~payload_type() {}
119119
std::string to_string() const { return detail::shorten(std::string(debug::type_name<T>())); }
120120
};
121-
121+
122122
} // namespace fsm_cxx
123123

124124
#define FSM_DEFINE_EVENT(n) \
@@ -541,7 +541,7 @@ namespace fsm_cxx {
541541
_ctx.reset(_initial);
542542
return (*this);
543543
}
544-
544+
545545
machine_t &on_transition(OnAction &&fn) {
546546
_on_action = fn;
547547
return (*this);
@@ -767,6 +767,16 @@ namespace fsm_cxx {
767767
return is;
768768
}
769769

770+
public:
771+
template<typename Evt,
772+
std::enable_if_t<std::is_base_of<Event, std::decay_t<Evt>>::value &&
773+
!std::is_same<Evt, std::string>::value,
774+
bool> = true>
775+
machine_t &operator<<(Evt const &ev) {
776+
step_by(ev);
777+
return (*this);
778+
}
779+
770780
private:
771781
ContextT _ctx{};
772782
StateT _initial{}, _terminated{}, _error{};

tests/basic.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,7 @@ namespace fsm_cxx { namespace test {
158158

159159
// processing
160160

161-
m.step_by(begin{});
162-
m.step_by(open{});
163-
m.step_by(close{});
164-
m.step_by(open{});
165-
m.step_by(end{});
161+
m << begin{} << open{} << close{} << open{} << end{};
166162

167163
std::printf("---- END OF test_state_meta_2()\n\n\n");
168164
}

0 commit comments

Comments
 (0)