-
Notifications
You must be signed in to change notification settings - Fork 0
/
test1.cpp
138 lines (123 loc) · 3.23 KB
/
test1.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
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
#include "mycommon.h"
using namespace std;
struct Connection {
int id;
explicit Connection(int id) : id(id) {}
};
struct ConnectionBuilderBase {
virtual Connection connect() = 0;
};
template <bool Ready = false>
struct ConnectionBuilder : ConnectionBuilderBase {
string port;
string add;
bool usessh = false;
string ss1;
string ss2;
std::vector<string> args;
ConnectionBuilder withport(string port)
{
this->port = port;
return *this;
}
ConnectionBuilder<true>& withadd(string add)
{
this->add = add;
return static_cast<ConnectionBuilder<true>&>(static_cast<ConnectionBuilderBase&>(*this));
}
ConnectionBuilder<true>& withaddandport(string p)
{
auto pos = p.find(':');
if (pos != string::npos) {
this->add = p.substr(0, pos);
this->port = p.substr(pos + 1);
}
return static_cast<ConnectionBuilder<true>&>(static_cast<ConnectionBuilderBase&>(*this));
}
ConnectionBuilder& withssh(string ss1, string ss2 = "caf")
{
usessh = true;
this->ss1 = ss1;
this->ss2 = ss2;
return *this;
}
ConnectionBuilder& withargs(std::string args)
{
this->args.push_back(args);
return *this;
}
Connection connect()
{
// static_assert(Ready, "ADDRESS IS NEEDED TO CONNECT");
int id = 0;
return Connection(id);
}
};
Connection c = ConnectionBuilder<false>().withaddandport("IP_ADDRESS:8080").withargs("arg1").withssh("ssh1").connect();
struct [[nodiscard]] Car {
int handle;
Car() {}
[[nodiscard]] Car&& setBenz(int range) &&
{
handle = int(range);
return move(*this);
}
Car(Car&&) = default;
Car(const Car&) = delete;
};
struct Drinkable;
struct Eatable;
struct FoodVisitor {
virtual void visit(Eatable* eat) {};
virtual void visit(Drinkable* drink) {};
virtual ~FoodVisitor() = default;
};
struct DrinkParams {
int volume;
};
struct EatParams {
int weight;
};
struct Food {
virtual ~Food() = default;
// virtual Drinkable* asDrinkable() { return nullptr; }
// virtual Eatable* asEatable() { return nullptr; }
virtual void accept(FoodVisitor* visitor) = 0;
};
struct Drinkable : virtual Food {
virtual void drink(DrinkParams params) = 0;
// Drinkable* asDrinkable() override { return this; }
void accept(FoodVisitor* visitor) override { visitor->visit(this); }
};
struct Eatable : virtual Food {
virtual void eat(EatParams params) = 0;
// Eatable* asEatable() override { return this; }
void accept(FoodVisitor* visitor) override { visitor->visit(this); }
};
struct Pudding : Drinkable, Eatable {
void drink(DrinkParams params) override { cout << "drink" << params.volume << endl; }
void eat(EatParams params) override { cout << "eat" << params.weight << endl; }
void accept(FoodVisitor* visitor) override
{
Eatable::accept(visitor);
Drinkable::accept(visitor);
}
};
struct User : FoodVisitor {
void visit(Eatable* eat) override { eat->eat({1}); }
void visit(Drinkable* drink) override { drink->drink({2}); }
};
void User_eat(Food* food)
{
User user1;
food->accept(&user1);
}
int main()
{
Car benz = Car().setBenz(100); // 调用移动而不是拷贝
// Car().setBenz(100); // 由于写了[[nodiscard]],编译器会警告
Pudding p;
// p.accept(&run);
User_eat(&p);
return 0;
}