Nix (Dev) 3.5.10
dev - 3.5.10 - 1af9301
Loading...
Searching...
No Matches
concepts.hpp
Go to the documentation of this file.
1#ifndef IV_SRC_CORE_CONCEPTS_HPP_
2#define IV_SRC_CORE_CONCEPTS_HPP_
3
4#include "core/assert.hpp"
5
6#include <compare>
7#include <concepts>
8#include <cstdint>
9#include <ostream>
10#include <ranges>
11#include <span>
12#include <type_traits>
13#include <vector>
14
15namespace iv
16{
17
18template<typename T, typename Tag, typename... Capabilities>
19class Strong;
20
21template<typename... Ts>
22struct isStrongType : std::false_type
23{
24};
25
26template<typename... Ts>
27struct isStrongType<Strong<Ts...>> : std::true_type
28{
29};
30
31template<typename... Ts>
33
34template<typename T0, typename... Ts>
35struct allSame
36{
37 static constexpr bool value = (std::is_same_v<T0, Ts> && ...);
38};
39
40template<typename... Ts>
41static constexpr bool allSameV = iv::allSame<Ts...>::value;
42
43namespace ranges
44{
45
46template<typename T>
47concept spanableRange = std::constructible_from<std::span<const std::ranges::range_value_t<T>>, T>;
48
49template<typename T>
51 iv::ranges::spanableRange<T> && decltype(std::span {std::declval<T &>()})::extent != std::dynamic_extent;
52
53inline constexpr size_t sizeBytes(iv::ranges::spanableRange auto &&r)
54{
55 return std::span {r}.size_bytes();
56}
57
58template<size_t expected, iv::ranges::spanableRange R>
59inline constexpr void assertExactByteLength(R &&r)
60{
61 const std::span s {r};
62
63 if constexpr (decltype(s)::extent != std::dynamic_extent)
64 {
65 static_assert(s.size_bytes() == expected, "memory region does not have expected byte lengths");
66 }
67 else
68 {
69 IV_ASSERT(s.size_bytes() == expected, "memory region does not have expected byte lengths");
70 }
71}
72
74inline constexpr void assertEqualByteLengths(R0 &&r0, Rs &&...rs)
75 requires(sizeof...(Rs) > 0)
76{
77 const std::span s0 {r0};
78
79 if constexpr (decltype(s0)::extent != std::dynamic_extent)
80 {
81 constexpr size_t expectedSize = s0.size_bytes();
83 }
84 else
85 {
86 const size_t expectedSize = s0.size_bytes();
87
88 IV_ARG_CHECK(((std::span<const std::ranges::range_value_t<Rs>> {rs}.size_bytes() == expectedSize) && ...),
89 "memory regions don't have equal lengths");
90 }
91}
92
93}// namespace ranges
94
95namespace concepts
96{
97
98template<class T>
99concept destructible = std::is_nothrow_destructible_v<T>;
100
101template<class T, class... Args>
102concept constructibleFrom = destructible<T> && std::is_constructible_v<T, Args...>;
103
104template<class T>
106 requires { ::new (static_cast<void *>(nullptr)) T; };
107
108template<typename IterT, typename ContainerT>
110 std::same_as<IterT, typename ContainerT::iterator> || std::same_as<IterT, typename ContainerT::const_iterator>;
111
112template<typename PtrT, typename ContainerT>
114 std::same_as<PtrT, typename ContainerT::pointer> || std::same_as<PtrT, typename ContainerT::const_pointer>;
115
116template<typename T>
117concept container = requires(T a) {
118 { a.begin() } -> iv::concepts::containerIterator<T>;
120 { a.cbegin() } -> iv::concepts::containerIterator<T>;
122 { a.size() } -> std::same_as<typename T::size_type>;
123 typename T::value_type;
124};
125
126template<typename T>
127concept contiguousContainer = container<T> && requires(T a) {
128 { a.data() } -> iv::concepts::containerPointer<T>;
129};
130
131template<typename T>
132concept hasEmpty = requires(T a) {
133 { a.empty() } -> std::same_as<bool>;
134};
135
136template<typename T>
137concept resizableContainer = container<T> && requires(T &c, typename T::size_type s) {
138 T(s);
139 c.resize(s);
140};
141
142template<typename T>
143concept reservableContainer = iv::concepts::container<T> && requires(T &c, typename T::size_type s) { c.reserve(s); };
144
145template<typename T>
147 std::same_as<typename T::value_type, uint8_t>;
148
149template<typename T>
150concept StringRepresentable = requires(T a) {
151 { std::string_view(a) } -> std::convertible_to<std::string_view>;
152} || std::same_as<T, char> || std::same_as<T, std::vector<char>>;
153
154template<typename T>
155concept streamable = requires(std::ostream &os, T a) { os << a; };
156
157template<class T>
159
160template<class T>
162
163template<class T>
164concept integralStrongType = iv::concepts::strongType<T> && std::integral<typename T::wrapped_type>;
165
166template<class T>
167concept unsignedIntegralStrongType = iv::concepts::strongType<T> && std::unsigned_integral<typename T::wrapped_type>;
168
169template<typename T, typename Capability>
170concept strongTypeWithCapability = T::template hasCapability<Capability>();
171
172template<typename T>
174 std::same_as<T, uint8_t> || std::same_as<T, int8_t> || std::same_as<T, uint16_t> || std::same_as<T, int16_t> ||
175 std::same_as<T, uint32_t> || std::same_as<T, int32_t> || std::same_as<T, uint64_t> ||
176 std::same_as<T, int64_t> || std::same_as<T, double> || std::same_as<T, float> || std::same_as<T, std::string> ||
177 std::same_as<T, std::string_view> || std::same_as<T, bool> || std::same_as<T, std::byte>;
178
179template<typename T>
181 std::same_as<T, uint8_t> || std::same_as<T, int8_t> || std::same_as<T, uint16_t> || std::same_as<T, int16_t> ||
182 std::same_as<T, uint32_t> || std::same_as<T, int32_t> || std::same_as<T, uint64_t> ||
183 std::same_as<T, int64_t> || std::same_as<T, double> || std::same_as<T, float> || std::same_as<T, std::string> ||
184 std::same_as<T, bool> || std::same_as<T, std::byte>;
185
186template<typename T>
187concept IntegerType = std::is_same_v<T, uint64_t> || std::is_same_v<T, uint32_t> || std::is_same_v<T, uint16_t> ||
188 std::is_same_v<T, uint8_t> || std::is_same_v<T, int64_t> || std::is_same_v<T, int32_t> ||
189 std::is_same_v<T, int16_t>;
190
191template<typename T>
192concept FloatingPointType = std::is_same_v<T, double> || std::is_same_v<T, float>;
193
194template<typename T>
195concept EnumType = std::is_enum_v<T>;
196
197}// namespace concepts
198
199}// namespace iv
200
201#endif//IV_SRC_CORE_CONCEPTS_HPP_
#define IV_ASSERT(expr, assertion_made,...)
Definition assert.hpp:98
#define IV_ARG_CHECK(expr, msg,...)
Definition assert.hpp:77
Definition concepts.hpp:19
Definition concepts.hpp:195
Definition concepts.hpp:192
Definition concepts.hpp:187
Definition concepts.hpp:180
Definition concepts.hpp:173
Definition concepts.hpp:150
Definition concepts.hpp:102
Definition concepts.hpp:109
Definition concepts.hpp:113
Definition concepts.hpp:117
Definition concepts.hpp:127
Definition concepts.hpp:161
Definition concepts.hpp:105
Definition concepts.hpp:99
Definition concepts.hpp:132
Definition concepts.hpp:164
Definition concepts.hpp:143
Definition concepts.hpp:146
Definition concepts.hpp:137
Definition concepts.hpp:155
Definition concepts.hpp:170
Definition concepts.hpp:158
Definition concepts.hpp:167
Definition concepts.hpp:47
Definition concepts.hpp:50
constexpr size_t sizeBytes(iv::ranges::spanableRange auto &&r)
Definition concepts.hpp:53
constexpr void assertEqualByteLengths(R0 &&r0, Rs &&...rs)
Definition concepts.hpp:74
constexpr void assertExactByteLength(R &&r)
Definition concepts.hpp:59
Definition AlarmsManager.cpp:18
constexpr bool isStrongTypeV
Definition concepts.hpp:32
static constexpr bool allSameV
Definition concepts.hpp:41
Definition concepts.hpp:36
static constexpr bool value
Definition concepts.hpp:37
Definition concepts.hpp:23