Skip to content

Commit

Permalink
🆕 [example] State names visitor
Browse files Browse the repository at this point in the history
Problem:
- There is no example showig how to print state names using a visitor.
- There is no easy support to print sub State Machines state names.

Solution:
- Add example to print state names using a custom visitor and a composite State Machine.
  • Loading branch information
krzysztof-jusiak committed May 27, 2020
1 parent 9fc46f5 commit da8a0cc
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 9 deletions.
2 changes: 2 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,6 @@ if (IS_COMPILER_GCC_LIKE)
add_example(testing example_testing testing.cpp)

add_example(transitions example_transitions transitions.cpp)

add_example(visitor example_visitor visitor.cpp)
endif()
84 changes: 84 additions & 0 deletions example/visitor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// Copyright (c) 2016-2020 Kris Jusiak (kris at jusiak dot net)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/sml.hpp>
#include <cassert>
#include <iostream>

namespace sml = boost::sml;

struct e1 {};
struct e2 {};
struct e3 {};
struct e4 {};
struct e5 {};

struct sub {
auto operator()() const {
using namespace sml;
// clang-format off
return make_transition_table(
*"idle"_s + event<e3> = "sub1"_s
, "sub1"_s + event<e4> = X
);
// clang-format on
}
};

struct composite {
auto operator()() const {
using namespace sml;
// clang-format off
return make_transition_table(
*"idle"_s + event<e1> = "s1"_s
, "s1"_s + event<e2> = state<sub>
, state<sub> + event<e5> = X
);
// clang-format on
}
};

template <class TSM>
class state_name_visitor {
public:
explicit state_name_visitor(const TSM& sm) : sm_{sm} {}

template <class TSub>
void operator()(boost::sml::aux::string<boost::sml::sm<TSub>>) const {
std::cout << boost::sml::aux::get_type_name<TSub>() << ':';
sm_.template visit_current_states<boost::sml::aux::identity<TSub>>(*this);
}

template <class TState>
void operator()(TState state) const {
std::cout << state.c_str() << '\n';
}

private:
const TSM& sm_;
};

int main() {
sml::sm<composite> sm{};

const auto state_name = state_name_visitor<decltype(sm)>{sm};

sm.process_event(e1{});
sm.visit_current_states(state_name); // s1

sm.process_event(e2{});
sm.visit_current_states(state_name); // sub:idle

sm.process_event(e3{});
sm.visit_current_states(state_name); // sub:sub1

sm.process_event(e4{});
sm.visit_current_states(state_name); // sub:terminate

sm.process_event(e5{});
sm.visit_current_states(state_name); // terminate
}
8 changes: 4 additions & 4 deletions include/boost/sml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ struct missing_ctor_parameter {
operator U() {
return {};
}
#if !defined(_MSC_VER) && !defined(__clang__)
#if !(defined(_MSC_VER) && !defined(__clang__))
template <class TMissing, __BOOST_SML_REQUIRES(!aux::is_base_of<pool_type_base, TMissing>::value)>
operator TMissing &() const {
static_assert(missing_ctor_parameter<TMissing>::value,
Expand Down Expand Up @@ -2589,7 +2589,7 @@ struct transition<state<internal>, state<S2>, front::event<E>, always, none> {
};
}
using _ = back::_;
#if !defined(_MSC_VER) && !defined(__clang__)
#if !(defined(_MSC_VER) && !defined(__clang__))
template <class TEvent>
constexpr front::event<TEvent> event{};
#else
Expand All @@ -2606,15 +2606,15 @@ template <class T>
front::event<back::exception<T>> exception __BOOST_SML_VT_INIT;
using anonymous = back::anonymous;
using initial = back::initial;
#if !defined(_MSC_VER) && !defined(__clang__)
#if !(defined(_MSC_VER) && !defined(__clang__))
template <class T>
constexpr typename front::state_sm<T>::type state{};
#else
template <class T>
typename front::state_sm<T>::type state __BOOST_SML_VT_INIT;
#endif
inline namespace literals {
#if !defined(_MSC_VER) && !defined(__clang__)
#if !(defined(_MSC_VER) && !defined(__clang__))
template <class T, T... Chrs>
constexpr auto operator""_s() {
return front::state<aux::string<T, Chrs...>>{};
Expand Down
2 changes: 1 addition & 1 deletion include/boost/sml/aux_/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ struct missing_ctor_parameter {
return {};
}

#if !defined(_MSC_VER) && !defined(__clang__) // __pph__
#if !(defined(_MSC_VER) && !defined(__clang__)) // __pph__
template <class TMissing, __BOOST_SML_REQUIRES(!aux::is_base_of<pool_type_base, TMissing>::value)>
operator TMissing &() const {
static_assert(missing_ctor_parameter<TMissing>::value,
Expand Down
1 change: 0 additions & 1 deletion include/boost/sml/back/policies/testing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace back {
namespace policies {

struct testing_policy__ {};

struct testing : aux::pair<testing_policy__, testing> {};

} // namespace policies
Expand Down
6 changes: 3 additions & 3 deletions include/boost/sml/transition_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using _ = back::_;

/// events

#if !defined(_MSC_VER) && !defined(__clang__) // __pph__
#if !(defined(_MSC_VER) && !defined(__clang__)) // __pph__
template <class TEvent>
constexpr front::event<TEvent> event{};
#else // __pph__
Expand All @@ -46,7 +46,7 @@ using initial = back::initial;

/// states

#if !defined(_MSC_VER) && !defined(__clang__) // __pph__
#if !(defined(_MSC_VER) && !defined(__clang__)) // __pph__
template <class T>
constexpr typename front::state_sm<T>::type state{};
#else // __pph__
Expand All @@ -55,7 +55,7 @@ typename front::state_sm<T>::type state __BOOST_SML_VT_INIT;
#endif // __pph__

inline namespace literals {
#if !defined(_MSC_VER) && !defined(__clang__) // __pph__
#if !(defined(_MSC_VER) && !defined(__clang__)) // __pph__
template <class T, T... Chrs>
constexpr auto operator""_s() {
return front::state<aux::string<T, Chrs...>>{};
Expand Down

0 comments on commit da8a0cc

Please sign in to comment.