13#include <unordered_map>
20#define AssertMove(T) \
21 static_assert(std::is_move_constructible_v<T>, #T " MUST BE MOVE CONSTRUCTIBLE!"); \
22 static_assert(std::is_move_assignable_v<T>, #T " MUST BE MOVE ASSIGNABLE!"); \
23 static_assert(std::is_nothrow_move_constructible_v<T>, #T " MUST BE NOTHROW MOVE CONSTRUCTIBLE!"); \
24 static_assert(std::is_nothrow_move_assignable_v<T>, #T " MUST BE NOTHROW MOVE ASSIGNABLE!")
32concept VoidT = std::is_void_v<T>;
36 std::is_arithmetic_v<T> ||
37 (std::is_enum_v<T> && std::is_convertible_v<std::underlying_type_t<T>,
int>);
50 std::is_same_v<T, char> || std::is_same_v<T, unsigned char> || std::is_same_v<T, signed char>
51#if defined(__cpp_char8_t)
52 || std::is_same_v<T, char8_t>
54 || std::is_same_v<T, char16_t> || std::is_same_v<T, char32_t> || std::is_same_v<T, wchar_t>;
57concept CStringT = std::is_same_v<T, const char*> || std::is_same_v<T, char*>;
60constexpr auto underlying(E e)
noexcept -> std::underlying_type_t<E>
62 return static_cast<std::underlying_type_t<E>
>(e);
66concept KeyT = std::convertible_to<T, std::string_view> ||
67 std::convertible_to<T, std::string> ||
68 std::convertible_to<T, const char*>;
70template<
class T,
class U>
73template<
typename To,
typename From>
75 std::is_polymorphic_v<From> && std::is_polymorphic_v<To> &&
76 (std::is_base_of_v<To, From> || std::is_base_of_v<From, To> ||
requires(From* f) {
86 { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;
92 { a <=> b } -> std::convertible_to<std::partial_ordering>;
98 { a == b } -> std::convertible_to<bool>;
99 { a != b } -> std::convertible_to<bool>;
105 { std::begin(a) } -> std::input_iterator;
106 { std::end(a) } -> std::sentinel_for<
decltype(std::begin(a))>;
111 std::ranges::input_range<R> &&
113 { r.size() } -> std::integral;
118 std::ranges::range<R> &&
119 requires(R& r, std::size_t n) {
125 { std::invoke(std::forward<F>(f)) } -> std::same_as<void>;
130template<
typename To,
typename From>
133 return dynamic_cast<To*
>(ptr);
138 return std::hash<T>{}(value);
142void iterate(
const T &container, std::function<
void(
const typename T::value_type &)> &&func) {
143 for (
const auto &e: container) {
152 return (std::numeric_limits<T>::min)();
157 return (std::numeric_limits<T>::max)();
185using Num = std::variant<int64_t, double>;
186using Any = std::variant<int64_t, double, std::string>;
189 return std::visit([](
const auto &
x) {
190 if constexpr (std::is_same_v<std::decay_t<
decltype(
x)>, std::string>) {
193 return std::to_string(
x);
199 return std::visit([
defaultI](
const auto &
x) {
200 if constexpr (std::is_same_v<std::decay_t<
decltype(
x)>, std::string>) {
209 return std::visit([
defaultF](
const auto &
x) {
210 if constexpr (std::is_same_v<std::decay_t<
decltype(
x)>, std::string>) {
213 return static_cast<double>(
x);
220#if defined(__cpp_lib_generic_unordered_lookup)
225 std::size_t operator()(
const T&
key)
const {
226 return std::hash<std::string_view>{}(
key);
231 template<
typename T1,
typename T2>
232 bool operator()(
const T1&
lhs,
const T2&
rhs)
const noexcept {
238 static_assert(
sizeof(
T1) == 0,
"Types must be comparable");
242using Dict = std::unordered_map<std::string, std::string, TransparentStringHash, TransparentEqual>;
243using DictAny = std::unordered_map<std::string, Any, TransparentStringHash, TransparentEqual>;
246using Dict = std::unordered_map<std::string, std::string>;
247using DictAny = std::unordered_map<std::string, Any>;
303 return {data, data +
len};
306template<CharacterT T>
311 const auto cstr =
reinterpret_cast<const char *
>(
ptr);
constexpr auto MinFloat128
Definition Types.hxx:168
constexpr auto MaxInt64
Definition Types.hxx:163
constexpr auto MaxFloat128
Definition Types.hxx:169
int32_t str2int32(const std::string &str, int32_t defaultI=MinInt32)
std::size_t getHash(const T &value)
Definition Types.hxx:137
constexpr auto MaxFloat32
Definition Types.hxx:165
To * dynamicCastX(From *ptr) noexcept
Definition Types.hxx:132
constexpr T MaxV()
Definition Types.hxx:156
std::string makeStr(const T *ptr, size_t len=0)
Definition Types.hxx:307
float str2float32(const std::string &str, float defaultF=MinFloat32)
constexpr auto MaxFloat64
Definition Types.hxx:167
std::optional< double > dictAnyReadFloat(const DictAny &dict, DictKeyT key)
Definition Types.hxx:275
constexpr auto MinFloat64
Definition Types.hxx:166
auto Any2String(const Any &v)
Definition Types.hxx:188
constexpr auto MaxInt32
Definition Types.hxx:161
const std::string & DictKeyT
Definition Types.hxx:248
std::unordered_map< std::string, Any > DictAny
Definition Types.hxx:247
std::variant< int64_t, double, std::string > Any
Definition Types.hxx:186
int64_t str2int64(const std::string &str, int64_t defaultI=MinInt64)
std::span< const byte > BytesView
Definition Types.hxx:289
void iterate(const T &container, std::function< void(const typename T::value_type &)> &&func)
Definition Types.hxx:142
std::vector< byte > Bytes
Definition Types.hxx:290
constexpr auto underlying(E e) noexcept -> std::underlying_type_t< E >
Definition Types.hxx:60
std::unordered_map< std::string, std::string > Dict
Definition Types.hxx:246
std::optional< std::string > dictAnyReadString(const DictAny &dict, DictKeyT key)
Definition Types.hxx:251
std::variant< int64_t, double > Num
Definition Types.hxx:185
Bytes makeBytes(const byte *data, size_t len)
Definition Types.hxx:299
constexpr auto MinInt32
Definition Types.hxx:160
auto Any2Integer(const Any &a, int64_t defaultI=MinInt64)
Definition Types.hxx:198
auto Any2Float(const Any &a, double defaultF=MinFloat64)
Definition Types.hxx:208
std::optional< int64_t > dictAnyReadInteger(const DictAny &dict, DictKeyT key)
Definition Types.hxx:263
double str2float64(const std::string &str, double defaultF=MinFloat64)
long double str2float128(const std::string &str, long double defaultF=MinFloat128)
constexpr auto MinFloat32
Definition Types.hxx:164
std::optional< const char * > nullTerminatedCStr(std::string_view str)
Definition Types.hxx:320
constexpr auto MinInt64
Definition Types.hxx:162
BytesView makeBytesView(const byte *data, size_t len)
Definition Types.hxx:292
constexpr T MinV()
Definition Types.hxx:151