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