neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
list.hpp
Go to the documentation of this file.
1// list.hpp
2/*
3 * Copyright (c) 2007 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <neolib/neolib.hpp>
39#include <list>
43
44namespace neolib
45{
46 template <typename T>
47 class list : public reference_counted<i_list<abstract_t<T>>>
48 {
49 typedef list<T> self_type;
51 // types
52 public:
54 typedef T value_type;
56 typedef std::list<value_type> std_type;
57 using typename abstract_type::size_type;
58 using typename abstract_type::const_iterator;
59 using typename abstract_type::iterator;
61 protected:
64 protected:
67 // construction
68 public:
69 list() {}
70 list(const list& aOther) :
71 iList{ aOther.begin(), aOther.end() } {}
72 list(list&& aOther) :
73 iList{ std::move(aOther.iList) } {}
74 list(const i_list<T>& aOther) :
75 iList{ aOther.begin(), aOther.end() } {}
76 list& operator=(const list& aOther)
77 {
78 assign(aOther);
79 return *this;
80 }
81 list& operator=(list&& aOther)
82 {
83 iList = std::move(aOther.iList);
84 return *this;
85 }
86 list& operator=(const i_list<T>& aOther)
87 {
88 assign(aOther);
89 return *this;
90 }
91 // operations
92 public:
93 const std_type& as_std_list() const
94 {
95 return iList;
96 }
98 {
99 return iList;
100 }
102 {
103 return iList;
104 }
105 // comparison
106 public:
107 constexpr bool operator==(const self_type& that) const noexcept
108 {
109 return as_std_list() == that.as_std_list();
110 }
111 constexpr std::partial_ordering operator<=>(const self_type& that) const noexcept
112 {
113 return as_std_list() <=> that.as_std_list();
114 }
115 // implementation
116 public:
117 // from i_container
118 size_type size() const noexcept final
119 {
120 return iList.size();
121 }
122 size_type max_size() const noexcept final
123 {
124 return iList.max_size();
125 }
126 void clear() final
127 {
128 iList.clear();
129 }
130 void assign(const generic_container_type& aOther) final
131 {
132 if (&aOther == this)
133 return;
134 iList.assign(aOther.begin(), aOther.end());
135 }
136 private:
137 // from i_container
138 abstract_const_iterator* do_begin(void* memory) const final
139 {
140 return new(memory) container_const_iterator(iList.begin());
141 }
142 abstract_const_iterator* do_end(void* memory) const final
143 {
144 return new(memory) container_const_iterator(iList.end());
145 }
146 abstract_iterator* do_begin(void* memory) final
147 {
148 return new(memory) container_iterator(iList.begin());
149 }
150 abstract_iterator* do_end(void* memory) final
151 {
152 return new(memory) container_iterator(iList.end());
153 }
154 abstract_iterator* do_erase(void* memory, const abstract_const_iterator& aPosition) final
155 {
156 return new (memory) container_iterator(iList.erase(static_cast<const container_const_iterator&>(aPosition)));
157 }
158 abstract_iterator* do_erase(void* memory, const abstract_const_iterator& aFirst, const abstract_const_iterator& aLast) final
159 {
160 return new (memory) container_iterator(iList.erase(static_cast<const container_const_iterator&>(aFirst), static_cast<const container_const_iterator&>(aLast)));
161 }
162 public:
163 // from i_sequence_container
164 size_type capacity() const final
165 {
166 return iList.max_size();
167 }
168 void reserve(size_type aCapacity) final
169 {
170 /* do nothing */
171 }
172 void resize(size_type aSize) final
173 {
174 if constexpr (std::is_default_constructible_v<value_type>)
175 iList.resize(aSize);
176 else if (aSize <= size())
177 iList.erase(std::next(iList.begin(), aSize), iList.end());
178 else
179 throw std::logic_error{ "neolib::list::value_type not default constructible" };
180 }
181 void resize(size_type aSize, const abstract_value_type& aValue) final
182 {
183 iList.resize(aSize, aValue);
184 }
185 void push_back(const abstract_value_type& aValue) final
186 {
187 iList.push_back(aValue);
188 }
189 void pop_back() final
190 {
191 iList.pop_back();
192 }
193 const abstract_value_type& back() const final
194 {
195 return to_abstract(iList.back());
196 }
198 {
199 return to_abstract(iList.back());
200 }
201 private:
202 // from i_sequence_container
203 abstract_iterator* do_insert(void* memory, const abstract_const_iterator& aPosition, const abstract_value_type& aValue) final { return new (memory) container_iterator(iList.insert(static_cast<const container_const_iterator&>(aPosition), aValue)); }
204 public:
205 // from i_list
206 void push_front(const abstract_value_type& aValue) final { iList.push_front(aValue); }
207 void pop_front() final { iList.pop_front(); }
208 const abstract_value_type& front() const final { return to_abstract(iList.front()); }
209 abstract_value_type& front() final { return to_abstract(iList.front()); }
210 // attributes
211 private:
212 std::list<value_type> iList;
213 };
214}
IteratorType abstract_iterator
const_iterator begin() const
abstract_const_iterator::iterator_wrapper const_iterator
abstract_iterator::iterator_wrapper iterator
ConstIteratorType abstract_const_iterator
const_iterator end() const
i_container< T, ConstIteratorType, IteratorType > generic_container_type
base_type::size_type size_type
Definition i_list.hpp:50
ConstIteratorType abstract_const_iterator
std_type to_std_list() const
Definition list.hpp:101
void pop_front() final
Definition list.hpp:207
abstract_t< T > abstract_value_type
Definition list.hpp:55
list & operator=(const i_list< T > &aOther)
Definition list.hpp:86
void pop_back() final
Definition list.hpp:189
constexpr std::partial_ordering operator<=>(const self_type &that) const noexcept
Definition list.hpp:111
abstract_value_type & front() final
Definition list.hpp:209
i_list< abstract_t< T > > abstract_type
Definition list.hpp:53
void assign(const generic_container_type &aOther) final
Definition list.hpp:130
list(const list &aOther)
Definition list.hpp:70
abstract_value_type & back() final
Definition list.hpp:197
container::const_iterator< T, typename std_type::const_iterator > container_const_iterator
Definition list.hpp:65
constexpr bool operator==(const self_type &that) const noexcept
Definition list.hpp:107
list & operator=(const list &aOther)
Definition list.hpp:76
void push_front(const abstract_value_type &aValue) final
Definition list.hpp:206
std::list< value_type > std_type
Definition list.hpp:56
void resize(size_type aSize) final
Definition list.hpp:172
void reserve(size_type aCapacity) final
Definition list.hpp:168
void resize(size_type aSize, const abstract_value_type &aValue) final
Definition list.hpp:181
size_type capacity() const final
Definition list.hpp:164
size_type size() const noexcept final
Definition list.hpp:118
list(const i_list< T > &aOther)
Definition list.hpp:74
std_type & as_std_list()
Definition list.hpp:97
size_type max_size() const noexcept final
Definition list.hpp:122
const abstract_value_type & back() const final
Definition list.hpp:193
const std_type & as_std_list() const
Definition list.hpp:93
void push_back(const abstract_value_type &aValue) final
Definition list.hpp:185
list(list &&aOther)
Definition list.hpp:72
void clear() final
Definition list.hpp:126
const abstract_value_type & front() const final
Definition list.hpp:208
list & operator=(list &&aOther)
Definition list.hpp:81
container::iterator< T, typename std_type::iterator, typename std_type::const_iterator > container_iterator
Definition list.hpp:66
typename detail::abstract_type< T >::type abstract_t
Definition neolib.hpp:178
const abstract_t< T > & to_abstract(const T &aArgument)
Definition neolib.hpp:181
Definition plf_hive.h:79
it_type next(it_type it, const typename iterator_traits< it_type >::difference_type distance=1)
Definition plf_hive.h:89