Commit graph

3713 commits

Author SHA1 Message Date
Timothy Flynn
fc20e61e72 AK: Introduce an as_if<OutputType>() cast
We currently have a fair amount of code of the form:

    if (is<Child>(parent)) {
        auto& child = static_cast<Child&>(parent);

        if (child.foo()) {
        }
    }

This new cast allows us to instead write code of the form:

    if (auto* child = as_if<Child>(parent); child && child->foo()) {
    }

N.B. The name "as_if" was chosen because we are considering renaming
verify_cast to "as".

Co-authored-by: Tim Ledbetter <tim.ledbetter@ladybird.org>
2025-01-21 14:58:32 +01:00
Timothy Flynn
c2741ea85e AK: Remove default parameter from StringBuilder's constructor
This separates the StringBuilder constructor into 2 constructors. This
essentially allows forming code of the following pattern:

    StringBuilder foo() { return {}; }

Otherwise, we would get the following compiler error:

    chosen constructor is explicit in copy-initialization

Due to the explicitness of the StringBuilder constructor.

This is required for an upcoming update to ICU 76, where we use our
StringBuilder in templated ICU code.
2025-01-18 17:56:40 -05:00
Ali Mohammad Pur
5876417357 AK: Add a const iterator for COWVector 2025-01-17 10:13:51 +01:00
stasoid
3e81d7ea2e AK: Show executable name in rich debug format on Windows 2025-01-13 20:05:05 -07:00
Sam Atkins
c86ad5c505 AK: Make ScopeGuard and ArmedScopeGuard [[nodiscard]]
Brought to you by a significant amount of pain and suffering, which this
would have prevented.
2025-01-09 18:36:34 +00:00
Ivan Zuzak
36190e3baf AK: Optimize performance in GenericShorthands via forwarding references
Functions in AK/GenericShorthands used pass-by-value which results in
copying values when calling those functions. This could be expensive
for complex types. To optimize performance, we switch the functions to
use forwarding references.
2025-01-09 09:03:50 +01:00
stasoid
9884cd0628 AK: Stub out AK::demangle on Windows 2025-01-05 15:02:32 +00:00
Aliaksandr Kalenik
ae9257bf3b AK: Define operator== for BitmapView 2025-01-04 20:32:35 +01:00
Lucas CHOLLET
d6abd44522 AK: Merge implementations of operator== for Optional
Instead of having a overload of the operator in each specialization of
Optional, use a free function as a common implementation.
2025-01-03 17:11:22 +01:00
stasoid
49bdda1475 AK: Simplify usage of windows.h and winsock2.h
Use <AK/Windows.h> instead of windows.h/winsock2.h
to avoid timeval-related errors.

Note: winsock2.h includes windows.h
2025-01-02 10:17:51 -07:00
Timothy Flynn
27478ec7d4 Everywhere: Run clang-format
The following command was used to clang-format these files:

    clang-format-19 -i $(find . \
        -not \( -path "./\.*" -prune \) \
        -not \( -path "./Build/*" -prune \) \
        -not \( -path "./Toolchain/*" -prune \) \
        -type f -name "*.cpp" -o -name "*.mm" -o -name "*.h")
2024-12-28 05:39:32 -08:00
Pavel Shliak
2be45c1ccf AK: Remove redundant condition from Quick Sort
if (size <= 1)
       return;
