2#include <UnCompute/Base/Logger.h>
3#include <UnCompute/Base/Platform.h>
4#include <UnCompute/Base/ResultCode.h>
15 using Int16 = int16_t;
16 using Int32 = int32_t;
17 using Int64 = int64_t;
19 using UInt8 = uint8_t;
20 using UInt16 = uint16_t;
21 using UInt32 = uint32_t;
22 using UInt64 = uint64_t;
24 using Float32 = float;
25 using Float64 = double;
29 using SSize = ptrdiff_t;
31 inline constexpr struct
33 int Major = 0, Minor = 1, Patch = 0;
37 template<
class T,
class = std::enable_if_t<std::is_
integral_v<T>,
void>>
46 : m_SizeInBytes(sizeInBytes)
53 m_SizeInBytes =
static_cast<T
>(other.m_SizeInBytes);
61 m_SizeInBytes = other;
72 using MemorySize = MemorySizeImpl<USize>;
73 using MemorySize32 = MemorySizeImpl<UInt32>;
74 using MemorySize64 = MemorySizeImpl<UInt64>;
90 inline constexpr std::string_view TrimTypeName(std::string_view name)
92 name.remove_prefix(name.find_first_not_of(
' '));
93 name.remove_suffix(name.length() - name.find_last_not_of(
' ') - 1);
99 inline constexpr SVWrapper TypeNameImpl()
102 std::string_view fn = __FUNCSIG__;
103 fn.remove_prefix(fn.find_first_of(
"<") + 1);
104 fn.remove_suffix(fn.length() - fn.find_last_of(
">"));
106 std::string_view fn = __PRETTY_FUNCTION__;
107 fn.remove_prefix(fn.find_first_of(
'=') + 1);
108 fn.remove_suffix(fn.length() - fn.find_last_of(
']'));
110 return SVWrapper{ TrimTypeName(fn) };
120 inline constexpr std::string_view TypeName()
122 return Internal::TypeNameImpl<T>().value;
126 inline constexpr USize MaximumAlignment = 16;
132 template<
class T,
class U = T>
133 inline T AlignUp(T x, U align)
135 return (x + (align - 1u)) & ~(align - 1u);
143 inline T* AlignUpPtr(
const T* x, USize align)
145 return reinterpret_cast<T*
>(AlignUp(
reinterpret_cast<USize
>(x), align));
152 template<UInt32 A,
class T>
153 inline constexpr T AlignUp(T x)
155 return (x + (A - 1)) & ~(A - 1);
162 template<
class T,
class U = T>
163 inline T AlignDown(T x, U align)
165 return (x & ~(align - 1));
173 inline constexpr T* AlignDownPtr(
const T* x, USize align)
175 return reinterpret_cast<T*
>(AlignDown(
reinterpret_cast<USize
>(x), align));
182 template<UInt32 A,
class T>
183 inline constexpr T AlignDown(T x)
185 return (x & ~(A - 1));
193 inline constexpr T MakeMask(T bitCount, T leftShift)
195 auto typeBitCount =
sizeof(T) * 8;
196 auto mask = bitCount == typeBitCount ?
static_cast<T
>(-1) : ((1 << bitCount) - 1);
197 return static_cast<T
>(mask << leftShift);
200 inline void HashCombine(std::size_t& ) {}
208 template<
typename T,
typename... Args>
209 inline void HashCombine(std::size_t& seed,
const T& value,
const Args&... args)
212 seed ^= hasher(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
213 HashCombine(seed, args...);
217#define UN_MAKE_HASHABLE(TypeName, Template, ...) \
221 struct hash<TypeName> \
223 inline size_t operator()(const TypeName& value) const noexcept \
226 ::UN::HashCombine(seed, __VA_ARGS__); \
234 inline constexpr bool ValidationEnabled =
true;
237 inline constexpr bool ValidationEnabled =
false;
251 template<
class TDest,
class TSrc>
252 inline std::enable_if_t<std::is_base_of_v<std::remove_pointer_t<TSrc>, std::remove_pointer_t<TDest>>
253 && std::is_pointer_v<TSrc> && std::is_pointer_v<TDest>,
255 un_verify_cast(TSrc pSourceObject)
257 if constexpr (std::is_same_v<TSrc, TDest>)
259 return pSourceObject;
262 if constexpr (ValidationEnabled)
264 if (
auto* result =
dynamic_cast<TDest
>(pSourceObject))
269 UN_Assert(
false,
"Verifying cast failed");
272 return static_cast<TDest
>(pSourceObject);
277struct fmt::formatter<UN::MemorySizeImpl<T>> : fmt::formatter<std::string_view>
279 template<
typename FormatContext>
283 auto bytes =
static_cast<UN::Float64
>(size.
GetBytes());
285 const char* units[] = {
"B",
"kB",
"MB",
"GB",
"TB",
"PB",
"EB",
"ZB",
"YB" };
286 while (bytes >= 1024)
292 return fmt::format_to(ctx.out(),
"{:.2f}{}", bytes, units[i]);
Stores memory size in bytes, this is useful to format human-readable memory sizes.
Definition: Base.h:39
T GetBytes() const
Get memory size in bytes.
Definition: Base.h:66
A simple std::string_view wrapper.
Definition: Base.h:85
std::string_view value
Actual value of the string view.
Definition: Base.h:86