forked from BowenFu/matchit.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomDs.cpp
79 lines (71 loc) · 1.89 KB
/
customDs.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
#include "matchit.h"
#include <iostream>
struct DummyStruct
{
int32_t size;
char const *name;
};
constexpr bool operator==(DummyStruct const &lhs, DummyStruct const &rhs)
{
return lhs.size == rhs.size && lhs.name == rhs.name;
}
template <size_t I>
constexpr auto const &get(DummyStruct const &d)
{
if constexpr (I == 0)
{
return d.size;
}
else if constexpr (I == 1)
{
return d.name;
}
}
namespace std
{
template <>
class tuple_size<DummyStruct> : public std::integral_constant<size_t, 2>
{
};
} // namespace std
constexpr auto getSecond(DummyStruct const &d)
{
using namespace matchit;
Id<char const *> i;
return match(d)(
// clang-format off
pattern | ds(2, i) = expr(i),
pattern | _ = expr("not matched")
// clang-format on
);
}
// #if __cplusplus > 201703L
static_assert(getSecond(DummyStruct{1, "123"}) ==
std::string_view{"not matched"});
static_assert(getSecond(DummyStruct{2, "123"}) == std::string_view{"123"});
// #endif
// Another option to destructure your struct / class.
constexpr auto dsByMember(DummyStruct const &v)
{
using namespace matchit;
// compose patterns for destructuring struct DummyStruct.
constexpr auto dsA = dsVia(&DummyStruct::size, &DummyStruct::name);
Id<char const *> i;
return match(v)(
// clang-format off
pattern | dsA(2, i) = expr(i),
pattern | _ = expr("not matched")
// clang-format on
);
}
static_assert(dsByMember(DummyStruct{1, "123"}) ==
std::string_view{"not matched"});
static_assert(dsByMember(DummyStruct{2, "123"}) == std::string_view{"123"});
int32_t main()
{
std::cout << getSecond(DummyStruct{1, "123"}) << std::endl;
std::cout << dsByMember(DummyStruct{1, "123"}) << std::endl;
std::cout << getSecond(DummyStruct{2, "123"}) << std::endl;
std::cout << dsByMember(DummyStruct{2, "123"}) << std::endl;
return 0;
}