mimic++ v2
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 // Uses compiler specific extensions if possible.
96 // Even if no extension is used, undefined behavior is still raised by
97 // an empty function body and the noreturn attribute.
98#if defined(_MSC_VER) && !defined(__clang__) // MSVC
99 __assume(false);
100#else // GCC, Clang
101 __builtin_unreachable();
102#endif
103
104 // ReSharper disable once CppUnreachableCode
105 assert(false);
106 }
107#endif
108
109 // GCOVR_EXCL_STOP
110
111 [[nodiscard]]
112 constexpr bool is_matching(const Constness lhs, const Constness rhs) noexcept
113 {
114 return std::cmp_not_equal(0, to_underlying(lhs) & to_underlying(rhs));
115 }
116
117 [[nodiscard]]
118 constexpr bool is_matching(const ValueCategory lhs, const ValueCategory rhs) noexcept
119 {
120 return std::cmp_not_equal(0, to_underlying(lhs) & to_underlying(rhs));
121 }
122}
123
124namespace mimicpp::detail
125{
126 template <typename Parsed, typename... Rest>
127 struct unique;
128
129 template <typename... Uniques, typename First, typename... Others>
130 struct unique<
131 std::tuple<Uniques...>,
132 First,
133 Others...>
134 {
135 using current_t = std::conditional_t<
136 same_as_any<First, Uniques...>,
137 std::tuple<Uniques...>,
138 std::tuple<Uniques..., First>>;
139
140 using type_t = typename unique<
141 current_t,
142 Others...>::type_t;
143 };
144
145 template <typename... Uniques>
146 struct unique<std::tuple<Uniques...>>
147 {
148 using type_t = std::tuple<Uniques...>;
149 };
150
151 template <typename... Types>
152 using unique_list_t = typename unique<std::tuple<>, Types...>::type_t;
153}
154
155#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:100
constexpr bool is_matching(const Constness lhs, const Constness rhs) noexcept
Definition Utility.hpp:112
Constness
Definition Fwd.hpp:93
constexpr std::underlying_type_t< T > to_underlying(const T value) noexcept
Definition Utility.hpp:73
Definition Utility.hpp:24
Definition Utility.hpp:32