summaryrefslogtreecommitdiff
path: root/AK/StdLibExtras.h
AgeCommit message (Collapse)Author
2023-04-30AK: Move taint_for_optimizer to StdLibExtras.hDan Klishch
Additionally, split it into two versions (for IsIntegral<T> -- asking to place value into register and for !IsIntegral<T> -- asking to place value into memory with memory clobber), so that Clang is no more completely confused about `taint_for_optimizer(AK::StringView&)`.
2023-04-12AK: Make grepping for "lerp" find mix()Nico Weber
2023-04-08AK: Bake CLion IDE check into AK_COMPILER_CLANGAndreas Kling
For whatever reason, when CLion does its code indexing thing, it doesn't define __clang__ despite using Clang. This causes it to run into various problems that we've solved by checking for Clang. Since CLion does define __CLION_IDE__ (or sometimes __CLION_IDE_, no idea why but I have seen this issue locally), let's make that part of the AK_COMPILER_CLANG check. This makes CLion stop highlighting various things as errors.
2023-03-09AK: Replace C-style castsSam Atkins
2023-03-04AK: Move compiletime_fail to StdLibExtras.hDan Klishch
This function will be used in the next commit in "BigIntBase.h".
2022-12-14AK: Bring back the AK_DONT_REPLACE_STD #defineAli Mohammad Pur
This was removed in a910961f37d1da9dafb6385e348266746354cf98 in favour of the more general USING_AK_GLOBALLY #define, but Ladybird (and probably other projects) depend on the smaller hammer to include STL headers and keep the USING_AK_GLOBALLY behaviour, so put it back and preserve its behaviour.
2022-12-14Everywhere: Stop shoving things into ::std and mentioning them as suchAli Mohammad Pur
Note that this still keeps the old behaviour of putting things in std by default on serenity so the tools can be happy, but if USING_AK_GLOBALLY is unset, AK behaves like a good citizen and doesn't try to put things in the ::std namespace. std::nothrow_t and its friends get to stay because I'm being told that compilers assume things about them and I can't yeet them into a different namespace...for now.
2022-12-13AK: Fix build with !USING_AK_GLOBALLYAli Mohammad Pur
A couple headers expected names to be in the global namespace, qualify those names to make sure they're resolved even when the names are not exported. One header placed its functions in the global namespace, move those to the AK namespace to make the concepts resolve.
2022-12-09Everywhere: Remove unnecessary AK and Detail namespace scopingMoustafa Raafat
2022-12-03Everywhere: Remove 'clang-format off' comments that are no longer neededLinus Groh
https://github.com/SerenityOS/serenity/pull/15654#issuecomment-1322554496
2022-12-03Everywhere: Run clang-formatLinus Groh
2022-11-27AK: Fully qualify some usages of AK features outside of the AK namespaceTim Schumacher
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-10-04AK+Everywhere: Add AK_COMPILER_{GCC,CLANG} and use them most placesNico Weber
Doesn't use them in libc headers so that those don't have to pull in AK/Platform.h. AK_COMPILER_GCC is set _only_ for gcc, not for clang too. (__GNUC__ is defined in clang builds as well.) Using AK_COMPILER_GCC simplifies things some. AK_COMPILER_CLANG isn't as much of a win, other than that it's consistent with AK_COMPILER_GCC.
2022-08-05AK: Suppress -Wunqualified-std-cast-call in the CLion IDEAndreas Kling
2022-04-01Everywhere: Run clang-formatIdan Horowitz
2022-01-29AK: Add is_power_of_2 helperPankaj Raghav
2022-01-04AK: Add `mix`Jelle Raaijmakers
2022-01-02AK: Include `utility` from the STD if we aren't replacing STDJames Puleo
If we didn't define our own `move` and `forward` and inject it into the `std` namespace, then we would error just after trying to `using` those, if no one has included it before. Now, we will include `utility` from the STD if we aren't replacing the `std`.
2021-11-14AK+Kernel: Suppress clang-tidy warnings from the cert-* categoryAndrew Kaster
cert-dcl50-cpp: No variadic functions, suppressed in RefCounted and ThreadSafeRefCounted for implementing the magic one_ref_left and will_be_destroyed functions. cert-dcl58-cpp: No opening ::std, suppressed in the places we put names in ::std to aid tools (move, forward, nullptr_t, align_val_t, etc).
2021-11-14AK: Use capitalized literal suffixes for AK::abs() overloadsAndrew Kaster
Using `l` for long double causes a clang-tidy warning, so use all caps suffixes for all of the AK::abs() overloads for consistency. Also, avoid leaking the internal __DEFINE_GENERIC_ABS macro.
2021-11-14AK: Avoid else after return in files commonly included by the KernelAndrew Kaster
2021-09-04AK: Make declaration of std::move and std::forward optionalStephan Unverwerth
This introduces a new define AK_DONT_REPLACE_STD that disables our own implementation of std::move and std::forward. Some ports include both STL and AK headers which causes conflicts when trying to resolve those functions. The port can define AK_DONT_REPLACE_STD before including Serenity headers in that case.
2021-09-01AK: Move forward() into the std namespaceAndreas Kling
Same as we already did with move(). This allows compiler diagnostics and static analyzers like SonarCloud to detect more issues.
2021-08-30AK: Return early from swap() when swapping the same objectTimothy Flynn
When swapping the same object, we could end up with a double-free error. This was found while quick-sorting a Vector of Variants holding complex types, reproduced by the new swap_same_complex_object test case.
2021-07-15AK: Add workaround for clang-format 12 problems with conceptsDaniel Bertalan
2021-07-08AK+Userland: Add generic `AK::abs()` function and use itDaniel Bertalan
Previously, in LibGFX's `Point` class, calculated distances were passed to the integer `abs` function, even if the stored type was a float. This caused the value to unexpectedly be truncated. Luckily, this API was not used with floating point types, but that can change in the future, so why not fix it now :^) Since we are in C++, we can use function overloading to make things easy, and to automatically use the right version. This is even better than the LibC/LibM functions, as using a bit of hackery, they are able to be constant-evaluated. They use compiler intrinsics, so they do not depend on external code and the compiler can emit the most optimized code by default. Since we aren't using the C++ standard library's trick of importing everything into the `AK` namespace, this `abs` function cannot be exported to the global namespace, as the names would clash.
2021-06-27AK: Make the constexpr StringView methods actually constexprAli Mohammad Pur
Also add some tests to ensure that they _remain_ constexpr. In general, any runtime assertions, weirdo C casts, pointer aliasing, and such shenanigans should be gated behind the (helpfully newly added) AK::is_constant_evaluated() function when the intention is to write constexpr-capable code. a.k.a. deliver promises of constexpr-ness :P
2021-06-23AK: Make {min,max,clamp}(T, U) work when U can be implicitly cast to TAli Mohammad Pur
It was really annoying to `static_cast` the arguments to be the same type, so instead of doing that, just convert the second one to the first one, and let the compiler warn about sign differences and truncation.
2021-06-15AK: Add a function that casts an enum to its underlying typeAli Mohammad Pur
This is basically the same thing as `std::to_underlying(Enum)`.
2021-06-06AK+Everywhere: Disallow constructing Functions from incompatible typesAli Mohammad Pur
Previously, AK::Function would accept _any_ callable type, and try to call it when called, first with the given set of arguments, then with zero arguments, and if all of those failed, it would simply not call the function and **return a value-constructed Out type**. This lead to many, many, many hard to debug situations when someone forgot a `const` in their lambda argument types, and many cases of people taking zero arguments in their lambdas to ignore them. This commit reworks the Function interface to not include any such surprising behaviour, if your function instance is not callable with the declared argument set of the Function, it can simply not be assigned to that Function instance, end of story.
2021-05-29AK: Extend round_to_power_of_two to types other than `unsigned`Andrew Kaster
The previous implementation hardcoded unsigned, when the same logic easily extends to unsigned long, signed types, and other Integral types.
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+Kernel: Make IntrusiveList capable of holding non-raw pointersAnotherTest
This should allow creating intrusive lists that have smart pointers, while remaining free (compared to the impl before this commit) when holding raw pointers :^) As a sidenote, this also adds a `RawPtr<T>` type, which is just equivalent to `T*`. Note that this does not actually use such functionality, but is only expected to pave the way for #6369, to replace NonnullRefPtrVector<T> with intrusive lists. As it is with zero-cost things, this makes the interface a bit less nice by requiring the type name of what an `IntrusiveListNode` holds (and optionally its container, if not RawPtr), and also requiring the type of the container (normally `RawPtr`) on the `IntrusiveList` instance.
2021-04-10AK+Everywhere: Make StdLibExtras templates less wrapper-yAnotherTest
This commit makes the user-facing StdLibExtras templates and utilities arguably more nice-looking by removing the need to reach into the wrapper structs generated by them to get the value/type needed. The C++ standard library had to invent `_v` and `_t` variants (likely because of backwards compat), but we don't need to cater to any codebase except our own, so might as well have good things for free. :^)
2021-03-28AK: Add IsSigned conterpart to IsUnsigned.Michel Hermier
2021-03-28AK: Make Concepts.h and StdLibExtras.h properly alias their own sumbols.Michel Hermier
2021-03-17AK: Move move() into the "std" namespaceAndreas Kling
This makes GCC emit warnings about redundant and pessimizing moves. It also allows static analyzers like clang-tidy to detect common bugs like use-after-move.
2021-03-09AK: Include Assertions.h in StdLibExtras.hMițca Dumitru
2021-03-05AK: Implement IsEnum<T> and UnderlyingType<T> type traitsBrian Gianforcaro
I needed these meta-programming type traits while working on something else. Add basic support for these two type traits as well as some tests.
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+Userland: Extend the compiletime format string check to other functionsAnotherTest
Thanks to @trflynn89 for the neat implicit consteval ctor trick! This allows us to basically slap `CheckedFormatString` on any formatting function, and have its format argument checked at compiletime. Note that there is a validator bug where it doesn't parse inner replaced fields like `{:~>{}}` correctly (what should be 'left align with next argument as size' is parsed as `{:~>{` following a literal closing brace), so the compiletime checks are disabled on these temporarily by forcing them to be StringViews. This commit also removes the now unused `AK::StringLiteral` type (which was introduced for use with NTTP strings).
2021-02-10AK: Make IsUnsigned<T> behave as you would expectAndreas Kling
2021-02-08AK: Add dbgln() format checkingAnotherTest
This checks the following things: - No unclosed braces in format string `dbgln("a:{}}", a)` where the '}}' would be interpreted as a literal '}' `dbgln("a:{", a)` where someone with a faulty keyboard like mine could generate - No extra closed braces in format string `dbgln("a:{{}", a)` where the '{{' would interpreted as a literal '{' `dbgln("a:}", a)` where someone with a faulty keyboard could generate - No references to nonexistent arguments `dbgln("a:{} b:{}", a)` where the value of `b` is not in the arguments list - No unconsumed argument `dbgln("a:{1}", not_used, 1)` where `not_used` is extraneous
2021-01-28Lagom+AK: Remove remains of clang -Wconsumed usageAndreas Kling
We stopped using that warning ages ago since it confused the compiler.
2020-12-30AK: Add tests for type traits and IndexSequenceAndrew Kaster
Use TypeLists to add test for IsIntegral, IsFloatingPoint, IsVoid, IsNullPointer, IsArithmetic, IsFundamental, and AddConst type traits. More can "easily" be added once the TypeList and macro magic is squinted at for long enough :).
2020-12-30AK: Add a TypeList class for expanded compile-time toolsAndrew Kaster
Also add IndexSequence and associated helpers. The TypeList class can be queried for what type is at a certain index, and there are two helper functions: for_each_type, and for_each_type_zipped. for_each_type will invoke a lambda with a TypeWrapper object for each type in the type list. The original type can be obtained by extracting the ::Type from the type of your generic lambda's one argument. for_each_type_zipped will walk two TypeLists in lockstep and pass a TypeWrapper object for the current index in each list to a generic lambda. The original type from the TypeList can again be extracted via the ::Type of the generic lambda's two parameters.
2020-12-30AK: Add IsArithmetic and IsFundamental type traitsAndrew Kaster
Also, make sure to using AK::IsNullPointer
2020-12-29AK+Format: Accept unsigned long in replacement fields.asynts
I ran into this exact but at least twenty times in Serenity alone. The C++ Standard dictates that 'unsigned long' and 'unsigned long long' are distinct types even though on most platforms they are usually both 64 bit integers. Also it wasn't possible to evaluate IsIntegral<T> for types that were not integers since it used MakeUnsigned<T> internally.
2020-12-26AK: Make AK::IsSame<T, U>::value a constexpr boolAnotherTest
It being an enum value was preventing it from being used without `!!` in requires clauses (bool also makes more sense anyway).