xtd 0.2.0
Loading...
Searching...
No Matches
xtd::collections::object_model::read_only_collection< type_t > Class Template Reference
Inheritance diagram for xtd::collections::object_model::read_only_collection< type_t >:
xtd::object xtd::collections::generic::ilist< type_t > xtd::collections::generic::icollection< type_t > xtd::collections::generic::ienumerable< type_t > xtd::interface xtd::collections::generic::enumerable_iterators< type_t, ienumerable< type_t > >

Definition

template<typename type_t>
class xtd::collections::object_model::read_only_collection< type_t >

Provides the base class for a generic read-only collection.

template<typename type_t>
Represents a collection of objects that can be individually accessed by index.
Definition ilist.h:41
Provides the base class for a generic read-only collection.
Definition read_only_collection.h:40
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
Header
#include <xtd/collections/object_model/read_only_collection>
Namespace
xtd::collections::object_model
Library
xtd.core
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"
// ""
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:71
void add(const type_t &item) override
Adds an object to the end of the xtd::collections::generic::list <type_t>.
Definition list.h:385
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
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

Public Aliases

using value_type = type_t
 Represents the list value type.
 
using base_type = ptr< xtd::collections::generic::ilist< value_type > >
 Represents the list base type.
 
using size_type = xtd::size
 Represents the list size type (usually xtd::size).
 
using difference_type = xtd::ptrdiff
 Represents the list difference type (usually xtd::ptrdiff).
 
using reference = value_type &
 Represents the reference of list value type.
 
using const_reference = const value_type &
 Represents the const reference of list value type.
 
using pointer = value_type *
 Represents the pointer of list value type.
 
using const_pointer = const value_type *
 Represents the const pointer of list value type.
 
using iterator = typename generic::icollection< type_t >::iterator
 Represents the iterator of list value type.
 
using const_iterator = typename generic::icollection< type_t >::const_iterator
 Represents the const iterator of list value type.
 

Public Fields

static constexpr xtd::size npos
 This is a special value equal to the maximum value representable by the type xtd::size.
 

Public Constructors

 read_only_collection (ptr< generic::ilist< value_type > > list)
 Initializes a new instance of the xtd::collections::object_model::read_only_collection <type_t> class that is a read-only wrapper around the specified list.
 

Public Properties

xtd::size count () const noexcept override
 Gets the number of elements contained in the xtd::collections::object_model::read_only_collection <type_t> instance.
 
const xtd::objectsync_root () const noexcept override
 Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollection <type_t>.
 
static const read_only_collection< value_type > & empty ()
 Gets an empty xtd::collections::object_model::read_only_collection <type_t>.
 

Public Methods

bool contains (const type_t &item) const noexcept override
 Determines whether an element is in the xtd::collections::object_model::read_only_collection <type_t>.
 
void copy_to (xtd::array< type_t > &array, xtd::size array_index) const override
 Copies the entire xtd::collections::object_model::read_only_collection <type_t> to a compatible one-dimensional Array, starting at the specified index of the target array.
 
generic::enumerator< type_t > get_enumerator () const noexcept override
 Returns an enumerator that iterates through the xtd::collections::object_model::read_only_collection <type_t>.
 
xtd::size index_of (const type_t &item) const noexcept override
 Searches for the specified object and returns the zero-based index of the first occurrence within the entire xtd::collections::object_model::read_only_collection <type_t>.
 

Public Operators

const_reference operator[] (size_type index) const override
 Returns a reference to the element at specified location pos.
 

Protected Properties

base_type items () noexcept
 Returns the xtd::collections::generic::ilist <type_t> that the xtd::collections::object_model::read_only_collection <type_t> wraps.
 

Additional Inherited Members

- Public Types inherited from xtd::collections::generic::ilist< type_t >
using iterator = typename icollection< type_t >::iterator
 Represents the iterator of xtd::collections::generic::ienumerable value type.
 
using const_iterator = typename icollection< type_t >::const_iterator
 Represents the const iterator of xtd::collections::generic::ienumerable value type.
 
- Public Types inherited from xtd::collections::generic::icollection< type_t >
using iterator = typename ienumerable< type_t >::iterator
 Represents the iterator of xtd::collections::generic::ienumerable value type.
 
using const_iterator = typename ienumerable< type_t >::const_iterator
 Represents the const iterator of xtd::collections::generic::ienumerable value type.
 
- Public Types inherited from xtd::collections::generic::ienumerable< type_t >
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 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.
 
- Static Public Attributes inherited from xtd::collections::generic::ilist< type_t >
static constexpr xtd::size npos
 This is a special value equal to the maximum value representable by the type xtd::size.
 
- Public Member Functions inherited from xtd::object
 object ()=default
 Create a new instance of the ultimate base class object.
 
virtual bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object.
 
virtual size_t get_hash_code () const noexcept
 Serves as a hash function for a particular type.
 
virtual type_object get_type () const noexcept
 Gets the type of the current instance.
 
