libfxd 0.2.dev
A fixed-point library for C++.
Loading...
Searching...
No Matches
expect.hpp
Go to the documentation of this file.
1/*
2 * libfxd - a fixed-point library for C++
3 *
4 * Copyright 2023 Daniel K. O.
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef LIBFXD_EXPECT_HPP
9#define LIBFXD_EXPECT_HPP
10
11#include <concepts>
12
13#include "concepts.hpp"
14#include "error.hpp"
15
16#include "detail/safe.hpp"
17#include "detail/safe-div.hpp"
18#include "detail/safe-mul.hpp"
19#include "detail/types.hpp"
20
21
23namespace fxd::expect {
24
26 template<typename T>
27 using maybe = detail::expected<T, error>;
28
29
30 using detail::safe::from_raw;
31
32 using detail::safe::make_fixed;
33
35 template<int Int, int Frac,
36 typename Raw = detail::select_int_t<Int + Frac>,
37 std::convertible_to<fixed<Int, Frac, Raw>> Src>
38 constexpr
40 make_fixed(Src src)
41 {
42 using Dst = fixed<Int, Frac, Raw>;
43 return expect::make_fixed<Dst>(src);
44 }
45
46
48 template<int Int, int Frac,
49 typename Raw = detail::select_uint_t<Int + Frac>,
50 std::convertible_to<fixed<Int, Frac, Raw>> Src>
51 constexpr
52 maybe<fixed<Int, Frac, Raw>>
53 make_ufixed(Src src)
54 {
55 using Dst = fixed<Int, Frac, Raw>;
56 return expect::make_fixed<Dst>(src);
57 }
58
59
60 using detail::safe::fixed_cast;
61
62
64 template<int Int, int Frac,
65 typename Raw = fxd::detail::select_int_t<Int + Frac>,
66 fixed_point Src>
67 [[nodiscard]]
68 constexpr
69 maybe<fixed<Int, Frac, Raw>>
70 fixed_cast(Src src)
71 noexcept
72 {
73 using Dst = fixed<Int, Frac, Raw>;
74 return expect::fixed_cast<Dst>(src);
75 }
76
77
79 template<int Int, int Frac,
80 typename Raw = fxd::detail::select_uint_t<Int + Frac>,
81 fixed_point Src>
82 [[nodiscard]]
83 constexpr
84 maybe<fixed<Int, Frac, Raw>>
85 ufixed_cast(Src src)
86 noexcept
87 {
88 using Dst = fixed<Int, Frac, Raw>;
89 return expect::fixed_cast<Dst>(src);
90 }
91
92
93 using detail::safe::to_int;
94
95
101 template<fixed_point Dst,
102 std::convertible_to<Dst> Src>
103 constexpr
104 maybe<Dst>
105 assign(Dst& dst,
106 Src src)
107 noexcept
108 {
109 auto result = expect::make_fixed<Dst>(src);
110 if (!result)
111 return result;
112 return dst = *result;
113 }
114
115
121 template<fixed_point Fxd>
122 constexpr
123 maybe<Fxd>
124 pre_inc(Fxd& f)
125 noexcept
126 {
127 auto result = detail::safe::incremented(f);
128 if (!result)
129 return result;
130 return f = *result;
131 }
132
133
135 template<fixed_point Fxd>
136 constexpr
137 maybe<Fxd>
138 post_inc(Fxd& f)
139 noexcept
140 {
141 Fxd old_f = f;
142 auto result = expect::pre_inc(f);
143 if (!result)
144 return result;
145 return old_f;
146 }
147
148
154 template<fixed_point Fxd>
155 constexpr
156 maybe<Fxd>
157 pre_dec(Fxd& f)
158 noexcept
159 {
160 auto result = detail::safe::decremented(f);
161 if (!result)
162 return result;
163 return f = *result;
164 }
165
166
168 template<fixed_point Fxd>
169 constexpr
170 maybe<Fxd>
171 post_dec(Fxd& f)
172 noexcept
173 {
174 Fxd old_f = f;
175 auto result = expect::pre_dec(f);
176 if (!result)
177 return result;
178 return old_f;
179 }
180
181
182 using detail::safe::negate;
183
184 using detail::safe::add;
185
186 using detail::safe::sub;
187
189 namespace down {
190
191 using detail::safe::down::div;
192
193 using detail::safe::down::mul;
194
195 } // namespace down
196
197
199 namespace up {
200
201 using detail::safe::up::div;
202
203 using detail::safe::up::mul;
204
205 } // namespace up
206
207
209 inline
210 namespace zero {
211
212 using detail::safe::zero::div;
213
214 using detail::safe::zero::mul;
215
216 } // namespace zero
217
218
219 using detail::safe::abs;
220
221}
222
223
224#endif
Concept to match any fxd::fixed
Definition: concepts.hpp:19
Return fxd::expect::maybe<...> result.
Definition: expect.hpp:23
constexpr maybe< fixed< Int, Frac, Raw > > ufixed_cast(Src src) noexcept
Convenience overload (unsigned version).
Definition: expect.hpp:85
constexpr maybe< Dst > assign(Dst &dst, Src src) noexcept
Assignment.
Definition: expect.hpp:105
constexpr maybe< fixed< Int, Frac, Raw > > make_ufixed(Src src)
Convenience overload (unsigned version).
Definition: expect.hpp:53
detail::expected< T, error > maybe
Alias to std::expected, or an equivalent implementation.
Definition: expect.hpp:27
constexpr maybe< Fxd > pre_dec(Fxd &f) noexcept
Pre-decrement (--f).
Definition: expect.hpp:157
constexpr maybe< Fxd > post_inc(Fxd &f) noexcept
Post-increment (f++).
Definition: expect.hpp:138
constexpr maybe< Fxd > post_dec(Fxd &f) noexcept
Post-decrement (f--).
Definition: expect.hpp:171
constexpr maybe< Fxd > pre_inc(Fxd &f) noexcept
Pre-increment (++f).
Definition: expect.hpp:124
constexpr Dst fixed_cast(Src src) noexcept
Convert a fixed point to a different type of fixed point.
Definition: casting.hpp:28
The fixed-point class template.
Definition: fixed.hpp:38