-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_left_ptr_macro.cpp
More file actions
171 lines (140 loc) · 5.85 KB
/
Copy path03_left_ptr_macro.cpp
File metadata and controls
171 lines (140 loc) · 5.85 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
#include <HXTest.hpp>
#include <HXLibs/macro/join.hpp>
#include <HXLibs/macro/for.hpp>
#include <vector>
#include <functional>
#include <any>
#define __HX_MACRO_FOR_IMPL_THIS_2__() __HX_MACRO_FOR_IMPL_2__
#define __HX_MACRO_FOR_IMPL_2__0(macro, k, x, ...) macro(k, x) HX_DELAY(__HX_MACRO_FOR_IMPL_THIS_2__)()(macro, k, __VA_ARGS__)
#define __HX_MACRO_FOR_IMPL_2__1(...)
#define __HX_MACRO_FOR_IMPL_2__(macro, k, x, ...) HX_JOIN(__HX_MACRO_FOR_IMPL_2__, IF_EMPTY(x))(macro, k, x, __VA_ARGS__)
/**
* @brief for展开宏,
* @param macro 宏函数, 应该接受一个变量
* @param x... 宏参数, 它们会被依次传入 macro 函数
*/
#define HX_FOR_2(macro, k, x, ...) HX_EVAL(__HX_MACRO_FOR_IMPL_2__(macro, k, x, __VA_ARGS__))
struct IsCopy {
constexpr IsCopy() noexcept = default;
IsCopy(IsCopy const&) noexcept {
log::hxLog.error("发生了拷贝构造");
}
constexpr IsCopy(IsCopy&&) noexcept = default;
IsCopy& operator=(IsCopy const&) noexcept {
log::hxLog.error("发生了拷贝赋值");
return *this;
}
IsCopy& operator=(IsCopy&&) noexcept = default;
};
#define REFLLEFT_ARROW_NAME(__CLASS_TYPE__, __NAME__) \
namespace __my_metadata__ { \
struct HX_JOIN(HX_JOIN(__hx_my_, __LINE__), __NAME__) {}; \
} \
\
namespace { \
[[maybe_unused]] inline constexpr __my_metadata__::HX_JOIN( \
HX_JOIN(__hx_my_, __LINE__), __NAME__) __NAME__; \
} \
\
constexpr auto& operator<( \
__my_metadata__::HX_JOIN(HX_JOIN(__hx_my_, __LINE__), __NAME__) \
const&, \
__CLASS_TYPE__ const& HX_JOIN(__hx_my_self_, __LINE__)) noexcept { \
return HX_JOIN(__hx_my_self_, __LINE__).__NAME__; \
} \
constexpr auto& operator<( \
__my_metadata__::HX_JOIN(HX_JOIN(__hx_my_, __LINE__), __NAME__) \
const&, \
__CLASS_TYPE__& HX_JOIN(__hx_my_self_, __LINE__)) noexcept { \
return HX_JOIN(__hx_my_self_, __LINE__).__NAME__; \
}
#define REFLLEFT_ARROW(__CLASS_TYPE__, ...) \
constexpr auto& operator-(__CLASS_TYPE__ const& self) noexcept { \
return self; \
} \
constexpr auto& operator-(__CLASS_TYPE__& self) noexcept { \
return self; \
} \
HX_FOR_2(REFLLEFT_ARROW_NAME, __CLASS_TYPE__, __VA_ARGS__)
struct LeftPtr {
IsCopy data_1;
LeftPtr* self = nullptr;
auto func1(int x, std::vector<int>& arr) {
int sum = 0;
for (auto& v : arr)
sum += (v += x);
return sum;
}
auto func2(int x, std::vector<int>& arr) {
return func1(x, arr);
}
};
// 注册
REFLLEFT_ARROW(LeftPtr, data_1, self)
//////////////////////////////////////////////////////////////////////////////////////
namespace {
struct __hx__func1 {
template <typename... Args>
auto& operator()(Args&&... args) {
_f = [..._args = std::forward<Args>(args)](LeftPtr& self) mutable {
if constexpr (!requires {
self.func1(_args...);
}) {
static_assert(sizeof...(Args) < 0, "sb");
}
return self.func1(_args...);
};
return *this;
}
std::function<std::any(LeftPtr&)> _f;
};
[[maybe_unused]] inline static __hx__func1 func1;
}
[[maybe_unused]] decltype(auto) operator<(__hx__func1 _func, LeftPtr& self) {
return _func._f(self);
}
//////////////////////////////////////////////////////////////////////////////////////
namespace {
enum class LeftPtrType {};
template <typename Lambda>
struct SelfLambda {
using Type = LeftPtrType;
Lambda _cb;
};
template <typename... Args>
inline auto func2(Args&&... args) {
SelfLambda _resCb{[..._args = std::forward<Args>(args)](LeftPtr& self) mutable {
if constexpr (!requires {
self.func2(_args...);
}) {
static_assert(sizeof...(Args) < 0, "sb");
}
return self.func2(_args...);
}};
return _resCb;
}
}
template <typename T>
requires(std::is_same_v<LeftPtrType, typename T::Type>)
[[maybe_unused]] decltype(auto) operator<(T&& _func2, LeftPtr& self) {
return _func2._cb(self);
}
//////////////////////////////////////////////////////////////////////////////////////
HX_NO_WARNINGS_BEGIN
int main() {
std::vector<int> arr{1, 2, 3, 4 ,5};
// auto& res = data_1<-*(self<-(*(self<-lp))); // ub: *nullptr
LeftPtr obj;
auto res = data_1<-obj;
auto funcRes01 = func1(1, arr)<-obj;
auto funcRes02 = func2(1, arr)<-obj;
if (std::any_cast<int>(funcRes01) == funcRes02) {
log::hxLog.info("对的对的");
log::hxLog.warning("res: ", funcRes02);
} else {
log::hxLog.debug(arr);
log::hxLog.error("大错特错", std::any_cast<int>(funcRes01), funcRes02);
}
return 0;
}
HX_NO_WARNINGS_END