From 84dfb17f7f2c13ed9f9da765339f8b4ac01a963e Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 20 Oct 2020 19:11:21 -0500 Subject: [PATCH] [ox/std] Remove inappropriate noexcepts and call destructors on erase and resize --- deps/ox/src/ox/std/vector.hpp | 101 ++++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 40 deletions(-) diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index 7d7169f9..d8d6159e 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2018 gtalent2@gmail.com + * Copyright 2015 - 2020 gary@drinkingtea.net * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -25,17 +25,17 @@ class Vector { public: Vector() noexcept = default; - explicit Vector(std::size_t size) noexcept; + explicit Vector(std::size_t size); - Vector(const Vector &other) noexcept; + Vector(const Vector &other); - Vector(Vector &&other) noexcept; + Vector(Vector &&other); - ~Vector() noexcept; + ~Vector(); - Vector &operator=(const Vector &other) noexcept; + Vector &operator=(const Vector &other); - Vector &operator=(Vector &&other) noexcept; + Vector &operator=(Vector &&other); T &operator[](std::size_t i) noexcept; @@ -51,43 +51,47 @@ class Vector { std::size_t size() const noexcept; - void resize(std::size_t size) noexcept; + [[nodiscard]] bool empty() const; - T *data(); + void clear(); - const T *data() const; + void resize(std::size_t size); - bool contains(T) const noexcept; + T *data() noexcept; - void insert(std::size_t pos, const T &val) noexcept; + const T *data() const noexcept; + + [[nodiscard]] bool contains(T) const; + + void insert(std::size_t pos, const T &val); template - void emplace_back(Args&&... args) noexcept; + void emplace_back(Args&&... args); - void push_back(const T &item) noexcept; + void push_back(const T &item); - void pop_back() noexcept; + void pop_back(); /** * Removes an item from the Vector. * @param pos position of item to remove */ - void erase(std::size_t pos) noexcept; + void erase(std::size_t pos); /** * Moves the last item in the Vector to position pos and decrements the * size by 1. * @param pos position of item to remove */ - void unordered_erase(std::size_t pos) noexcept; + void unordered_erase(std::size_t pos); private: - void expandCap(std::size_t cap) noexcept; + void expandCap(std::size_t cap); }; template -Vector::Vector(std::size_t size) noexcept { +Vector::Vector(std::size_t size) { m_size = size; m_cap = m_size; m_items = reinterpret_cast(new AllocAlias[m_cap]); @@ -97,7 +101,7 @@ Vector::Vector(std::size_t size) noexcept { } template -Vector::Vector(const Vector &other) noexcept { +Vector::Vector(const Vector &other) { m_size = other.m_size; m_cap = other.m_cap; m_items = reinterpret_cast(new AllocAlias[m_cap]); @@ -107,7 +111,7 @@ Vector::Vector(const Vector &other) noexcept { } template -Vector::Vector(Vector &&other) noexcept { +Vector::Vector(Vector &&other) { m_size = other.m_size; m_cap = other.m_cap; m_items = other.m_items; @@ -117,7 +121,7 @@ Vector::Vector(Vector &&other) noexcept { } template -Vector::~Vector() noexcept { +Vector::~Vector() { if constexpr(ox::is_class()) { for (std::size_t i = 0; i < m_size; i++) { m_items[i].~T(); @@ -128,7 +132,7 @@ Vector::~Vector() noexcept { } template -Vector &Vector::operator=(const Vector &other) noexcept { +Vector &Vector::operator=(const Vector &other) { this->~Vector(); m_size = other.m_size; m_cap = other.m_cap; @@ -140,7 +144,7 @@ Vector &Vector::operator=(const Vector &other) noexcept { } template -Vector &Vector::operator=(Vector &&other) noexcept { +Vector &Vector::operator=(Vector &&other) { this->~Vector(); m_size = other.m_size; m_cap = other.m_cap; @@ -187,28 +191,44 @@ std::size_t Vector::size() const noexcept { } template -void Vector::resize(std::size_t size) noexcept { +bool Vector::empty() const { + return !m_size; +} + +template +void Vector::clear() { + resize(0); +} + +template +void Vector::resize(std::size_t size) { if (m_cap < size) { expandCap(size); } - for (std::size_t i = m_size; i < size; i++) { - m_items[i] = {}; + if (m_size < size) { + for (std::size_t i = m_size; i < size; i++) { + m_items[i] = {}; + } + } else { + for (std::size_t i = size; i < m_size; i++) { + m_items[i].~T(); + } } m_size = size; } template -T *Vector::data() { +T *Vector::data() noexcept { return m_items; } template -const T *Vector::data() const { +const T *Vector::data() const noexcept { return m_items; } template -bool Vector::contains(T v) const noexcept { +bool Vector::contains(T v) const { for (std::size_t i = 0; i < m_size; i++) { if (m_items[i] == v) { return true; @@ -218,13 +238,13 @@ bool Vector::contains(T v) const noexcept { } template -void Vector::insert(std::size_t pos, const T &val) noexcept { +void Vector::insert(std::size_t pos, const T &val) { // TODO: insert should ideally have its own expandCap if (m_size == m_cap) { expandCap(m_cap ? m_cap * 2 : 100); } for (auto i = m_size; i > pos; --i) { - m_items[i] = m_items[i - 1]; + m_items[i] = ox::move(m_items[i - 1]); } m_items[pos] = val; ++m_size; @@ -232,7 +252,7 @@ void Vector::insert(std::size_t pos, const T &val) noexcept { template template -void Vector::emplace_back(Args&&... args) noexcept { +void Vector::emplace_back(Args&&... args) { if (m_size == m_cap) { expandCap(m_cap ? m_cap * 2 : 100); } @@ -241,7 +261,7 @@ void Vector::emplace_back(Args&&... args) noexcept { } template -void Vector::push_back(const T &item) noexcept { +void Vector::push_back(const T &item) { if (m_size == m_cap) { expandCap(m_cap ? m_cap * 2 : 100); } @@ -250,26 +270,27 @@ void Vector::push_back(const T &item) noexcept { } template -void Vector::pop_back() noexcept { +void Vector::pop_back() { --m_size; + m_items[m_size].~T(); } template -void Vector::erase(std::size_t pos) noexcept { +void Vector::erase(std::size_t pos) { m_size--; for (auto i = pos; i < m_size; i++) { - m_items[i] = m_items[i + 1]; + m_items[i] = ox::move(m_items[i + 1]); } } template -void Vector::unordered_erase(std::size_t pos) noexcept { +void Vector::unordered_erase(std::size_t pos) { m_size--; - m_items[pos] = m_items[m_size]; + m_items[pos] = ox::move(m_items[m_size]); } template -void Vector::expandCap(std::size_t cap) noexcept { +void Vector::expandCap(std::size_t cap) { auto oldItems = m_items; m_cap = cap; m_items = reinterpret_cast(new AllocAlias[m_cap]);