summaryrefslogtreecommitdiff
path: root/AK/Optional.h
AgeCommit message (Collapse)Author
2022-01-23AK: Make Optional<T> explicitly constructible from Optional<U>Idan Horowitz
As long as T is constructible from U. This allows us to avoid patterns like `abc.has_value() ? Optional<U>(abc.value()) : Optional<U>()`.
2021-10-25AK: Always inline Optional::release_value()Andreas Kling
This allows the compiler to optimize away the VERIFY(m_has_value) in many cases.
2021-09-08AK: Add note about an internal compile error with Optional in GCC 10.3+sin-ack
This bit me because I accidentally made the destructor for a class which was wrapped in an Optional private. This causes none of the Optional destructors to be able to be deduced, which when combined with concepts causes an internal compile error in GCC 10.3.0+. This commit adds a note here to make sure that future encounters of this bug does not surprise people.
2021-09-04AK: Add rvalue-ref qualifiers for Optional's value() and value_or()Andreas Kling
This avoids a value copy when calling value() or value_or() on a temporary Optional. This is very common when using the HashMap::get() API like this: auto value = hash_map.get(key).value_or(fallback_value);
2021-09-04AK: Convert Optional.h to east-const styleAndreas Kling
2021-07-04AK: Use conditionally trivial special member functionsDaniel Bertalan
This commit makes use of the conditionally trivial special member functions introduced in C++20. Basically, `Optional` and `Variant` inherits whether its wrapped type is trivially copy constructible, trivially copy assignable or trivially destructible. This lets the compiler optimize optimize a large number of their use cases. The constraints have been applied to `Optional`'s converting constructors too in order to make the API more explicit. This feature is not supported by Clang yet, so we use conditional compilation so that Lagom can be built on macOS. Once Clang has P0848R3 support, these can be removed.
2021-07-03Everywhere: Fix some alignment issuesDaniel Bertalan
When creating uninitialized storage for variables, we need to make sure that the alignment is correct. Fixes a KUBSAN failure when running kernels compiled with Clang. In `Syscalls/socket.cpp`, we can simply use local variables, as `sockaddr_un` is a POD type. Along with moving the `alignas` specifier to the correct member, `AK::Optional`'s internal buffer has been made non-zeroed by default. GCC emitted bogus uninitialized memory access warnings, so we now use `__builtin_launder` to tell the compiler that we know what we are doing. This might disable some optimizations, but judging by how GCC failed to notice that the memory's initialization is dependent on `m_has_value`, I'm not sure that's a bad thing.
2021-06-01AK: Enable direct comparsion of Optional<T> and TMax Wipfli
This patch introduces a new operator== to compare an Optional to its contained type directly. If the Optional does not contain a value, the comparison will always return false. This also adds a test case for the new behavior as well as comparison between Optional objects themselves.
2021-05-08AK: Add constructors to Optional that accept non const qualified inputsItamar
2021-04-22Everything: Move to SPDX license identifiers in all files.Brian Gianforcaro
SPDX License Identifiers are a more compact / standardized way of representing file license information. See: https://spdx.dev/resources/use/#identifiers This was done with the `ambr` search and replace tool. ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-16AK: Add type alias for AK::OptionalTimothy Flynn
2021-02-23Everywhere: Rename ASSERT => VERIFYAndreas Kling
(...and ASSERT_NOT_REACHED => VERIFY_NOT_REACHED) Since all of these checks are done in release builds as well, let's rename them to VERIFY to prevent confusion, as everyone is used to assertions being compiled out in release. We can introduce a new ASSERT macro that is specifically for debug checks, but I'm doing this wholesale conversion first since we've accumulated thousands of these already, and it's not immediately obvious which ones are suitable for ASSERT.
2021-02-23AK: Slap Optional with the ALWAYS_INLINE stickAndreas Kling
I saw some Optional constructors when profiling the dynamic loader and that seemed silly since we can inline them at no/little cost.
2021-02-15AK: Mark Optional getters as [[nodiscard]]Brian Gianforcaro
There is no reason to call a getter without observing the result, doing so indicates an error in the code. Mark these methods as [[nodiscard]] to find these cases.
2021-02-08Everywhere: Remove unnecessary headers 3/4Ben Wiederhake
Arbitrarily split up to make git bisect easier. These unnecessary #include's were found by combining an automated tool (which determined likely candidates) and some brain power (which decided whether the #include is also semantically superfluous).
2021-01-12AK: Simplify constructors and conversions from nullptr_tLenny Maiorani
Problem: - Many constructors are defined as `{}` rather than using the ` = default` compiler-provided constructor. - Some types provide an implicit conversion operator from `nullptr_t` instead of requiring the caller to default construct. This violates the C++ Core Guidelines suggestion to declare single-argument constructors explicit (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c46-by-default-declare-single-argument-constructors-explicit). Solution: - Change default constructors to use the compiler-provided default constructor. - Remove implicit conversion operators from `nullptr_t` and change usage to enforce type consistency without conversion.
2020-12-31Everywhere: Re-format with clang-format-11Linus Groh
Compared to version 10 this fixes a bunch of formatting issues, mostly around structs/classes with attributes like [[gnu::packed]], and incorrect insertion of spaces in parameter types ("T &"/"T &&"). I also removed a bunch of // clang-format off/on and FIXME comments that are no longer relevant - on the other hand it tried to destroy a couple of neatly formatted comments, so I had to add some as well.
2020-12-31AK: Add operator* and operator-> overloads in Optional.asynts
2020-11-22AK: Cleanup missing includes and #ifdef evaluationLenny Maiorani
Problem: - Several files have missing includes. This results in complaints from `clang-tidy`. - `#ifdef` is followed by `#elif <value>` which evaluates to `0`. Solution: - Add missing includes. - Change to `#elif defined(<value>)`.
2020-08-30AK: Add Optional::emplace method.asynts
2020-08-23AK: Add operator== to AK::OptionalPeter Elliott
The semantics: - two Optionals are equal if they are both None - two Optionals are equal if they are both Some, and their values are operator==
2020-08-05AK: Decorate Optional<T> with [[nodisard]]Brian Gianforcaro
2020-05-16AK: Remove experimental clang -Wconsumed stuffAndreas Kling
This stopped working quite some time ago due to Clang losing track of typestates for some reason and everything becoming "unknown". Since we're primarily using GCC anyway, it doesn't seem worth it to try and maintain this non-working experiment for a secondary compiler. Also it doesn't look like the Clang team is actively maintaining this flag anyway. So good-bye, -Wconsumed. :/
2020-04-30AK: Add ALWAYS_INLINE, NEVER_INLINE and FLATTEN macrosAndreas Kling
It's tedious to write (and look at) [[gnu::always_inline]] etc. :^)
2020-04-12AK: Inline Optional functions more aggressivelyAndreas Kling
This turns into much less code in the most common cases, here's why: The normal Optional usage pattern is something like: auto foo = get_me_an_optional(); if (foo.has_value()) do_stuff_with(foo.value()); In this typical scenario, we check has_value() before calling value(). Without inlining, value() will double-check has_value() itself and assert if it fails. Inlining allows the compiler to optimize all of this away.
2020-03-08AK: Move memory stuff (fast memcpy, etc) to a separate headerAndreas Kling
Move the "fast memcpy" stuff out of StdLibExtras.h and into Memory.h. This will break a ton of things that were relying on StdLibExtras.h to include a bunch of other headers. Fix will follow immediately after. This makes it possible to include StdLibExtras.h from Types.h, which is the main point of this exercise.
2020-03-08AK: Use __builtin_memset() and such to reduce header dependenciesAndreas Kling
We can use __builtin_memset() without including <string.h>. This is pretty neat, as it will allow us to reduce the header deps of AK templates a bit, if applied consistently. Note that this is an enabling change for an upcoming #include removal.
2020-03-06AK: Remove Optional::operator bool()Andreas Kling
This was causing some obvious-in-hindsight but hard to spot bugs where we'd implicitly convert the bool to an integer type and carry on with the number 1 instead of the actual value().
2020-02-24AK: Zero-initialize the internal storage of OptionalAndreas Kling
2020-02-14LibCore: Add a forward declaration headerAndreas Kling
This patch adds <LibCore/Forward.h> and uses it in various places to shrink the header dependency graph.
2020-02-06AK: Add missing StdLibExtras.h include in Optional.hAndreas Kling
2020-01-18Meta: Add license header to source filesAndreas Kling
As suggested by Joshua, this commit adds the 2-clause BSD license as a comment block to the top of every source file. For the first pass, I've just added myself for simplicity. I encourage everyone to add themselves as copyright holders of any file they've added or modified in some significant way. If I've added myself in error somewhere, feel free to replace it with the appropriate copyright holder instead. Going forward, all new source files should include a license header.
2019-08-25AK: Optional::operator bool() should consume the OptionalAndreas Kling
We use consumable annotations to catch bugs where you get the .value() of an Optional before verifying that it's okay. The bug here was that only has_value() would set the consumed state, even though operator bool() does the same job.
2019-08-08AK: Add Optional<T>(const U&)Andreas Kling
This replaces Optional<T>(U&&) which clang-tidy complained may hide the regular copy and move constructors. That's a good point, clang-tidy, and I appreciate you pointing that out!
2019-08-07AK: Fix -Wconsumed warnings in Optional move-ctor and move-assignAndreas Kling
Our protocol says we have to call has_value() before release_value(). The code was already safe, but the compiler had no way of knowing that.
2019-08-05AK: Fix leak in Optional(Optional&&)Andreas Kling
We were *copying* the other Optional's value and then marking it as not having a value. This prevented us from ever destroying the original.
2019-08-05AK: Optional::operator=(Optional&&) should clear movee's has_value bitAndreas Kling
We were forgetting to clear m_has_value in the Optional being moved from when using operator=(Optional&&).
2019-07-31Optional: Add consumable checksRobin Burchell
These will, when building with clang, prevent using Optional::value without first checking Optional::has_value() - at compile time.
2019-07-24AK: Make HashMap::get(Key) return an Optional<Value>.Andreas Kling
This allows HashMap::get() to be used for value types that cannot be default constructed (e.g NonnullOwnPtr.)
2019-07-24AK: Add Optional::value_or(T).Andreas Kling
This is like value() but with a fallback in case there's no value set.
2019-07-08AK: Add a simple Optional<T> template.Andreas Kling
This can be used to store any type, with a flag that says if any value is present or not.