template<typename object_t >
xtd::uptr< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
 
virtual xtd::string to_string () const noexcept
 Returns a xtd::string that represents the current object.
 
- Public Member Functions inherited from xtd::collections::generic::ilist< type_t >
- Public Member Functions inherited from xtd::collections::generic::icollection< type_t >
- Public Member Functions inherited from xtd::collections::generic::ienumerable< type_t >
- 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.
 
- Static Public Member Functions inherited from xtd::object
template<typename object_a_t , typename object_b_t >
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
 
template<typename object_a_t , typename object_b_t >
static bool reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are the same instance.
 

Member Typedef Documentation

◆ value_type

template<typename type_t >
using xtd::collections::object_model::read_only_collection< type_t >::value_type = type_t

Represents the list value type.

◆ base_type

Represents the list base type.

◆ size_type

template<typename type_t >
using xtd::collections::object_model::read_only_collection< type_t >::size_type = xtd::size

Represents the list size type (usually xtd::size).

◆ difference_type

template<typename type_t >
using xtd::collections::object_model::read_only_collection< type_t >::difference_type = xtd::ptrdiff

Represents the list difference type (usually xtd::ptrdiff).

◆ reference

template<typename type_t >
using xtd::collections::object_model::read_only_collection< type_t >::reference = value_type&

Represents the reference of list value type.

◆ const_reference

template<typename type_t >
using xtd::collections::object_model::read_only_collection< type_t >::const_reference = const value_type&

Represents the const reference of list value type.

◆ pointer

template<typename type_t >
using xtd::collections::object_model::read_only_collection< type_t >::pointer = value_type*

Represents the pointer of list value type.

◆ const_pointer

template<typename type_t >
using xtd::collections::object_model::read_only_collection< type_t >::const_pointer = const value_type*

Represents the const pointer of list value type.

◆ iterator

template<typename type_t >
using xtd::collections::object_model::read_only_collection< type_t >::iterator = typename generic::icollection<type_t>::iterator

Represents the iterator of list value type.

◆ const_iterator

template<typename type_t >
using xtd::collections::object_model::read_only_collection< type_t >::const_iterator = typename generic::icollection<type_t>::const_iterator

Represents the const iterator of list value type.

Constructor & Destructor Documentation

◆ read_only_collection()

template<typename type_t >
xtd::collections::object_model::read_only_collection< type_t >::read_only_collection ( ptr< generic::ilist< value_type > >  list)
inlineexplicit

Initializes a new instance of the xtd::collections::object_model::read_only_collection <type_t> class that is a read-only wrapper around the specified list.

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"
// ""
Remarks
To prevent any modifications to list, expose list only through this wrapper.
A collection that is read-only is simply a collection with a wrapper that prevents modifying the collection; therefore, if changes are made to the underlying collection, the read-only collection reflects those changes.
This constructor is an O(1) operation.

Member Function Documentation

◆ count()

template<typename type_t >
xtd::size xtd::collections::object_model::read_only_collection< type_t >::count ( ) const
inlineoverridevirtualnoexcept

Gets the number of elements contained in the xtd::collections::object_model::read_only_collection <type_t> instance.

Returns
The number of elements contained in the xtd::collections::object_model::read_only_collection <type_t> instance.
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"
// ""
Remarks
Retrieving the value of this property is an O(1) operation.

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

◆ empty()

template<typename type_t >
static const read_only_collection< value_type > & xtd::collections::object_model::read_only_collection< type_t >::empty ( )
inlinestatic

Gets an empty xtd::collections::object_model::read_only_collection <type_t>.

Returns
An empty xtd::collections::object_model::read_only_collection <type_t>.
Remarks
The returned instance is immutable and will always be empty.

◆ sync_root()

template<typename type_t >
const xtd::object & xtd::collections::object_model::read_only_collection< type_t >::sync_root ( ) const
inlineoverridevirtualnoexcept

Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollection <type_t>.

Returns
An object that can be used to synchronize access to the the xtd::collections::generic::icollection <type_t>.
Remarks
For collections whose underlying store is not publicly available, the expected implementation is to return the current instance. Note that the pointer to the current instance might not be sufficient for collections that wrap other collections; those should return the underlying collection's sync_root property.
Most collection classes in the xts::.collections namespace also implement a synchronized method, which provides a synchronized wrapper around the underlying collection. However, derived classes can provide their own synchronized version of the collection using the xtd::collections::generic::icollection::sync_root property. The synchronizing code must perform operations on the xtd::collections::generic::icollection::sync_root property of the collection, not directly on the collection. This ensures proper operation of collections that are derived from other objects. Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the collection instance.
In the absence of a synchronized method on a collection, the expected usage for the xtd::collections::generic::icollection::sync_root looks as follows:
icollection& my_collection = some_collection;
lock_(my_collection.sync_root()) {
// Some operation on the collection, which is now thread safe.
}
@encode
@remarks Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
@remarks The following code example shows how to lock the collection using the xtd::collections::generic::icollection::sync_root property during the entire enumeration.
@code
icollection& my_collection = some_collection;
lock_(my_collection.sync_root()) {
for (auto item : my_collection) {
// Insert your code here.
}
}
Supports a simple iteration over a generic collection.
Definition enumerator.h:31
Defines methods to manipulate generic collections.
Definition icollection.h:44
virtual const xtd::object & sync_root() const noexcept=0
Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollecti...
Defines the base class for predefined exceptions in the xtd namespace.
Definition exception.h:28
Provides a mechanism that synchronizes access to objects with xtd::threading::monitor.
Definition lock_guard.h:32
#define lock_(object)
The lock_ keyword marks a statement block as a critical section by obtaining the mutual-exclusion loc...
Definition lock.h:85
bool is(std::any value)
Checks if the result of an expression is compatible with a given type.
Definition is.h:365
@ other
The operating system is other.
@ a
The A key.

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

