summaryrefslogtreecommitdiff
path: root/AK/Variant.h
AgeCommit message (Collapse)Author
2023-02-17AK+LibWeb: Implement Variant equality operatorkleines Filmröllchen
And make use of it for CSS StyleValues.
2022-12-26AK: Specialize TypeList for Variant typesTimothy Flynn
This allows callers to use the following semantics: using MyVariant = Variant<Empty, int>; template<typename T> size_t size() { return TypeList<T>::size; } auto s = size<MyVariant>(); This will be needed for an upcoming IPC change, which will result in us knowing the Variant type, but not the underlying variadic types that the Variant holds.
2022-12-13AK: Make Variant's index type publickleines Filmröllchen
This will allow the IPC system to use the exact required index type, saving transmission space, once it can send variants.
2022-12-11AK: Allow the user to access the variant indexAli Mohammad Pur
The average user has no need for this, but the Jakt compiler uses this to avoid going through the expensive ::visit() and ::get<>() APIs.
2022-12-03Everywhere: Run clang-formatLinus Groh
2022-11-26AK: Make it possible to not `using` AK classes into the global namespaceAndreas Kling
This patch adds the `USING_AK_GLOBALLY` macro which is enabled by default, but can be overridden by build flags. This is a step towards integrating Jakt and AK types.
2022-11-10AK: Allow Variant::downcast<OtherVariantType>()Ali Mohammad Pur
We usually give type aliases to variants, so their variant types are not always available, so make it possible to downcast to another variant type.
2022-10-15AK: Explictly disallow lvalue reference types within VariantTimothy Flynn
This prevents an ICE with GCC trying to declare e.g. Variant<String&>. Using a concept is a bit overkill here, but clang otherwise trips over the friendship declaration to other Variant types: template<typename... NewTs> friend struct Variant; Without using a concept, clang believes this is re-declaring the Variant type with differing requirements ("error: requires clause differs in template redeclaration").
2022-05-21AK: Use static_cast to cast to base typeAli Mohammad Pur
This is an issue on systems that don't have the empty base class optimisation (such as windows), and we normally don't need to care - however static_cast is technically the right thing to use, so let's use that instead. Co-Authored-By: Daniel Bertalan <dani@danielbertalan.dev>
2022-04-01Everywhere: Run clang-formatIdan Horowitz
2022-02-15AK: Conditionally disable a few variant ctors/assignmentsAli Mohammad Pur
We shouldn't let copy/move ctors or assignments be instantiated if the assignee type does not have a copy/move constructor (even if they're not used anywhere).
2022-02-06AK: Replace 'consteval' with 'constexpr' in some Variant helpersAli Mohammad Pur
CLion and/or clangd didn't like the consteval and highlighted visit() as an error, just replace it with constexpr as it makes no difference here.
2022-01-28AK: Simplify Variant's explicit overload detection mechanism a bitAli Mohammad Pur
This also allows us to remove the max-64-visitors restriction, and so removes that assertion.
2022-01-14AK: Make Variant::visit() prefer overloads accepting T const& over T&Ali Mohammad Pur
This makes the following code behave as expected: Variant<int, String> x { some_string() }; x.visit( [](String const&) {}, // Expectation is for this to be called [](auto&) {});
2022-01-14AK+Everywhere: Make Variant::visit() respect the Variant's constnessAli Mohammad Pur
...and fix all the instances of visit() taking non-const arguments.
2021-12-25AK: Remove Variant<Ts...>::operator Variant<NewTs...>()Ali Mohammad Pur
This is an interface to downcast(), which degrades errors into runtime errors, and allows seemingly-correct-but-not-quite constructs like the following to appear to compile, but fail at runtime: Variant<NonnullRefPtr<T>, U> foo = ...; Variant<RefPtr<T>, U> bar = foo; The expectation here is that `foo` is converted to a RefPtr<T> if it contains one, and remains a U otherwise, but in reality, the NonnullRefPtr<T> variant is simply dropped on the floor, and the resulting variant becomes invalid, failing the assertion in downcast(). This commit adds a Variant<Ts...>(Variant<NewTs...>) constructor that ensures that no alternative can be left out at compiletime, for the users that were using this interface for merely increasing the number of alternatives (for instance, LibSQL's Value class).
2021-11-14AK: Resolve clang-tidy warnings about unusual assignment operatorsAndrew Kaster
Either not returning *this, or in the case of Variant, not checking for self assignment. In AK::Atomic, we can't return *this due to the wrapper semantics Atomic implements.
2021-09-21AK: Introduce ability to default-initialize a VariantBen Wiederhake
I noticed that Variant<Empty, …> is a somewhat common pattern while working on #10080, and this will simplify a few use-cases. :^)
2021-09-16AK: Use default constructor/destructor instead of declaring an empty oneBrian Gianforcaro
Default implementations allow for more optimizations. See: https://pvs-studio.com/en/docs/warnings/v832/
2021-09-06AK: Use the full name of 'integer_sequence_generate_array' in Variant.hAli Mohammad Pur
c27abaabc449e20e8cffac245d1e2686f94e2ba6 moved this out of the global namespace, but did not qualify its users. While this seems to be fine (sometimes, somehow), let's qualify it to avoid random breakage.
2021-08-13AK+Everywhere: Delete Variant's default constructorAli Mohammad Pur
This was exposed to the user by mistake, and even accumulated a bunch of users that didn't blow up out of sheer luck.
2021-08-12AK: Don't zero Variant data in the move constructorAli Mohammad Pur
There's no reason to zero the data that will be immediately overwritten.
2021-07-04AK: Destroy original value when assigning to VariantDaniel Bertalan
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-06-28AK: Add and use the RemoveCVReference<T> type traitAli Mohammad Pur
2021-06-27AK: Add explicit Variant conversion operatorsAli Mohammad Pur
This allows converting between Variants of different types with less pain.
2021-06-26AK: Undo bogus Variant::downcast() renameAndreas Kling
I accidentally renamed these to verify_cast() when doing the global AK::downcast() rename.
2021-06-24AK: Rename downcast<T> => verify_cast<T>Andreas Kling
This makes it much clearer what this cast actually does: it will VERIFY that the thing we're casting is a T (using is<T>()).
2021-06-09AK: Make a bunch of Variant methods ALWAYS_INLINEAli Mohammad Pur
2021-06-02AK+LibWasm+LibJS: Disallow Variant.has() on types that aren't containedAli Mohammad Pur
Checking for this (and get()'ing it) is always invalid, so let's just disallow it. This also finds two bugs where the code is checking for types that can never actually be in the variant (which was actually a refactor artifact).
2021-05-22AK: Fix Variant construction from lvalue referencesAli Mohammad Pur
Fixes #7371 and appends its test cases.
2021-05-22AK: Remove [[gnu::noinline]] attribute from some variant membersAli Mohammad Pur
These were left-overs from a debugging session :P
2021-05-20Variant: Remove redundant inline keywordLenny Maiorani
Problem: - `constexpr inline` is redundant because `constexpr` implies `inline`. Solution: - Remove redundancy.
2021-05-19AK: Allow AK::Variant::visit to return a valueTimothy Flynn
This changes Variant::visit() to forward the value returned by the selected visitor invocation. By perfectly forwarding the returned value, this allows for the visitor to return by value or reference. Note that all provided visitors must return the same type - the compiler will otherwise fail with the message: "inconsistent deduction for auto return type".
2021-05-17Everywhere: Fix a bunch of typosLinus Groh
2021-05-13AK: Fix Variant's copy constructor trying to delegate to the wrong baseAli Mohammad Pur
This was forgotten in 4fdbac2.
2021-05-11AK/Variant: Deduplicate the contained typesAli Mohammad Pur
This allows the construction of `Variant<int, int, int>`. While this might not seem useful, it is very useful for making variants that contain a series of member function pointers, which I plan to use in LibGL for glGenLists() and co.
2021-05-11AK: Avoid the use of typeinfo in VariantAli Mohammad Pur
typeid() and RTTI was a nice clutch to implement this, but let's move away from the horrible slowness and implement variants using type indices for faster variants.
2021-05-05AK: Add a Variant<Ts...> implementationAli Mohammad Pur
Also adds an AK::Empty struct, because 'empty' variants are useful, but this implementation leaves that to the user (i.e. a variant cannot actually be empty, but it can contain an instance of Empty - i.e. a byte). Note that this is more of a constrained Any type, but they basically do the same things anyway :^)