This means that size is at the very minimum 2,
and pivot_point at the very minimum equals 1 as size / 2;
The condition if (pivot_point) can never be false.
2024-12-22 12:33:41 +01:00
stasoid
0a90143420 AK: Fix BumpAllocator.h compilation errors on Windows 2024-12-14 16:12:12 +01:00
Ali Mohammad Pur
44798f44ef AK: Add Vector::get(index) convenience function
This is similar to HashMap::get(key), which returns an Optional, empty
if the index is out of bounds for the vector.
2024-12-13 10:00:16 +01:00
Pavel Shliak
ff7cca38bd AK: Remove clang-specific templates 2024-12-12 15:12:27 -07:00
Pavel Shliak
6f81b80114 Everywhere: Include HashMap only where it's actually used 2024-12-09 12:31:16 +01:00
Pavel Shliak
97d99aa8d6 AK: Verify that HashMap is not empty in take_first
This makes the behavior uniform with:
- HashTable
- SinglyLinkedList
- Vector
2024-12-09 12:31:16 +01:00
R-Goc
f380b4b95b AK: Port BumpAllocator.h to Windows
Done by forward declaring the required functions and defining the needed
constants. The defines shouldn't collide as they are from memoryapi.h.
This is done to avoid including windows.h.
2024-12-08 17:36:37 -07:00
Totto16
14e808e574 AK: Add Flatten Variant helper
This will be used in future commits.
It has to be present for types, that are generated by the IDL since the
IDL flattens variants of variants.
2024-12-06 13:10:27 +00:00
Pavel Shliak
c16c20b836 AK: Fix InsertionSort to respect specified bounds
Previously, it ignored 'start', sorting from the array's
beginning. This caused unintended changes and slower
performance. Fix ensures sorting stays within 'start'
and 'end' indices only.
2024-12-06 06:44:27 +00:00
Pavel Shliak
b521badbef AK: Remove QuickSelect 2024-12-05 16:53:28 +01:00
Pavel Shliak
ede0dbafc6 AK: Remove Statistics.h
This also removes MedianCut and GIFWriter
2024-12-05 16:53:28 +01:00
Jonne Ransijn
6d1fa64f5f AK: Allow NonnullRawPtr to be used in constant expressions 2024-12-04 16:45:58 +00:00
Jonne Ransijn
e2b69fe7e2 AK: Restrict template parameter of JsonArray constructor
This constructor doesn't need to accept iterable containers of anything
other than `JsonValue`s.
2024-12-04 16:45:58 +00:00
Jonne Ransijn
a4f23c512c AK: Add IterableContainerOf concept
This concept not only checks that the given type is iterable, but it
also checks that the iterated value is of the expected type.
2024-12-04 16:45:58 +00:00
Jonne Ransijn
e8b2d35410 AK: Add IsTemplateBaseOf<Base, Derived> concept
This concept checks if any specialization of `Base<...>` is a base class
of `Derived`.
2024-12-04 16:45:58 +00:00
Jonne Ransijn
b7e22f0916 AK: Disallow creating NonnullRawPtrs from r-value references
This would always result in a use-after-free.
2024-12-04 16:45:58 +00:00
Jonne Ransijn
bea602f5ed AK: Use Noncopyable.h in NonnullRawPtr 2024-12-04 16:45:58 +00:00
Jonne Ransijn
501a7dbaa3 AK: Use Noncopyable.h in Badge 2024-12-04 16:45:58 +00:00
Jonne Ransijn
cca84aa28d AK: Fix ASSERT_NOT_REACHED macro when NDEBUG is not defined
This is supposed to be a function-like macro just like
`VERIFY_NOT_REACHED()` and when `NDEBUG` is defined.
2024-12-04 16:45:58 +00:00
Jonne Ransijn
d5fbf7323a AK+LibJS: Allow {TRY,MUST}{,_OR_THROW_OOM} on list-initializers
List initializers may contain commas without surrounding parenthesis,
causing them to be passed as multiple macro arguments.
2024-12-04 16:45:58 +00:00
Jonne Ransijn
f617127772 AK: Allow calling ASSERT and VERIFY on list-initializers
List initializers may contain commas without surrounding parenthesis,
causing them to be passed as multiple macro arguments.
2024-12-04 16:45:58 +00:00
Jonne Ransijn
0de15264ab AK: Remove clang-tidy warnings for ASSERT(a || b) lines
Lines like these were getting a warning to simplify the expanded
boolean expression from `!(a || b)` to `(a && b)`, but since the
`!(...)` is part of the macro, that is never going to happen.
2024-12-04 16:45:58 +00:00
Jonne Ransijn
211dc5659c LibWeb: Fix stack-use-after-scope
The refactor in the previous commit was storing a reference to a stack
allocated `Infrastructure::Request::BodyType` which was then immediately
freed. To fix this, we can store the `Infrastructure::Request::BodyType`
in a variable beforehand, so it becomes safe to reference.
2024-12-04 01:58:22 +01:00
Jonne Ransijn
d7596a0a61 AK: Don't implicitly convert Optional<T&> to Optional<T>
C++ will jovially select the implicit conversion operator, even if it's
complete bogus, such as for unknown-size types or non-destructible
types. Therefore, all such conversions (which incur a copy) must
(unfortunately) be explicit so that non-copyable types continue to work.

