-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathdependency_injection.cpp
More file actions
77 lines (68 loc) · 1.58 KB
/
dependency_injection.cpp
File metadata and controls
77 lines (68 loc) · 1.58 KB
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
68
69
70
71
72
73
74
75
76
77
//
// 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)
//
// clang-format off
#if __has_include(<boost/di.hpp>)
// clang-format on
#include <boost/sml.hpp>
#include <boost/di.hpp>
#include <cassert>
#include <typeinfo>
#include <iostream>
namespace sml = boost::sml;
namespace di = boost::di;
namespace {
struct e1 {};
struct e2 {};
struct e3 {};
auto guard = [](int i, double d) {
assert(42 == i);
assert(87.0 == d);
std::cout << "guard" << std::endl;
return true;
};
auto action = [](int i, auto e) {
assert(42 == i);
std::cout << "action: " << typeid(e).name() << std::endl;
};
struct example {
auto operator()() const noexcept {
using namespace sml;
// clang-format off
return make_transition_table(
*"idle"_s + event<e1> = "s1"_s
, "s1"_s + event<e2> [ guard ] / action = "s2"_s
, "s2"_s + event<e3> / [] { std::cout << "in place action" << std::endl; } = X
);
// clang-format on
}
};
class controller {
public:
explicit controller(sml::sm<example>& sm) : sm(sm) {}
void start() {
sm.process_event(e1{});
sm.process_event(e2{});
sm.process_event(e3{});
assert(sm.is(sml::X));
}
private:
sml::sm<example>& sm;
};
} // namespace
int main() {
// clang-format off
const auto injector = di::make_injector(
di::bind<>.to(42)
, di::bind<>.to(87.0)
);
// clang-format off
injector.create<controller>().start();
}
#else
int main() {}
#endif