-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCppCheese.spd
More file actions
executable file
·97 lines (75 loc) · 2.16 KB
/
CppCheese.spd
File metadata and controls
executable file
·97 lines (75 loc) · 2.16 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
https://pboyd.io/posts/one-c++-footgun // original article
class CheeseShop // An example to show how bad c++ is compared to speedie.
|messagetable| inventory
|string| ClerkName
constructor (|message| cfg)
.inventory = cfg["cheeses"]
.clerkName = cfg["clerk"]
syntax access (|string| cheeseName, |string|)
if (cheeseName ~= .clerkName)
return "Sir?"
return ("Yes", "No")(.inventory[cheeseName])
main
|| shop = CheeseShop(ConfigData.parse #require)
"Red Leicester? ${shop[`red leicester`]}"
"Tilsit? ${shop[`tilsit`]}"
"Wenslydale? ${shop[`wenslydale`]}"
"Cheddar? ${shop[`cheddar`]}"
"Barris? ${shop[`barris`]}"
/*
The original C++ uses 'toml::parse' that raises an exception... causing issues.
Speedie has no exceptions, but a far better error-handling system.
Speedie is refcounted, avoiding C++'s issues around constructors and destructors.
*/
|| ConfigData = `
cheeses
"Red Leicester"
"Flamange"
"Chester"
"Cheddar"
clerk
Barris
`
/* original C++
class CheeseShop {
public:
CheeseShop() :
clerkName{nullptr} {}
CheeseShop(std::string configPath) :
clerkName{nullptr}
{
auto cfg = toml::parse(configPath);
auto cheeses = toml::find<std::vector<std::string>>(cfg, "cheeses");
for (auto name : cheeses)
inventory.insert(name);
if (cfg.contains("clerk")) {
clerkName = new std::string{toml::find<std::string>(cfg, "clerk")};
}
}
~CheeseShop() {
if (clerkName)
delete clerkName;
}
std::string gotAny(std::string cheeseName) const {
if (clerkName && cheeseName == *clerkName)
return "Sir?";
return inventory.count(cheeseName) == 1 ? "Yes" : "No";
}
private:
std::unordered_set<std::string> inventory;
std::string *clerkName;
};
int main() {
CheeseShop shop;
try {
shop = CheeseShop{"config.toml"};
} catch(std::exception &e) {
// Silently continue anyway..
}
std::cout << "Red Leicester? " << shop.gotAny("Red Leicester") << std::endl;
std::cout << "Tilsit? " << shop.gotAny("Tilsit") << std::endl;
std::cout << "Wenslydale? " << shop.gotAny("Wenslydale") << std::endl;
std::cout << "Cheddar? " << shop.gotAny("Cheddar") << std::endl;
return 0;
}
*/