xtd 0.2.0
Loading...
Searching...
No Matches
xtd::collections::generic::ienumerator< type_t > Class Template Referenceabstract
Inheritance diagram for xtd::collections::generic::ienumerator< type_t >:
xtd::interface xtd::collections::generic::enumerator< type_t >

Definition

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.
// Defines the enumerator for the boxes collection.
// (Some prefer this class nested in the collection class.)
class box_enumerator : public ienumerator<box> {
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;}
void reset() override {cur_index = box_integer<size>::max_value;}
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
Remarks
xtd::collections::generic::ienumerator <type_t> is the base interface for all generic enumerators.
The for each statement of the C++ language hides the complexity of the enumerators. Therefore, using for is recommended, instead of directly manipulating the enumerator.
Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.
Initially, the enumerator is positioned before the first element in the collection. At this position, xtd::collections::generic::ienumerator::current is undefined. Therefore, you must call xtd::collections::generic::ienumerator::move_next to advance the enumerator to the first element of the collection before reading the value of xtd::collections::generic::ienumerator::current.
xtd::collections::generic::ienumerator::current returns the same object until xtd::collections::generic::ienumerator::move_next is called. xtd::collections::generic::ienumerator::move_next sets xtd::collections::generic::ienumerator::current to the next element.
If xtd::collections::generic::ienumerator::move_next passes the end of the collection, the enumerator is positioned after the last element in the collection and xtd::collections::generic::ienumerator::move_next returns false. When the enumerator is at this position, subsequent calls to xtd::collections::generic::ienumerator::move_next also return false. If the last call to xtd::collections::generic::ienumerator::move_next returned false, xtd::collections::generic::ienumerator::current is undefined. You cannot set xtd::collections::generic::ienumerator::current to the first element of the collection again; you must create a new enumerator instance instead.
The xtd::collections::generic::ienumerator::reset method is provided for COM interoperability. It does not necessarily need to be implemented; instead, the implementer can simply throw a NotSupportedException. However, if you choose to do this, you should make sure no callers are relying on the xtd::collections::generic::ienumerator::reset functionality.
If changes are made to the collection, such as adding, modifying, or deleting elements, the behavior of the enumerator is undefined.
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
Default implementations of collections in the xtd::collections::generic namespace are not synchronized.

Public Properties

virtual const type_t & current () const =0
 Gets the element in the collection at the current position of the enumerator.
 

Public Methods

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.
 

The documentation for this class was generated from the following file: