C++Spec 1.1.0
BDD testing for C++
Loading...
Searching...
No Matches
util.hpp
Go to the documentation of this file.
1
5#pragma once
6#include <algorithm>
7#include <concepts>
8#include <functional>
9#include <optional>
10#include <ranges>
11#include <sstream>
12#include <string>
13#include <string_view>
14#include <type_traits>
15#include <utility>
16
17#ifdef __GNUG__
18#include <cxxabi.h>
19
20#include <memory>
21
22#endif
23
24namespace CppSpec::Util {
25
37inline std::string strip_type_keywords(std::string_view name) {
38 static constexpr std::string_view keywords[] = {"enum ", "class ", "struct ", "union "};
39 const auto continues_identifier = [](char c) {
40 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
41 };
42 std::string stripped;
43 stripped.reserve(name.size());
44 std::size_t i = 0;
45 while (i < name.size()) {
46 const bool at_word_start = i == 0 || !continues_identifier(name[i - 1]);
47 const auto* keyword = std::ranges::find_if(
48 keywords, [&](std::string_view word) { return at_word_start && name.substr(i).starts_with(word); });
49 if (keyword != std::ranges::end(keywords)) {
50 i += keyword->size();
51 } else {
52 stripped += name[i];
53 ++i;
54 }
55 }
56 return stripped;
57}
58
66#ifdef __GNUG__
67inline std::string demangle(const char* mangled) {
68 int status;
69 std::unique_ptr<char[], void (*)(void*)> result{
70 abi::__cxa_demangle(mangled, nullptr, nullptr, &status),
71 std::free,
72 };
73 return (status == 0) ? result.get() : mangled;
74}
75#else
76// MSVC's type_info::name() is already readable, but names every type with its keyword.
77inline std::string demangle(const char* name) {
78 return strip_type_keywords(name);
79}
80#endif
81
85template <bool result>
87 static_assert(result, "Assertion failed (see below for more information)");
88 static bool const value = result;
89};
90
103template <typename C>
104concept is_container = requires(C c) {
105 requires std::ranges::range<C>;
106 { c.max_size() } -> std::integral;
107 { c.empty() } -> std::convertible_to<bool>;
108};
109
119template <typename C>
120concept is_streamable = requires(std::ostream& os, const C& obj) {
121 { os << obj } -> std::same_as<std::ostream&>;
122};
123
124template <typename C>
126 // The C++ reference says that all "functions" (not FunctionObjects) respond to
127 // std::is_function<T>
128 std::is_function_v<C> ||
129
130 // An alternative from Stack Overflow question 18107368. Anything that could
131 // possibly be "functional" should be able to be cast to std::function<void()>
132 std::is_convertible_v<C, std::function<void()>> ||
133
134 // The C++ reference also says that something qualifies as a FunctionObject iff
135 // std::is_object_v<T> is true and the object responds to the call operator()
136 requires(C func) {
137 requires std::is_object_v<C>;
138 { func() } -> std::invocable<>;
139 };
140
141template <typename C>
143
151template <typename T>
152concept is_c_string = std::is_pointer_v<std::decay_t<T>> &&
153 std::same_as<std::remove_const_t<std::remove_pointer_t<std::decay_t<T>>>, char>;
154
161template <typename T>
162concept is_wide_character = std::same_as<std::remove_cv_t<T>, wchar_t> || std::same_as<std::remove_cv_t<T>, char8_t> ||
163 std::same_as<std::remove_cv_t<T>, char16_t> || std::same_as<std::remove_cv_t<T>, char32_t>;
164
174template <typename T>
176 std::is_pointer_v<T> && (std::same_as<std::remove_const_t<std::remove_pointer_t<T>>, signed char> ||
177 std::same_as<std::remove_const_t<std::remove_pointer_t<T>>, unsigned char> ||
178 std::same_as<std::remove_const_t<std::remove_pointer_t<T>>, wchar_t> ||
179 std::same_as<std::remove_const_t<std::remove_pointer_t<T>>, char8_t> ||
180 std::same_as<std::remove_const_t<std::remove_pointer_t<T>>, char16_t> ||
181 std::same_as<std::remove_const_t<std::remove_pointer_t<T>>, char32_t>);
182
188template <typename T>
189inline constexpr bool is_optional_v = false;
190
191template <typename T>
192inline constexpr bool is_optional_v<std::optional<T>> = true;
193
200template <typename T>
201concept is_string_like = is_c_string<T> || std::convertible_to<const std::remove_cvref_t<T>&, std::string_view>;
202
210template <typename A, typename E>
213 (std::ranges::range<A> &&
214 (std::ranges::range<E> ||
215 (is_string_like<E> && std::same_as<std::remove_cv_t<std::ranges::range_value_t<A>>, char>)));
216
223template <typename T>
224concept is_cmp_integer = std::integral<std::remove_cv_t<T>> && !std::same_as<std::remove_cv_t<T>, bool> &&
225 !std::same_as<std::remove_cv_t<T>, char> && !is_wide_character<T>;
226
231template <typename A, typename B>
232constexpr bool less(const A& a, const B& b) {
233 if constexpr (is_cmp_integer<A> && is_cmp_integer<B>) {
234 return std::cmp_less(a, b);
235 } else {
236 return a < b;
237 }
238}
239
243template <typename A, typename B>
244constexpr bool less_equal(const A& a, const B& b) {
245 if constexpr (is_cmp_integer<A> && is_cmp_integer<B>) {
246 return std::cmp_less_equal(a, b);
247 } else {
248 return a <= b;
249 }
250}
251
255template <typename A, typename B>
256constexpr bool greater(const A& a, const B& b) {
257 if constexpr (is_cmp_integer<A> && is_cmp_integer<B>) {
258 return std::cmp_greater(a, b);
259 } else {
260 return a > b;
261 }
262}
263
267template <typename A, typename B>
268constexpr bool greater_equal(const A& a, const B& b) {
269 if constexpr (is_cmp_integer<A> && is_cmp_integer<B>) {
270 return std::cmp_greater_equal(a, b);
271 } else {
272 return a >= b;
273 }
274}
275
276template <typename T>
277concept not_c_string = !std::is_same_v<T, const char*> && !std::is_same_v<T, char*> &&
278 !std::is_convertible_v<T, const char*> && !std::is_convertible_v<T, char*>;
279
290[[nodiscard]] inline std::string join(std::ranges::range auto& iterable, const std::string& separator = "") {
291 std::ostringstream oss;
292 bool first = true;
293 for (auto& thing : iterable) {
294 if (!first) {
295 oss << separator;
296 }
297 oss << thing; // use operator<< to convert and append
298 first = false;
299 }
300 return oss.str();
301}
302
303[[nodiscard]] inline std::string join_endl(std::ranges::range auto&& iterable) {
304 std::ostringstream oss;
305 bool first = true;
306 for (auto& thing : iterable) {
307 if (!first) {
308 oss << std::endl;
309 }
310 oss << thing; // use operator<< to convert and append
311 first = false;
312 }
313 return oss.str();
314}
315
316} // namespace CppSpec::Util
317
318namespace CppSpec {
319
336template <Util::is_string_like T>
337[[nodiscard]] constexpr std::optional<std::string_view> string_view_of(const T& text) {
338 if constexpr (std::is_array_v<T>) {
339 return std::string_view(std::ranges::begin(text), std::ranges::find(text, '\0'));
340 } else if constexpr (Util::is_c_string<T>) {
341 if (text == nullptr) {
342 return std::nullopt;
343 }
344 return std::string_view(text);
345 } else {
346 return std::string_view(text);
347 }
348}
349
350std::optional<std::string_view> string_view_of(const std::string&& text) = delete;
351
352} // namespace CppSpec
Checks whether to_start_with and to_end_with compare an actual value of type A with an expected one o...
Definition util.hpp:211
Checks whether T is a C string: a pointer to char, const or not, or a char array, which decays to one...
Definition util.hpp:152
Checks whether T is an integer type that std::cmp_less and its kin accept: every integral type but bo...
Definition util.hpp:224
Concept for checking whether T is a container type.
Definition util.hpp:104
Definition util.hpp:125
Checks whether T is a pointer to a character type other than char: signed char, unsigned char,...
Definition util.hpp:175
Definition util.hpp:142
Checks whether T can be streamed.
Definition util.hpp:120
Checks whether T is text that CppSpec::string_view_of() can view: a C string, or a type that converts...
Definition util.hpp:201
Checks whether T is a character type that std::ostream does not print as a character: wchar_t,...
Definition util.hpp:162
Definition util.hpp:277
Helper class for static assertions that has a built-in error string.
Definition util.hpp:86
constexpr bool is_optional_v
Checks whether T is a std::optional.
Definition util.hpp:189
std::string demangle(const char *name)
Definition util.hpp:77
constexpr std::optional< std::string_view > string_view_of(const T &text)
View text as a std::string_view, the one form the string matchers and the printer compare and print.
Definition util.hpp:337
constexpr bool less(const A &a, const B &b)
a < b, by value when both are integers (std::cmp_less, so a negative signed value is below any unsign...
Definition util.hpp:232
std::string strip_type_keywords(std::string_view name)
Removes the enum , class , struct and union keywords MSVC writes before every type in a type name.
Definition util.hpp:37
constexpr bool greater_equal(const A &a, const B &b)
a >= b, by value when both are integers (std::cmp_greater_equal), and with >= otherwise.
Definition util.hpp:268
constexpr bool greater(const A &a, const B &b)
a > b, by value when both are integers (std::cmp_greater), and with > otherwise.
Definition util.hpp:256
constexpr bool less_equal(const A &a, const B &b)
a <= b, by value when both are integers (std::cmp_less_equal), and with <= otherwise.
Definition util.hpp:244
std::string join(std::ranges::range auto &iterable, const std::string &separator="")
Implode a string.
Definition util.hpp:290