xtd 0.2.0
Loading...
Searching...
No Matches
xtd::collections::generic::ienumerable< type_t > Class Template Referenceabstract
Inheritance diagram for xtd::collections::generic::ienumerable< type_t >:
xtd::interface xtd::collections::generic::enumerable_iterators< type_t, ienumerable< type_t > > xtd::collections::generic::icollection< list_type_t > xtd::collections::generic::icollection< type_t > xtd::collections::generic::ilist< list_type_t > xtd::collections::generic::ilist< type_t > xtd::basic_array< type_t, allocator_t > xtd::basic_array< type_t, allocator_t > xtd::collections::generic::list< type_t, allocator_t > xtd::collections::object_model::read_only_collection< type_t > xtd::array_< type_t, 1, allocator_t > xtd::array_< type_t, 2, allocator_t > xtd::array_< type_t, 3, allocator_t >

Definition

template<typename type_t>
class xtd::collections::generic::ienumerable< type_t >

Exposes the enumerator, which supports a simple iteration over a collection of a specified type.

Definition
template<typename type_t>
class ienumarable interface_
#define interface_
This keyword is use to represent an interface.
Definition interface.h:58
Header
#include <xtd/collections/ienumarable
Namespace
xtd::collections::generic
Library
xtd.core
Examples
The following example demonstrates how to implement the xtd::collections::generic::ienumerable <type_t> interface. When you implement xtd::collections::generic::ienumerable <type_t>, you must also implement xtd::collections::generic::ienumerator <type_t>.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
using namespace xtd::collections::generic;
class program {
public:
static auto main() -> void {
test_stream_reader_enumerable();
console::write_line("---");
test_reading_file();
}
static void test_stream_reader_enumerable() {
// Check the memory before the iterator is used.
auto memory_before = memory_information::get_used_process_memory();
auto strings_found = array<string> {};
// Open a file with the stream_reader_enumerable and check for a string.
try {
for (auto line : stream_reader_enumerable {path::combine(path::get_temp_path(), "temp_file.txt")})
if (line.contains("string to search for")) strings_found.push_back(line);
console::write_line("Found: {}", strings_found.size());
} catch (const file_not_found_exception&) {
console::write_line("This example requires a file named {}.", path::combine(path::get_temp_path(), "temp_file.txt"));
return;
}
// Check the memory after the iterator and output it to the console.
auto memory_after = memory_information::get_used_process_memory();
console::write_line("Memory Used With Iterator = \t{} kb", (memory_after - memory_before) / 1024);
}
static void test_reading_file() {
size memory_before = memory_information::get_used_process_memory();
auto file_contents = list<string> {};
try {
auto sr = stream_reader {path::combine(path::get_temp_path(), "temp_file.txt")};
// Add the file contents to a generic list of strings.
while (!sr.end_of_stream())
file_contents.add(sr.read_line());
sr.close();
} catch (const file_not_found_exception&) {
console::write_line("This example requires a file named {}.", path::combine(path::get_temp_path(), "temp_file.txt"));
return;
}
// Check for the string.
auto strings_found = array<string> {};
for (auto line : file_contents)
if (line.contains("string to search for")) strings_found.push_back(line);
console::write_line("Found: {}", strings_found.size());
// Check the memory after when the iterator is not used, and output it to the console.
auto memory_after = memory_information::get_used_process_memory();
console::write_line("Memory Used Without Iterator = \t{} kb", (memory_after - memory_before) / 1024);
}
// you must also implement xtd::collections::generic::ienumerable <type_t> and xtd::collections::generic::ienumerator <type_t>
class stream_reader_enumerable : public ienumerable<string> {
private:
string file_path_;
public:
stream_reader_enumerable(const string& file_path) : file_path_ {file_path} {}
// Must implement get_enumerator, which returns a new stream_reader_enumerator.
enumerator<string> get_enumerator() const override {return {new_ptr<stream_reader_enumerator>(file_path_)};}
};
// When you implement xtd::collections::generic::ienumerable <type_t>, you must also implement xtd::collections::generic::ienumerator <type_t>, which will walk through the contents of the file one line at a time.
class stream_reader_enumerator : public object, public ienumerator<string> {
private:
std::optional<string> current_;
public:
stream_reader_enumerator(const string& file_path) : sr_ {file_path} {}
~stream_reader_enumerator() {sr_.close();}
// Implement current, move_next and reset, which are required by ienumerator.
const string& current() const override {
if (!current_.has_value()) throw invalid_operation_exception {csf_};
return current_.value();
}
bool move_next() override {
if (sr_.end_of_stream()) current_.reset();
else current_ = sr_.read_line();
return current_.has_value();
}
void reset() override {
sr_.base_stream()->get().seekg(0, std::ios_base::seekdir::beg);
current_.reset();
}
};
};
startup_(program::main);
// This code produces the following output :
//
// Found: 2
// Memory Used With Iterator = 0 kb
// ---
// Found: 2
// Memory Used Without Iterator = 64 kb
Supports a simple iteration over a generic collection.
Definition enumerator.h:31
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:72
The exception that is thrown when a method call is invalid for the object's current state.
Definition invalid_operation_exception.h:18
The exception that is thrown when an attempt to access a file that does not exist on disk fails.
Definition file_not_found_exception.h:29
Performs operations on std::basic_string instances that contain file or directory path information....
Definition path.h:36
Implements a xtd::io::text_reader that reads characters from a byte stream.
Definition stream_reader.h:28
std::optional< std::reference_wrapper< std::istream > > base_stream() const
Returns the underlying stream.
void close() override
Closes the stream_reader object and the underlying stream, and releases any system resources associat...
bool end_of_stream() const
Gets a value that indicates whether the current stream position is at the end of the stream.
virtual xtd::string read_line()
Reads a line of characters from the current stream and returns the data as a string.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
#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
#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
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:15
The xtd::io namespace contains types that allow reading and writing to files and data streams,...
Definition binary_reader.h:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
std::vector< type_t > array
Definition __array_definition.h:18
Remarks
xtd::collections::generic::ienumerable <type_t> is the base interface for collections in the xtd::collections::generic namespace such as xtd::collections::generic::list <type_t>, xtd::collections::generic::dictionary <key_t, value_t>, and xtd::collections::generic::stack <type_t> and other generic collections such as xtd::collections::object_model::observable_collection <type_t> and xtd::collections::concurent::concurrent_stack <type_t>. Collections that implement xtd::collections::generic::ienumerable <type_t> can be enumerated by using the for each statement.
For the non-generic version of this interface, see xtd::collections::ienumerable.
xtd::collections::generic::ienumerable <type_t> contains a single method that you must implement when implementing this interface; xtd::collections::generic::ienumerable::get_enumerator, which returns an xtd::collections::generic::enumerator <type_t> object. The returned xtd::collections::generic::enumerator <type_t> provides the ability to iterate through the collection by exposing a xtd::collections::generic::enumerator::current property.

