xtd 0.2.0
ienumerator.cpp

Shows how to use xtd::collections::ienumerator interface.

#include <xtd/collections/array_list>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
class program {
public:
// Simple business object.
struct person : public object, public iequatable<person>, public icomparable<person> {
public:
person(const string& first_name, const string last_name) : first_name {first_name}, last_name {last_name} {}
string first_name;
string last_name;
int32 compare_to(const person& o) const noexcept override {
if (first_name == o.first_name && last_name == o.last_name) return 0;
if (first_name > o.first_name || (first_name == o.first_name && last_name > o.last_name)) return 1;
return -1;
}
bool equals(const object& o) const noexcept override {return is<person>(o) && equals(as<person>(o));}
bool equals(const person& o) const noexcept override {return compare_to(o) == 0;}
string to_string() const noexcept override {return string::format("{} {}", first_name, last_name);}
};
class people_enumerator : public collections::ienumerator {
private:
const collections::array_list& people_;
// Enumerators are positioned before the first element until the first xtd::collections::ienumerator::move_next() call.
public:
people_enumerator(const collections::array_list& list) : people_(list) {}
bool move_next() override {return ++position < people_.count();}
void reset() override {position = collections::array_list::npos;}
const any_object& current() const override {
if (position >= people_.size()) throw invalid_operation_exception {};
return people_[position];
}
};
static auto main() -> void {
auto people_array = collections::array_list {
person {"John", "Smith"},
person {"Jim", "Johnson"},
person {"Sue", "Rabon"},
};
auto enumerator = people_enumerator {people_array};
while (enumerator.move_next())
}
};
startup_(program::main);
// This code produces the following output :
//
// John Smith
// Jim Johnson
// Sue Rabon
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.hpp:29
static constexpr size_type npos
Definition list.hpp:160
size_type count() const noexcept override
Gets the number of elements contained in the xtd::collections::generic::list <type_t>.
Definition list.hpp:340
virtual size_type size() const noexcept
Returns the number of elements in the container, i.e. std::distance(xtd::collections::generic::list::...
Definition list.hpp:438
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
Defines a generalized comparison method that a value type or class implements to create a type-specif...
Definition icomparable.hpp:21
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:22
The exception that is thrown when the format of an argument does not meet the parameter specification...
Definition invalid_operation_exception.hpp:19
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:44
generic::ienumerator< xtd::any_object > ienumerator
Supports a simple iteration over a non-generic collection.
Definition ienumerator.hpp:38
generic::list< xtd::any_object > array_list
Represents a collection of xtd::any_object.
Definition array_list.hpp:31
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:167
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
bool is(std::any value)
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:365
type_t as(any_object &o)
Casts a type into another type.
Definition __as_any_object.hpp:59
@ o
The O key.
Definition console_key.hpp:116
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
string to_string() const noexcept override
Returns the string representation of this xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:375
bool equals(const object &obj) const noexcept override
Determines whether the specified object is equal to the current object.
Definition read_only_span.hpp:272
const type_t & current() const override
Gets the element in the collection at the current position of the enumerator.
Definition enumerator.hpp:62
bool move_next() override
Advances the enumerator to the next element of the collection.
Definition enumerator.hpp:71