◆ contains()

template<typename type_t >
bool xtd::collections::object_model::read_only_collection< type_t >::contains ( const type_t &  item) const
inlineoverridevirtualnoexcept

Determines whether an element is in the xtd::collections::object_model::read_only_collection <type_t>.

Parameters
itemThe object to locate in the xtd::collections::object_model::read_only_collection <type_t>. The value can be null for reference types.
Returns
true if value is found in the xtd::collections::object_model::read_only_collection <type_t>; otherwise, false.
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"
// ""
Remarks
This method determines equality using the default equality comparer xtd::collections::generic::equality_comparer::default_comparer.
This method performs a linear search; therefore, this method is an O(n) operation, where n is xtd::collections::object_model::read_only_collection::count.

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

◆ copy_to()

template<typename type_t >
void xtd::collections::object_model::read_only_collection< type_t >::copy_to ( xtd::array< type_t > &  array,
xtd::size  array_index 
) const
inlineoverridevirtual

Copies the entire xtd::collections::object_model::read_only_collection <type_t> to a compatible one-dimensional Array, starting at the specified index of the target array.

Parameters
arrayThe one-dimensional xtd::array that is the destination of the elements copied from xtd::collections::object_model::read_only_collection <type_t>. The xtd::array must have zero-based indexing.
array_indexThe zero-based index in array at which copying begins.
Exceptions
xtd::argument_exceptionThe number of elements in the source xtd::collections::object_model::read_only_collection <type_t> is greater than the available space from index to the end of the destination array.
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"
// ""
Remarks
This method uses xtd::array::copy to copy the elements.
The elements are copied to the xtd::array in the same order that the enumerator iterates through the xtd::collections::object_model::read_only_collection <type_t>.
This method is an O(n) operation, where n is xtd::collections::object_model::read_only_collection::count.

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

◆ 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"
// ""
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 >.

◆ index_of()

template<typename type_t >
xtd::size xtd::collections::object_model::read_only_collection< type_t >::index_of ( const type_t &  item) const
inlineoverridevirtualnoexcept

Searches for the specified object and returns the zero-based index of the first occurrence within the entire xtd::collections::object_model::read_only_collection <type_t>.

Parameters
itemThe object to locate in the xtd::collections::object_model::read_only_collection <type_t>.
Returns
The zero-based index of the first occurrence of item within the entire xtd::collections::object_model::read_only_collection <type_t>, if found; otherwise, xtd::collections::object_model::read_only_collection::npos.
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"
// ""
Remarks
The xtd::collections::object_model::read_only_collection <type_t> is searched forward starting at the first element and ending at the last element.
This method determines equality using the default comparer xtd::collections::generic::equality_comparer::default_comparer.
This method performs a linear search; therefore, this method is an O(n) operation, where n is xtd::collections::object_model::read_only_collection::count.

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

◆ operator[]()

template<typename type_t >
const_reference xtd::collections::object_model::read_only_collection< type_t >::operator[] ( size_type  index) const
inlineoverridevirtual

Returns a reference to the element at specified location pos.

Parameters
indexThe position of the element to return.
Returns
Reference to the requested element.
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"
// ""
Remarks
Retrieving the value of this property is an O(1) operation.
This property provides the ability to access a specific element in the collection by using the following C++ syntax: my_collection[index].
Retrieving the value of this property is an O(1) operation.

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

◆ items()

template<typename type_t >
base_type xtd::collections::object_model::read_only_collection< type_t >::items ( )
inlineprotectednoexcept

Returns the xtd::collections::generic::ilist <type_t> that the xtd::collections::object_model::read_only_collection <type_t> wraps.

Returns
The xtd::collections::generic::ilist <type_t> that the xtd::collections::object_model::read_only_collection <type_t> wraps.
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"
// ""
Remarks
Retrieving the value of this property is an O(1) operation.

Member Data Documentation

◆ npos

template<typename type_t >
constexpr xtd::size xtd::collections::object_model::read_only_collection< type_t >::npos
inlinestaticconstexpr

This is a special value equal to the maximum value representable by the type xtd::size.


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