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
17 changed files with 60 additions and 61 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -6,6 +6,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Concepts.h>
#include <AK/DeprecatedString.h>
#include <LibJS/Print.h>
#include <LibJS/Runtime/Array.h>
@ -362,9 +363,8 @@ ErrorOr<void> print_async_generator(JS::PrintContext& print_context, JS::AsyncGe
return {};
}
template<typename T>
template<Arithmetic T>
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, "{}", number));

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/Concepts.h>
#include <AK/Forward.h>
#include <LibCrypto/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
template<typename T, typename U>
template<Arithmetic T, Arithmetic U>
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.
VERIFY(y != 0);

View file

@ -10,6 +10,7 @@
#include "RegexMatch.h"
#include "RegexOptions.h"
#include <AK/Concepts.h>
#include <AK/DisjointChunks.h>
#include <AK/Format.h>
#include <AK/Forward.h>
@ -339,9 +340,8 @@ public:
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)
requires(IsIntegral<T>)
{
if (!maximum.has_value()) {
if (minimum == 0)
@ -410,9 +410,8 @@ public:
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)
requires(IsIntegral<T>)
{
// LABEL _LOOP
// REGEXP