2020-09-09 11:41:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-09 11:41:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Traits.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class TypedTransfer {
|
|
|
|
public:
|
2021-02-07 11:00:56 +00:00
|
|
|
static void move(T* destination, T* source, size_t count)
|
2020-09-09 11:41:58 +00:00
|
|
|
{
|
2021-11-06 20:12:16 +00:00
|
|
|
if (count == 0)
|
2021-02-07 11:00:56 +00:00
|
|
|
return;
|
2021-02-05 18:46:31 +00:00
|
|
|
|
2020-09-09 11:41:58 +00:00
|
|
|
if constexpr (Traits<T>::is_trivial()) {
|
|
|
|
__builtin_memmove(destination, source, count * sizeof(T));
|
2021-02-07 11:00:56 +00:00
|
|
|
return;
|
2020-09-09 11:41:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
if (destination <= source)
|
2022-12-13 06:59:30 +00:00
|
|
|
new (&destination[i]) T(AK::move(source[i]));
|
2020-09-09 11:41:58 +00:00
|
|
|
else
|
2022-12-13 06:59:30 +00:00
|
|
|
new (&destination[count - i - 1]) T(AK::move(source[count - i - 1]));
|
2020-09-09 11:41:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 07:03:52 +00:00
|
|
|
static size_t copy(T* destination, T const* source, size_t count)
|
2020-09-09 11:41:58 +00:00
|
|
|
{
|
2021-11-06 20:12:16 +00:00
|
|
|
if (count == 0)
|
2021-02-05 18:46:31 +00:00
|
|
|
return 0;
|
|
|
|
|
2020-09-09 11:41:58 +00:00
|
|
|
if constexpr (Traits<T>::is_trivial()) {
|
2021-12-17 11:36:25 +00:00
|
|
|
if (count == 1)
|
|
|
|
*destination = *source;
|
|
|
|
else
|
|
|
|
__builtin_memmove(destination, source, count * sizeof(T));
|
2020-09-09 11:41:58 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
if (destination <= source)
|
|
|
|
new (&destination[i]) T(source[i]);
|
|
|
|
else
|
|
|
|
new (&destination[count - i - 1]) T(source[count - i - 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2022-11-03 07:03:52 +00:00
|
|
|
static bool compare(T const* a, T const* b, size_t count)
|
2020-09-09 11:41:58 +00:00
|
|
|
{
|
2021-11-06 20:12:16 +00:00
|
|
|
if (count == 0)
|
2021-02-05 18:46:31 +00:00
|
|
|
return true;
|
|
|
|
|
2020-09-09 11:41:58 +00:00
|
|
|
if constexpr (Traits<T>::is_trivial())
|
|
|
|
return !__builtin_memcmp(a, b, count * sizeof(T));
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
if (a[i] != b[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-11-03 07:03:52 +00:00
|
|
|
|
|
|
|
static void delete_(T* ptr, size_t count)
|
|
|
|
{
|
|
|
|
if (count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if constexpr (Traits<T>::is_trivial()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; ++i)
|
|
|
|
ptr[i].~T();
|
|
|
|
}
|
2020-09-09 11:41:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|