xtd 0.2.0
enumerator.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "ienumerator.hpp"
6#include "../../helpers/throw_helper.hpp"
7#include "../../new_ptr.hpp"
8#include "../../static.hpp"
9
11namespace xtd {
13 namespace collections {
15 namespace generic {
17 template <class type_t = std::nullptr_t>
18 struct enumerator;
20
37 template<class type_t>
38 struct enumerator : ienumerator<type_t> {
40
43 enumerator() = default;
46 enumerator(ptr<ienumerator<type_t>> enumerator) : enumerator_ {enumerator} {} // Can't be explicit by design.
48
50 enumerator(enumerator&& enumerator) = default;
51 enumerator(const enumerator& enumerator) = default;
52 enumerator& operator =(enumerator&& enumerator) = default;
53 enumerator& operator =(const enumerator& enumerator) = default;
55
58
62 const type_t& current() const override {return enumerator_->current();}
64
66
71 bool move_next() override {return enumerator_->move_next();}
72
76 void reset() override {enumerator_->reset();}
78
79 private:
80 ptr<ienumerator<type_t>> enumerator_;
81 };
82
99 template <>
102
113 template <class collection_t, class vertion_t = std::nullptr_t>
114 static auto create(const collection_t& items, const vertion_t* current_version = nullptr) noexcept {
115 using value_type = typename collection_t::value_type;
116 using const_iterator = typename collection_t::const_iterator;
117 struct internal_enumerator : public ienumerator<value_type> {
118 public:
119 explicit internal_enumerator(const collection_t& items, const vertion_t* current_version) : items_(items), version_(current_version ? *current_version : vertion_t {}), current_version_(current_version) {}
120
121 const value_type& current() const override {
122 if (current_version_ && version_ != *current_version_) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
123 if (iterator_ != items_.cend()) return *iterator_;
124 static auto default_value_type = value_type {};
125 return default_value_type;
126 }
127
128 bool move_next() override {
129 if (current_version_ && version_ != *current_version_) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
130 if (!reset_) return ++iterator_ != items_.cend();
131 reset_ = false;
132 iterator_ = items_.cbegin();
133 return iterator_ != items_.cend();
134 }
135
136 void reset() override {
137 reset_ = true;
138 version_ = current_version_ ? *current_version_ : vertion_t {};
139 iterator_ = items_.cend();
140 }
141
142 protected:
143 bool reset_ = true;
144 const collection_t& items_;
145 const_iterator iterator_ = items_.cend();
146 vertion_t version_ = vertion_t {};
147 const vertion_t* current_version_ = nullptr;
148 };
149 return enumerator<value_type> {new_ptr<internal_enumerator>(items, current_version)};
150 }
152 };
153 }
154 }
155}
Supports a simple iteration over a generic collection.
Definition ienumerator.hpp:58
static void throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current())
Throws an exption with specified exception case.
The xtd::shared_ptr_object is a shared pointer as std::shared_ptr.
Definition shared_ptr_object.hpp:30
generic::enumerator< xtd::any_object > enumerator
Supports a simple iteration over a non-generic collection.
Definition enumerator.hpp:28
@ invalid_operation
The operation is not valid.
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.hpp:37
Contains xtd::collections::ienumerator alias.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
static auto create(const collection_t &items, const vertion_t *current_version=nullptr) noexcept
Create an enumerator from specified collection and version.
Definition enumerator.hpp:114
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:38
const type_t & current() const override
Gets the element in the collection at the current position of the enumerator.
Definition enumerator.hpp:62
enumerator(ptr< ienumerator< type_t > > enumerator)
Initializes a new instance of the xtd::collections::generic::enumerator <type_t> class with specified...
Definition enumerator.hpp:46
enumerator()=default
Initializes a new instance of the xtd::collections::generic::enumerator <type_t> class.
bool move_next() override
Advances the enumerator to the next element of the collection.
Definition enumerator.hpp:71
void reset() override
Sets the enumerator to its initial position, which is before the first element in the collection.
Definition enumerator.hpp:76