diff --git a/include/const_list.h b/include/const_list.h index 5fc2331..4b3d736 100644 --- a/include/const_list.h +++ b/include/const_list.h @@ -119,11 +119,11 @@ namespace cc { constexpr void assign(std::initializer_list> init) noexcept; - [[nodiscard]] constexpr reference front() noexcept{ return dynamic_cast(_tail._next); } - [[nodiscard]] constexpr const_reference front() const noexcept { return dynamic_cast(_tail._next); } + [[nodiscard]] constexpr reference front() noexcept{ return *dynamic_cast(_tail._next); } + [[nodiscard]] constexpr const_reference front() const noexcept { return *dynamic_cast(_tail._next); } - [[nodiscard]] constexpr reference back() noexcept { return dynamic_cast(_tail._prev); } - [[nodiscard]] constexpr const_reference back() const noexcept { return dynamic_cast(_tail._prev); } + [[nodiscard]] constexpr reference back() noexcept { return *dynamic_cast(_tail._prev); } + [[nodiscard]] constexpr const_reference back() const noexcept { return *dynamic_cast(_tail._prev); } [[nodiscard]] constexpr iterator begin() noexcept { return iterator(_tail._next); }; [[nodiscard]] constexpr const_iterator begin() const noexcept { return const_iterator(_tail._next); }; @@ -226,6 +226,9 @@ namespace cc { constexpr const_list_node(const_list_node&& other) noexcept; virtual constexpr ~const_list_node(); + constexpr const_list_node& operator=(const const_list_node& other) noexcept; + constexpr const_list_node& operator=(const_list_node&& other) noexcept; + protected: constexpr void on_delete(void (*cb)()) noexcept; @@ -274,14 +277,15 @@ namespace cc { template constexpr const_list::const_list(const_list &&other) noexcept - : _tail(std::move(other._tail)), - _size(other._size) + : _size(other._size) { asserts(); - for (auto node : other) { + for (auto& node : other) { node._owner = this; } + + _tail = std::move(other._tail); } template @@ -327,6 +331,7 @@ namespace cc { for (auto& value : init) { _tail.push_before(std::addressof(value.get())); + value.get()._owner = this; } _size += init.size(); } @@ -380,6 +385,10 @@ namespace cc { { _prev->_next = this; _next->_prev = this; + + // without this the destructor of other would delete moved nodes + other._next = std::addressof(other); + other._prev = std::addressof(other); } template @@ -390,6 +399,29 @@ namespace cc { unlink(); } + template + constexpr const_list_node &const_list_node::operator=(const const_list_node &other) noexcept + { + return *this; + } + + template + constexpr const_list_node &const_list_node::operator=(const_list_node &&other) noexcept + { + _prev = other._prev; + _next = other._next; + _delete_cb = other._delete_cb; + + _prev->_next = this; + _next->_prev = this; + + // without this the destructor of other would delete moved nodes + other._next = std::addressof(other); + other._prev = std::addressof(other); + + return *this; + } + template constexpr void const_list_node::on_delete(void (*cb)()) noexcept {