Commit graph

107 commits

Author SHA1 Message Date
Andrew Kaster 3bf77f01a7 AK: Add a TypeList class for expanded compile-time tools
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-30 11:32:20 +01:00
Andrew Kaster fe4b44b489 AK: Add IsArithmetic and IsFundamental type traits
Also, make sure to using AK::IsNullPointer
2020-12-30 11:32:20 +01:00
asynts 620b73b3d5 AK+Format: Accept unsigned long in replacement fields.
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-29 02:36:32 +01:00
AnotherTest ad646420dd AK: Make AK::IsSame<T, U>::value a constexpr bool
It being an enum value was preventing it from being used without `!!` in
requires clauses (bool also makes more sense anyway).
2020-12-26 12:32:27 +01:00
Lenny Maiorani 765936ebae
Everywhere: Switch from (void) to [[maybe_unused]] (#4473)
Problem:
- `(void)` simply casts the expression to void. This is understood to
  indicate that it is ignored, but this is really a compiler trick to
  get the compiler to not generate a warning.

Solution:
- Use the `[[maybe_unused]]` attribute to indicate the value is unused.

Note:
- Functions taking a `(void)` argument list have also been changed to
  `()` because this is not needed and shows up in the same grep
  command.
2020-12-21 00:09:48 +01:00
Lenny Maiorani f5ced347e6 AK: Prefer using instead of typedef
Problem:
- `typedef` is a keyword which comes from C and carries with it old
  syntax that is hard to read.
- Creating type aliases with the `using` keyword allows for easier
  future maintenance because it supports template syntax.
- There is inconsistent use of `typedef` vs `using`.

Solution:
- Use `clang-tidy`'s checker called `modernize-use-using` to update
  the syntax to use the newer syntax.
- Remove unused functions to make `clang-tidy` happy.
- This results in consistency within the codebase.
2020-11-12 10:19:04 +01:00
Tom 75f61fe3d9 AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.

Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.

In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-11-10 19:11:52 +01:00
Tom 3c1ef744f6 AK: Add RefPtrTraits to allow implementing custom null pointers
This adds the ability to implement custom null states that allow
storing state in null pointers.
2020-11-10 19:11:52 +01:00
Lenny Maiorani d1fe6a0b53
Everywhere: Redundant inline specifier on constexpr functions (#3807)
Problem:
- `constexpr` functions are decorated with the `inline` specifier
  keyword. This is redundant because `constexpr` functions are
  implicitly `inline`.
- [dcl.constexpr], §7.1.5/2 in the C++11 standard): "constexpr
  functions and constexpr constructors are implicitly inline (7.1.2)".

Solution:
- Remove the redundant `inline` keyword.
2020-10-20 18:08:13 +02:00
Lenny Maiorani a40abd6ce3 Checked: constexpr support
Problem:
- `Checked` is not `constexpr`-aware.

Solution:
- Decorate member functions with `constexpr` keyword.
- Add tests to ensure the functionality where possible.
2020-10-20 16:31:24 +02:00
AnotherTest b42c6ea281 LibIPC: Make IPC::encode() and ::decode() fail at compiletime when used
This would previously fail at runtime, and it would have zero indication
of what exactly went wrong.
Also adds `AK::DependentFalse<Ts...>', which is a...dependent false.
2020-10-04 23:12:28 +02:00
Tom bb92eab9ce AK: Add is_trivial and is_trivially_copyable 2020-10-02 15:38:07 +02:00
asynts 90536a1558 AK: Consider long and unsigned long as integral types.
Two things I hate about C++:

 1. 'int', 'signed int' and 'unsigned int' are two distinct types while
    'char, 'signed char' and 'unsigned char' are *three* distinct types.

    This is because 'signed int' is an alias for 'int' but 'signed char'
    can't be an alias for 'char' because on some weird systems 'char' is
    unsigned.

    One might think why not do it the other way around, make 'int' an
    alias for 'signed int' and 'char' an alias for whatever that is on
    the platform, or make 'char' signed on all platforms. But who am I
    to ask?

 2. 'unsigned long' and 'unsigned long long' are always different types,
    even if both are 64 bit numbers.

This commit fixes a few bugs that coming from this.

See Also: 1b3169f405.
2020-09-22 15:06:40 +02:00
AnotherTest 72edb33670 AK: Generalise 'PrintfImplementation'
This makes PrintfImplementation usable with any sequence, provided that
a 'next element' function can be written for it.
Does not affect the behaviour of printf() and co.
2020-09-11 21:41:23 +02:00
asynts 1b3169f405 AK: Define MakeUnsigned and MakeSigned for char.
For some weird reason the C++ standard considers char, signed char and
unsigned char *three* different types. On the other hand int is just an
alias for signed int, meaning that int, signed int and unsigned int are
just *two* different types.

https://stackoverflow.com/a/32856568/8746648
2020-08-27 15:49:01 +02:00
Ben Wiederhake 53abc626c2 AK: Print RHS and LHS in EXPECT_EQ if we can
This makes error messages more useful during debugging.

Old:

    START Running test compare_views
    FAIL: ../AK/Tests/TestStringView.cpp:59: EXPECT_EQ(view1, "foobar") failed

New:

    START Running test compare_views
    FAIL: ../AK/Tests/TestStringView.cpp:59: EXPECT_EQ(view1, "foobar") failed: LHS="foo", RHS="foobar"
2020-08-23 11:24:55 +02:00
Muhammad Zahalqa a68650a7b4
AK: HashTable add a constructor that allows preallocation of capacity + Use in CppLexer. (#3147)
1. Add general utility to get array number of elements.
2. Add Needed API to AK::HashTable
3. Refactor CppLexer initialization
2020-08-16 11:04:00 +02:00
asynts 5f7427ba4b AK: Add Integral and FloatingPoint concepts. 2020-08-06 10:33:16 +02:00
asynts 05abfc0e1f AK: Rename MakeUnsigned::type to MakeUnsigned::Type.
Also renames MakeSigned::type to MakeSigned::Type.
2020-08-06 10:33:16 +02:00
Muhammad Zahalqa 9495eeb075
AK: Make min/max behave like the STL for equivalent inputs (#2976)
min(a, b) now returns a if both are equivalent.
max(a, b) now returns a if both are equivalent.
2020-08-06 09:58:45 +02:00
Andreas Kling ce2c5b375c AK: Add global is<T>() and downcast<T>()
Let's unify the is<T>/to<T> implementations that currently exist in
separate versions in LibCore and LibWeb.
2020-07-26 17:51:00 +02:00
Andreas Kling cd4bc81dbb AK: Add a couple more helper templates to StdLibExtras 2020-07-24 02:38:17 +02:00
Andreas Kling 70cb4491d7 AK: Use "signed char" as the opposite of "unsigned char"
I totally forgot about the C++ basics here. There are three distinct
types: "char", "signed char" and "unsigned char". Whether "char" is
signed or unsigned is implementation specific.
2020-07-18 17:57:40 +02:00
Andreas Kling e90ecdda48 AK: Add MakeSigned<T> helper template 2020-05-23 15:25:43 +02:00
Andreas Kling a59453d4b7 AK: Tweak exchange() implementation
Make it constexpr and do perfect forwarding.
2020-04-22 12:36:35 +02:00
Andreas Kling 9a5dba9e09 AK: Add MakeUnsigned<T> helper template 2020-04-15 16:58:46 +02:00
Andreas Kling f8942411ac AK: Add forward() overload that refuses to forward lvalue as rvalue
This matches what other forward() implementations do.
2020-04-07 15:56:19 +02:00
Andreas Kling 900f51ccd0 AK: Move memory stuff (fast memcpy, etc) to a separate header
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-08 13:06:51 +01:00
Andreas Kling b98d8ad5b0 AK: Add a Conditional<condition, TrueType, FalseType> template
This allows you to select a type based on a compile-time condition.
2020-03-08 13:06:51 +01:00
Liav A 8bdb08c354 AK: Apply changes for the Bootstrapper environment 2020-02-09 19:38:17 +01:00
Andreas Kling 2309029cb4 AK: Allow clamp() with min==max 2020-01-20 13:49:05 +01:00
Shannon Booth de74458f13 AK: Add clamp() function
This function can be used to more cleanly write the common operation of
clamping a value between two values.
2020-01-20 10:35:12 +01:00
Andreas Kling 604c5cb98e AK: Add some missing "inline" keywords in StdLibExtras.h 2020-01-19 10:33:26 +01:00
Andreas Kling 94ca55cefd Meta: Add license header to source files
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.
2020-01-18 09:45:54 +01:00
Andreas Kling 57c29491a3 Kernel+AK: Remove AK/StdLibExtras.cpp, moving kernel stuff to Kernel/.
We had some kernel-specific gizmos in AK that should really just be in the
Kernel subdirectory instead. The only thing remaining after moving those
was mmx_memcpy() which I moved to the ARCH(i386)-specific section of
LibC/string.cpp.
2019-07-29 11:58:44 +02:00
Andreas Kling 27f699ef0c AK: Rename the common integer typedefs to make it obvious what they are.
These types can be picked up by including <AK/Types.h>:

* u8, u16, u32, u64 (unsigned)
* i8, i16, i32, i64 (signed)
2019-07-03 21:20:13 +02:00
Andreas Kling 7f613c79cd AK: Oops, fix typo in RemoveVolatile<T> helper. 2019-06-27 16:01:24 +02:00
Andreas Kling 2dd54f062a AK: Mark some helper things constexpr. 2019-06-24 10:13:28 +02:00
Andreas Kling 39d1a9ae66 Meta: Tweak .clang-format to not wrap braces after enums. 2019-06-07 17:13:23 +02:00
Andreas Kling b34b084619 AK: Run clang-format on everything. 2019-06-07 11:46:22 +02:00
Andreas Kling b8e705da0e LibCore: CObjects without is<T> specialization shouldn't LARP as others. 2019-06-01 14:11:31 +02:00
Robin Burchell 0dc9af5f7e Add clang-format file
Also run it across the whole tree to get everything using the One True Style.
We don't yet run this in an automated fashion as it's a little slow, but
there is a snippet to do so in makeall.sh.
2019-05-28 17:31:20 +02:00
Andreas Kling cec16105cc Make sure all GraphicsBitmap scanlines are 16-byte aligned.
This is a prerequisite for some optimizations.
2019-05-06 14:04:54 +02:00
Andreas Kling 6693cfb26a Kernel: Don't use MMX memcpy() in the kernel.
I just discovered the hard way that clobbering FPU/MMX/SSE registers in the
kernel makes things very confusing for userspace (and other kernel threads.)

Let's banish all of those things from the kernel to keep things simple.
2019-04-22 17:13:18 +02:00
Andreas Kling 301a269ca0 Get rid of SERENITY macro since the compiler already defines __serenity__
This makes it a bit easier to use AK templates out-of-tree.
2019-04-20 12:58:49 +02:00
Andreas Kling 47d270b577 WindowServer: Factor out window frame logic into a WSWindowFrame class.
The window frame is an object that contains a window, its title bar and
window border. This way WSWindowManager doesn't have to know about all the
different types of window borders, titlebar rects, etc.
2019-04-05 15:54:56 +02:00
Andreas Kling 9dfcd95cd7 Use 64-bit integers inside Stopwatch to enable longer timings. 2019-03-21 13:41:36 +01:00
Andreas Kling 2cfcbdc735 AK: Add Retained<T>, like RetainPtr, but never null.
Also use some Clang attribute wizardry to get a warning for use-after-move.
2019-02-25 12:43:52 +01:00
Andreas Kling 809ffa56d7 Kernel: Reduce code duplication in exception handlers. 2019-02-20 12:28:41 +01:00
Andreas Kling 022f7790db Use modern C++ attributes instead of __attribute__ voodoo.
This is quite nice, although I wish [[gnu::always_inline]] implied inline.
Also "gnu::" is kind of a wart, but whatcha gonna do.
2019-02-15 12:30:48 +01:00
Andreas Kling 1f159eaab0 Add a fast memcpy() using MMX when we're moving >= 1KB.
This is a nice speedup for WindowServer. I'll eventually have to do this
with SSE but the kernel doesn't support SSE yet so this is it for now.
2019-02-07 08:46:52 +01:00
Andreas Kling ffab6897aa Big, possibly complete sweep of naming changes. 2019-01-31 17:31:23 +01:00
Andreas Kling b5c76d7559 Get rid of #ifdef SERENITY. We're past that phase of bootstrapping. 2019-01-17 01:41:36 +01:00
Andreas Kling f651405694 Optimize the Painter::blit() loop a bit. ~3% fewer cycles, I'll take it. 2019-01-16 19:50:25 +01:00
Andreas Kling 17c7bf01a5 Fix Userland build. 2019-01-13 04:31:16 +01:00
Andreas Kling edc827077e Optimize WindowManager::flush() with fast_dword_copy(). 2019-01-12 21:45:45 +01:00
Andreas Kling ca6847b5bb Import a simple text editor I started working on. 2018-12-04 00:27:16 +01:00
Renamed from AK/StdLib.h (Browse further)