-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAny.cpp
More file actions
176 lines (143 loc) · 4.9 KB
/
Any.cpp
File metadata and controls
176 lines (143 loc) · 4.9 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// =====================================================================================
// Any.cpp // Klasse std::any
// =====================================================================================
module modern_cpp:any;
namespace AnySamples {
static void test_01_any()
{
std::any a{ 1 };
{
const std::string& type{ a.type().name() };
auto value{ std::any_cast<int>(a) };
std::println("Value: {} / Type: {}", value, type);
}
a = 3.14;
{
const std::string& type{ a.type().name() };
auto value{ std::any_cast<double>(a) };
std::println("Value: {} / Type: {}", value, type);
}
a = true;
{
const std::string& type{ a.type().name() };
auto value{ std::any_cast<bool>(a) };
std::println("Value: {} / Type: {}", value, type);
}
// bad cast
try
{
a = 1;
auto value{ std::any_cast<float>(a) };
}
catch (const std::bad_any_cast & e)
{
std::println("{}", e.what());
}
// has value
a = 1;
if (a.has_value())
{
const std::string& type{ a.type().name() };
auto value{ std::any_cast<int>(a) };
std::println("Value: {} / Type: {}", value, type);
}
// reset
a.reset();
if (!a.has_value())
{
std::println("No value!");
}
// pointer to contained data
a = 1;
int* i = std::any_cast<int>(&a);
std::println("{}", *i);
}
static void test_02_any()
{
std::any a1{ std::make_any<std::string>("Hello, std::any!") };
std::any a2{ std::make_any<double>(123.456) };
std::println("{}", std::any_cast<std::string&>(a1));
std::println("{}", std::any_cast<double&>(a2));
}
using Row = std::tuple<std::any, std::any, std::any>;
// helper method (forward declaration)
static std::string toString(const std::any&);
static void test_03_any()
{
Row row1{ 1, 2, 3 };
Row row2{ '1', std::string{"ABC"}, 99.99 };
Row row3{ true, 123, false };
std::vector<Row> mySheet{ };
// note: conversion to base class 'std::tuple<std::any, std::any, std::any>'
mySheet.push_back(row1);
mySheet.push_back(row2);
mySheet.push_back(row3);
for (const auto& [val1, val2, val3] : mySheet) {
std::cout
<< "Value1: " << toString(val1) << std::endl
<< "Value2: " << toString(val2) << std::endl
<< "Value3: " << toString(val3) << std::endl;
std::println("Value1: {}", toString(val1));
std::println("Value2: {}", toString(val2));
std::println("Value3: {}", toString(val3));
}
}
static std::string toString(const std::any& var)
{
if (var.type() == typeid (int)) {
return std::to_string(std::any_cast<int>(var));
}
else if (var.type() == typeid (double)) {
return std::to_string(std::any_cast<double>(var));
}
else if (var.type() == typeid (bool)) {
return std::to_string(std::any_cast<bool>(var));
}
else if (var.type() == typeid (char)) {
return std::to_string(std::any_cast<char>(var));
}
else if (var.type() == typeid (std::string)) {
return std::any_cast<std::string>(var);
}
else {
return std::string("<Unknown>");
}
}
static void test_04_any() {
// array with three instances of std::any (note: brace elision)
const std::array<std::any, 3> many
{
std::any{ 42 }, // int
std::any{ 1.23 }, // double
std::any{ std::string{"Hello"} } // std::string
};
// use random access on the container elements.
auto n{ std::any_cast<int>(many[0]) };
auto f{ std::any_cast<double>(many[1]) };
auto s{ std::any_cast<std::string>(many[2]) };
// query the container size
const std::size_t size = many.size();
// iterate container with an algorithm and execute a member function
bool hasValues{ std::all_of (
many.cbegin(),
many.cend(),
[] (const auto& a) -> bool {
return a.has_value();
}
) };
if (hasValues) {
std::println("All std::any objects contain a value.");
}
}
}
void main_any()
{
using namespace AnySamples;
test_01_any();
test_02_any();
test_03_any();
test_04_any();
}
// =====================================================================================
// End-of-File
// =====================================================================================