Public Aliases

using iterator = typename enumerable_iterators< type_t, ienumerable< type_t > >::iterator
 Represents the iterator of xtd::collections::generic::ienumerable value type.
 
using const_iterator = typename enumerable_iterators< type_t, ienumerable< type_t > >::const_iterator
 Represents the const iterator of xtd::collections::generic::ienumerable value type.
 

Public Methods

virtual enumerator< type_t > get_enumerator () const =0
 Returns an enumerator that iterates through a collection.
 

Additional Inherited Members

- Public Types inherited from xtd::collections::generic::enumerable_iterators< type_t, ienumerable< type_t > >
using const_iterator = const iterator
 Represents the const iterator of enumarable value type.
 
- Public Member Functions inherited from xtd::collections::generic::enumerable_iterators< type_t, ienumerable< type_t > >
virtual const_iterator begin () const
 Returns an iterator to the first element of the enumarable.
 
virtual iterator begin ()
 Returns an iterator to the first element of the enumarable.
 
virtual const_iterator cbegin () const
 Returns an iterator to the first element of the enumarable.
 
virtual const_iterator cend () const
 Returns an iterator to the element following the last element of the enumarable.
 
virtual const_iterator end () const
 Returns an iterator to the element following the last element of the enumarable.
 
virtual iterator end ()
 Returns an iterator to the element following the last element of the enumarable.
 

Member Typedef Documentation

◆ iterator

template<typename type_t >
using xtd::collections::generic::ienumerable< type_t >::iterator = typename enumerable_iterators<type_t, ienumerable<type_t> >::iterator

Represents the iterator of xtd::collections::generic::ienumerable value type.

◆ const_iterator

template<typename type_t >
using xtd::collections::generic::ienumerable< type_t >::const_iterator = typename enumerable_iterators<type_t, ienumerable<type_t> >::const_iterator

Represents the const iterator of xtd::collections::generic::ienumerable value type.

Member Function Documentation

◆ get_enumerator()

template<typename type_t >
virtual enumerator< type_t > xtd::collections::generic::ienumerable< type_t >::get_enumerator ( ) const
pure virtual

The documentation for this class was generated from the following file: