1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:57:44 +00:00

AK+Everywhere: Make StdLibExtras templates less wrapper-y

This commit makes the user-facing StdLibExtras templates and utilities
arguably more nice-looking by removing the need to reach into the
wrapper structs generated by them to get the value/type needed.
The C++ standard library had to invent `_v` and `_t` variants (likely
because of backwards compat), but we don't need to cater to any codebase
except our own, so might as well have good things for free. :^)
This commit is contained in:
AnotherTest 2021-04-10 18:29:06 +04:30 committed by Andreas Kling
parent d8d16dea95
commit a6e4482080
41 changed files with 650 additions and 662 deletions

View file

@ -277,19 +277,19 @@ struct ReadElement {
case Short:
return ReadElementConcrete<T, short, kind> {}(input_lexer, ap);
case Long:
if constexpr (IsSame<T, int>::value)
if constexpr (IsSame<T, int>)
return ReadElementConcrete<T, long, kind> {}(input_lexer, ap);
if constexpr (IsSame<T, unsigned>::value)
if constexpr (IsSame<T, unsigned>)
return ReadElementConcrete<T, unsigned, kind> {}(input_lexer, ap);
if constexpr (IsSame<T, float>::value)
if constexpr (IsSame<T, float>)
return ReadElementConcrete<int, double, kind> {}(input_lexer, ap);
return false;
case LongLong:
if constexpr (IsSame<T, int>::value)
if constexpr (IsSame<T, int>)
return ReadElementConcrete<long long, long long, kind> {}(input_lexer, ap);
if constexpr (IsSame<T, unsigned>::value)
if constexpr (IsSame<T, unsigned>)
return ReadElementConcrete<unsigned long long, unsigned long long, kind> {}(input_lexer, ap);
if constexpr (IsSame<T, float>::value)
if constexpr (IsSame<T, float>)
return ReadElementConcrete<long long, double, kind> {}(input_lexer, ap);
return false;
case IntMax:

View file

@ -68,7 +68,7 @@ public:
template<typename T>
T* data()
{
static_assert(IsVoid<T>::value || is_trivial<T>());
static_assert(IsVoid<T> || IsTrivial<T>);
if (!m_impl)
return nullptr;
return (T*)m_impl->data();
@ -77,7 +77,7 @@ public:
template<typename T>
const T* data() const
{
static_assert(IsVoid<T>::value || is_trivial<T>());
static_assert(IsVoid<T> || IsTrivial<T>);
if (!m_impl)
return nullptr;
return (const T*)m_impl->data();

View file

@ -90,13 +90,13 @@ public:
}
template<typename T, typename Callback>
void for_each_child_of_type(Callback callback) requires IsBaseOf<Object, T>::value;
void for_each_child_of_type(Callback callback) requires IsBaseOf<Object, T>;
template<typename T>
T* find_child_of_type_named(const String&) requires IsBaseOf<Object, T>::value;
T* find_child_of_type_named(const String&) requires IsBaseOf<Object, T>;
template<typename T>
T* find_descendant_of_type_named(const String&) requires IsBaseOf<Object, T>::value;
T* find_descendant_of_type_named(const String&) requires IsBaseOf<Object, T>;
bool is_ancestor_of(const Object&) const;
@ -187,7 +187,7 @@ struct AK::Formatter<Core::Object> : AK::Formatter<FormatString> {
namespace Core {
template<typename T, typename Callback>
inline void Object::for_each_child_of_type(Callback callback) requires IsBaseOf<Object, T>::value
inline void Object::for_each_child_of_type(Callback callback) requires IsBaseOf<Object, T>
{
for_each_child([&](auto& child) {
if (auto* child_as_t = dynamic_cast<T*>(&child); child_as_t)
@ -197,7 +197,7 @@ inline void Object::for_each_child_of_type(Callback callback) requires IsBaseOf<
}
template<typename T>
T* Object::find_child_of_type_named(const String& name) requires IsBaseOf<Object, T>::value
T* Object::find_child_of_type_named(const String& name) requires IsBaseOf<Object, T>
{
T* found_child = nullptr;
for_each_child_of_type<T>([&](auto& child) {
@ -212,7 +212,7 @@ T* Object::find_child_of_type_named(const String& name) requires IsBaseOf<Object
}
template<typename T>
T* Object::find_descendant_of_type_named(const String& name) requires IsBaseOf<Object, T>::value
T* Object::find_descendant_of_type_named(const String& name) requires IsBaseOf<Object, T>
{
auto* this_as_t = dynamic_cast<T*>(this);
if (this_as_t && this->name() == name)

View file

@ -37,13 +37,13 @@ class ItemListModel : public Model {
public:
static constexpr auto IsTwoDimensional = requires(Container data)
{
requires !IsVoid<ColumnNameListType>::value;
requires !IsVoid<ColumnNameListType>;
data.at(0).at(0);
data.at(0).size();
};
// Substitute 'void' for a dummy u8.
using ColumnNamesT = typename Conditional<IsVoid<ColumnNameListType>::value, u8, ColumnNameListType>::Type;
using ColumnNamesT = Conditional<IsVoid<ColumnNameListType>, u8, ColumnNameListType>;
static NonnullRefPtr<ItemListModel> create(const Container& data, const ColumnNamesT& column_names, const Optional<size_t>& row_count = {}) requires(IsTwoDimensional)
{

View file

@ -31,7 +31,7 @@
namespace Gfx {
template<size_t N, typename = typename AK::EnableIf<N % 2 == 1>::Type>
template<size_t N, typename = typename EnableIf<N % 2 == 1>::Type>
class SpatialGaussianBlurFilter : public GenericConvolutionFilter<N> {
public:
SpatialGaussianBlurFilter() { }

View file

@ -62,7 +62,7 @@ public:
auto* memory = allocate_cell(sizeof(T));
new (memory) T(forward<Args>(args)...);
auto* cell = static_cast<T*>(memory);
constexpr bool is_object = IsBaseOf<Object, T>::value;
constexpr bool is_object = IsBaseOf<Object, T>;
if constexpr (is_object)
static_cast<Object*>(cell)->disable_transitions();
cell->initialize(global_object);

View file

@ -101,7 +101,7 @@ public:
return Value((i32)data()[property_index]);
} else if constexpr (sizeof(T) == 4 || sizeof(T) == 8) {
auto value = data()[property_index];
if constexpr (IsFloatingPoint<T>::value) {
if constexpr (IsFloatingPoint<T>) {
return Value((double)value);
} else if constexpr (NumericLimits<T>::is_signed()) {
if (value > NumericLimits<i32>::max() || value < NumericLimits<i32>::min())

View file

@ -68,7 +68,7 @@ Result<T, ThreadError> Thread::join()
}
m_tid = 0;
if constexpr (IsVoid<T>::value)
if constexpr (IsVoid<T>)
return {};
else
return { static_cast<T>(thread_return) };

View file

@ -34,12 +34,10 @@
namespace Web::Bindings {
template<typename>
struct IsExceptionOr : AK::FalseType {
};
constexpr bool IsExceptionOr = false;
template<typename T>
struct IsExceptionOr<DOM::ExceptionOr<T>> : AK::TrueType {
};
constexpr bool IsExceptionOr<DOM::ExceptionOr<T>> = true;
template<typename T>
ALWAYS_INLINE bool throw_dom_exception(JS::VM& vm, JS::GlobalObject& global_object, DOM::ExceptionOr<T>& result)
@ -51,17 +49,17 @@ ALWAYS_INLINE bool throw_dom_exception(JS::VM& vm, JS::GlobalObject& global_obje
return false;
}
template<typename F, typename T = decltype(declval<F>()()), typename Ret = typename Conditional<!IsExceptionOr<T>::value && !IsVoid<T>::value, T, JS::Value>::Type>
template<typename F, typename T = decltype(declval<F>()()), typename Ret = Conditional<!IsExceptionOr<T> && !IsVoid<T>, T, JS::Value>>
Ret throw_dom_exception_if_needed(auto&& vm, auto&& global_object, F&& fn)
{
if constexpr (IsExceptionOr<T>::value) {
if constexpr (IsExceptionOr<T>) {
auto&& result = fn();
if (throw_dom_exception(vm, global_object, result))
return JS::Value();
if constexpr (requires(T v) { v.value(); })
return result.value();
return JS::Value();
} else if constexpr (IsVoid<T>::value) {
} else if constexpr (IsVoid<T>) {
fn();
return JS::js_undefined();
} else {
@ -72,7 +70,7 @@ Ret throw_dom_exception_if_needed(auto&& vm, auto&& global_object, F&& fn)
template<typename T>
bool should_return_empty(T&& value)
{
if constexpr (IsSame<JS::Value, T>::value)
if constexpr (IsSame<JS::Value, T>)
return value.is_empty();
return false;
}

View file

@ -39,7 +39,7 @@ class Performance final
, public Bindings::Wrappable {
public:
using WrapperType = Bindings::PerformanceWrapper;
using AllowOwnPtr = AK::TrueType;
using AllowOwnPtr = TrueType;
explicit Performance(DOM::Window&);
~Performance();

View file

@ -34,7 +34,7 @@ namespace Web::NavigationTiming {
class PerformanceTiming final : public Bindings::Wrappable {
public:
using WrapperType = Bindings::PerformanceTimingWrapper;
using AllowOwnPtr = AK::TrueType;
using AllowOwnPtr = TrueType;
explicit PerformanceTiming(DOM::Window&);
~PerformanceTiming();

View file

@ -49,7 +49,7 @@ public:
VERIFY(!m_in_removed_last_ref);
VERIFY(m_ref_count);
if (!--m_ref_count) {
if constexpr (IsBaseOf<DOM::Node, T>::value) {
if constexpr (IsBaseOf<DOM::Node, T>) {
m_in_removed_last_ref = true;
static_cast<T*>(this)->removed_last_ref();
} else {

View file

@ -47,7 +47,7 @@ template<typename T>
struct TypeTrivia {
static const size_t bits = sizeof(T) * 8;
static const T sign_bit = 1 << (bits - 1);
static const T mask = typename MakeUnsigned<T>::Type(-1);
static const T mask = MakeUnsigned<T>(-1);
};
template<typename T, typename U>