template<typename type_t>
class xtd::collections::generic::ienumerator< type_t >
Supports a simple iteration over a generic collection.
- Definition
template<typename type_t>
Supports a simple iteration over a generic collection.
Definition ienumerator.h:58
#define interface_
This keyword is use to represent an interface.
Definition interface.h:58
- Header
#include <xtd/collections/ienumerator
- Namespace
- xtd::collections::generic
- Library
- xtd.core
- Examples
- The following example shows an implementation of the xtd::collections::generic::ienumerator <type_t> interface for a collection class of custom objects. The custom object is an instance of the type Box, and the collection class is BoxCollection. This code example is part of a larger example provided for the ICollection<T> interface.
public:
explicit box_enumerator(const box_collection& boxes) : boxes {boxes} {}
const box& current()
const override {
return boxes[cur_index];}
bool move_next() override {return ++cur_index < boxes.count() ? true : false;}
private:
const box_collection& boxes;
size cur_index = box_integer<size>::max_value;
};
Represents a boxed integer object.
Definition box_integer.h:52
Represents a boxed object.
Definition box.h:53
- Examples
- generic_icollection.cpp, generic_ienumerable.cpp, generic_ienumerable2.cpp, generic_ienumerator.cpp, generic_ilist.cpp, ienumerable.cpp, and ienumerator.cpp.
|
virtual const type_t & | current () const =0 |
| Gets the element in the collection at the current position of the enumerator.
|
|
|
virtual bool | move_next ()=0 |
| Advances the enumerator to the next element of the collection.
|
|
virtual void | reset ()=0 |
| Sets the enumerator to its initial position, which is before the first element in the collection.
|
|
◆ current()
template<typename type_t >
Gets the element in the collection at the current position of the enumerator.
- Returns
- The element in the collection at the current position of the enumerator.
- Examples
- The following code example demonstrates the implementation of the IEnumerator interfaces for a custom collection. In this example, xtd::collections::generic::ienumerator::move_next is not explicitly called, but it is implemented to support the use of foreach (for each in Visual Basic). This code example is part of a larger example for the IEnumerator interface.
private:
public:
explicit people_enum(
const list<person>& people) : people {people} {}
bool move_next() override {
++position_;
return position < people.
count();
}
const person& current() const override {
try {
return people[position_];
} catch (const index_out_of_range_exception& e) {
throw invalid_operation_exception {};
}
}
};
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:71
size_type count() const noexcept override
Gets the number of elements contained in the xtd::collections::generic::list <type_t>.
Definition list.h:288
size_t size
Represents a size of any object in bytes.
Definition size.h:23
- Notes to Implementers
- Implementing this interface requires implementing the nongeneric IEnumerator interface. The xtd::collections::generic::ienumerator::current property appears on both interfaces, and has different return types. Implement the nongeneric xtd::collections::generic::ienumerator::current property as an explicit interface implementation. This allows any consumer of the nongeneric interface to consume the generic interface.
Implemented in xtd::collections::generic::enumerator< type_t >.
◆ move_next()
template<typename type_t >
Advances the enumerator to the next element of the collection.
- Returns
- true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
- Exceptions
-
- Examples
- The following code example demonstrates the implementation of the IEnumerator interfaces for a custom collection. In this example, xtd::collections::generic::ienumerator::move_next is not explicitly called, but it is implemented to support the use of foreach (for each in Visual Basic). This code example is part of a larger example for the IEnumerator interface.
private:
public:
explicit people_enum(
const list<person>& people) : people {people} {}
bool move_next() override {
++position_;
return position < people.
count();
}
const person& current() const override {
try {
return people[position_];
} catch (const index_out_of_range_exception& e) {
throw invalid_operation_exception {};
}
}
};
Implemented in xtd::collections::generic::enumerator< type_t >.
◆ reset()
template<typename type_t >
Sets the enumerator to its initial position, which is before the first element in the collection.
- Exceptions
-
- Examples
- The following code example demonstrates the implementation of the IEnumerator interfaces for a custom collection. In this example, xtd::collections::generic::ienumerator::move_next is not explicitly called, but it is implemented to support the use of foreach (for each in Visual Basic). This code example is part of a larger example for the IEnumerator interface.
private:
public:
explicit people_enum(
const list<person>& people) : people {people} {}
bool move_next() override {
++position_;
return position < people.
count();
}
const person& current() const override {
try {
return people[position_];
} catch (const index_out_of_range_exception& e) {
throw invalid_operation_exception {};
}
}
};
- Notes to Implementers
- All calls to xtd::collections::generic::ienumerator::reset() must result in the same state for the enumerator. The preferred implementation is to move the enumerator to the beginning of the collection, before the first element. This invalidates the enumerator if the collection has been modified since the enumerator was created, which is consistent with xtd::collections::generic::ienumerator::move_next() and xtd::collections::generic::ienumerator::current.
Implemented in xtd::collections::generic::enumerator< type_t >.
The documentation for this class was generated from the following file: