xtd 0.2.0
arranged_element_collection.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include "sorter_none.hpp"
7#include <xtd/array>
8#include <xtd/argument_out_of_range_exception>
9#include <xtd/event_args>
10#include <xtd/event_handler>
11#include <xtd/event>
12#include <xtd/object>
13#include <xtd/size_object>
14#include <ostream>
15#include <vector>
16
18namespace xtd {
20 namespace forms {
22 namespace layout {
34 template<class type_t, class sorter_t = sorter_none>
36 public:
38 class value_type : public type_t {
39 public:
41 value_type() = default;
42 value_type(const value_type&) = default;
43 value_type(value_type&&) = default;
44 template<class ...args_t>
45 value_type(args_t&& ...args) : type_t(args...) {}
46 value_type& operator =(const value_type& value) {
47 if (value.owner) owner = value.owner;
48 if (owner != nullptr && !owner->inserting_ && !owner->erasing_) owner->on_item_updated(pos, static_cast<type_t&>(const_cast<value_type&>(value)));
49 type_t::operator =(value);
50 return *this;
51 }
52 value_type& operator =(value_type&& value) {
53 if (value.owner) owner = value.owner;
54 if (owner != nullptr && !owner->inserting_ && !owner->erasing_) owner->on_item_updated(pos, static_cast<type_t&>(value));
55 type_t::operator =(value);
56 return *this;
57 }
58 operator type_t() {return *this;}
59 friend std::ostream& operator <<(std::ostream& os, const value_type& value) {return os << static_cast<const type_t&>(value);}
61
62 private:
63 friend class arranged_element_collection;
64 size_t pos = size_object::max_value;
65 arranged_element_collection* owner = nullptr;
66 };
67
69
72 using allocator_type = std::allocator<value_type>;
74 using size_type = std::size_t;
76 using difference_type = std::ptrdiff_t;
82 using pointer = typename std::allocator_traits<allocator_type>::pointer;
84 using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
86 using iterator = typename std::vector<value_type>::iterator;
88 using const_iterator = typename std::vector<value_type>::const_iterator;
90 using reverse_iterator = typename std::vector<value_type>::reverse_iterator;
92 using const_reverse_iterator = typename std::vector<value_type>::const_reverse_iterator;
94
96
99 inline static constexpr xtd::size npos = xtd::size_object::max_value;
101
103
108 explicit arranged_element_collection(const allocator_type& allocator = allocator_type()) : collection_(allocator) {}
111 arranged_element_collection(const std::initializer_list<type_t>& il) {
112 for (auto item : il)
113 push_back(item);
114 }
116
118 explicit arranged_element_collection(const std::vector<type_t>& collection) {
119 for (auto item : collection)
120 push_back(item);
121 }
122 arranged_element_collection(const arranged_element_collection& collection) {push_back_range(collection);}
123 arranged_element_collection& operator =(const arranged_element_collection& collection) {
124 clear();
125 push_back_range(collection);
126 return *this;
127 }
128 arranged_element_collection(arranged_element_collection&&) = default;
129 bool operator ==(const arranged_element_collection& value) const {return collection_ == value.collection_;}
130 bool operator !=(const arranged_element_collection& value) const {return !operator ==(value);}
132
134
138 allocator_type get_allocator() const noexcept {return collection_.get_allocator();}
139
144 collection_[pos].pos = pos;
145 collection_[pos].owner = this;
146 return collection_.at(pos);
147 }
151 const_reference at(size_type pos) const {return collection_.at(pos);}
152
155 reference front() {return collection_.front();}
158 const_reference front() const {return collection_.front();}
159
162 reference back() {return collection_.back();}
165 const_reference back() const {return collection_.back();}
166
169 pointer data() {return collection_.data();}
172 const_pointer data() const {return collection_.data();}
173
176 iterator begin() noexcept {return collection_.begin();}
179 const_iterator begin() const noexcept {return collection_.begin();}
182 const_iterator cbegin() const noexcept {return collection_.cbegin();}
183
186 iterator end() noexcept {return collection_.end();}
189 const_iterator end() const noexcept {return collection_.end();}
192 const_iterator cend() const noexcept {return collection_.cend();}
193
196 reverse_iterator rbegin() noexcept {return collection_.rbegin();}
199 const_reverse_iterator rbegin() const noexcept {return collection_.rbegin();}
202 const_reverse_iterator crbegin() const noexcept {return collection_.crbegin();}
203
206 reverse_iterator rend() noexcept {return collection_.rend();}
209 const_reverse_iterator rend() const noexcept {return collection_.rend();}
212 const_reverse_iterator crend() const noexcept {return collection_.crend();}
213
216 bool empty() const noexcept {return collection_.empty();}
217
220 size_type size() const noexcept {return collection_.size();}
221
224 size_type max_size() const noexcept {return collection_.max_size();}
225
227 void reserve(size_type size) {collection_.reserve(size);}
228
231 size_type capacity() const noexcept {return collection_.capacity();}
232
234 void shrink_to_fit() {collection_.shrink_to_fit();}
235
238 virtual bool sorted() const noexcept {return sorted_;}
241 virtual void sorted(bool value) {
242 if (sorted_ != value) {
243 sorted_ = value;
244 if (sorted_) sort();
245 }
246 }
247
249 virtual void clear() noexcept {
250 iterator it = begin();
251 while (it != end())
252 it = erase(it);
253 }
254
258 virtual iterator insert(const_iterator pos, const value_type& value) {
259 size_t index = pos - begin();
260 inserting_ = true;
261 iterator result = collection_.insert(pos, value);
262 inserting_ = false;
263 (*this)[index].owner = this;
264 (*this)[index].pos = index;
265 on_item_added(index, collection_[index]);
266 if (sorted_) sort();
267 return result;
268 }
272 virtual iterator insert(const_iterator pos, value_type&& value) {
273 size_t index = pos - begin();
274 inserting_ = true;
275 iterator result = collection_.insert(pos, value);
276 inserting_ = false;
277 (*this)[index].owner = this;
278 (*this)[index].pos = index;
279 on_item_added(index, collection_[index]);
280 if (sorted_) sort();
281 return result;
282 }
283
287 virtual void insert_at(size_t index, const value_type& value) {
289 insert(begin() + index, value);
290 }
291
295 template<class ...args_t>
296 void emplace(const_iterator pos, args_t&& ... args) {
297 size_t index = pos - begin();
298 inserting_ = true;
299 iterator result = collection_.insert(pos, args...);
300 inserting_ = false;
301 (*this)[index].owner = this;
302 (*this)[index].pos = index;
303 on_item_added(index, collection_[index]);
304 if (sorted_) sort();
305 return result;
306 }
307
310 template<class ...args_t>
311 void emplace_back(args_t&& ... args) {
312 collection_.push_back(args...);
313 size_t index = collection_.size() - 1;
314 (*this)[index].owner = this;
315 (*this)[index].pos = index;
316 on_item_added(index, collection_[index]);
317 if (sorted_) sort();
318 }
319
322 virtual iterator erase(iterator pos) {
323 on_item_removed(pos - begin(), *pos);
324 erasing_ = true;
325 iterator result = collection_.erase(pos);
326 erasing_ = false;
327 return result;
328 }
332 on_item_removed(pos - begin(), *reinterpret_cast<iterator&>(pos));
333 erasing_ = true;
334 iterator result = collection_.erase(pos);
335 erasing_ = false;
336 return result;
337 }
338
342 virtual iterator erase(iterator first, iterator last) {
343 iterator result = end();
344 for (iterator it = first; it <= last; ++it)
345 result = erase(it);
346 return result;
347 }
352 iterator result = end();
353 for (const_iterator it = first; it <= last; ++it)
354 result = erase(it);
355 return result;
356 }
357
360 virtual void erase_at(size_t index) {
362 erase(begin() + index);
363 }
364
366 virtual void pop_back() {
367 if (size() != 0) erase_at(size() - 1);
368 }
369
372 virtual void push_back(const value_type& item) {
373 collection_.push_back(item);
374 size_t index = collection_.size() - 1;
375 (*this)[index].owner = this;
376 (*this)[index].pos = index;
377 on_item_added(index, collection_[index]);
378 if (sorted_) sort();
379 }
382 virtual void push_back(value_type&& item) {
383 collection_.push_back(item);
384 size_t index = collection_.size() - 1;
385 (*this)[index].owner = this;
386 (*this)[index].pos = index;
387 on_item_added(index, collection_[index]);
388 if (sorted_) sort();
389 }
390
393 virtual void push_back_range(const arranged_element_collection& collection) {
394 for (value_type item : collection)
395 push_back(item);
396 }
399 virtual void push_back_range(const std::vector<value_type>& collection) {
400 for (value_type item : collection)
401 push_back(item);
402 }
405 virtual void push_back_range(const std::initializer_list<value_type>& collection) {
406 for (value_type item : collection)
407 push_back(item);
408 }
411 template<class collection_t>
412 void push_back_range(collection_t&& collection) {
413 for (auto& item : collection)
414 push_back(value_type(item));
415 }
418 template<class iterator_t>
419 void push_back_range(iterator_t begin, iterator_t end) {
420 for (auto it = begin; it != end; ++it)
421 push_back(value_type(*it));
422 }
423
425 virtual void sort() {
426 sorter_t sorter;
427 sorter(begin(), end());
428 }
429
432 xtd::array<type_t> to_array() const noexcept {
433 return collection_.size() ? xtd::array<type_t>(collection_.data(), collection_.size()) : xtd::array<type_t> {};
434 }
435
438 std::vector<type_t> to_vector() const noexcept {return to_array();}
440
442
447 collection_[pos].pos = pos;
448 collection_[pos].owner = this;
449 return collection_[pos];
450 }
454 collection_[pos].pos = pos;
455 collection_[pos].owner = const_cast<arranged_element_collection*>(this);
456 return collection_[pos];
457 }
459
461
465 event<arranged_element_collection, delegate<void(size_t, type_t& item)>> item_added;
466
469 event<arranged_element_collection, delegate<void(size_t, type_t& item)>> item_updated;
470
473 event<arranged_element_collection, delegate<void(size_t, type_t& item)>> item_removed;
475
477
482 virtual void on_item_added(size_t index, type_t& item) {
483 item_added(index, item);
484 }
485
489 virtual void on_item_updated(size_t index, type_t& item) {
490 item_updated(index, item);
491 }
492
496 virtual void on_item_removed(size_t index, type_t& item) {
497 item_removed(index, item);
498 }
500
501 private:
502 mutable std::vector<value_type, allocator_type> collection_;
503 bool inserting_ = false;
504 bool erasing_ = false;
505 bool sorted_ = false;
506 };
507 }
508 }
509}
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:59
static constexpr type_t max_value
Represents the largest possible value of type_t. This field is constant.
Definition box_integer.hpp:67
Represents the value type of the collection.
Definition arranged_element_collection.hpp:38
Represents a collection of objects.
Definition arranged_element_collection.hpp:35
virtual void erase_at(size_t index)
Erases element at specified index.
Definition arranged_element_collection.hpp:360
virtual iterator insert(const_iterator pos, value_type &&value)
Inserts specified element at specified position.
Definition arranged_element_collection.hpp:272
virtual void on_item_added(size_t index, type_t &item)
Raises the xtd::forms::layout::arranged_element_collection::item_added event.
Definition arranged_element_collection.hpp:482
arranged_element_collection(const std::initializer_list< type_t > &il)
Creates a new object xtd::diagnostics::trace_listener_collection with specified initializer list.
Definition arranged_element_collection.hpp:111
iterator begin() noexcept
Returns an iterator to the beginning.
Definition arranged_element_collection.hpp:176
iterator end() noexcept
Returns an iterator to the end.
Definition arranged_element_collection.hpp:186
void emplace_back(args_t &&... args)
Adds an element to the end.
Definition arranged_element_collection.hpp:311
static constexpr xtd::size npos
This is a special value equal to the maximum value representable by the type size_t.
Definition arranged_element_collection.hpp:99
typename std::vector< value_type >::reverse_iterator reverse_iterator
Represents the reverse iterator type of the collection.
Definition arranged_element_collection.hpp:90
const_reference front() const
Access the first element.
Definition arranged_element_collection.hpp:158
size_type capacity() const noexcept
Returns the number of elements that can be held in currently allocated storage.
Definition arranged_element_collection.hpp:231
virtual void pop_back()
Removes the last element of the container.
Definition arranged_element_collection.hpp:366
virtual void on_item_removed(size_t index, type_t &item)
Raises the xtd::forms::layout::arranged_element_collection::item_removed event.
Definition arranged_element_collection.hpp:496
virtual void sorted(bool value)
Sets the container is sorted.
Definition arranged_element_collection.hpp:241
event< arranged_element_collection, delegate< void(size_t, type_t &item)> > item_updated
Occurs when an item is updated in the collection.
Definition arranged_element_collection.hpp:469
const_reverse_iterator rend() const noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.hpp:209
const_iterator end() const noexcept
Returns an iterator to the end.
Definition arranged_element_collection.hpp:189
std::size_t size_type
Represents the size type of the collection.
Definition arranged_element_collection.hpp:74
typename std::vector< value_type >::iterator iterator
Represents the iterator type of the collection.
Definition arranged_element_collection.hpp:86
std::allocator< value_type > allocator_type
Represents the allocator type of the collection.
Definition arranged_element_collection.hpp:72
virtual iterator insert(const_iterator pos, const value_type &value)
Inserts specified element at specified position.
Definition arranged_element_collection.hpp:258
virtual iterator erase(const_iterator pos)
Erases element at specified position.
Definition arranged_element_collection.hpp:331
void push_back_range(collection_t &&collection)
Adds elements to the end.
Definition arranged_element_collection.hpp:412
virtual void sort()
Sorts the content.
Definition arranged_element_collection.hpp:425
typename std::allocator_traits< allocator_type >::const_pointer const_pointer
Represents the value type const pointer of the collection.
Definition arranged_element_collection.hpp:84
const_reverse_iterator crend() const noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.hpp:212
virtual void insert_at(size_t index, const value_type &value)
Inserts specified element at specified index.
Definition arranged_element_collection.hpp:287
reference operator[](size_type pos)
Access specified element.
Definition arranged_element_collection.hpp:446
virtual void push_back_range(const std::vector< value_type > &collection)
Adds elements to the end.
Definition arranged_element_collection.hpp:399
allocator_type get_allocator() const noexcept
Returns the associated allocator.
Definition arranged_element_collection.hpp:138
reference back()
Access the last element.
Definition arranged_element_collection.hpp:162
pointer data()
Direct access to the underlying array.
Definition arranged_element_collection.hpp:169
void shrink_to_fit()
Reduces memory usage by freeing unused memory.
Definition arranged_element_collection.hpp:234
const_iterator begin() const noexcept
Returns an iterator to the beginning.
Definition arranged_element_collection.hpp:179
virtual iterator erase(iterator pos)
Erases element at specified position.
Definition arranged_element_collection.hpp:322
virtual void push_back(const value_type &item)
Adds an element to the end.
Definition arranged_element_collection.hpp:372
xtd::array< type_t > to_array() const noexcept
Gets an array with the elements of the container.
Definition arranged_element_collection.hpp:432
reference front()
Access the first element.
Definition arranged_element_collection.hpp:155
virtual void push_back_range(const arranged_element_collection &collection)
Adds elements to the end.
Definition arranged_element_collection.hpp:393
typename std::vector< value_type >::const_iterator const_iterator
Represents the const iterator type of the collection.
Definition arranged_element_collection.hpp:88
reverse_iterator rbegin() noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.hpp:196
std::vector< type_t > to_vector() const noexcept
Gets an array with the elements of the container.
Definition arranged_element_collection.hpp:438
const_reverse_iterator crbegin() const noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.hpp:202
const_reference back() const
Access the last element.
Definition arranged_element_collection.hpp:165
const_reference at(size_type pos) const
Access specified element with bounds checking.
Definition arranged_element_collection.hpp:151
virtual void push_back(value_type &&item)
Adds an element to the end.
Definition arranged_element_collection.hpp:382
virtual iterator erase(const_iterator first, const_iterator last)
Erases elements at specified range.
Definition arranged_element_collection.hpp:351
void emplace(const_iterator pos, args_t &&... args)
Inserts specified element at specified position.
Definition arranged_element_collection.hpp:296
const_iterator cbegin() const noexcept
Returns an iterator to the beginning.
Definition arranged_element_collection.hpp:182
arranged_element_collection(const allocator_type &allocator=allocator_type())
Creates a new object xtd::forms::layout::arranged_element_collection with specified allocator (option...
Definition arranged_element_collection.hpp:108
typename std::allocator_traits< allocator_type >::pointer pointer
Represents the value type pointer of the collection.
Definition arranged_element_collection.hpp:82
bool empty() const noexcept
Checks whether the container is empty.
Definition arranged_element_collection.hpp:216
const_reverse_iterator rbegin() const noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.hpp:199
const_pointer data() const
Direct access to the underlying array.
Definition arranged_element_collection.hpp:172
void reserve(size_type size)
Reserves storage.
Definition arranged_element_collection.hpp:227
event< arranged_element_collection, delegate< void(size_t, type_t &item)> > item_removed
Occurs when an item is removed from the collection.
Definition arranged_element_collection.hpp:473
void push_back_range(iterator_t begin, iterator_t end)
Adds elements to the end.
Definition arranged_element_collection.hpp:419
virtual void push_back_range(const std::initializer_list< value_type > &collection)
Adds elements to the end.
Definition arranged_element_collection.hpp:405
size_type max_size() const noexcept
Returns the maximum possible number of elements.
Definition arranged_element_collection.hpp:224
virtual void clear() noexcept
clears the contents.
Definition arranged_element_collection.hpp:249
reverse_iterator rend() noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.hpp:206
virtual iterator erase(iterator first, iterator last)
Erases elements at specified range.
Definition arranged_element_collection.hpp:342
typename std::vector< value_type >::const_reverse_iterator const_reverse_iterator
Represents the const reverse iterator type of the collection.
Definition arranged_element_collection.hpp:92
virtual void on_item_updated(size_t index, type_t &item)
Raises the xtd::forms::layout::arranged_element_collection::item_updated event.
Definition arranged_element_collection.hpp:489
reference at(size_type pos)
Access specified element with bounds checking.
Definition arranged_element_collection.hpp:143
const_iterator cend() const noexcept
Returns an iterator to the end.
Definition arranged_element_collection.hpp:192
virtual bool sorted() const noexcept
Checks whether the container is sorted.
Definition arranged_element_collection.hpp:238
event< arranged_element_collection, delegate< void(size_t, type_t &item)> > item_added
Occurs when an item is added to the collection.
Definition arranged_element_collection.hpp:465
size_type size() const noexcept
Returns the number of elements.
Definition arranged_element_collection.hpp:220
std::ptrdiff_t difference_type
Represents the pointer difference type of the collection.
Definition arranged_element_collection.hpp:76
static void throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current())
Throws an exption with specified exception case.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:44
@ argument_out_of_range
The argument is out of range.
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
@ insert
The INSERT key.
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition xtd_about_box.hpp:12
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Contains xtd::forms::layout::sorter_none class.