Quantify
An extensible C++ units library
Loading...
Searching...
No Matches
concepts.cppm
Go to the documentation of this file.
1// Copyright (c) 2024-2025, Víctor Castillo Agüero.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
20
21export module quantify.core:concepts;
22import :reduce_rules;
23import :preface;
24
25namespace quantify {
26 export template<typename U_FROM, typename U_TO, typename T>
29
30 // export template<typename S_FROM, typename S_TO, typename U_FROM, typename U_TO, typename T>
31 // concept ConvertibleScales = requires { scale_conversion_t<S_FROM, S_TO>::template forward<U_FROM, U_TO, T>; }
32 // || requires { scale_conversion_t<S_TO, S_FROM>::template backward<U_FROM, U_TO, T>; };
33 export template<typename S_FROM, typename S_TO, typename U_FROM, typename U_TO, typename T>
36
37 // export {
39 template <typename...>
40 struct compare {
41 template <typename...>
42 struct equals: std::false_type {};
43 };
44
45 template <typename A, typename B>
46 using compare_eq = typename compare<A>::template equals<B>;
47
48 template <typename Arg1>
49 struct compare<Arg1> {
50 template <typename...>
51 struct equals;
52
53 template <typename Arg2>
54 struct equals<Arg2> {
55 constexpr static bool value = std::is_same_v<Arg1, Arg2>;
56 };
57 };
58
59 template <template <typename...> typename Pack, typename Arg1>
60 struct compare<Pack<Arg1>> {
61 template <typename...>
62 struct equals;
63
64 template <typename Arg2>
65 struct equals<Pack<Arg2>> {
66 constexpr static bool value = compare<Arg1>::template equals<Arg2>::value;
67 };
68
69 template <typename Any>
70 struct equals<Any>: std::false_type {};
71 };
72
73 template <template <typename...> typename Pack, typename... Args1>
74 struct compare<Pack<Args1...>> {
75 template <typename...>
76 struct equals;
77
78 template <typename Arg2, typename... Args2>
79 requires (sizeof...(Args1) == (sizeof...(Args2) + 1) && (compare_eq<Arg2, Args1>::value || ...))
80 struct equals<Pack<Arg2, Args2...>> {
81 private:
82 using tmp = typename packtl::take_one_out_w_predicate<typename compare<Arg2>::template equals<void>, packtl::pack<Args1...>>::type;
83 public:
84 constexpr static bool value = compare_eq<tmp, packtl::pack<Args2...>>::value;
85 };
86
87 template <typename Arg2, typename... Args2>
88 requires (sizeof...(Args1) == (sizeof...(Args2) + 1) && (!compare_eq<Arg2, Args1>::value && ...))
89 struct equals<Pack<Arg2, Args2...>>: std::false_type { };
90
91 template <typename... Args2>
92 requires (sizeof...(Args1) != sizeof...(Args2))
93 struct equals<Pack<Args2...>>: std::false_type { };
94
95 template <typename Any>
96 struct equals<Any>: std::false_type {};
97 };
99 // }
100
101 export template<typename U1, typename U2>
102 concept CompareScales = compare_eq<typename reduce_scale<U1>::type, typename reduce_scale<U2>::type>::value;
103
104 export template<typename U1, typename U2>
106
109 export template<typename Q, typename S>
110 concept Quantity = std::same_as<typename Q::unit::scale, S>;
111
112
113 export template<typename U, typename T>
114 struct quantity;
115
126 export template <typename T>
127 using is_quantity = packtl::is_type<quantity, T>;
128
138 export template <typename T>
140
150 export template <typename T>
152}
Predicate that checks if a type is a quantity regardless of its unit.
Unit agnostic type concept for use with the auto keyword.
Core components of the Quantify library.
constexpr bool is_quantity_v
Predicate that checks if a type is a quantity regardless of its unit.
packtl::is_type< quantity, T > is_quantity
Predicate that checks if a type is a quantity regardless of its unit.