一呼百應, "one call, a hundred responses"
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1#pragma once
2
13#include <string>
14#include <string_view>
15#include <cxxabi.h>
16#include "attributes/common.hpp"
17
18using namespace std;
19
21
22namespace ein {
25
29template <typename T>
30const string_view type = [] ein_nodiscard ein_const static noexcept -> string_view {
31 // nb: copying into string is necessary because the demangled name is ephemeral
32 static const string body = [] ein_nodiscard ein_const static noexcept -> char const * {
33 int status;
34 return abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status);
35 }();
36 return body;
37}();
38
42const string_view type_of(auto const & t) noexcept {
43 return type<remove_cvref_t<decltype(t)>>;
44}
45
47template <typename T, typename ... candidates>
48concept one_of_t = (std::is_same_v<T,candidates> || ... || false);
49
51template <typename T, typename ... candidates>
52concept not_one_of_t = (!one_of_t<T,candidates...>);
54}
55
56#if defined(EIN_TESTING) || defined(EIN_TESTING_TYPES)
57#include <string_view>
58#include "types.hpp"
59
60struct Custom {};
61
62TEST_CASE("types","[types]") {
63 using namespace ein;
64 SECTION("provides correct demangled names") {
65 SECTION("Basic types") {
66 CHECK("int"s == type<int>);
67 CHECK("double"s == type<double>);
68 }
69
70 SECTION("User-defined types") {
71 CHECK(type<Custom> == "Custom"s);
72 }
73
74 SECTION("Pointers and References") {
75 CHECK(type<int*> == "int*");
76 CHECK(type<int&> == "int");
77 CHECK(type<int&> == "int");
78 }
79 }
80
81 SECTION("type_of extracts type from object") {
82 SECTION("Literals and basic types") {
83 int i = 42;
84 double d = 3.14;
85 CHECK(type_of(i) == "int");
86 CHECK(type_of(d) == "double");
87 }
88
89 SECTION("Constant and reference types") {
90 const int ci = 42;
91 CHECK(type_of(ci) == "int");
92
93 int i = 34;
94 int& ri = i;
95 REQUIRE(type_of(ri) == "int");
96 }
97 }
98
99 SECTION("one_of_t concept identifies types within list") {
100 using namespace ein;
101 SECTION("Valid and invalid cases") {
102 REQUIRE(one_of_t<int, int, double, float>);
103 REQUIRE_FALSE(one_of_t<char, int, double, float>);
104 }
105 }
106
107 SECTION("not_one_of_t concept identifies types outside of list") {
108 using namespace ein;
109 SECTION("Valid and invalid cases") {
110 REQUIRE(not_one_of_t<char, int, double, float>);
111 REQUIRE_FALSE(not_one_of_t<int, int, double, float>);
112 }
113 }
114}
115#endif
type T is not one of the candidates
Definition types.hpp:52
type T is one of the candidates
Definition types.hpp:48
#define ein_nodiscard
C++17 [[nodiscard]].
Definition common.hpp:165
#define ein_const
[[const]] is not const
Definition common.hpp:84
const string_view type_of(auto const &t) noexcept
returns the unmangled name of a the type of the (unused) argument passed to this function
Definition types.hpp:42
const string_view type
returns the unmangled name of a the type T
Definition types.hpp:30
Definition cpuid.cpp:16
Definition bf16.cpp:11