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

◆ get_enumerator()

template<typename type_t >
generic::enumerator< type_t > xtd::collections::object_model::read_only_collection< type_t >::get_enumerator ( ) const
inlineoverridevirtualnoexcept

Returns an enumerator that iterates through the xtd::collections::object_model::read_only_collection <type_t>.

Returns
An xtd::collections::generic::enumerator<T> for the xtd::collections::object_model::read_only_collection <type_t>.
Examples
The following code example demonstrates several members of the xtd::collections::object_model::read_only_collection <type_t> class. The code example creates a xtd::collections::generic::list <type_t> of strings and adds four dinosaur names to it. The code example then wraps the list in a xtd::collections::object_model::read_only_collection <type_t>.

After demonstrating the xtd::collections::object_model::read_only_collection::count, xtd::collections::object_model::read_only_collection::contains, xtd::collections::object_model::read_only_collection:: opertor [], and xtd::collections::generic::ilist::index_of members, the code example shows that the xtd::collections::object_model::read_only_collection <type_t> is just a wrapper for the original xtd::collections::generic::list <type_t> by adding a new item to the xtd::collections::generic::list <type_t> and displaying the contents of the xtd::collections::object_model::read_only_collection <type_t>.

Finally, the code example creates an array larger than the collection and uses the xtd::collections::object_model::read_only_collection::copy_to method to insert the elements of the collection into the middle of the array.

#include <xtd/xtd>
using namespace xtd;
using namespace xtd::collections::generic;
class example {
public:
static auto main() -> void {
auto dinosaurs = list<string> {};
dinosaurs.add("Tyrannosaurus");
dinosaurs.add("Amargasaurus");
dinosaurs.add("Deinonychus");
dinosaurs.add("Compsognathus");
auto read_only_dinosaurs = dinosaurs.as_read_only();
for (auto dinosaur : read_only_dinosaurs)
console::write_line(dinosaur);
console::write_line("\ncount: {0}", read_only_dinosaurs.count());
console::write_line("\ncontains(\"Deinonychus\"): {0}", dinosaurs.contains("Deinonychus"));
console::write_line("\nread_only_dinosaurs[3]: {0}", dinosaurs[3]);
console::write_line("\nindex_of(\"Compsognathus\"): {0}", read_only_dinosaurs.index_of("Compsognathus"));
console::write_line("\nInsert into the wrapped List:");
console::write_line("insert(2, \"Oviraptor\")");
dinosaurs.insert(2, "Oviraptor");
console::write_line();
for (auto dinosaur : read_only_dinosaurs)
console::write_line(dinosaur);
auto dino_array = array<string>(read_only_dinosaurs.count() + 2);
read_only_dinosaurs.copy_to(dino_array, 1);
console::write_line("\nCopied array has {0} elements:", dino_array.size());
for (auto dinosaur : dino_array )
console::write_line("\"{0}\"", dinosaur);
}
};
startup_(example::main);
// This code produces the following output :
//
// Tyrannosaurus
// Amargasaurus
// Deinonychus
// Compsognathus
//
// count: 4
//
// contains("Deinonychus"): true
//
// read_only_dinosaurs[3]: Compsognathus
//
// index_of("Compsognathus"): 3
//
// Insert into the wrapped List:
// insert(2, "Oviraptor")
//
// Tyrannosaurus
// Amargasaurus
// Oviraptor
// Deinonychus
// Compsognathus
//
// Copied array has 7 elements:
// ""
// "Tyrannosaurus"
// "Amargasaurus"
// "Oviraptor"
// "Deinonychus"
// "Compsognathus"
// ""
void add(const type_t &item) override
Adds an object to the end of the xtd::collections::generic::list <type_t>.
Definition list.h:386
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:72
Represents the standard input, output, and error streams for console applications.
Definition console.h:36
#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
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:15
std::vector< type_t > array
Definition __array_definition.h:18
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Remarks
The for each statement of the C++ language hides the complexity of the enumerators. Therefore, using for each 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::enumerator::current is undefined. Therefore, you must call xtd::collections::generic::enumerator::move_next to advance the enumerator to the first element of the collection before reading the value of xtd::collections::generic::enumerator::current.
xtd::collections::generic::enumerator::current returns the same object until xtd::collections::generic::enumerator::move_next is called. xtd::collections::generic::enumerator::move_next sets xtd::collections::generic::enumerator::current to the next element.
If xtd::collections::generic::enumerator::move_next passes the end of the collection, the enumerator is positioned after the last element in the collection and xtd::collections::generic::enumerator::move_next returns false. When the enumerator is at this position, subsequent calls to MoveNext also return false. If the last call to MoveNext returned false, Current is undefined. You cannot set xtd::collections::generic::enumerator::current to the first element of the collection again; you must create a new enumerator instance instead.
An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and its behavior 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 xtd::collections::generic are not synchronized.
This method is an O(1) operation

Implements xtd::collections::generic::ienumerable< type_t >.