-
Notifications
You must be signed in to change notification settings - Fork 1
/
parts.h
119 lines (91 loc) · 2.91 KB
/
parts.h
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
#ifndef PARTS_H_INCLUDED
#define PARTS_H_INCLUDED
#include <iostream>
#include <type_traits>
namespace parts{
template<class Generator>
class Body{
private:
Generator& generator;
public:
Body(Generator& gen): generator(gen){}
};
class Engine{
};
template<class Generator>
class GasEngine:Engine{
private:
Generator& generator;
public:
GasEngine(Generator& gen): generator(gen){}
};
template<class Generator>
class ElectroEngine:Engine{
private:
Generator& generator;
public:
ElectroEngine(Generator& gen): generator(gen){}
};
class Transmission{
};
template<class Generator>
class AutomaticTransmission: public Transmission{
private:
Generator& generator;
public:
AutomaticTransmission(Generator& gen): generator(gen){}
};
template<class Generator>
class ManuelTransmission: Transmission{
private:
Generator& generator;
public:
ManuelTransmission(Generator& gen): generator(gen){}
void clutch(){
cout<<"use clutch"<<endl;
}
void switchGear(){
cout<<"use clutch"<<endl;
}
};
template<class Generator>
class CarClassification{
private:
Generator& generator;
public:
CarClassification(Generator& gen): generator(gen){
static_assert(Generator::template containsPart<parts::Body>(), "a car neads a Body");
static_assert(Generator::template containsPart<parts::Engine>(), "a car neads a Engine");
static_assert(Generator::template containsPart<parts::Transmission>(), "a car neads a Transmission");
};
};
template<class Generator>
class AutoPilot{
private:
Generator& generator;
template<class Gen>
struct AutomaticClutch{
static void run(Gen& generator){cout<<"no nead to use a clutch"<<endl;}
};
template<class Gen>
struct ManuelClutch{
static void run(Gen& generator){generator.clutch();}
};
template<class Gen>
using UseClutch = typename conditional<Gen::template containsPart<parts::AutomaticTransmission>(),
AutomaticClutch<Gen>,
ManuelClutch<Gen>>
::type;
void useClutch(){
UseClutch<Generator>::run(generator);
}
public:
AutoPilot(Generator& gen): generator(gen){
static_assert(Generator::template containsPart<parts::Body>(), "a car neads a Body");
static_assert(Generator::template containsPart<parts::Engine>(), "a car neads a Engine");
static_assert(Generator::template containsPart<parts::Transmission>(), "a car neads a Transmission");
useClutch();
};
};
}
#endif // PARTS_H_INCLUDED