Skip to content

how to know weather state is a sub-state machine or a normal state #604

Open
@magni-mar

Description

@magni-mar

Here is a minimal sample which one can work from to answer my question:

#include <boost/asio.hpp>
#include <boost/sml.hpp>
#include <iostream>

struct running {
  static constexpr std::string_view name{ "running" };
};
struct not_running {
  static constexpr std::string_view name{ "not_running" };
};

struct state_machine {
  auto operator()() {
    using boost::sml::event;
    using boost::sml::operator""_s;

    auto table =
        boost::sml::make_transition_table(*"on"_s + event<running> = "off"_s, "off"_s + event<not_running> = "on"_s);
    return table;
  }
};

struct control_modes {
  auto operator()() {
    using boost::sml::event;
    using boost::sml::state;
    using boost::sml::operator""_s;

    auto table = boost::sml::make_transition_table(*"not_running"_s + event<running> = state<state_machine>,
                                                   state<state_machine> + event<running> = "running"_s);
    return table;
  }
};

auto main(int argc, char** argv) -> int {
  using boost::sml::state;
  using boost::sml::operator""_s;

  using state_machine_t = boost::sml::sm<control_modes, state_machine>;
  std::shared_ptr<state_machine_t> const sm{ std::make_shared<state_machine_t>(control_modes{}, state_machine{}) };

  // "not_running"_s and state<state_machine>
  auto s1 = "not_running"_s;
  auto s2 = state<state_machine>;

  std::cout << "sm: " << sm->is(s1) << std::endl;
  std::cout << "sm: " << sm->is(s2) << std::endl;

  sm->process_event(running{});

  std::cout << "sm: " << sm->is(s1) << std::endl;
  std::cout << "sm: " << sm->is(s2) << std::endl;
}

So I have been looking for a way to determine weather a state is a sub-state machine or not. So far, I have not found a way. In essence, in the above code I want a way to determine weather the outer state machine is currently in a state represented by the inner state machine. So I am looking for some way to be able to do the following:

  std::cout << s1.is_normal_state() << std::endl;
  std::cout << s2.is_sub_state_machine() << std::endl;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions