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

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

#include <xtd/xtd>
using namespace xtd;
using namespace xtd::collections::generic;
class program {
public:
static auto main() -> void {
auto boxes = box_collection {{10, 20, 30}, {20, 5, 10}, {12, 3, 7}};
auto enumerator = boxes.get_enumerator();
console::write_line(enumerator.current().to_string());
console::write_line();
for (auto box : boxes)
console::write_line(box.to_string());
console::write_line();
for (auto iterator = boxes.begin(); iterator != boxes.end(); ++iterator)
console::write_line(iterator->to_string());
}
struct box : public iequatable<program::box>, public icomparable<program::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 program::box& o) const noexcept override {return 0;}
bool equals(const program::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);}
};
class box_collection : public ienumerable<program::box> {
public:
box_collection(const std::initializer_list<program::box>& boxes) : boxes_(boxes) {}
enumerator<program::box> get_enumerator() const override {
class box_enumerator : public ienumerator<program::box> {
public:
explicit box_enumerator(const list<program::box>& items) : items_(items) {}
const program::box& current() const override {return items_[index_];}
bool move_next() override {return ++index_ < items_.count();}
void reset() override {index_ = box_integer<size>::max_value;}
protected:
const list<program::box>& items_;
};
return {new_ptr<box_enumerator>(boxes_)};
}
private:
};
};
startup_(program::main);
// 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]
//
// box [length=10, width=20, height=30]
// box [length=20, width=5, height=10]
// box [length=12, width=3, height=7]
//
// box [length=10, width=20, height=30]
// box [length=20, width=5, height=10]
// box [length=12, width=3, height=7]
Represents a boxed integer object.
Definition box_integer.h:52
Represents a boxed object.
Definition box.h:53
xtd::string to_string() const noexcept override
Returns a xtd::string that represents the current object.
Definition box.h:109
Supports a simple iteration over a generic collection.
Definition enumerator.h:31
const type_t & current() const override
Gets the element in the collection at the current position of the enumerator.
Definition enumerator.h:55
bool move_next() override
Advances the enumerator to the next element of the collection.
Definition enumerator.h:64
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.h:35
Supports a simple iteration over a generic collection.
Definition ienumerator.h:58
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:71
Defines a generalized comparison method that a value type or class implements to create a type-specif...
Definition icomparable.h:21
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.h:22
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
int32_t int32
Represents a 32-bit signed integer.
Definition int32.h:23
size_t size
Represents a size of any object in bytes.
Definition size.h:23
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:15
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10