tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
proxy_reference.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_LINALG_PROXY_REFERENCE_HPP
4#define TETL_LINALG_PROXY_REFERENCE_HPP
5
8
9namespace etl::linalg::detail {
10
11struct proxy_reference_base { };
12
13template <typename Reference, typename Value, typename Derived>
14struct proxy_reference : proxy_reference_base {
15 using reference_type = Reference;
16 using value_type = Value;
17 using derived_type = Derived;
18
19 constexpr explicit proxy_reference(Reference reference) // NOLINT(bugprone-crtp-constructor-accessibility)
20 : _reference(reference)
21 {
22 }
23
24 constexpr operator value_type() const // NOLINT(readability-const-return-type)
25 {
26 return static_cast<value_type>(static_cast<Derived const&>(*this).to_value(_reference));
27 }
28
29 constexpr friend auto operator-(derived_type const& cs) { return -value_type(cs); }
30
31 template <typename Rhs>
33 constexpr friend auto operator+(derived_type lhs, Rhs rhs)
34 {
35 using rhs_value_type = typename Rhs::value_type;
36 return value_type(lhs) + rhs_value_type(rhs);
37 }
38
39 template <typename Rhs>
41 constexpr friend auto operator+(derived_type lhs, Rhs rhs)
42 {
43 return value_type(lhs) + rhs;
44 }
45
46 template <typename Lhs>
48 constexpr friend auto operator+(Lhs lhs, derived_type rhs)
49 {
50 return lhs + value_type(rhs);
51 }
52
53 template <typename Rhs>
55 constexpr friend auto operator-(derived_type lhs, Rhs rhs)
56 {
57 using rhs_value_type = typename Rhs::value_type;
58 return value_type(lhs) - rhs_value_type(rhs);
59 }
60
61 template <typename Rhs>
63 constexpr friend auto operator-(derived_type lhs, Rhs rhs)
64 {
65 return value_type(lhs) - rhs;
66 }
67
68 template <typename Lhs>
70 constexpr friend auto operator-(Lhs lhs, derived_type rhs)
71 {
72 return lhs - value_type(rhs);
73 }
74
75 template <typename Rhs>
77 constexpr friend auto operator*(derived_type lhs, Rhs rhs)
78 {
79 using rhs_value_type = typename Rhs::value_type;
80 return value_type(lhs) * rhs_value_type(rhs);
81 }
82
83 template <typename Rhs>
85 constexpr friend auto operator*(derived_type lhs, Rhs rhs)
86 {
87 return value_type(lhs) * rhs;
88 }
89
90 template <typename Lhs>
92 constexpr friend auto operator*(Lhs lhs, derived_type rhs)
93 {
94 return lhs * value_type(rhs);
95 }
96
97 template <typename Rhs>
99 constexpr friend auto operator/(derived_type lhs, Rhs rhs)
100 {
101 using rhs_value_type = typename Rhs::value_type;
102 return value_type(lhs) / rhs_value_type(rhs);
103 }
104
105 template <typename Rhs>
107 constexpr friend auto operator/(derived_type lhs, Rhs rhs)
108 {
109 return value_type(lhs) / rhs;
110 }
111
112 template <typename Lhs>
114 constexpr friend auto operator/(Lhs lhs, derived_type rhs)
115 {
116 return lhs / value_type(rhs);
117 }
118
119 constexpr friend auto abs(derived_type const& x)
120 {
121 return abs_if_needed(value_type(static_cast<this_type const&>(x)));
122 }
123
124 constexpr friend auto real(derived_type const& x)
125 {
126 return real_if_needed(value_type(static_cast<this_type const&>(x)));
127 }
128
129 constexpr friend auto imag(derived_type const& x)
130 {
131 return imag_if_needed(value_type(static_cast<this_type const&>(x)));
132 }
133
134 constexpr friend auto conj(derived_type const& x)
135 {
136 return conj_if_needed(value_type(static_cast<this_type const&>(x)));
137 }
138
139private:
140 using this_type = proxy_reference<Reference, Value, Derived>;
141 Reference _reference; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members)
142};
143
144} // namespace etl::linalg::detail
145
146#endif // TETL_LINALG_PROXY_REFERENCE_HPP
constexpr bool is_base_of_v
Definition is_base_of.hpp:39