mimic++ v4
Loading...
Searching...
No Matches
Utility.hpp
Go to the documentation of this file.
1// // Copyright Dominic (DNKpp) Koepke 2024 - 2024.
2// // Distributed under the Boost Software License, Version 1.0.
3// // (See accompanying file LICENSE_1_0.txt or copy at
4// // https://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef MIMICPP_UTILITY_HPP
7#define MIMICPP_UTILITY_HPP
8
9#pragma once
10
11#include "mimic++/Fwd.hpp"
12
13#include <cassert>
14#include <source_location>
15#include <string_view>
16#include <tuple>
17#include <utility>
18
19namespace mimicpp
20{
21 template <typename...>
23 : public std::bool_constant<false>
24 {
25 };
26
27 template <std::size_t priority>
30 : public priority_tag<priority - 1>
32 {
33 };
34
35 template <>
36 struct priority_tag<0>
37 {
38 };
39
40 [[nodiscard]]
42 const std::source_location& lhs,
43 const std::source_location& rhs
44 ) noexcept
45 {
46 return std::string_view{lhs.file_name()} == std::string_view{rhs.file_name()}
47 && std::string_view{lhs.function_name()} == std::string_view{rhs.function_name()}
48 && lhs.line() == rhs.line()
49 && lhs.column() == rhs.column();
50 }
51
52 template <typename From, typename To>
54 requires
55 {
56 static_cast<To>(std::declval<From>());
57 };
58
59 template <typename From, typename To>
62 && requires
63 {
64 { static_cast<To>(std::declval<From>()) } noexcept;
65 };
66
67 template <typename T, typename... Others>
68 concept same_as_any = (... || std::same_as<T, Others>);
69
70 template <typename T>
71 requires std::is_enum_v<T>
72 [[nodiscard]]
73 constexpr std::underlying_type_t<T> to_underlying(const T value) noexcept
74 {
75 return static_cast<std::underlying_type_t<T>>(value);
76 }
77
78 template <typename T, template <typename> typename Trait>
79 concept satisfies = Trait<T>::value;
80
81 // GCOVR_EXCL_START
82
83#ifdef __cpp_lib_unreachable
84 using std::unreachable;
85#else
86
92 [[noreturn]]
93 inline void unreachable()
94 {
95 assert(false);
96
97 // Uses compiler specific extensions if possible.
98 // Even if no extension is used, undefined behavior is still raised by
99 // an empty function body and the noreturn attribute.
100#if defined(_MSC_VER) && !defined(__clang__) // MSVC
101 __assume(false);
102#else // GCC, Clang
103 __builtin_unreachable();
104#endif
105 }
106#endif
107
108 // GCOVR_EXCL_STOP
109
110 [[nodiscard]]
111 constexpr bool is_matching(const Constness lhs, const Constness rhs) noexcept
112 {
113 return std::cmp_not_equal(0, to_underlying(lhs) & to_underlying(rhs));
114 }
115
116 [[nodiscard]]
117 constexpr bool is_matching(const ValueCategory lhs, const ValueCategory rhs) noexcept
118 {
119 return std::cmp_not_equal(0, to_underlying(lhs) & to_underlying(rhs));
120 }
121}
122
123namespace mimicpp::detail
124{
125 template <typename Parsed, typename... Rest>
126 struct unique;
127
128 template <typename... Uniques, typename First, typename... Others>
129 struct unique<
130 std::tuple<Uniques...>,
131 First,
132 Others...>
133 {
134 using current_t = std::conditional_t<
135 same_as_any<First, Uniques...>,
136 std::tuple<Uniques...>,
137 std::tuple<Uniques..., First>>;
138
139 using type_t = typename unique<
140 current_t,
141 Others...>::type_t;
142 };
143
144 template <typename... Uniques>
145 struct unique<std::tuple<Uniques...>>
146 {
147 using type_t = std::tuple<Uniques...>;
148 };
149
150 template <typename... Types>
151 using unique_list_t = typename unique<std::tuple<>, Types...>::type_t;
152}
153
154#endif
Definition Utility.hpp:53
Definition Utility.hpp:68
Definition Utility.hpp:79
Definition BoostTest.hpp:20
void unreachable()
Invokes undefined behavior.
Definition Utility.hpp:93
constexpr bool is_same_source_location(const std::source_location &lhs, const std::source_location &rhs) noexcept
Definition Utility.hpp:41
ValueCategory
Definition Fwd.hpp:195
constexpr bool is_matching(const Constness lhs, const Constness rhs) noexcept
Definition Utility.hpp:111
Constness
Definition Fwd.hpp:188
constexpr std::underlying_type_t< T > to_underlying(const T value) noexcept
Definition Utility.hpp:73
Definition Utility.hpp:24
Definition Utility.hpp:32