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.
Examples
generic_icollection.cpp, generic_ienumerable.cpp, generic_ienumerable2.cpp, generic_ienumerator.cpp, generic_ilist.cpp, ienumerable.cpp, and ienumerator.cpp.

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.
 

Member Function Documentation

◆ current()

template<typename type_t >
virtual const type_t & xtd::collections::generic::ienumerator< type_t >::current ( ) const
pure virtual

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.
// When you implement ienumerable, you must also implement ienumerator.
class people_enum : public ienumerator<person> {
private:
// Enumerators are positioned before the first element
// until the first move_next() call.
public:
const list<person>& people;
explicit people_enum(const list<person>& people) : people {people} {}
bool move_next() override {
++position_;
return position < people.count();
}
void reset() override {position_ = box_integer<size>::max_value;}
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
Remarks
xtd::collections::generic::ienumerator::current is undefined under any of the following conditions:
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.
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 >
virtual bool xtd::collections::generic::ienumerator< type_t >::move_next ( )
pure virtual

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
xtd::invalid_operation_exceptionThe collection was modified after the enumerator was created.
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.
// When you implement IEnumerable, you must also implement IEnumerator.
class people_enum : public ienumerator<person> {
private:
// Enumerators are positioned before the first element
// until the first move_next() call.
public:
const list<person>& people;
explicit people_enum(const list<person>& people) : people {people} {}
bool move_next() override {
++position_;
return position < people.count();
}
void reset() override {position_ = box_integer<size>::max_value;}
const person& current() const override {
try {
return people[position_];
} catch (const index_out_of_range_exception& e) {
throw invalid_operation_exception {};
}
}
};
Remarks
After an enumerator is created or after the xtd::collections::generic::ienumerator::reset method is called, an enumerator is positioned before the first element of the collection, and the first call to the xtd::collections::generic::ienumerator::move_next method moves the enumerator over the first element of the collection.
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 until xtd::collections::generic::ienumerator::reset is called.
If changes are made to the collection, such as adding, modifying, or deleting elements, the behavior of xtd::collections::generic::ienumerator::move_next is undefined.

Implemented in xtd::collections::generic::enumerator< type_t >.

◆ reset()

template<typename type_t >
virtual void xtd::collections::generic::ienumerator< type_t >::reset ( )
pure virtual

Sets the enumerator to its initial position, which is before the first element in the collection.

Exceptions
xtd::invalid_operation_exceptionThe collection was modified after the enumerator was created.
xtd::not_supported_exceptionThe enumerator does not support being reset.
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.
// When you implement IEnumerable, you must also implement IEnumerator.
class people_enum : public ienumerator<person> {
private:
// Enumerators are positioned before the first element
// until the first move_next() call.
public:
const list<person>& people;
explicit people_enum(const list<person>& people) : people {people} {}
bool move_next() override {
++position_;
return position < people.count();
}
void reset() override {position_ = box_integer<size>::max_value;}
const person& current() const override {
try {
return people[position_];
} catch (const index_out_of_range_exception& e) {
throw invalid_operation_exception {};
}
}
};
Remarks
If changes are made to the collection, such as adding, modifying, or deleting elements, the behavior of xtd::collections::generic::ienumerator::reset is undefined.
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.
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: