xtd 0.2.0
Loading...
Searching...
No Matches

◆ 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 {csf_};
}
}
};
Represents a boxed integer object.
Definition box_integer.h:52
Supports a simple iteration over a generic collection.
Definition ienumerator.h:58
size_type count() const noexcept override
Gets the number of elements contained in the xtd::collections::generic::list <type_t>.
Definition list.h:289
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:72
#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 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 >.