From 686452fb1bcf9454df8d816443925746f6645ca0 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Sat, 17 Apr 2021 19:01:02 +0200 Subject: BLI: add Vector.append_as method This method is similar to `std::vector::emblace_back` in that it constructs the new object inplace in the vector, removing the need for a move. The `_as` suffix is consistent with similar behavior in Map and Set data structures. --- source/blender/blenlib/BLI_vector.hh | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'source/blender/blenlib/BLI_vector.hh') diff --git a/source/blender/blenlib/BLI_vector.hh b/source/blender/blenlib/BLI_vector.hh index 328d623787b..b7ff488ff0f 100644 --- a/source/blender/blenlib/BLI_vector.hh +++ b/source/blender/blenlib/BLI_vector.hh @@ -437,13 +437,17 @@ class Vector { */ void append(const T &value) { - this->ensure_space_for_one(); - this->append_unchecked(value); + this->append_as(value); } void append(T &&value) + { + this->append_as(std::move(value)); + } + /* This is similar to std::vector::emblace_back. */ + template void append_as(ForwardValue &&... value) { this->ensure_space_for_one(); - this->append_unchecked(std::move(value)); + this->append_unchecked_as(std::forward(value)...); } /** @@ -474,10 +478,18 @@ class Vector { * behavior when not enough capacity has been reserved beforehand. Only use this in performance * critical code. */ - template void append_unchecked(ForwardT &&value) + void append_unchecked(const T &value) + { + this->append_unchecked_as(value); + } + void append_unchecked(T &&value) + { + this->append_unchecked_as(std::move(value)); + } + template void append_unchecked_as(ForwardT &&... value) { BLI_assert(end_ < capacity_end_); - new (end_) T(std::forward(value)); + new (end_) T(std::forward(value)...); end_++; UPDATE_VECTOR_SIZE(this); } -- cgit v1.2.3