Everywhere: Use C++ concepts instead of requires clauses

This commit is contained in:
Moustafa Raafat 2022-11-14 18:20:59 +00:00 committed by Sam Atkins
parent 9721da2e6a
commit ae2abcebbb
Notes: sideshowbarker 2024-07-17 03:36:21 +09:00
17 changed files with 60 additions and 61 deletions

View file

@ -282,9 +282,8 @@ public:
return vformatted(fmtstr.view(), variadic_format_parameters); return vformatted(fmtstr.view(), variadic_format_parameters);
} }
template<typename T> template<Arithmetic T>
[[nodiscard]] static DeprecatedString number(T value) [[nodiscard]] static DeprecatedString number(T value)
requires IsArithmetic<T>
{ {
return formatted("{}", value); return formatted("{}", value);
} }

View file

@ -6,15 +6,15 @@
#pragma once #pragma once
#include <AK/Concepts.h>
#include <AK/Math.h> #include <AK/Math.h>
#include <AK/QuickSort.h> #include <AK/QuickSort.h>
#include <AK/StdLibExtraDetails.h>
#include <AK/Vector.h> #include <AK/Vector.h>
namespace AK { namespace AK {
template<typename T = float> template<Arithmetic T = float>
requires(IsArithmetic<T>) class Statistics { class Statistics {
public: public:
Statistics() = default; Statistics() = default;
~Statistics() = default; ~Statistics() = default;

View file

@ -134,16 +134,14 @@ InputStream& operator>>(InputStream& stream, Optional<T>& value)
return stream; return stream;
} }
template<typename Integral> template<Integral I>
InputStream& operator>>(InputStream& stream, Integral& value) InputStream& operator>>(InputStream& stream, I& value)
requires IsIntegral<Integral>
{ {
stream.read_or_error({ &value, sizeof(value) }); stream.read_or_error({ &value, sizeof(value) });
return stream; return stream;
} }
template<typename Integral> template<Integral I>
OutputStream& operator<<(OutputStream& stream, Integral value) OutputStream& operator<<(OutputStream& stream, I value)
requires IsIntegral<Integral>
{ {
stream.write_or_error({ &value, sizeof(value) }); stream.write_or_error({ &value, sizeof(value) });
return stream; return stream;
@ -151,16 +149,14 @@ requires IsIntegral<Integral>
#ifndef KERNEL #ifndef KERNEL
template<typename FloatingPoint> template<FloatingPoint F>
InputStream& operator>>(InputStream& stream, FloatingPoint& value) InputStream& operator>>(InputStream& stream, F& value)
requires IsFloatingPoint<FloatingPoint>
{ {
stream.read_or_error({ &value, sizeof(value) }); stream.read_or_error({ &value, sizeof(value) });
return stream; return stream;
} }
template<typename FloatingPoint> template<FloatingPoint F>
OutputStream& operator<<(OutputStream& stream, FloatingPoint value) OutputStream& operator<<(OutputStream& stream, F value)
requires IsFloatingPoint<FloatingPoint>
{ {
stream.write_or_error({ &value, sizeof(value) }); stream.write_or_error({ &value, sizeof(value) });
return stream; return stream;

View file

@ -6,6 +6,7 @@
#pragma once #pragma once
#include <AK/Concepts.h>
#include <AK/Format.h> #include <AK/Format.h>
#include <AK/Forward.h> #include <AK/Forward.h>
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
@ -74,9 +75,8 @@ public:
[[nodiscard]] u32 hash() const; [[nodiscard]] u32 hash() const;
template<typename T> template<Arithmetic T>
static ErrorOr<String> number(T value) static ErrorOr<String> number(T value)
requires IsArithmetic<T>
{ {
return formatted("{}", value); return formatted("{}", value);
} }

View file

@ -29,8 +29,8 @@ template<typename T>
struct Traits : public GenericTraits<T> { struct Traits : public GenericTraits<T> {
}; };
template<typename T> template<Integral T>
requires(IsIntegral<T>) struct Traits<T> : public GenericTraits<T> { struct Traits<T> : public GenericTraits<T> {
static constexpr bool is_trivial() { return true; } static constexpr bool is_trivial() { return true; }
static constexpr unsigned hash(T value) static constexpr unsigned hash(T value)
{ {
@ -42,8 +42,8 @@ requires(IsIntegral<T>) struct Traits<T> : public GenericTraits<T> {
}; };
#ifndef KERNEL #ifndef KERNEL
template<typename T> template<FloatingPoint T>
requires(IsFloatingPoint<T>) struct Traits<T> : public GenericTraits<T> { struct Traits<T> : public GenericTraits<T> {
static constexpr bool is_trivial() { return true; } static constexpr bool is_trivial() { return true; }
static constexpr unsigned hash(T value) static constexpr unsigned hash(T value)
{ {

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include "WidgetWithLabel.h" #include "WidgetWithLabel.h"
#include <AK/Concepts.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibDSP/ProcessorParameter.h> #include <LibDSP/ProcessorParameter.h>
#include <LibGUI/ComboBox.h> #include <LibGUI/ComboBox.h>
@ -14,8 +15,8 @@
#include <LibGUI/Label.h> #include <LibGUI/Label.h>
#include <LibGUI/ModelIndex.h> #include <LibGUI/ModelIndex.h>
template<typename EnumT> template<Enum EnumT>
requires(IsEnum<EnumT>) class ProcessorParameterDropdown : public GUI::ComboBox { class ProcessorParameterDropdown : public GUI::ComboBox {
C_OBJECT(ProcessorParameterDropdown); C_OBJECT(ProcessorParameterDropdown);
public: public:

View file

@ -460,9 +460,8 @@ void ArgsParser::add_option(StringView& value, char const* help_string, char con
add_option(move(option)); add_option(move(option));
} }
template<typename Integral> template<Integral I>
void ArgsParser::add_option(Integral& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode) void ArgsParser::add_option(I& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode)
requires(IsIntegral<Integral>)
{ {
Option option { Option option {
OptionArgumentMode::Required, OptionArgumentMode::Required,
@ -472,11 +471,11 @@ requires(IsIntegral<Integral>)
value_name, value_name,
[&value](char const* s) { [&value](char const* s) {
auto view = StringView { s, strlen(s) }; auto view = StringView { s, strlen(s) };
Optional<Integral> opt; Optional<I> opt;
if constexpr (IsSigned<Integral>) if constexpr (IsSigned<I>)
opt = view.to_int<Integral>(); opt = view.to_int<I>();
else else
opt = view.to_uint<Integral>(); opt = view.to_uint<I>();
value = opt.value_or(0); value = opt.value_or(0);
return opt.has_value(); return opt.has_value();
}, },

View file

@ -6,6 +6,7 @@
#pragma once #pragma once
#include <AK/Concepts.h>
#include <AK/DeprecatedString.h> #include <AK/DeprecatedString.h>
#include <AK/Function.h> #include <AK/Function.h>
#include <AK/Vector.h> #include <AK/Vector.h>
@ -89,9 +90,8 @@ public:
void add_option(char const*& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None); void add_option(char const*& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None);
void add_option(DeprecatedString& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None); void add_option(DeprecatedString& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None);
void add_option(StringView& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None); void add_option(StringView& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None);
template<typename Integral> template<Integral I>
void add_option(Integral& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None) void add_option(I& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None);
requires(IsIntegral<Integral>);
void add_option(double& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None); void add_option(double& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None);
void add_option(Optional<double>& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None); void add_option(Optional<double>& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None);
void add_option(Optional<size_t>& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None); void add_option(Optional<size_t>& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None);

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <AK/Concepts.h>
#include <AK/Span.h> #include <AK/Span.h>
#include <LibCrypto/BigInt/UnsignedBigInteger.h> #include <LibCrypto/BigInt/UnsignedBigInteger.h>
@ -16,8 +17,8 @@ struct SignedDivisionResult;
class SignedBigInteger { class SignedBigInteger {
public: public:
template<typename T> template<Signed T>
requires(IsSigned<T> && sizeof(T) <= sizeof(i32)) requires(sizeof(T) <= sizeof(i32))
SignedBigInteger(T value) SignedBigInteger(T value)
: m_sign(value < 0) : m_sign(value < 0)
, m_unsigned_data(abs(static_cast<i32>(value))) , m_unsigned_data(abs(static_cast<i32>(value)))

View file

@ -9,6 +9,7 @@
#pragma once #pragma once
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/Concepts.h>
#include <AK/DeprecatedString.h> #include <AK/DeprecatedString.h>
#include <AK/Span.h> #include <AK/Span.h>
#include <AK/Types.h> #include <AK/Types.h>
@ -25,8 +26,8 @@ public:
static constexpr size_t BITS_IN_WORD = 32; static constexpr size_t BITS_IN_WORD = 32;
// This constructor accepts any unsigned with size up to Word. // This constructor accepts any unsigned with size up to Word.
template<typename T> template<Integral T>
requires(IsIntegral<T> && sizeof(T) <= sizeof(Word)) requires(sizeof(T) <= sizeof(Word))
UnsignedBigInteger(T value) UnsignedBigInteger(T value)
{ {
m_words.append(static_cast<Word>(value)); m_words.append(static_cast<Word>(value));

View file

@ -6,6 +6,7 @@
#pragma once #pragma once
#include <AK/Concepts.h>
#include <AK/DeprecatedString.h> #include <AK/DeprecatedString.h>
#include <AK/FixedPoint.h> #include <AK/FixedPoint.h>
#include <AK/Format.h> #include <AK/Format.h>
@ -146,8 +147,8 @@ private:
Logarithmic const m_logarithmic; Logarithmic const m_logarithmic;
}; };
template<typename EnumT> template<Enum EnumT>
requires(IsEnum<EnumT>) class ProcessorEnumParameter final : public Detail::ProcessorParameterSingleValue<EnumT> { class ProcessorEnumParameter final : public Detail::ProcessorParameterSingleValue<EnumT> {
public: public:
ProcessorEnumParameter(DeprecatedString name, EnumT initial_value) ProcessorEnumParameter(DeprecatedString name, EnumT initial_value)
: Detail::ProcessorParameterSingleValue<EnumT>(move(name), ParameterType::Enum, initial_value) : Detail::ProcessorParameterSingleValue<EnumT>(move(name), ParameterType::Enum, initial_value)

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/Concepts.h>
#include <AK/Function.h> #include <AK/Function.h>
#include <AK/QuickSort.h> #include <AK/QuickSort.h>
#include <LibEDID/EDID.h> #include <LibEDID/EDID.h>
@ -165,16 +166,16 @@ T Parser::read_host(T const* field) const
return value; return value;
} }
template<typename T> template<Integral T>
requires(IsIntegral<T> && sizeof(T) > 1) requires(sizeof(T) > 1)
T Parser::read_le(T const* field) const T Parser::read_le(T const* field) const
{ {
static_assert(sizeof(T) > 1); static_assert(sizeof(T) > 1);
return AK::convert_between_host_and_little_endian(read_host(field)); return AK::convert_between_host_and_little_endian(read_host(field));
} }
template<typename T> template<Integral T>
requires(IsIntegral<T> && sizeof(T) > 1) requires(sizeof(T) > 1)
T Parser::read_be(T const* field) const T Parser::read_be(T const* field) const
{ {
static_assert(sizeof(T) > 1); static_assert(sizeof(T) > 1);

View file

@ -8,6 +8,7 @@
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/ByteReader.h> #include <AK/ByteReader.h>
#include <AK/Concepts.h>
#include <AK/Endian.h> #include <AK/Endian.h>
#include <AK/Error.h> #include <AK/Error.h>
#include <AK/FixedPoint.h> #include <AK/FixedPoint.h>
@ -437,12 +438,12 @@ private:
template<typename T> template<typename T>
T read_host(T const*) const; T read_host(T const*) const;
template<typename T> template<Integral T>
requires(IsIntegral<T> && sizeof(T) > 1) requires(sizeof(T) > 1)
T read_le(T const*) const; T read_le(T const*) const;
template<typename T> template<Integral T>
requires(IsIntegral<T> && sizeof(T) > 1) requires(sizeof(T) > 1)
T read_be(T const*) const; T read_be(T const*) const;
Definitions::EDID const& raw_edid() const; Definitions::EDID const& raw_edid() const;

View file

@ -44,8 +44,8 @@ public:
u32 crc(); u32 crc();
private: private:
template<typename T> template<Unsigned T>
requires(IsUnsigned<T>) ErrorOr<void> add(T); ErrorOr<void> add(T);
ByteBuffer m_data; ByteBuffer m_data;
DeprecatedString m_type; DeprecatedString m_type;
@ -77,8 +77,8 @@ u32 PNGChunk::crc()
return crc; return crc;
} }
template<typename T> template<Unsigned T>
requires(IsUnsigned<T>) ErrorOr<void> PNGChunk::add(T data) ErrorOr<void> PNGChunk::add(T data)
{ {
TRY(m_data.try_append(&data, sizeof(T))); TRY(m_data.try_append(&data, sizeof(T)));
return {}; return {};

View file

@ -6,6 +6,7 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/Concepts.h>
#include <AK/DeprecatedString.h> #include <AK/DeprecatedString.h>
#include <LibJS/Print.h> #include <LibJS/Print.h>
#include <LibJS/Runtime/Array.h> #include <LibJS/Runtime/Array.h>
@ -362,9 +363,8 @@ ErrorOr<void> print_async_generator(JS::PrintContext& print_context, JS::AsyncGe
return {}; return {};
} }
template<typename T> template<Arithmetic T>
ErrorOr<void> print_number(JS::PrintContext& print_context, T number) ErrorOr<void> print_number(JS::PrintContext& print_context, T number)
requires IsArithmetic<T>
{ {
TRY(js_out(print_context, "\033[35;1m")); TRY(js_out(print_context, "\033[35;1m"));
TRY(js_out(print_context, "{}", number)); TRY(js_out(print_context, "{}", number));

View file

@ -6,6 +6,7 @@
#pragma once #pragma once
#include <AK/Concepts.h>
#include <AK/Forward.h> #include <AK/Forward.h>
#include <LibCrypto/Forward.h> #include <LibCrypto/Forward.h>
#include <LibJS/Forward.h> #include <LibJS/Forward.h>
@ -167,9 +168,8 @@ Vector<T> merge_lists(Vector<T> const& a, Vector<T> const& b)
} }
// x modulo y, https://tc39.es/ecma262/#eqn-modulo // x modulo y, https://tc39.es/ecma262/#eqn-modulo
template<typename T, typename U> template<Arithmetic T, Arithmetic U>
auto modulo(T x, U y) auto modulo(T x, U y)
requires(IsArithmetic<T>, IsArithmetic<U>)
{ {
// The notation “x modulo y” (y must be finite and non-zero) computes a value k of the same sign as y (or zero) such that abs(k) < abs(y) and x - k = q × y for some integer q. // The notation “x modulo y” (y must be finite and non-zero) computes a value k of the same sign as y (or zero) such that abs(k) < abs(y) and x - k = q × y for some integer q.
VERIFY(y != 0); VERIFY(y != 0);

View file

@ -10,6 +10,7 @@
#include "RegexMatch.h" #include "RegexMatch.h"
#include "RegexOptions.h" #include "RegexOptions.h"
#include <AK/Concepts.h>
#include <AK/DisjointChunks.h> #include <AK/DisjointChunks.h>
#include <AK/Format.h> #include <AK/Format.h>
#include <AK/Forward.h> #include <AK/Forward.h>
@ -339,9 +340,8 @@ public:
Optimizer::append_alternation(*this, move(left), move(right)); Optimizer::append_alternation(*this, move(left), move(right));
} }
template<typename T> template<Integral T>
static void transform_bytecode_repetition_min_max(ByteCode& bytecode_to_repeat, T minimum, Optional<T> maximum, size_t min_repetition_mark_id, size_t max_repetition_mark_id, bool greedy = true) static void transform_bytecode_repetition_min_max(ByteCode& bytecode_to_repeat, T minimum, Optional<T> maximum, size_t min_repetition_mark_id, size_t max_repetition_mark_id, bool greedy = true)
requires(IsIntegral<T>)
{ {
if (!maximum.has_value()) { if (!maximum.has_value()) {
if (minimum == 0) if (minimum == 0)
@ -410,9 +410,8 @@ public:
bytecode_to_repeat = move(new_bytecode); bytecode_to_repeat = move(new_bytecode);
} }
template<typename T> template<Integral T>
void insert_bytecode_repetition_n(ByteCode& bytecode_to_repeat, T n, size_t repetition_mark_id) void insert_bytecode_repetition_n(ByteCode& bytecode_to_repeat, T n, size_t repetition_mark_id)
requires(IsIntegral<T>)
{ {
// LABEL _LOOP // LABEL _LOOP
// REGEXP // REGEXP