mirror of
https://github.com/godotengine/godot.git
synced 2025-10-19 07:53:26 +00:00
Add move semantics to core containers.
Adds to SWAP, Variant, StringName, List, CowData and LocalVector. Co-Authored-By: Lukas Tenbrink <lukas.tenbrink@gmail.com>
This commit is contained in:
parent
cd92ad0f69
commit
d549b98c5c
6 changed files with 106 additions and 26 deletions
|
@ -38,6 +38,7 @@
|
|||
#include "core/vector.h"
|
||||
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
template <class T, class U = uint32_t, bool force_trivial = false>
|
||||
class LocalVector {
|
||||
|
@ -67,9 +68,9 @@ public:
|
|||
}
|
||||
|
||||
if (!std::is_trivially_constructible<T>::value && !force_trivial) {
|
||||
memnew_placement(&data[count++], T(p_elem));
|
||||
memnew_placement(&data[count++], T(std::move(p_elem)));
|
||||
} else {
|
||||
data[count++] = p_elem;
|
||||
data[count++] = std::move(p_elem);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,7 +78,7 @@ public:
|
|||
ERR_FAIL_UNSIGNED_INDEX(p_index, count);
|
||||
count--;
|
||||
for (U i = p_index; i < count; i++) {
|
||||
data[i] = data[i + 1];
|
||||
data[i] = std::move(data[i + 1]);
|
||||
}
|
||||
if (!std::is_trivially_destructible<T>::value && !force_trivial) {
|
||||
data[count].~T();
|
||||
|
@ -90,7 +91,7 @@ public:
|
|||
ERR_FAIL_INDEX(p_index, count);
|
||||
count--;
|
||||
if (count > p_index) {
|
||||
data[p_index] = data[count];
|
||||
data[p_index] = std::move(data[count]);
|
||||
}
|
||||
if (!std::is_trivially_destructible<T>::value && !force_trivial) {
|
||||
data[count].~T();
|
||||
|
@ -193,13 +194,13 @@ public:
|
|||
void insert(U p_pos, T p_val) {
|
||||
ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
|
||||
if (p_pos == count) {
|
||||
push_back(p_val);
|
||||
push_back(std::move(p_val));
|
||||
} else {
|
||||
resize(count + 1);
|
||||
for (U i = count - 1; i > p_pos; i--) {
|
||||
data[i] = data[i - 1];
|
||||
data[i] = std::move(data[i - 1]);
|
||||
}
|
||||
data[p_pos] = p_val;
|
||||
data[p_pos] = std::move(p_val);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -284,6 +285,17 @@ public:
|
|||
data[i] = r[i];
|
||||
}
|
||||
}
|
||||
|
||||
LocalVector(LocalVector &&p_from) {
|
||||
data = p_from.data;
|
||||
count = p_from.count;
|
||||
capacity = p_from.capacity;
|
||||
|
||||
p_from.data = nullptr;
|
||||
p_from.count = 0;
|
||||
p_from.capacity = 0;
|
||||
}
|
||||
|
||||
inline LocalVector &operator=(const LocalVector &p_from) {
|
||||
resize(p_from.size());
|
||||
for (U i = 0; i < p_from.count; i++) {
|
||||
|
@ -291,6 +303,22 @@ public:
|
|||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline void operator=(LocalVector &&p_from) {
|
||||
if (unlikely(this == &p_from)) {
|
||||
return;
|
||||
}
|
||||
reset();
|
||||
|
||||
data = p_from.data;
|
||||
count = p_from.count;
|
||||
capacity = p_from.capacity;
|
||||
|
||||
p_from.data = nullptr;
|
||||
p_from.count = 0;
|
||||
p_from.capacity = 0;
|
||||
}
|
||||
|
||||
inline LocalVector &operator=(const Vector<T> &p_from) {
|
||||
resize(p_from.size());
|
||||
for (U i = 0; i < count; i++) {
|
||||
|
@ -298,6 +326,14 @@ public:
|
|||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline void operator=(Vector<T> &&p_from) {
|
||||
resize(p_from.size());
|
||||
for (U i = 0; i < count; i++) {
|
||||
data[i] = std::move(p_from[i]);
|
||||
}
|
||||
}
|
||||
|
||||
inline LocalVector &operator=(const PoolVector<T> &p_from) {
|
||||
resize(p_from.size());
|
||||
typename PoolVector<T>::Read r = p_from.read();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue