mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:17:35 +00:00
AK/Format: Compute TypeErasedParameter type and size at compile-time
Problem: - Type and size information is known at compile-time, but computations are being performed using run-time parameters. Solution: - Move function arguments to be template arguments. - Convert to `consteval` where possible. - Decorate functions with `constexpr` which are used in both run-time and compile-time contexts.
This commit is contained in:
parent
6cbf88ad4c
commit
e117756d9f
2 changed files with 44 additions and 45 deletions
53
AK/Format.h
53
AK/Format.h
|
@ -65,25 +65,26 @@ struct TypeErasedParameter {
|
|||
Custom
|
||||
};
|
||||
|
||||
static Type get_type_from_size(size_t size, bool is_unsigned)
|
||||
template<size_t size, bool is_unsigned>
|
||||
static consteval Type get_type_from_size()
|
||||
{
|
||||
if (is_unsigned) {
|
||||
if (size == 1)
|
||||
if constexpr (is_unsigned) {
|
||||
if constexpr (size == 1)
|
||||
return Type::UInt8;
|
||||
if (size == 2)
|
||||
if constexpr (size == 2)
|
||||
return Type::UInt16;
|
||||
if (size == 4)
|
||||
if constexpr (size == 4)
|
||||
return Type::UInt32;
|
||||
if (size == 8)
|
||||
if constexpr (size == 8)
|
||||
return Type::UInt64;
|
||||
} else {
|
||||
if (size == 1)
|
||||
if constexpr (size == 1)
|
||||
return Type::Int8;
|
||||
if (size == 2)
|
||||
if constexpr (size == 2)
|
||||
return Type::Int16;
|
||||
if (size == 4)
|
||||
if constexpr (size == 4)
|
||||
return Type::Int32;
|
||||
if (size == 8)
|
||||
if constexpr (size == 8)
|
||||
return Type::Int64;
|
||||
}
|
||||
|
||||
|
@ -91,15 +92,41 @@ struct TypeErasedParameter {
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
static Type get_type()
|
||||
static consteval Type get_type()
|
||||
{
|
||||
if constexpr (IsIntegral<T>)
|
||||
return get_type_from_size(sizeof(T), IsUnsigned<T>);
|
||||
return get_type_from_size<sizeof(T), IsUnsigned<T>>();
|
||||
else
|
||||
return Type::Custom;
|
||||
}
|
||||
|
||||
size_t to_size() const;
|
||||
constexpr size_t to_size() const
|
||||
{
|
||||
i64 svalue;
|
||||
|
||||
if (type == TypeErasedParameter::Type::UInt8)
|
||||
svalue = *static_cast<const u8*>(value);
|
||||
else if (type == TypeErasedParameter::Type::UInt16)
|
||||
svalue = *static_cast<const u16*>(value);
|
||||
else if (type == TypeErasedParameter::Type::UInt32)
|
||||
svalue = *static_cast<const u32*>(value);
|
||||
else if (type == TypeErasedParameter::Type::UInt64)
|
||||
svalue = *static_cast<const u64*>(value);
|
||||
else if (type == TypeErasedParameter::Type::Int8)
|
||||
svalue = *static_cast<const i8*>(value);
|
||||
else if (type == TypeErasedParameter::Type::Int16)
|
||||
svalue = *static_cast<const i16*>(value);
|
||||
else if (type == TypeErasedParameter::Type::Int32)
|
||||
svalue = *static_cast<const i32*>(value);
|
||||
else if (type == TypeErasedParameter::Type::Int64)
|
||||
svalue = *static_cast<const i64*>(value);
|
||||
else
|
||||
VERIFY_NOT_REACHED();
|
||||
|
||||
VERIFY(svalue >= 0);
|
||||
|
||||
return static_cast<size_t>(svalue);
|
||||
}
|
||||
|
||||
// FIXME: Getters and setters.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue