STX  1.0.0
common.h
Go to the documentation of this file.
1 
30 #pragma once
31 
32 #include <functional>
33 #include <type_traits>
34 
35 #include "stx/config.h"
36 
38 
39 template <typename Fn, typename... Args>
40 using invoke_result = typename std::invoke_result_t<Fn, Args...>;
41 
42 template <typename Fn, typename... Args>
43 constexpr bool invocable = std::is_invocable_v<Fn, Args...>;
44 
45 template <typename T>
46 constexpr bool movable = std::is_object_v<T>&& std::is_move_constructible_v<T>&&
47  std::is_assignable_v<T&, T>&& std::is_swappable_v<T>;
48 
49 template <typename T>
50 constexpr bool copy_constructible = std::is_copy_constructible_v<T>;
51 
52 template <typename From, typename To>
53 constexpr bool convertible = std::is_convertible_v<From, To>;
54 
55 template <typename T, typename Cmp = T, typename = void>
56 struct is_equality_comparable : std::false_type {};
57 
58 template <typename T, typename Cmp>
60  T, Cmp,
61  typename std::enable_if_t<
62  true,
63  decltype((std::declval<std::remove_reference_t<T> const&>() ==
64  std::declval<std::remove_reference_t<Cmp> const&>()) &&
65  (std::declval<std::remove_reference_t<T> const&>() !=
66  std::declval<std::remove_reference_t<Cmp> const&>()),
67  (void)0)>> : std::true_type {};
68 
70 template <typename T, typename Cmp = T>
72 
73 template <typename T>
74 constexpr bool is_reference = std::is_reference_v<T>;
75 
79 template <typename T>
80 using Ref = std::reference_wrapper<T>;
81 
83 template <typename T>
84 using ConstRef =
85  std::reference_wrapper<std::add_const_t<std::remove_reference_t<T>>>;
86 
88 template <typename T>
89 using MutRef =
90  std::reference_wrapper<std::remove_const_t<std::remove_reference_t<T>>>;
91 
92 #if defined(__cpp_concepts)
93 #if __cpp_concepts >= 201907L
94 template <typename T, typename Base>
95 concept Impl = std::is_base_of_v<Base, T>;
96 #endif
97 #endif
98 
99 namespace internal {
100 
110 constexpr int kxPtrFmtSize = static_cast<int>((sizeof(void*) << 1) + 2 + 2);
111 
112 // note that the sizes are also affected by the word size. i.e. int64_t and
113 // size_t will be 32-bit on some platforms, we thus can't allocate less memory
114 // than required on the stack, though it can be a bit much, such as int64_t on
115 // 32-bit platforms
116 
118 constexpr int kI32FmtSize = 11;
120 constexpr int kU32FmtSize = 10;
122 constexpr int kI16FmtSize = 6;
124 constexpr int kU16FmtSize = 5;
126 constexpr int kI8FmtSize = 4;
128 constexpr int kU8FmtSize = 3;
129 
130 } // namespace internal
131 
constexpr int kU16FmtSize
5 digits
Definition: common.h:124
std::reference_wrapper< std::remove_const_t< std::remove_reference_t< T > >> MutRef
MutRef is an always-mutable Ref
Definition: common.h:90
constexpr int kU32FmtSize
10 digits
Definition: common.h:120
std::reference_wrapper< std::add_const_t< std::remove_reference_t< T > >> ConstRef
ConstRef is an always-const Ref.
Definition: common.h:85
std::reference_wrapper< T > Ref
Definition: common.h:80
constexpr bool equality_comparable
Checks if the type has a compatible &#39;operator ==&#39; and &#39;operator!=&#39;.
Definition: common.h:71
constexpr bool copy_constructible
Definition: common.h:50
Definition: common.h:56
constexpr bool invocable
Definition: common.h:43
constexpr int kI16FmtSize
5 digits + 1 sign
Definition: common.h:122
constexpr bool convertible
Definition: common.h:53
constexpr bool movable
Definition: common.h:46
constexpr int kI8FmtSize
3 digits + 1 sign
Definition: common.h:126
typename std::invoke_result_t< Fn, Args... > invoke_result
Definition: common.h:40
#define STX_END_NAMESPACE
Definition: config.h:329
constexpr int kI32FmtSize
10 digits + 1 sign
Definition: common.h:118
constexpr int kxPtrFmtSize
Definition: common.h:110
constexpr int kU8FmtSize
3 digits
Definition: common.h:128
#define STX_BEGIN_NAMESPACE
Definition: config.h:325
constexpr bool is_reference
Definition: common.h:74