-
Notifications
You must be signed in to change notification settings - Fork 177
/
data.cpp
67 lines (56 loc) · 1.79 KB
/
data.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// 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)
//
#define BOOST_SML_CREATE_DEFAULT_CONSTRUCTIBLE_DEPS
#include <boost/sml.hpp>
#include <cassert>
#include <iostream>
namespace sml = boost::sml;
namespace {
struct connect {
int id{};
};
struct disconnect {};
struct interrupt {};
struct Disconnected {};
struct Connected {
int id{}; /// per state data
};
struct Interrupted {
int id{}; /// per state data
};
class data {
using Self = data;
public:
explicit data(const std::string& address) : address{address} {}
auto operator()() {
using namespace boost::sml;
const auto set = [](const auto& event, Connected& state) { state.id = event.id; };
const auto update = [](Connected& src_state, Interrupted& dst_state) { dst_state.id = src_state.id; };
// clang-format off
return make_transition_table(
* state<Disconnected> + event<connect> / (set, &Self::print) = state<Connected>
, state<Connected> + event<interrupt> / (update, &Self::print) = state<Interrupted>
, state<Interrupted> + event<connect> / (set, &Self::print) = state<Connected>
, state<Connected> + event<disconnect> / (&Self::print) = X
);
// clang-format on
}
private:
void print(Connected& state) { std::cout << address << ':' << state.id << '\n'; };
std::string address{}; /// shared data between states
};
} // namespace
int main() {
data d{std::string{"127.0.0.1"}};
sml::sm<data> sm{d, Connected{1}};
sm.process_event(connect{1024});
sm.process_event(interrupt{});
sm.process_event(connect{1025});
sm.process_event(disconnect{});
assert(sm.is(sml::X));
}