xtd 0.2.0
Loading...
Searching...
No Matches
generic_ienumerator.cpp

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

#include <xtd/xtd>
struct box : public iequatable<::box>, public icomparable<::box> {
box() = default;
box(int l, int w, int h) : length{l}, width {w}, height {h} {}
int length = 0;
int width = 0;
int height = 0;
int32 compare_to(const ::box& o) const noexcept override {return 0;}
bool equals(const ::box& o) const noexcept override {return length == o.length && width == o.width && height == o.height;}
string to_string() const noexcept {return string::format("box [length={}, width={}, height={}]", length, width, height);}
};
using box_collection = list<::box>;
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.size();}
void reset() override {cur_index = box_integer<size>::max_value;}
private:
const box_collection& boxes;
size cur_index = box_integer<size>::max_value;
};
auto main() -> int {
auto boxes = box_collection {{10, 20, 30}, {20, 5, 10}, {12, 3, 7}};
auto enumerator = box_enumerator {boxes};
console::write_line(enumerator.current().to_string());
}
// This code produces the following output :
//
// box [length=10, width=20, height=30]
// box [length=20, width=5, height=10]
// box [length=12, width=3, height=7]
string to_string() const noexcept override
Returns a xtd::string that represents the current object.
Definition any_object.hpp:110
generic::enumerator< xtd::any_object > enumerator
Supports a simple iteration over a non-generic collection.
Definition enumerator.hpp:28
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
@ current
Specifies the current position within a stream.
Definition seek_origin.hpp:20
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