ActiveLib
Loading...
Searching...
No Matches
List.h
1
6#ifndef ACTIVE_CONTAINER_LIST
7#define ACTIVE_CONTAINER_LIST
8
9#include "Active/Utility/Cloner.h"
10#include "Active/Utility/Mover.h"
11
12#include <list>
13#include <memory>
14
15namespace active::container {
16
26 template<class T> requires utility::Clonable<T>
27 class List : public std::list<std::unique_ptr<T>> {
28 public:
29
30 // MARK: Types
31
33 using value_t = std::unique_ptr<T>;
35 using base = std::list<value_t>;
37 using size_type = typename base::size_type;
39 using iterator = typename base::iterator;
41 using const_iterator = typename base::const_iterator;
42
43 // MARK: Constructors
44
48 List() : base() {}
53 template<class Derived>
54 explicit List(const std::initializer_list<Derived>& items) : base() {
55 for (const auto& item : items)
56 emplace_back(item);
57 }
62 List(const List& source) : base () { cloneFrom(source); }
67 List(List&& source) : base(std::move(source)) {}
71 virtual ~List() = default;
72
73 // MARK: Operators
74
80 auto operator= (const List& source) {
81 if (this != &source) {
82 base::clear();
83 cloneFrom(source);
84 }
85 return *this;
86 }
92 auto operator= (List&& source) {
93 if (this != &source)
94 base::operator=(std::move(source));
95 return *this;
96 }
97
98 // MARK: Functions (mutating)
99
104 void push_back(T* item) { base::push_back(value_t(item)); }
109 void push_back(T& item) { base::push_back(clone(item)); }
114 void emplace_back(T&& item) {
115 if constexpr(utility::Movable<T>)
116 base::emplace_back(cloneMove(std::move(item)));
117 else
118 base::emplace_back(clone(item));
119 }
124 void emplace_back(const T& item) { base::emplace_back(clone(item)); }
129 void emplace_back(value_t&& item) { base::emplace_back(std::move(item)); }
134 void emplace_back(value_t& item) { base::emplace_back(std::move(item)); }
139 void push_front(T* item) { base::push_front(value_t(item)); }
144 void emplace_front(T&& item) {
145 if constexpr(utility::Movable<T>)
146 base::emplace_front(cloneMove(std::move(item)));
147 else
148 base::emplace_front(clone(item));
149 }
154 void emplace_front(value_t&& item) { base::emplace_front(std::move(item)); }
160 auto release(iterator& pos) {
161 auto item = std::move(*pos);
162 pos = this->erase(pos);
163 return item;
164 }
165
166 private:
171 void cloneFrom(const List& source) {
172 for (const auto& item : source)
173 base::push_back(item ? clone(*item) : value_t());
174 }
175 };
176
177}
178
179#endif //ACTIVE_CONTAINER_LIST
Definition List.h:27
std::unique_ptr< T > value_t
Stored type.
Definition List.h:33
typename base::const_iterator const_iterator
Container const iterator type.
Definition List.h:41
List(const List &source)
Definition List.h:62
List(const std::initializer_list< Derived > &items)
Definition List.h:54
std::list< value_t > base
Base container type.
Definition List.h:35
void emplace_back(value_t &&item)
Definition List.h:129
List(List &&source)
Definition List.h:67
typename base::size_type size_type
Container size (index) type.
Definition List.h:37
auto release(iterator &pos)
Definition List.h:160
List()
Definition List.h:48
auto operator=(const List &source)
Definition List.h:80
typename base::iterator iterator
Container iterator type.
Definition List.h:39
virtual ~List()=default
void emplace_front(value_t &&item)
Definition List.h:154
void emplace_back(const T &item)
Definition List.h:124
void emplace_back(value_t &item)
Definition List.h:134
void emplace_front(T &&item)
Definition List.h:144
void emplace_back(T &&item)
Definition List.h:114
void push_front(T *item)
Definition List.h:139
void push_back(T *item)
Definition List.h:104
void push_back(T &item)
Definition List.h:109
Movable concept for classes/functions dependent on cloning with a move.
Definition Mover.h:33
Definition HashMap.h:14