Compare commits

...

2 Commits

1 changed files with 56 additions and 8 deletions

View File

@ -6,6 +6,7 @@
#define UDIFF_CONST_LIST_H_
#include <concepts>
#include <functional>
#include <initializer_list>
#include <iterator>
@ -104,11 +105,8 @@ namespace cc {
constexpr const_list& operator=(const const_list&) = delete;
constexpr const_list& operator=(const_list&& other) noexcept;
constexpr const_list& operator=(std::initializer_list<std::reference_wrapper<value_type>> init) noexcept;
constexpr void assign(size_type count, const value_type& value) noexcept;
template<std::input_iterator InputIt>
constexpr void assign(InputIt first, InputIt last);
constexpr void assign(std::initializer_list<value_type> values);
constexpr void assign(std::initializer_list<std::reference_wrapper<value_type>> init) noexcept;
[[nodiscard]] constexpr reference front() noexcept{ return *_start; }
[[nodiscard]] constexpr const_reference front() const noexcept { return *_start; }
@ -212,11 +210,19 @@ namespace cc {
const_list_node *_prev = nullptr;
const_list_node *_next = nullptr;
const_list<D> *_owner = nullptr;
void (*_delete_cb)() = nullptr;
public:
public:
constexpr const_list_node() noexcept { asserts(); }
virtual constexpr ~const_list_node();
protected:
constexpr void on_delete(void (*cb)());
private:
void _remove();
friend class const_list<D>;
friend class _const_list_iterator_base<D>;
};
@ -312,17 +318,59 @@ namespace cc {
template<typename Node>
constexpr const_list<Node> &const_list<Node>::operator=(std::initializer_list<std::reference_wrapper<value_type>> init) noexcept
{
*this = const_list(init);
assign(init);
return *this;
}
template<typename Node>
constexpr void const_list<Node>::assign(std::initializer_list<std::reference_wrapper<value_type>> init) noexcept
{
auto prev = init.begin();
prev->get()._owner = this;
for (auto it = std::next(init.begin()); it != init.end(); ++it) {
prev->get()._next = std::addressof(it->get());
it->get()._prev = std::addressof(prev->get());
it->get()._owner = this;
prev = it;
}
}
template<typename Node>
void const_list<Node>::clear() noexcept
{
for (auto node : *this) {
node._remove();
}
}
template<typename D>
constexpr const_list_node<D>::~const_list_node()
{
if (_delete_cb)
(*_delete_cb)();
_owner->remove(*this);
}
template<typename D>
constexpr void const_list_node<D>::on_delete(void (*cb)())
{
_delete_cb = cb;
}
template<typename D>
void const_list_node<D>::_remove()
{
if (_delete_cb)
(*_delete_cb)();
_prev = nullptr;
_next = nullptr;
_owner = nullptr;
}
} // cc
#endif //UDIFF_CONST_LIST_H_