libfxd 0.2.dev
A fixed-point library for C++.
Loading...
Searching...
No Matches
traits.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_TRAITS_HPP
9#define LIBFXD_TRAITS_HPP
10
11#include <concepts>
12#include <type_traits>
13
14#include "fixed.hpp"
15
16
17namespace fxd {
18
20 template<typename T>
21 struct is_fixed_point :
22 std::false_type
23 {};
25
27 template<int Int, int Frac, typename Raw>
28 struct is_fixed_point<fixed<Int, Frac, Raw>> :
29 std::true_type
30 {};
31
33 template<typename T>
34 inline constexpr
35 bool is_fixed_point_v = is_fixed_point<T>::value;
36
37
39 template<typename T>
40 struct is_signed_fixed_point : std::false_type {};
42
44 template<int Int, int Frac, typename Raw>
45 struct is_signed_fixed_point<fixed<Int, Frac, Raw>> :
46 std::is_signed<Raw> {};
47
49 template<typename T>
50 inline constexpr
51 bool is_signed_fixed_point_v = is_signed_fixed_point<T>::value;
52
53
55 template<typename T>
56 struct is_unsigned_fixed_point : std::false_type {};
58
60 template<int Int, int Frac, typename Raw>
61 struct is_unsigned_fixed_point<fixed<Int, Frac, Raw>> :
62 std::is_unsigned<Raw> {};
63
65 template<typename T>
66 inline constexpr
67 bool is_unsigned_fixed_point_v = is_unsigned_fixed_point<T>::value;
68
69
71 template<typename T>
72 requires (is_fixed_point_v<T>)
73 struct make_unsigned {
74 using type = fixed<T::int_bits,
75 T::frac_bits,
76 std::make_unsigned_t<typename T::raw_type>>;
77 };
78
80 template<typename T>
81 using make_unsigned_t = typename make_unsigned<T>::type;
82
83
84}
85
86
87namespace std {
88
90 template<typename Fxd>
91 requires (fxd::is_fixed_point_v<Fxd>)
92 struct common_type<Fxd, Fxd> {
94 using type = Fxd;
96 };
97
98
100 template<typename Fxd1,
101 typename Fxd2>
102 requires (fxd::is_fixed_point_v<Fxd1> && fxd::is_fixed_point_v<Fxd2>
103 &&
104 (is_convertible_v<Fxd1, Fxd2> || is_convertible_v<Fxd2, Fxd1>))
105 struct common_type<Fxd1, Fxd2> {
106 using type = conditional_t<
107 is_convertible_v<Fxd2, Fxd1>,
108 Fxd1,
109 Fxd2
110 >;
111 };
112
113
115 template<typename Fxd,
116 typename Other>
117 requires (fxd::is_fixed_point_v<Fxd> && is_arithmetic_v<Other>)
118 struct common_type<Fxd, Other> {
120 using type = Fxd;
122 };
123
124
126 template<typename Other,
127 typename Fxd>
128 requires (is_arithmetic_v<Other> && fxd::is_fixed_point_v<Fxd>)
129 struct common_type<Other, Fxd> {
131 using type = Fxd;
133 };
134
135}
136
137
138
139#endif
This is the namespace where the entire library is defined.
Definition: casting.hpp:19
constexpr bool is_signed_fixed_point_v
Helper template variable for fxd::is_signed_fixed_point
Definition: traits.hpp:51
constexpr bool is_fixed_point_v
Helper template variable for fxd::is_fixed_point
Definition: traits.hpp:35
STL namespace.
The fixed-point class template.
Definition: fixed.hpp:38
conditional_t< is_convertible_v< Fxd2, Fxd1 >, Fxd1, Fxd2 > type
Definition: traits.hpp:110