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 {
31 template<typename type_t, typename sorter_t = sorter_none>
33 public:
35 class value_type : public type_t {
36 public:
38 value_type() = default;
39 value_type(const value_type&) = default;
40 value_type(value_type&&) = default;
41 template <typename ...args_t>
42 value_type(args_t&& ...args) : type_t(args...) {}
43 value_type& operator =(const value_type& value) {
44 if (value.owner) owner = value.owner;
45 if (owner != nullptr && !owner->inserting_ && !owner->erasing_) owner->on_item_updated(pos, static_cast<type_t&>(const_cast<value_type&>(value)));
46 type_t::operator =(value);
47 return *this;
48 }
49 value_type& operator =(value_type&& value) {
50 if (value.owner) owner = value.owner;
51 if (owner != nullptr && !owner->inserting_ && !owner->erasing_) owner->on_item_updated(pos, static_cast<type_t&>(value));
52 type_t::operator =(value);
53 return *this;
54 }
55 operator type_t() {return *this;}
56 friend std::ostream& operator <<(std::ostream& os, const value_type& value) {return os << static_cast<const type_t&>(value);}
58
59 private:
60 friend class arranged_element_collection;
61 size_t pos = size_object::max_value;
62 arranged_element_collection* owner = nullptr;
63 };
64
66
69 using allocator_type = std::allocator<value_type>;
71 using size_type = std::size_t;
73 using difference_type = std::ptrdiff_t;
79 using pointer = typename std::allocator_traits<allocator_type>::pointer;
81 using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
83 using iterator = typename std::vector<value_type>::iterator;
85 using const_iterator = typename std::vector<value_type>::const_iterator;
87 using reverse_iterator = typename std::vector<value_type>::reverse_iterator;
89 using const_reverse_iterator = typename std::vector<value_type>::const_reverse_iterator;
91
93
96 inline static const size_t npos = size_object::max_value;
98
100
105 explicit arranged_element_collection(const allocator_type& allocator = allocator_type()) : collection_(allocator) {}
108 arranged_element_collection(const std::initializer_list<type_t>& il) {
109 for (auto item : il)
110 push_back(item);
111 }
113
115 explicit arranged_element_collection(const std::vector<type_t>& collection) {
116 for (auto item : collection)
117 push_back(item);
118 }
119 arranged_element_collection(const arranged_element_collection& collection) {push_back_range(collection);}
120 arranged_element_collection& operator =(const arranged_element_collection& collection) {
121 clear();
122 push_back_range(collection);
123 return *this;
124 }
125 arranged_element_collection(arranged_element_collection&&) = default;
126 bool operator ==(const arranged_element_collection& value) const {return collection_ == value.collection_;}
127 bool operator !=(const arranged_element_collection& value) const {return !operator ==(value);}
129
131
135 allocator_type get_allocator() const noexcept {return collection_.get_allocator();}
136
141 collection_[pos].pos = pos;
142 collection_[pos].owner = this;
143 return collection_.at(pos);
144 }
148 const_reference at(size_type pos) const {return collection_.at(pos);}
149
152 reference front() {return collection_.front();}
155 const_reference front() const {return collection_.front();}
156
159 reference back() {return collection_.back();}
162 const_reference back() const {return collection_.back();}
163
166 pointer data() {return collection_.data();}
169 const_pointer data() const {return collection_.data();}
170
173 iterator begin() noexcept {return collection_.begin();}
176 const_iterator begin() const noexcept {return collection_.begin();}
179 const_iterator cbegin() const noexcept {return collection_.cbegin();}
180
183 iterator end() noexcept {return collection_.end();}
186 const_iterator end() const noexcept {return collection_.end();}
189 const_iterator cend() const noexcept {return collection_.cend();}
190
193 reverse_iterator rbegin() noexcept {return collection_.rbegin();}
196 const_reverse_iterator rbegin() const noexcept {return collection_.rbegin();}
199 const_reverse_iterator crbegin() const noexcept {return collection_.crbegin();}
200
203 reverse_iterator rend() noexcept {return collection_.rend();}
206 const_reverse_iterator rend() const noexcept {return collection_.rend();}
209 const_reverse_iterator crend() const noexcept {return collection_.crend();}
210
213 bool empty() const noexcept {return collection_.empty();}
214
217 size_type size() const noexcept {return collection_.size();}
218
221 size_type max_size() const noexcept {return collection_.max_size();}
222
224 void reserve(size_type size) {collection_.reserve(size);}
225
228 size_type capacity() const noexcept {return collection_.capacity();}
229
231 void shrink_to_fit() {collection_.shrink_to_fit();}
232
235 virtual bool sorted() const noexcept {return sorted_;}
238 virtual void sorted(bool value) {
239 if (sorted_ != value) {
240 sorted_ = value;
241 if (sorted_) sort();
242 }
243 }
244
246 virtual void clear() noexcept {
247 iterator it = begin();
248 while (it != end())
249 it = erase(it);
250 }
251
255 virtual iterator insert(const_iterator pos, const value_type& value) {
256 size_t index = pos - begin();
257 inserting_ = true;
258 iterator result = collection_.insert(pos, value);
259 inserting_ = false;
260 (*this)[index].owner = this;
261 (*this)[index].pos = index;
262 on_item_added(index, collection_[index]);
263 if (sorted_) sort();
264 return result;
265 }
269 virtual iterator insert(const_iterator pos, const value_type&& value) {
270 size_t index = pos - begin();
271 inserting_ = true;
272 iterator result = collection_.insert(pos, value);
273 inserting_ = false;
274 (*this)[index].owner = this;
275 (*this)[index].pos = index;
276 on_item_added(index, collection_[index]);
277 if (sorted_) sort();
278 return result;
279 }
280
284 virtual void insert_at(size_t index, const value_type& value) {
285 if (index > size()) throw argument_out_of_range_exception {csf_};
286 insert(begin() + index, value);
287 }
288
292 template<typename ...args_t>
293 void emplace(const_iterator pos, args_t&& ... args) {
294 size_t index = pos - begin();
295 inserting_ = true;
296 iterator result = collection_.insert(pos, args...);
297 inserting_ = false;
298 (*this)[index].owner = this;
299 (*this)[index].pos = index;
300 on_item_added(index, collection_[index]);
301 if (sorted_) sort();
302 return result;
303 }
304
307 template<typename ...args_t>
308 void emplace_back(args_t&& ... args) {
309 collection_.push_back(args...);
310 size_t index = collection_.size() - 1;
311 (*this)[index].owner = this;
312 (*this)[index].pos = index;
313 on_item_added(index, collection_[index]);
314 if (sorted_) sort();
315 }
316
319 virtual iterator erase(iterator pos) {
320 on_item_removed(pos - begin(), *pos);
321 erasing_ = true;
322 iterator result = collection_.erase(pos);
323 erasing_ = false;
324 return result;
325 }
329 on_item_removed(pos - begin(), *reinterpret_cast<iterator&>(pos));
330 erasing_ = true;
331 iterator result = collection_.erase(pos);
332 erasing_ = false;
333 return result;
334 }
335
339 virtual iterator erase(iterator first, iterator last) {
340 iterator result = end();
341 for (iterator it = first; it <= last; ++it)
342 result = erase(it);
343 return result;
344 }
349 iterator result = end();
350 for (const_iterator it = first; it <= last; ++it)
351 result = erase(it);
352 return result;
353 }
354
357 virtual void erase_at(size_t index) {
358 if (index > size()) throw argument_out_of_range_exception {csf_};
359 erase(begin() + index);
360 }
361
363 virtual void pop_back() {
364 if (size() != 0) erase_at(size() - 1);
365 }
366
369 virtual void push_back(const value_type& item) {
370 collection_.push_back(item);
371 size_t index = collection_.size() - 1;
372 (*this)[index].owner = this;
373 (*this)[index].pos = index;
374 on_item_added(index, collection_[index]);
375 if (sorted_) sort();
376 }
379 virtual void push_back(value_type&& item) {
380 collection_.push_back(item);
381 size_t index = collection_.size() - 1;
382 (*this)[index].owner = this;
383 (*this)[index].pos = index;
384 on_item_added(index, collection_[index]);
385 if (sorted_) sort();
386 }
387
390 virtual void push_back_range(const arranged_element_collection& collection) {
391 for (value_type item : collection)
392 push_back(item);
393 }
396 virtual void push_back_range(const std::vector<value_type>& collection) {
397 for (value_type item : collection)
398 push_back(item);
399 }
402 virtual void push_back_range(const std::initializer_list<value_type>& collection) {
403 for (value_type item : collection)
404 push_back(item);
405 }
408 template<typename collection_t>
409 void push_back_range(collection_t&& collection) {
410 for (auto& item : collection)
411 push_back(value_type(item));
412 }
415 template<typename iterator_t>
416 void push_back_range(iterator_t begin, iterator_t end) {
417 for (auto it = begin; it != end; ++it)
418 push_back(value_type(*it));
419 }
420
422 virtual void sort() {
423 sorter_t sorter;
424 sorter(begin(), end());
425 }
426
429 std::vector<type_t> to_array() const noexcept {
430 std::vector<type_t> array;
431 std::for_each(collection_.begin(), collection_.end(), [&](auto item) {array.push_back(item);});
432 return array;
433 }
434
437 std::vector<type_t> to_vector() const noexcept {return to_array();}
439
441
446 collection_[pos].pos = pos;
447 collection_[pos].owner = this;
448 return collection_[pos];
449 }
453 collection_[pos].pos = pos;
454 collection_[pos].owner = const_cast<arranged_element_collection*>(this);
455 return collection_[pos];
456 }
458
460
464 event<arranged_element_collection, delegate<void(size_t, type_t& item)>> item_added;
465
468 event<arranged_element_collection, delegate<void(size_t, type_t& item)>> item_updated;
469
472 event<arranged_element_collection, delegate<void(size_t, type_t& item)>> item_removed;
474
476
481 virtual void on_item_added(size_t index, type_t& item) {
482 item_added(index, item);
483 }
484
488 virtual void on_item_updated(size_t index, type_t& item) {
489 item_updated(index, item);
490 }
491
495 virtual void on_item_removed(size_t index, type_t& item) {
496 item_removed(index, item);
497 }
499
500 private:
501 mutable std::vector<value_type, allocator_type> collection_;
502 bool inserting_ = false;
503 bool erasing_ = false;
504 bool sorted_ = false;
505 };
506 }
507 }
508}
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:20
static constexpr type_t max_value
Represents the largest possible value of type_t. This field is constant.
Definition box_integer.h:65
Represents the value type of the collection.
Definition arranged_element_collection.h:35
Represents a collection of objects.
Definition arranged_element_collection.h:32
virtual void erase_at(size_t index)
Erases element at specified index.
Definition arranged_element_collection.h:357
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:481
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:108
iterator begin() noexcept
Returns an iterator to the beginning.
Definition arranged_element_collection.h:173
iterator end() noexcept
Returns an iterator to the end.
Definition arranged_element_collection.h:183
void emplace_back(args_t &&... args)
Adds an element to the end.
Definition arranged_element_collection.h:308
virtual iterator insert(const_iterator pos, const value_type &&value)
Inserts specified element at specified position.
Definition arranged_element_collection.h:269
typename std::vector< value_type >::reverse_iterator reverse_iterator
Represents the reverse iterator type of the collection.
Definition arranged_element_collection.h:87
const_reference front() const
Access the first element.
Definition arranged_element_collection.h:155
size_type capacity() const noexcept
Returns the number of elements that can be held in currently allocated storage.
Definition arranged_element_collection.h:228
virtual void pop_back()
Removes the last element of the container.
Definition arranged_element_collection.h:363
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:495
virtual void sorted(bool value)
Sets the container is sorted.
Definition arranged_element_collection.h:238
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:468
const_reverse_iterator rend() const noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.h:206
const_iterator end() const noexcept
Returns an iterator to the end.
Definition arranged_element_collection.h:186
std::size_t size_type
Represents the size type of the collection.
Definition arranged_element_collection.h:71
typename std::vector< value_type >::iterator iterator
Represents the iterator type of the collection.
Definition arranged_element_collection.h:83
std::allocator< value_type > allocator_type
Represents the allocator type of the collection.
Definition arranged_element_collection.h:69
virtual iterator insert(const_iterator pos, const value_type &value)
Inserts specified element at specified position.
Definition arranged_element_collection.h:255
virtual iterator erase(const_iterator pos)
Erases element at specified position.
Definition arranged_element_collection.h:328
void push_back_range(collection_t &&collection)
Adds elements to the end.
Definition arranged_element_collection.h:409
virtual void sort()
Sorts the content.
Definition arranged_element_collection.h:422
typename std::allocator_traits< allocator_type >::const_pointer const_pointer
Represents the value type const pointer of the collection.
Definition arranged_element_collection.h:81
const_reverse_iterator crend() const noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.h:209
virtual void insert_at(size_t index, const value_type &value)
Inserts specified element at specified index.
Definition arranged_element_collection.h:284
reference operator[](size_type pos)
Access specified element.
Definition arranged_element_collection.h:445
virtual void push_back_range(const std::vector< value_type > &collection)
Adds elements to the end.
Definition arranged_element_collection.h:396
allocator_type get_allocator() const noexcept
Returns the associated allocator.
Definition arranged_element_collection.h:135
reference back()
Access the last element.
Definition arranged_element_collection.h:159
pointer data()
Direct access to the underlying array.
Definition arranged_element_collection.h:166
void shrink_to_fit()
Reduces memory usage by freeing unused memory.
Definition arranged_element_collection.h:231
const_iterator begin() const noexcept
Returns an iterator to the beginning.
Definition arranged_element_collection.h:176
virtual iterator erase(iterator pos)
Erases element at specified position.
Definition arranged_element_collection.h:319
virtual void push_back(const value_type &item)
Adds an element to the end.
Definition arranged_element_collection.h:369
reference front()
Access the first element.
Definition arranged_element_collection.h:152
virtual void push_back_range(const arranged_element_collection &collection)
Adds elements to the end.
Definition arranged_element_collection.h:390
typename std::vector< value_type >::const_iterator const_iterator
Represents the const iterator type of the collection.
Definition arranged_element_collection.h:85
reverse_iterator rbegin() noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.h:193
std::vector< type_t > to_array() const noexcept
Gets an array with the elements of the container.
Definition arranged_element_collection.h:429
std::vector< type_t > to_vector() const noexcept
Gets an array with the elements of the container.
Definition arranged_element_collection.h:437
const_reverse_iterator crbegin() const noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.h:199
const_reference back() const
Access the last element.
Definition arranged_element_collection.h:162
const_reference at(size_type pos) const
Access specified element with bounds checking.
Definition arranged_element_collection.h:148
virtual void push_back(value_type &&item)
Adds an element to the end.
Definition arranged_element_collection.h:379
virtual iterator erase(const_iterator first, const_iterator last)
Erases elements at specified range.
Definition arranged_element_collection.h:348
void emplace(const_iterator pos, args_t &&... args)
Inserts specified element at specified position.
Definition arranged_element_collection.h:293
const_iterator cbegin() const noexcept
Returns an iterator to the beginning.
Definition arranged_element_collection.h:179
static const size_t npos
This is a special value equal to the maximum value representable by the type size_t.
Definition arranged_element_collection.h:96
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:105
typename std::allocator_traits< allocator_type >::pointer pointer
Represents the value type pointer of the collection.
Definition arranged_element_collection.h:79
bool empty() const noexcept
Checks whether the container is empty.
Definition arranged_element_collection.h:213
const_reverse_iterator rbegin() const noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.h:196
const_pointer data() const
Direct access to the underlying array.
Definition arranged_element_collection.h:169
void reserve(size_type size)
Reserves storage.
Definition arranged_element_collection.h:224
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:472
void push_back_range(iterator_t begin, iterator_t end)
Adds elements to the end.
Definition arranged_element_collection.h:416
virtual void push_back_range(const std::initializer_list< value_type > &collection)
Adds elements to the end.
Definition arranged_element_collection.h:402
size_type max_size() const noexcept
Returns the maximum possible number of elements.
Definition arranged_element_collection.h:221
virtual void clear() noexcept
clears the contents.
Definition arranged_element_collection.h:246
reverse_iterator rend() noexcept
Returns a reverse iterator to the end.
Definition arranged_element_collection.h:203
virtual iterator erase(iterator first, iterator last)
Erases elements at specified range.
Definition arranged_element_collection.h:339
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:89
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:488
reference at(size_type pos)
Access specified element with bounds checking.
Definition arranged_element_collection.h:140
const_iterator cend() const noexcept
Returns an iterator to the end.
Definition arranged_element_collection.h:189
virtual bool sorted() const noexcept
Checks whether the container is sorted.
Definition arranged_element_collection.h:235
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:464
size_type size() const noexcept
Returns the number of elements.
Definition arranged_element_collection.h:217
std::ptrdiff_t difference_type
Represents the pointer difference type of the collection.
Definition arranged_element_collection.h:73
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:32
#define csf_
Provides information about the current stack frame.
Definition current_stack_frame.h:30
size_t size
Represents a size of any object in bytes.
Definition types.h:197
@ 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
Contains xtd::forms::layout::sorter_none class.