-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpressionFunctor.hpp
119 lines (101 loc) · 5.32 KB
/
ExpressionFunctor.hpp
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
#pragma once
#include <concepts>
#include <type_traits>
#include <utility>
#include <cmath>
namespace tomolatoon
{
template <class Source>
struct ExpressionFunctor;
template <class T>
struct is_ExpressionFunctor : std::false_type
{};
template <class T>
struct is_ExpressionFunctor<ExpressionFunctor<T>> : std::true_type
{};
template <class T>
concept as_ExpressionFunctor = is_ExpressionFunctor<std::remove_cvref_t<T>>::value;
template <class Source>
struct ExpressionFunctor
{
constexpr ExpressionFunctor(Source source)
: source(source) {}
#define TOMOLATOON_EXPRESSION_FUNCTOR_OP(op) \
template <class Rhs> \
friend auto operator##op(ExpressionFunctor& functor, Rhs&& rhs) \
{ \
const auto lambda = [&functor, rhs = std::forward<Rhs>(rhs)]() { return functor.source() op rhs; }; \
return ExpressionFunctor<decltype(lambda)>(std::move(lambda)); \
} \
template <class Rhs> \
friend auto operator##op(ExpressionFunctor&& functor, Rhs&& rhs) \
{ \
const auto lambda = [functor = std::move(functor), rhs = std::forward<Rhs>(rhs)]() { return functor.source() op rhs; }; \
return ExpressionFunctor<decltype(lambda)>(std::move(lambda)); \
} \
template <as_ExpressionFunctor Rhs> \
friend auto operator##op(ExpressionFunctor& functor, Rhs&& rhs) \
{ \
const auto lambda = [&functor, rhs = std::forward<Rhs>(rhs)]() { return functor.source() op rhs.source(); }; \
return ExpressionFunctor<decltype(lambda)>(std::move(lambda)); \
} \
template <as_ExpressionFunctor Rhs> \
friend auto operator##op(ExpressionFunctor&& functor, Rhs&& rhs) \
{ \
const auto lambda = [functor = std::move(functor), rhs = std::forward<Rhs>(rhs)]() { return functor.source() op rhs.source(); }; \
return ExpressionFunctor<decltype(lambda)>(std::move(lambda)); \
}
TOMOLATOON_EXPRESSION_FUNCTOR_OP(+);
TOMOLATOON_EXPRESSION_FUNCTOR_OP(-);
TOMOLATOON_EXPRESSION_FUNCTOR_OP(*);
TOMOLATOON_EXPRESSION_FUNCTOR_OP(/);
TOMOLATOON_EXPRESSION_FUNCTOR_OP(%);
auto operator()() const noexcept(noexcept(source()))
{
return source();
}
private:
Source source;
};
namespace Operators
{
namespace detail
{
// clang-format off
template <class F>
concept unary_arithmetic_chainer_function = requires(F f, double a)
{
{f(a)} -> std::convertible_to<double>;
};
// clang-format on
// clang-format off
template <class F>
concept unary_arithmetic_chainer_source_function = requires(F f)
{
{f()} -> std::convertible_to<double>;
};
// clang-format on
} // namespace detail
template <detail::unary_arithmetic_chainer_function F>
struct unary_arithmetic_chainer
{
constexpr unary_arithmetic_chainer(F f)
: f(f) {}
template <detail::unary_arithmetic_chainer_source_function F>
friend auto operator|(F f, const unary_arithmetic_chainer& self)
{
return [f, &self]() { return self.f(f()); };
}
template <std::convertible_to<double> A>
friend double operator|(A a, const unary_arithmetic_chainer& self)
{
return self.f(a);
}
private:
F f;
};
inline constexpr unary_arithmetic_chainer Floorf{[](const double v) { return std::floor(v); }};
inline constexpr unary_arithmetic_chainer Roundf{[](const double v) { return std::round(v); }};
inline constexpr unary_arithmetic_chainer Ceilf{[](const double v) { return std::ceil(v); }};
} // namespace Operators
} // namespace tomolatoon