summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Gianforcaro <bgianf@serenityos.org>2021-09-05 00:55:16 -0700
committerAndreas Kling <kling@serenityos.org>2021-09-05 20:08:57 +0200
commit112de58fe0601ef95544ab474f35ade14f947391 (patch)
treef7319afeff0c2a981590e2cfafe9cc57932f4e01
parent100b3835f023205db34129cca3cf854ad908478c (diff)
downloadserenity-112de58fe0601ef95544ab474f35ade14f947391.zip
AK: Add AssertSize utility template to provide rich type size assertions
This type is useful, as the sizes will be visible in the compiler error messages, as they will be part of the template parameters. This is not possible with a normal static_assert of the sizeof a type.
-rw-r--r--AK/StdLibExtraDetails.h16
1 files changed, 16 insertions, 0 deletions
diff --git a/AK/StdLibExtraDetails.h b/AK/StdLibExtraDetails.h
index f1f499e5fd..d6541037e1 100644
--- a/AK/StdLibExtraDetails.h
+++ b/AK/StdLibExtraDetails.h
@@ -475,6 +475,21 @@ using AddRvalueReference = typename __AddReference<T>::RvalueType;
template<class T>
requires(IsEnum<T>) using UnderlyingType = __underlying_type(T);
+template<typename T, unsigned ExpectedSize, unsigned ActualSize>
+struct __AssertSize : TrueType {
+ static_assert(ActualSize == ExpectedSize,
+ "actual size does not match expected size");
+
+ consteval explicit operator bool() const { return value; }
+};
+
+// Note: This type is useful, as the sizes will be visible in the
+// compiler error messages, as they will be part of the
+// template parameters. This is not possible with a
+// static_assert on the sizeof a type.
+template<typename T, unsigned ExpectedSize>
+using AssertSize = __AssertSize<T, ExpectedSize, sizeof(T)>;
+
template<typename T>
inline constexpr bool IsTrivial = __is_trivial(T);
@@ -543,6 +558,7 @@ inline constexpr bool IsSpecializationOf<U<Us...>, U> = true;
using AK::Detail::AddConst;
using AK::Detail::AddLvalueReference;
using AK::Detail::AddRvalueReference;
+using AK::Detail::AssertSize;
using AK::Detail::CommonType;
using AK::Detail::Conditional;
using AK::Detail::CopyConst;