-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppless.hh
More file actions
67 lines (59 loc) · 2.59 KB
/
cppless.hh
File metadata and controls
67 lines (59 loc) · 2.59 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
//====================================================================================================
// Copyright (C) 2016-present ShIroRRen <http://shiror.ren>. =
// =
// Licensed under the F2DLPR License. =
// =
// YOU MAY NOT USE THIS FILE EXCEPT IN COMPLIANCE WITH THE LICENSE. =
// Provided "AS IS", WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, =
// unless required by applicable law or agreed to in writing. =
// =
// For the F2DLPR License terms and conditions, visit: <http://license.fileto.download>. =
//====================================================================================================
#pragma once
#include <initializer_list>
#include <utility>
#define __cppless_concat_impl(a, b) a##b
#define __cppless_concat(a, b) __cppless_concat_impl(a, b)
#define defer_func(_____) \
[[maybe_unused]] \
auto const __cppless_concat(__defer_func__, __COUNTER__) \
= [&]<typename _>(_&& __) { \
struct ___ { \
_ __; \
~___() { __(); } \
} ____{ std::forward<_>(__) }; \
return ____; \
}([&]() _____)
#define let(_, __) ([&](auto&& it) -> decltype(auto) __(_))
#define also(__, ___) \
([&]<typename _>(_&& it) -> decltype(auto) { \
___; \
return std::forward<_>(it); \
}(__))
template<typename T>
class when {
public:
explicit when(T const& v): val(v), ok(false) {}
template<class F>
when& add_case(T const& target, F&& func) {
if (!ok && val == target) ok = (func(val), true);
return *this;
}
template<class F>
when& add_case(std::initializer_list<T> targets, F&& func) {
if (!ok)
for (auto const& target : targets)
if (val == target) {
ok = (func(val), true);
break;
}
return *this;
}
template<class F>
void default_case(F&& func) {
if (!ok) func(val);
}
private:
T const& val;
bool ok;
};