From 3453681c4830c84b1b426b65ad534dadf0362398 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Sat, 10 Apr 2021 14:13:59 +0200 Subject: [PATCH] Fixed a bad vector handling --- include/gp/containers/vector.hpp | 54 +++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/include/gp/containers/vector.hpp b/include/gp/containers/vector.hpp index 510d830..7713a97 100644 --- a/include/gp/containers/vector.hpp +++ b/include/gp/containers/vector.hpp @@ -24,11 +24,21 @@ namespace gp{ using associated_riterator = pointer_iterator; using associated_const_riterator = const_pointer_iterator; + /** + * @brief Construct a new vector object from an allocator + * + * @param v the allocator + */ vector(allocator& v) : ary() , alloc(v) {} + /** + * @brief Construct a new vector object from another vector, copying it + * + * @param oth the other vector + */ vector(vector& oth) : alloc(oth.alloc) { @@ -46,6 +56,11 @@ namespace gp{ } } + /** + * @brief Construct a new vector object by moving objects out of another vector + * + * @param oth + */ vector(vector&& oth) : ary(oth.ary) , sz(oth.sz) @@ -55,6 +70,12 @@ namespace gp{ oth.ary = nullptr; } + /** + * @brief Copy assignment + * + * @param oth + * @return vector& the reference to this changed vector + */ vector& operator=(vector& oth) { gp_config::assertion(reserve(oth.size()), "could not reserve space on assign"); @@ -75,12 +96,37 @@ namespace gp{ return *this; } + /** + * @brief Move assignment + * + * Will not change the allocator of the local vector, is EXPENSIVE if the both vectors have different allocators + * + * @param oth + * @return vector& + */ vector& operator=(vector&& oth) { - gp::swap(ary, oth.ary); - gp::swap(alloc, oth.alloc); - gp::swap(sz, oth.sz); - gp::swap(cap, oth.cap); + if(&alloc.get() == &oth.alloc.get()) + { + gp::swap(ary, oth.ary); + gp::swap(sz, oth.sz); + gp::swap(cap, oth.cap); + } else { + for(auto& elem : *this) { + elem->~T(); + } + sz = 0; + if(capacity()