-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuple.cpp
172 lines (116 loc) · 4.76 KB
/
fuple.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
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
//
// Copyright (c) 2022-present DeepGrace (complex dot invoke at gmail dot com)
//
// 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)
//
// Official repository: https://github.com/deepgrace/smp
//
// g++ -I include -m64 -std=c++23 -s -Wall -O3 -o /tmp/fuple example/fuple.cpp
#include <cassert>
#include <iostream>
#include <fuple.hpp>
// fuple (flat tuple) implemented with multiple inheritance is a drop-in replacement for std::tuple
int main(int argc, char* argv[])
{
smp::fuple f0;
smp::fuple f1(std::string("C++23"));
int i = 2022;
double j = 11.05;
smp::fuple<short, char> f2;
smp::fuple<int&, smp::fuple<double&>, char> f3(i, smp::tie(j), 'X');
static_assert(smp::fuple_index<char>(f2) == 1);
static_assert(smp::fuple_index<short>(f2) == 0);
static_assert(smp::fuple_index<int>(f3) == 3);
static_assert(smp::fuple_index<int&, 0>(f3) == 0);
static_assert(smp::fuple_index<int, 1>(f3) == 0);
static_assert(smp::fuple_index<int&, 1>(f3) == 0);
int p = 23;
std::string s = "tmp";
smp::tie(p, s, smp::ignore) = smp::make_fuple(45, std::string("C++ TMP"), 9.8);
assert(p == 45);
assert(s == "C++ TMP");
auto f4 = smp::make_fuple(j, std::string("template"));
auto f5 = smp::forward_as_fuple(p, s, std::string("smp"));
auto f6 = smp::fuple_cat(f0, f1, f2, f3, f4, f5);
smp::get<int>(f5) = 75;
smp::get<1>(f5) = std::string("c++ tmp");
assert(p == 75);
assert(s == "c++ tmp");
using type = decltype(f6);
static_assert(smp::is_fuple_v<type>);
static_assert(smp::fuple_size_v<type> == 11);
static_assert(std::is_same_v<smp::fuple_element_t<0, type>, std::string>);
static_assert(std::is_same_v<smp::fuple_element_t<4, type>, smp::fuple<double&>>);
// extract the nth member by base class type
assert((f3.smp::element<0, int&>::value == 2022));
assert((f3.smp::element<1, smp::fuple<double&>>::value.smp::element<0, double&>::value == 11.05));
smp::fuple f(1, 3, 2, 'X', std::string("template"), 11.2d);
smp::fuple g(4, 7, 8, 'Y', std::string("C++23"), 22.4d);
auto w = smp::make_fuple(f, g);
using element_t = smp::element<4, std::string>;
assert(f.element_t::value == "template");
assert((w.smp::element<0, decltype(f)>::value.element_t::value == "template"));
assert((w.smp::element<1, decltype(f)>::value.element_t::value == "C++23"));
using triple = smp::fuple<int, double, std::string>;
triple f7(20, 11.06, "smp");
smp::fuple f8(f7);
smp::fuple f9 = f8;
triple f10(std::move(f9));
triple f11(30, 22.12, "smp");
triple f12(40, 9.23, "tmp");
triple f13(50, 12.77, "cpp");
f10 = f11;
const smp::fuple<> cf1;
const smp::fuple<> cf2;
f11.swap(f12);
cf1.swap(cf2);
assert(smp::get<double>(f8) == 11.06);
assert(smp::get<std::string>(f9) == "");
assert(smp::get<int>(f10) == 30);
assert(smp::get<1>(f11) == 9.23);
assert(smp::get<2>(f12) == "smp");
auto print_backward = []<typename... Args>(Args&&... args)
{
const char* sep = " ";
((std::cout << args << sep, sep) = ... = " ");
std::cout << std::endl;
};
smp::for_each(print_backward, f12);
std::cout << std::endl;
smp::for_each(print_backward, f11);
std::cout << std::endl;
smp::for_each(print_backward, f13);
std::cout << std::endl;
smp::apply(print_backward, f12);
smp::apply(print_backward, f11);
smp::apply(print_backward, f13);
std::cout << std::endl;
// apply a function to every nth field of smp::fuples, takes the longest common fields number
smp::zip(print_backward, f12, f11, f13);
std::cout << std::endl;
std::cout << "f12 < f13 " << (f12 < f13) << std::endl;
std::cout << "f12 <= f13 " << (f12 <= f13) << std::endl;
std::cout << "f12 != f13 " << (f12 != f13) << std::endl;
std::cout << "f12 == f13 " << (f12 == f13) << std::endl;
std::cout << "f12 >= f13 " << (f12 >= f13) << std::endl;
std::cout << "f12 > f13 " << (f12 > f13) << std::endl;
// convert a fuple to a tuple or vice versa
auto mf = smp::make_fuple(9, 's');
auto mt = std::make_tuple(9, 's');
auto ft = smp::fuple_to_tuple(mf);
auto tf = smp::tuple_to_fuple(mt);
assert(mf == tf);
assert(mt == ft);
// create a smp::fuple or std::tuple of lvalue references from another smp::fuple or std::tuple
// i.e
// smp::fuple<Args...> -> smp::fuple<Args&...>
// std::tuple<Args...> -> std::tuple<Args&...>
auto x = smp::tied_fuple(tf);
auto y = smp::tied_tuple(ft);
smp::get<0>(x) = 100;
std::get<1>(y) = 'S';
assert(smp::get<0>(tf) == 100);
assert(std::get<1>(ft) == 'S');
return 0;
}