xtd 0.2.0
Loading...
Searching...
No Matches
arranged_element_collection.h
Go to the documentation of this file.
1
4#pragma once
5
6#include "sorter_none.h"
7#include <xtd/argument_out_of_range_exception>
8#include <xtd/event_args>
9#include <xtd/event_handler>
10#include <xtd/event>
11#include <xtd/object>
12#include <xtd/size_object>
13#include <ostream>
14#include <vector>
15
17namespace xtd {
19 namespace forms {
21 namespace layout {
33 template<typename type_t, typename sorter_t = sorter_none>
35 public:
37 class value_type : public type_t {
38 public:
40 value_type() = default;
41 value_type(const value_type&) = default;
42 value_type(value_type&&) = default;
43 template <typename ...args_t>
44 value_type(args_t&& ...args) : type_t(args...) {}
45 value_type& operator =(const value_type& value) {
46 if (value.owner) owner = value.owner;
47 if (owner != nullptr && !owner->inserting_ && !owner->erasing_) owner->on_item_updated(pos, static_cast<type_t&>(const_cast<value_type&>(value)));
48 type_t::operator =(value);
49 return *this;
50 }
51 value_type& operator =(value_type&& value) {
52 if (value.owner) owner = value.owner;
53 if (owner != nullptr && !owner->inserting_ && !owner->erasing_) owner->on_item_updated(pos, static_cast<type_t&>(value));
54 type_t::operator =(value);
55 return *this;
56 }
57 operator type_t() {return *this;}
58 friend std::ostream& operator <<(std::ostream& os, const value_type& value) {return os << static_cast<const type_t&>(value);}
60
61 private:
62 friend class arranged_element_collection;
63 size_t pos = size_object::max_value;
64 arranged_element_collection* owner = nullptr;
65 };
66
68
71 using allocator_type = std::allocator<value_type>;
73 using size_type = std::size_t;
75 using difference_type = std::ptrdiff_t;
81 using pointer = typename std::allocator_traits<allocator_type>::pointer;
83 using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
85 using iterator = typename std::vector<value_type>::iterator;
87 using const_iterator = typename std::vector<value_type>::const_iterator;
89 using reverse_iterator = typename std::vector<value_type>::reverse_iterator;
91 using const_reverse_iterator = typename std::vector<value_type>::const_reverse_iterator;
93
95
98 inline static constexpr xtd::size npos = xtd::size_object::max_value;
100
102
107 explicit arranged_element_collection(const allocator_type& allocator = allocator_type()) : collection_(allocator) {}
110 arranged_element_collection(const std::initializer_list<type_t>& il) {
111 for (auto item : il)
112 push_back(item);
113 }
115
117 explicit arranged_element_collection(const std::vector<type_t>& collection) {
118 for (auto item : collection)
119 push_back(item);
120 }
121 arranged_element_collection(const arranged_element_collection& collection) {push_back_range(collection);}
122 arranged_element_collection& operator =(const arranged_element_collection& collection) {
123 clear();
124 push_back_range(collection);
125 return *this;
126 }
127 arranged_element_collection(arranged_element_collection&&) = default;
128 bool operator ==(const arranged_element_collection& value) const {return collection_ == value.collection_;}
129 bool operator !=(const arranged_element_collection& value) const {return !operator ==(value);}
131
133
137 allocator_type get_allocator() const noexcept {return collection_.get_allocator();}
138
143 collection_[pos].pos = pos;
144 collection_[pos].owner = this;
145 return collection_.at(pos);
146 }
150 const_reference at(size_type pos) const {return collection_.at(pos);}
151
154 reference front() {return collection_.front();}
157 const_reference front() const {return collection_.front();}
158
161 reference back() {return collection_.back();}
164 const_reference back() const {return collection_.back();}
165
168 pointer data() {return collection_.data();}
171 const_pointer data() const {return collection_.data();}
172
175 iterator begin() noexcept {return collection_.begin();}
178 const_iterator begin() const noexcept {return collection_.begin();}
181 const_iterator cbegin() const noexcept {return collection_.cbegin();}
182
185 iterator end() noexcept {return collection_.end();}
188 const_iterator end() const noexcept {return collection_.end();}
191 const_iterator cend() const noexcept {return collection_.cend();}
192
195 reverse_iterator rbegin() noexcept {return collection_.rbegin();}
198 const_reverse_iterator rbegin() const noexcept {return collection_.rbegin();}
201 const_reverse_iterator crbegin() const noexcept {return collection_.crbegin();}
202
205 reverse_iterator rend() noexcept {return collection_.rend();}
208 const_reverse_iterator rend() const noexcept {return collection_.rend();}
211 const_reverse_iterator crend() const noexcept {return collection_.crend();}
212
215 bool empty() const noexcept {return collection_.empty();}
216
219 size_type size() const noexcept {return collection_.size();}
220
223 size_type max_size() const noexcept {return collection_.max_size();}
224
226 void reserve(size_type size) {collection_.reserve(size);}
227
230 size_type capacity() const noexcept {return collection_.capacity();}
231
233 void shrink_to_fit() {collection_.shrink_to_fit();}
234
237 virtual bool sorted() const noexcept {return sorted_;}
240 virtual void sorted(bool value) {
241 if (sorted_ != value) {
242 sorted_ = value;
243 if (sorted_) sort();
244 }
245 }
246
248 virtual void clear() noexcept {
249 iterator it = begin();
250 while (it != end())
251 it = erase(it);
252 }
253
257 virtual iterator insert(const_iterator pos, const value_type& value) {
258 size_t index = pos - begin();
259 inserting_ = true;
260 iterator result = collection_.insert(pos, value);
261 inserting_ = false;
262 (*this)[index].owner = this;
263 (*this)[index].pos = index;
264 on_item_added(index, collection_[index]);
265 if (sorted_) sort();
266 return result;
267 }
271 virtual iterator insert(const_iterator pos, value_type&& value) {
272 size_t index = pos - begin();
273 inserting_ = true;
274 iterator result = collection_.insert(pos, value);
275 inserting_ = false;
276 (*this)[index].owner = this;
277 (*this)[index].pos = index;
278 on_item_added(index, collection_[index]);
279 if (sorted_) sort();
280 return result;
281 }
282
286 virtual void insert_at(size_t index, const value_type& value) {
287 if (index > size()) throw argument_out_of_range_exception {};
288 insert(begin() + index, value);
289 }
290
294 template<typename ...args_t>
295 void emplace(const_iterator pos, args_t&& ... args) {
296 size_t index = pos - begin();
297 inserting_ = true;
298 iterator result = collection_.insert(pos, args...);
299 inserting_ = false;
300 (*this)[index].owner = this;
301 (*this)[index].pos = index;
302 on_item_added(index, collection_[index]);
303 if (sorted_) sort();
304 return result;
305 }
306
309 template<typename ...args_t>
310 void emplace_back(args_t&& ... args) {
311 collection_.push_back(args...);
312 size_t index = collection_.size() - 1;
313 (*this)[index].owner = this;
314 (*this)[index].pos = index;
315 on_item_added(index, collection_[index]);
316 if (sorted_) sort();
317 }
318
321 virtual iterator erase(iterator pos) {
322 on_item_removed(pos - begin(), *pos);
323 erasing_ = true;
324 iterator result = collection_.erase(pos);
325 erasing_ = false;
326 return result;
327 }
331 on_item_removed(pos - begin(), *reinterpret_cast<iterator&>(pos));
332 erasing_ = true;
333 iterator result = collection_.erase(pos);
334 erasing_ = false;
335 return result;
336 }
337
341 virtual iterator erase(iterator first, iterator last) {
342 iterator result = end();
343 for (iterator it = first; it <= last; ++it)
344 result = erase(it);
345 return result;
346 }
351 iterator result = end();
352 for (const_iterator it = first; it <= last; ++it)
353 result = erase(it);
354 return result;
355 }
356
359 virtual void erase_at(size_t index) {
360 if (index > size()) throw argument_out_of_range_exception {};
361 erase(begin() + index);
362 }
363
365 virtual void pop_back() {
366 if (size() != 0) erase_at(size() - 1);
367 }
368
371 virtual void push_back(const value_type& item) {
372 collection_.push_back(item);
373 size_t index = collection_.size() - 1;
374 (*this)[index].owner = this;
375 (*this)[index].pos = index;
376 on_item_added(index, collection_[index]);
377 if (sorted_) sort();
378 }
381 virtual void push_back(value_type&& item) {
382 collection_.push_back(item);
383 size_t index = collection_.size() - 1;
384 (*this)[index].owner = this;
385 (*this)[index].pos = index;
386 on_item_added(index, collection_[index]);
387 if (sorted_) sort();
388 }
389
392 virtual void push_back_range(const arranged_element_collection& collection) {
393 for (value_type item : collection)
394 push_back(item);
395 }
398 virtual void push_back_range(const std::vector<value_type>& collection) {
399 for (value_type item : collection)
400 push_back(item);
401 }
404 virtual void push_back_range(const std::initializer_list<value_type>& collection) {
405 for (value_type item : collection)
406 push_back(item);
407 }
410 template<typename collection_t>
411 void push_back_range(collection_t&& collection) {
412 for (auto& item : collection)
413 push_back(value_type(item));
414 }
417 template<typename iterator_t>
418 void push_back_range(iterator_t begin, iterator_t end) {
419 for (auto it = begin; it != end; ++it)
420 push_back(value_type(*it));
421 }
422
424 virtual void sort() {
425 sorter_t sorter;
426 sorter(begin(), end());
427 }
428
431 std::vector<type_t> to_array() const noexcept {
432 std::vector<type_t> array;
433 std::for_each(collection_.begin(), collection_.end(), [&](auto item) {array.push_back(item);});
434 return array;
435 }
436
439 std::vector<type_t> to_vector() const noexcept {return to_array();}
441
443
448 collection_[pos].pos = pos;
449 collection_[pos].owner = this;
450 return collection_[pos];
451 }
455 collection_[pos].pos = pos;
456 collection_[pos].owner = const_cast<arranged_element_collection*>(this);
457 return collection_[pos];
458 }
460
462
466 event<arranged_element_collection, delegate<void(size_t, type_t& item)>> item_added;
467
470 event<arranged_element_collection, delegate<void(size_t, type_t& item)>> item_updated;
471
474 event<arranged_element_collection, delegate<void(size_t, type_t& item)>> item_removed;
476
478
483 virtual void on_item_added(size_t index, type_t& item) {
484 item_added(index, item);
485 }
486
490 virtual void on_item_updated(size_t index, type_t& item) {
491 item_updated(index, item);
492 }
493
497 virtual void on_item_removed(size_t index, type_t& item) {
498 item_removed(index, item);
499 }
501
502 private:
503 mutable std::vector<value_type, allocator_type> collection_;
504 bool inserting_ = false;
505 bool erasing_ = false;
506 bool sorted_ = false;
507 };
508 }
509 }
510}
The exception that is thrown when one of the arguments provided to a method is out of range.
Definition argument_out_of_range_exception.h:23
static constexpr type_t max_value
Represents the largest possible value of type_t. This field is constant.
Definition box_integer.h:67
Represents the value type of the collection.
Definition arranged_element_collection.h:37
Represents a collection of objects.
Definition arranged_element_collection.h:34
virtual void erase_at(size_t index)
Erases element at specified index.
Definition arranged_element_collection.h:359
virtual iterator insert(const_iterator pos, value_type &&value)
Inserts specified element at specified position.
Definition arranged_element_collection.h:271
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.h:483
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.h:110
iterator begin() noexcept
Returns an iterator to the beginning.
Definition arranged_element_collection.h:175
iterator end() noexcept
Returns an iterator to the end.
Definition arranged_element_collection.h:185
void emplace_back(args_t &&... args)
Adds an element to the end.
Definition arranged_element_collection.h:310
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.h:98
typename std::vector< value_type >::reverse_iterator reverse_iterator
Represents the reverse iterator type of the collection.
Definition arranged_element_collection.h:89
const_reference front() const
Access the first element.
Definition arranged_element_collection.h:157
size_type capacity() const noexcept
Returns the number of elements that can be held in currently allocated storage.
Definition arranged_element_collection.h:230
virtual void pop_back()
Removes the last element of the container.
Definition arranged_element_collection.h:365
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.h:497
virtual void sorted(bool value)
Sets the container is sorted.
Definition arranged_element_collection.h:240
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.h:470
const_reverse_iterator rend() const noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.h:208
const_iterator end() const noexcept
Returns an iterator to the end.
Definition arranged_element_collection.h:188
std::size_t size_type
Represents the size type of the collection.
Definition arranged_element_collection.h:73
typename std::vector< value_type >::iterator iterator
Represents the iterator type of the collection.
Definition arranged_element_collection.h:85
std::allocator< value_type > allocator_type
Represents the allocator type of the collection.
Definition arranged_element_collection.h:71
virtual iterator insert(const_iterator pos, const value_type &value)
Inserts specified element at specified position.
Definition arranged_element_collection.h:257
virtual iterator erase(const_iterator pos)
Erases element at specified position.
Definition arranged_element_collection.h:330
void push_back_range(collection_t &&collection)
Adds elements to the end.
Definition arranged_element_collection.h:411
virtual void sort()
Sorts the content.
Definition arranged_element_collection.h:424
typename std::allocator_traits< allocator_type >::const_pointer const_pointer
Represents the value type const pointer of the collection.
Definition arranged_element_collection.h:83
const_reverse_iterator crend() const noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.h:211
virtual void insert_at(size_t index, const value_type &value)
Inserts specified element at specified index.
Definition arranged_element_collection.h:286
reference operator[](size_type pos)
Access specified element.
Definition arranged_element_collection.h:447
virtual void push_back_range(const std::vector< value_type > &collection)
Adds elements to the end.
Definition arranged_element_collection.h:398
allocator_type get_allocator() const noexcept
Returns the associated allocator.
Definition arranged_element_collection.h:137
reference back()
Access the last element.
Definition arranged_element_collection.h:161
pointer data()
Direct access to the underlying array.
Definition arranged_element_collection.h:168
void shrink_to_fit()
Reduces memory usage by freeing unused memory.
Definition arranged_element_collection.h:233
const_iterator begin() const noexcept
Returns an iterator to the beginning.
Definition arranged_element_collection.h:178
virtual iterator erase(iterator pos)
Erases element at specified position.
Definition arranged_element_collection.h:321
virtual void push_back(const value_type &item)
Adds an element to the end.
Definition arranged_element_collection.h:371
reference front()
Access the first element.
Definition arranged_element_collection.h:154
virtual void push_back_range(const arranged_element_collection &collection)
Adds elements to the end.
Definition arranged_element_collection.h:392
typename std::vector< value_type >::const_iterator const_iterator
Represents the const iterator type of the collection.
Definition arranged_element_collection.h:87
reverse_iterator rbegin() noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.h:195
std::vector< type_t > to_array() const noexcept
Gets an array with the elements of the container.
Definition arranged_element_collection.h:431
std::vector< type_t > to_vector() const noexcept
Gets an array with the elements of the container.
Definition arranged_element_collection.h:439
const_reverse_iterator crbegin() const noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.h:201
const_reference back() const
Access the last element.
Definition arranged_element_collection.h:164
const_reference at(size_type pos) const
Access specified element with bounds checking.
Definition arranged_element_collection.h:150
virtual void push_back(value_type &&item)
Adds an element to the end.
Definition arranged_element_collection.h:381
virtual iterator erase(const_iterator first, const_iterator last)
Erases elements at specified range.
Definition arranged_element_collection.h:350
void emplace(const_iterator pos, args_t &&... args)
Inserts specified element at specified position.
Definition arranged_element_collection.h:295
const_iterator cbegin() const noexcept
Returns an iterator to the beginning.
Definition arranged_element_collection.h:181
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.h:107
typename std::allocator_traits< allocator_type >::pointer pointer
Represents the value type pointer of the collection.
Definition arranged_element_collection.h:81
bool empty() const noexcept
Checks whether the container is empty.
Definition arranged_element_collection.h:215
const_reverse_iterator rbegin() const noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.h:198
const_pointer data() const
Direct access to the underlying array.
Definition arranged_element_collection.h:171
void reserve(size_type size)
Reserves storage.
Definition arranged_element_collection.h:226
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.h:474
void push_back_range(iterator_t begin, iterator_t end)
Adds elements to the end.
Definition arranged_element_collection.h:418
virtual void push_back_range(const std::initializer_list< value_type > &collection)
Adds elements to the end.
Definition arranged_element_collection.h:404
size_type max_size() const noexcept
Returns the maximum possible number of elements.
Definition arranged_element_collection.h:223
virtual void clear() noexcept
clears the contents.
Definition arranged_element_collection.h:248
reverse_iterator rend() noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.h:205
virtual iterator erase(iterator first, iterator last)
Erases elements at specified range.
Definition arranged_element_collection.h:341
typename std::vector< value_type >::const_reverse_iterator const_reverse_iterator
Represents the const reverse iterator type of the collection.
Definition arranged_element_collection.h:91
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.h:490
reference at(size_type pos)
Access specified element with bounds checking.
Definition arranged_element_collection.h:142
const_iterator cend() const noexcept
Returns an iterator to the end.
Definition arranged_element_collection.h:191
virtual bool sorted() const noexcept
Checks whether the container is sorted.
Definition arranged_element_collection.h:237
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.h:466
size_type size() const noexcept
Returns the number of elements.
Definition arranged_element_collection.h:219
std::ptrdiff_t difference_type
Represents the pointer difference type of the collection.
Definition arranged_element_collection.h:75
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
size_t size
Represents a size of any object in bytes.
Definition size.h:23
@ insert
The INSERT key.
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition xtd_about_box.h:12
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
std::vector< type_t > array
Definition __array_definition.h:18
Contains xtd::forms::layout::sorter_none class.