diff --git a/fsm.h b/fsm.h index 1e37eb7..69ec26c 100644 --- a/fsm.h +++ b/fsm.h @@ -237,6 +237,26 @@ class Fsm { } + template + Fsm(InputIt start, InputIt end) : + m_transitions(), m_cs(Initial), m_debug_fn(nullptr) + { + add_transitions(start, end); + } + + template + Fsm(Coll &&c) : + m_transitions(), m_cs(Initial), m_debug_fn(nullptr) + { + add_transitions(c); + } + + Fsm(std::initializer_list&& i) : + m_transitions(), m_cs(Initial), m_debug_fn(nullptr) + { + add_transitions(i); + } + /** * Sets the current state to the given state. Defaults to the Initial state. * diff --git a/tests/fsm_test.cpp b/tests/fsm_test.cpp index c97978b..2871ea8 100644 --- a/tests/fsm_test.cpp +++ b/tests/fsm_test.cpp @@ -244,6 +244,47 @@ TEST_CASE("Test single argument add_transitions function") } } +TEST_CASE("Test single argument constructors with transitions") +{ + enum class States { Initial, A, Final }; + using F = FSM::Fsm; + + SECTION("Test raw array") + { + F::Trans v[] = { + {States::Initial, States::A, 'a', nullptr, nullptr}, + {States::A, States::Final, 'b', nullptr, nullptr}, + }; + F fsm(&v[0], &v[2]); + fsm.execute('a'); + fsm.execute('b'); + REQUIRE(fsm.state() == States::Final); + } + + SECTION("Test vector") + { + std::vector v = { + {States::Initial, States::A, 'a', nullptr, nullptr}, + {States::A, States::Final, 'b', nullptr, nullptr}, + }; + F fsm(v); + fsm.execute('a'); + fsm.execute('b'); + REQUIRE(fsm.state() == States::Final); + } + + SECTION("Test initializer list") + { + F fsm{ + {States::Initial, States::A, 'a', nullptr, nullptr}, + {States::A, States::Final, 'b', nullptr, nullptr}, + }; + fsm.execute('a'); + fsm.execute('b'); + REQUIRE(fsm.state() == States::Final); + } +} + TEST_CASE("Test int as type for states") { int INITIAL = 1;