sparrow 0.9.0
Loading...
Searching...
No Matches
decimal_reference.hpp
Go to the documentation of this file.
1// Copyright 2024 Man Group Operations Limited
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#if defined(__cpp_lib_format)
18# include <format>
19# include <ostream>
20#endif
21
23
24namespace sparrow
25{
31 template <class L>
33 {
34 public:
35
37 using value_type = typename L::inner_value_type;
38 using reference = typename L::inner_reference;
39 using const_reference = typename L::inner_const_reference;
40 using size_type = typename L::size_type;
41 using difference_type = std::ptrdiff_t;
42
45 decimal_reference(decimal_reference&&) noexcept = default;
46
47 self_type& operator=(self_type&& rhs);
48 self_type& operator=(const self_type& rhs);
49
50 self_type& operator=(value_type&& rhs);
51 self_type& operator=(const value_type& rhs);
52
53 explicit operator float() const
54 requires(!is_int_placeholder_v<typename value_type::integer_type>);
55 explicit operator double() const
56 requires(!is_int_placeholder_v<typename value_type::integer_type>);
57 explicit operator long double() const
58 requires(!is_int_placeholder_v<typename value_type::integer_type>);
59
60 [[nodiscard]] explicit operator std::string() const
61 requires(!is_int_placeholder_v<typename value_type::integer_type>);
62
63 bool operator==(const value_type& rhs) const;
64 auto operator<=>(const value_type& rhs) const;
65
66 [[nodiscard]] const_reference value() const;
67 [[nodiscard]] value_type::integer_type storage() const;
68 [[nodiscard]] int scale() const;
69
70 private:
71
72 L* p_layout = nullptr;
73 size_type m_index = size_type(0);
74 };
75
76 /*************************************
77 * decimal_reference implementation *
78 *************************************/
79
80 template <typename L>
82 : p_layout(layout)
83 , m_index(index)
84 {
85 }
86
87 template <typename L>
89 {
90 p_layout->assign(std::forward<value_type>(rhs), m_index);
91 return *this;
92 }
93
94 template <typename L>
96 {
97 p_layout->assign(rhs, m_index);
98 return *this;
99 }
100
101 template <typename L>
103 {
104 this->operator=(rhs.value());
105 return *this;
106 }
107
108 template <typename L>
110 {
111 this->operator=(rhs.value());
112 return *this;
113 }
114
115 template <typename L>
117 {
118 return value() == rhs;
119 }
120
121 template <typename L>
123 {
124 return value() <=> rhs;
125 }
126
127 template <typename L>
129 {
130 return static_cast<const L*>(p_layout)->value(m_index);
131 }
132
133 template <typename L>
134 auto decimal_reference<L>::storage() const -> value_type::integer_type
135 {
136 return value().storage();
137 }
138
139 template <typename L>
141 {
142 return value().scale();
143 }
144
145 template <typename L>
147 requires(!is_int_placeholder_v<typename value_type::integer_type>)
148 {
149 return static_cast<float>(value());
150 }
151
152 template <typename L>
154 requires(!is_int_placeholder_v<typename value_type::integer_type>)
155 {
156 return static_cast<double>(value());
157 }
158
159 template <typename L>
161 requires(!is_int_placeholder_v<typename value_type::integer_type>)
162 {
163 return static_cast<long double>(value());
164 }
165}
166
167#if defined(__cpp_lib_format)
168
169template <typename L>
170struct std::formatter<sparrow::decimal_reference<L>>
171{
172 constexpr auto parse(std::format_parse_context& ctx)
173 {
174 return ctx.begin(); // Simple implementation
175 }
176
177 auto format(const sparrow::decimal_reference<L>& ref, std::format_context& ctx) const
178 {
179 const auto& value = ref.value();
180 return std::format_to(ctx.out(), "{}", value);
181 }
182};
183
184template <typename L>
185inline std::ostream& operator<<(std::ostream& os, const sparrow::decimal_reference<L>& value)
186{
187 os << std::format("{}", value);
188 return os;
189}
190
191#endif
decimal_reference(decimal_reference &&) noexcept=default
auto operator<=>(const value_type &rhs) const
value_type::integer_type storage() const
self_type & operator=(self_type &&rhs)
typename array_type::inner_value_type value_type
decimal_reference(const decimal_reference &)=default
decimal_reference< array_type > self_type
bool operator==(const value_type &rhs) const
typename array_type::inner_reference reference
typename array_type::size_type size_type
decimal_reference(L *layout, size_type index)
typename array_type::inner_const_reference const_reference
constexpr bool is_int_placeholder_v
Definition large_int.hpp:82
std::ostream & operator<<(std::ostream &os, const sparrow::nullval_t &)
Definition nullable.hpp:933