forked from jll63/yomm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adventure.cpp
124 lines (97 loc) · 3.2 KB
/
adventure.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
// Copyright (c) 2018 Jean-Louis Leroy
// 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)
#include <iostream>
#include <memory>
#include <string>
#include <yorel/yomm2/cute.hpp>
using yorel::yomm2::virtual_;
struct Character {
virtual ~Character() {}
};
struct Warrior : Character {};
struct Device {
virtual ~Device() {}
};
struct Hands : Device {};
struct Axe : Device {};
struct Banana : Device {};
struct Creature {
virtual ~Creature() {}
};
struct Dragon : Creature {};
struct Bear : Creature {};
register_class(Character);
register_class(Warrior, Character);
register_class(Device);
register_class(Hands, Device);
register_class(Axe, Device);
register_class(Banana, Device);
register_class(Creature);
register_class(Dragon, Creature);
register_class(Bear, Creature);
declare_method(
std::string,
fight,
(virtual_<Character&>, virtual_<Creature&>, virtual_<Device&>));
define_method(std::string, fight, (Character& x, Creature& y, Banana& z)) {
return "are you insane?";
}
define_method(std::string, fight, (Character& x, Creature& y, Axe& z)) {
return "not agile enough to wield";
}
define_method(std::string, fight, (Warrior& x, Creature& y, Axe& z)) {
return "and cuts it into pieces";
}
define_method(std::string, fight, (Warrior& x, Dragon& y, Axe& z)) {
return "and dies a honorable death";
}
define_method(std::string, fight, (Character& x, Dragon& y, Hands& z)) {
return "you just killed a dragon with your bare hands. Incredible isn't it?";
}
int main()
{
yorel::yomm2::update_methods();
std::unique_ptr<Character>
bob = std::make_unique<Character>(),
rambo = std::make_unique<Warrior>();
std::unique_ptr<Creature>
elliott = std::make_unique<Dragon>(),
paddington = std::make_unique<Bear>();
std::unique_ptr<Device>
hands = std::make_unique<Hands>(),
axe = std::make_unique<Axe>(),
chiquita = std::make_unique<Banana>();
std::cout
<< "bob fights elliot with axe:\n"
<< fight(*bob, *elliott, *axe) << "\n";
// bob fights elliot with axe:
// not agile enough to wield
std::cout
<< "rambo fights paddington with axe:\n"
<< fight(*rambo, *paddington, *axe) << "\n";
// rambo fights paddington with axe:
// and cuts it into pieces
std::cout
<< "rambo fights paddington with banana:\n"
<< fight(*rambo, *paddington, *chiquita) << "\n";
// rambo fights paddington with banana:
// are you insane?
std::cout
<< "rambo fights elliott with axe:\n"
<< fight(*rambo, *elliott, *axe) << "\n";
// rambo fights elliott with axe:
// and dies a honorable death
std::cout
<< "bob fights elliot with hands:\n"
<< fight(*bob, *elliott, *hands) << "\n";
// bob fights elliot with hands:
// you just killed a dragon with your bare hands. Incredible isn't it?
std::cout
<< "rambo fights elliot with hands:\n"
<< fight(*rambo, *elliott, *hands) << "\n";
// rambo fights elliot with hands:
// you just killed a dragon with your bare hands. Incredible isn't it?
return 0;
}