-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcar-assembly-states.cpp
More file actions
149 lines (129 loc) · 3.76 KB
/
car-assembly-states.cpp
File metadata and controls
149 lines (129 loc) · 3.76 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <memory>
#include "requirecpp/decorators.hpp"
#include "requirecpp/requirecpp.hpp"
using requirecpp::decorator::Tagged;
// parts
class Motor {};
class Wheels {
public:
virtual std::string get_type() const = 0;
};
class SummerTires : public Wheels {
std::string get_type() const override { return "Summer"; }
};
class WinterTires : public Wheels {
std::string get_type() const override { return "Winter"; };
};
// interiour
class SteeringWheel {};
class Seats {};
struct Car {
std::shared_ptr<Motor> motor;
std::shared_ptr<Wheels> wheels;
std::shared_ptr<SteeringWheel> steering_wheel;
std::shared_ptr<Seats> seats;
std::string color;
};
enum class CarState {
ASSEMBLED,
PAINTED,
INTERIOR_ASSEMBLED,
QUALITY_CONTROL_PASSED
};
class CarFactory {
public:
explicit CarFactory(const std::function<void(Car&)>& on_finished)
: m_finished{on_finished} {
m_ctx.require(
[this](std::shared_ptr<Wheels> wheels, std::shared_ptr<Motor> motor) {
m_car.motor = motor;
m_car.wheels = wheels;
m_ctx.emplace<Tagged<Car, CarState::ASSEMBLED>>();
},
"outside");
m_ctx.require(
[this](std::shared_ptr<SteeringWheel> sw,
std::shared_ptr<Seats> seats) {
m_car.steering_wheel = sw;
m_car.seats = seats;
m_ctx.emplace<Tagged<Car, CarState::INTERIOR_ASSEMBLED>>();
},
"interior");
m_ctx.require([this](Tagged<Car, CarState::ASSEMBLED>&,
Tagged<Car, CarState::INTERIOR_ASSEMBLED>&,
Tagged<Car, CarState::PAINTED>&) { m_ready = true; },
"quality_control");
m_ctx.require(
[this](Tagged<Car, CarState::QUALITY_CONTROL_PASSED>&) {
m_finished(m_car);
},
"finish");
}
void paint(std::string_view color) {
std::cout << "paint " << color << std::endl;
m_car.color = color;
m_ctx.emplace<Tagged<Car, CarState::PAINTED>>();
}
void deliver_wheels(const std::shared_ptr<Wheels>& wheels) {
std::cout << "add wheels" << std::endl;
m_ctx.push(wheels);
}
void deliver_motor(const std::shared_ptr<Motor>& motor) {
std::cout << "add motor" << std::endl;
m_ctx.push(motor);
}
void install_steeringwheel() {
std::cout << "add steeringwheel" << std::endl;
m_ctx.emplace<SteeringWheel>();
}
void install_seats() {
std::cout << "add seats" << std::endl;
m_ctx.emplace<Seats>();
}
bool pull_car() {
// make sure car has wheels while pulling
auto wheels = m_ctx.get<Wheels>()->optional();
if (wheels) {
std::cout << "pulling car to different location" << std::endl;
} else {
std::cout << "car cannot be pulled, no wheels!" << std::endl;
}
return wheels != nullptr;
}
bool inspect() {
std::cout << "inspection ";
if (m_ready) {
std::cout << "passed!" << std::endl;
m_ctx.emplace<Tagged<Car, CarState::QUALITY_CONTROL_PASSED>>();
return true;
}
std::cout << "failed!" << std::endl;
return false;
}
void print_missing() { m_ctx.print_pending(); }
private:
std::function<void(Car&)> m_finished;
requirecpp::Context m_ctx;
Car m_car;
bool m_ready{false};
};
int main() {
CarFactory factory{[](Car& car) {
std::cout << "Car finished! Tires: " << car.wheels->get_type()
<< ", Color: " << car.color << std::endl;
}};
factory.pull_car();
factory.deliver_wheels(std::make_shared<SummerTires>());
factory.pull_car();
factory.deliver_motor(std::make_shared<Motor>());
factory.install_seats();
factory.paint("red");
if (!factory.inspect()) {
std::cout << "missing steps:" << std::endl;
factory.print_missing();
}
factory.install_steeringwheel();
factory.inspect();
return 0;
}