NOTE: We make an exception for trivially copyable types, since they
are, well, trivially copyable.

Co-authored-by: kleines Filmröllchen <filmroellchen@serenityos.org>
2024-12-04 01:58:22 +01:00
kleines Filmröllchen
8468fb9ae5 AK: Use StringView as its own peek type
StringView is already a reference type, and copying it is cheap, so we
should use it as its own peek type.
2024-12-04 01:58:22 +01:00
Jonne Ransijn
55383998b1 AK: Use Noncopyable.h in Optional<T&>
This makes them trivially copyable/movable, silencing

> "parameter is copied for each invocation"

warnings on `Optional<T&>`, which are unnecessairy,
since `Optional<T&>` is just a trivially copyable pointer.

This creates a slight change in behaviour when moving out of an
`Optional<T&>`, since the moved-from optional no longer gets cleared.

Moved-from values should be considered to be in an undefined state,
and if clearing a moved-from `Optional<T&>` is desired, you should be
using `Optional<T&>::release_value()` instead.
2024-12-04 01:58:22 +01:00
Jonne Ransijn
e38b206957 AK: Print a more useful error message when a MUST(...) fails
```
VERIFICATION FAILED: !_temporary_result.is_error()
```

is not really a helpful error message.

When we are including `AK/Format.h`, which is most of the time,
we can easily print a much more useful error message:

```
UNEXPECTED ERROR: Cannot allocate memory (errno=12)
```
2024-12-02 20:07:38 +01:00
stasoid
ac590fe30d AK: Don't include winsock2.h in Time.h
This is architecturally preferable, see https://github.com/LadybirdBrowser/ladybird/blob/master/Documentation/Porting.md#windows
and https://discord.com/channels/1247070541085671459/1306918361732616212/1310437609679487006
(note: winsock2.h includes windows.h)
2024-11-29 10:55:52 +01:00
Andrew Kaster
fc3ebd7efd AK: Add workaround for older Xcode versions in Swift.h 2024-11-26 11:00:48 +01:00
Jonne Ransijn
75b482bbb9 AK: Fix "assignment from temporary" check of Optional::operator=
There was an existing check to ensure that `U` was an lvalue reference,
but when this check fails, overload resolution will just move right on
to the copy asignment operator, which will cause the temporary to be
assigned anyway.

Disallowing `Optional<T&>`s to be created from temporaries entirely
would be undesired, since existing code has valid reasons for creating
`Optional<T&>`s from temporaries, such as for function call arguments.

This fix explicitly deletes the `Optional::operator=(U&&)` operator,
so overload resolution stops.
2024-11-24 23:04:34 -07:00
Ali Mohammad Pur
3bcd91b109 LibDNS: Hide some debug logs behind DNS_DEBUG 2024-11-20 21:37:58 +01:00
Ali Mohammad Pur
b93d8ef875 AK: Disable implicit conversion from char* -> ipv4 -> ipv6
This is a footgun with some massive bullets.
2024-11-20 21:37:58 +01:00
Pavel Shliak
8a07131229 LibGfx: Clean up #include directives
We actually include what we use where we use it.
This change aims to improve the speed of incremental builds.
2024-11-20 21:13:23 +01:00
stasoid
a423493dd8 AK: Add LexicalPath::is_root() 2024-11-19 22:07:01 -07:00
stasoid
ddd15e96b6 AK: Define pid_t on Windows 2024-11-19 14:40:03 -07:00
stasoid
69f5f40617 AK: Add static bool LexicalPath::is_absolute_path(StringView path); 2024-11-19 14:35:52 -07:00
Andrew Kaster
32cf4d1e29 AK: Add missing swift/bridging empty defines for non-Swift compilers 2024-11-19 14:32:11 -07:00
Andrew Kaster
458167935c AK: Add an extension to construct an AK.String from a Swift.String 2024-11-19 14:32:11 -07:00
Andrew Kaster
a95f761cb4 AK: Include missing StdLibExtras from NeverDestroyed 2024-11-19 14:32:11 -07:00