xtd 1.0.0
Loading...
Searching...
No Matches
xtd::collections::concurrent::concurrent_bag< type_t > Class Template Reference
Inheritance diagram for xtd::collections::concurrent::concurrent_bag< type_t >:
xtd::object xtd::collections::concurrent::iproducer_consumer_collection< type_t >

Definition

template<typename type_t>
class xtd::collections::concurrent::concurrent_bag< type_t >

Represents a thread-safe, unordered collection of objects.

Definition
template<typename type_t>
concurrent_bag()=default
Initializes a new instance of the xtd::collections::concurrent::concurrent_bag <type_t> class.
Defines methods to manipulate thread-safe collections intended for producer/consumer usage....
Definition iproducer_consumer_collection.hpp:30
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
Header
#include <xtd/collections/concurrent/iproducer_consumer_collection>
Namespace
xtd::collections::concurrent
Library
xtd.core

Public Aliases

using value_type
 Represents the concurrent bag value type.
using reference
 Represents the reference of list value type.
using const_reference
 Represents the const reference of list value type.
using collection_type
 Represents the concurrent bag collection type.
using local_storage_type
 Represents the concurrent bag collection type.
using storage_type
 Represents the concurrent bag collection type.

Public Methods

auto add (const_reference item) -> void override
 Adds an item to the xtd::collections::concurrent::concurrent_bag <type_t>.
auto copy_to (xtd::array< value_type > &array, xtd::usize array_index) const -> void override
 Copies the elements of the xtd::collections::concurrent::concurrent_bag <type_t> to an xtd::array, starting at a particular xtd::array index.
auto get_enumerator () const -> xtd::collections::generic::enumerator< value_type > override
 Returns an enumerator that iterates through a collection.
auto to_array () const -> xtd::array< value_type > override
 Copies the elements contained in the xtd::collections::concurrent::concurrent_bag <type_t> to a new array.
auto to_string () const noexcept -> xtd::string override
 Returns a xtd::string that represents the current object.
auto try_add (const_reference item) -> bool override
 Attempts to add an object to the xtd::collections::concurrent::concurrent_bag <type_t>.
auto try_peek (reference item) const -> bool
 Attempts to return an object from the xtd::collections::concurrent::concurrent_bag <type_t> without removing it.
auto try_take (reference item) -> bool override
 Attempts to remove and return an object from the xtd::collections::concurrent::concurrent <type_t>.

Public Conctructors

 concurrent_bag ()=default
 Initializes a new instance of the xtd::collections::concurrent::concurrent_bag <type_t> class.
 concurrent_bag (std::initializer_list< type_t > items)
 Constructs the container with the contents of the specified initializer list, and allocator.
 concurrent_bag (const xtd::collections::generic::ienumerable< value_type > &collection)
 Initializes a new instance of the xtd::collections::concurrent::concurrent_bag <type_t> class that contains elements copied from the specified collection.

Public Properties

auto count () const noexcept -> xtd::usize override
 Gets the number of elements contained in the xtd::collections::generic::icollection.
auto is_empty () const noexcept -> bool
 Gets a value indicating whether the xtd::collections::concurrent::concurrent_bag <type_t> is emtpy.
auto is_read_only () const noexcept -> bool override
 Gets a value indicating whether the xtd::collections::concurrent::concurrent_bag <type_t> is read-only.
auto is_synchronized () const noexcept -> bool override
 Gets a value indicating whether access to the xtd::collections::concurrent::concurrent_bag <type_t> is synchronized (thread safe).

Additional Inherited Members

 object ()=default
 Create a new instance of the ultimate base class object.
virtual auto equals (const object &obj) const noexcept -> bool
 Determines whether the specified object is equal to the current object.
virtual auto get_hash_code () const noexcept -> xtd::usize
 Serves as a hash function for a particular type.
virtual auto get_type () const noexcept -> type_object
 Gets the type of the current instance.
template<typename object_t>
auto memberwise_clone () const -> xtd::unique_ptr_object< object_t >
 Creates a shallow copy of the current object.
virtual auto copy_to (xtd::array< type_t > &array, xtd::usize array_index) const -> void=0
 Copies the elements of the xtd::collections::generic::icollection <type_t> to an xtd::array, starting at a particular xtd::array index.
virtual auto try_add (const type_t &item) -> bool=0
 Attempts to add an object to the xtd::collections::concurrent::iproducer_consumer_collection <type_t>.
virtual auto try_take (type_t &item) -> bool=0
 Attempts to remove and return an object from the xtd::collections::concurrent::iproducer_consumer_collection <type_t>.
template<typename object_a_t, typename object_b_t>
static auto equals (const object_a_t &object_a, const object_b_t &object_b) noexcept -> bool
 Determines whether the specified object instances are considered equal.
template<typename object_a_t, typename object_b_t>
static auto reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept -> bool
 Determines whether the specified object instances are the same instance.

Member Typedef Documentation

◆ value_type

template<typename type_t>
using xtd::collections::concurrent::concurrent_bag< type_t >::value_type

Represents the concurrent bag value type.

◆ reference

template<typename type_t>
using xtd::collections::concurrent::concurrent_bag< type_t >::reference

Represents the reference of list value type.

◆ const_reference

template<typename type_t>
using xtd::collections::concurrent::concurrent_bag< type_t >::const_reference

Represents the const reference of list value type.

◆ collection_type

template<typename type_t>
using xtd::collections::concurrent::concurrent_bag< type_t >::collection_type

Represents the concurrent bag collection type.

◆ local_storage_type

template<typename type_t>
using xtd::collections::concurrent::concurrent_bag< type_t >::local_storage_type

Represents the concurrent bag collection type.

◆ storage_type

template<typename type_t>
using xtd::collections::concurrent::concurrent_bag< type_t >::storage_type

Represents the concurrent bag collection type.

Constructor & Destructor Documentation

◆ concurrent_bag() [1/3]

template<typename type_t>
xtd::collections::concurrent::concurrent_bag< type_t >::concurrent_bag ( )
default

Initializes a new instance of the xtd::collections::concurrent::concurrent_bag <type_t> class.

◆ concurrent_bag() [2/3]

template<typename type_t>
xtd::collections::concurrent::concurrent_bag< type_t >::concurrent_bag ( std::initializer_list< type_t > items)
inline

Constructs the container with the contents of the specified initializer list, and allocator.

Parameters
itemsThe initializer list to initialize the elements of the container with.

◆ concurrent_bag() [3/3]

template<typename type_t>
xtd::collections::concurrent::concurrent_bag< type_t >::concurrent_bag ( const xtd::collections::generic::ienumerable< value_type > & collection)
inline

Initializes a new instance of the xtd::collections::concurrent::concurrent_bag <type_t> class that contains elements copied from the specified collection.

Member Function Documentation

◆ count()

template<typename type_t>
auto xtd::collections::concurrent::concurrent_bag< type_t >::count ( ) const -> xtd::usize
inlinenodiscardoverridevirtualnoexcept

Gets the number of elements contained in the xtd::collections::generic::icollection.

Returns
The number of elements contained in the xtd::collections::generic::icollection.

Implements xtd::collections::concurrent::iproducer_consumer_collection< type_t >.

◆ is_empty()

template<typename type_t>
auto xtd::collections::concurrent::concurrent_bag< type_t >::is_empty ( ) const -> bool
inlinenodiscardnoexcept

Gets a value indicating whether the xtd::collections::concurrent::concurrent_bag <type_t> is emtpy.

Returns
true if the xtd::collections::concurrent::concurrent_bag <type_t> is empty; otherwise, false.

◆ is_read_only()

template<typename type_t>
auto xtd::collections::concurrent::concurrent_bag< type_t >::is_read_only ( ) const -> bool
inlinenodiscardoverridenoexcept

Gets a value indicating whether the xtd::collections::concurrent::concurrent_bag <type_t> is read-only.

Returns
true if the xtd::collections::concurrent::concurrent_bag <type_t> is read-only; otherwise, false.
Remarks
A collection that is read-only does not allow the addition or removal of elements after the collection is created. Note that read-only in this context does not indicate whether individual elements of the collection can be modified, since the xtd::collections::concurrent::concurrent_bag <type_t> interface only supports addition and removal operations. For example, the xtd::collections::concurrent::concurrent_bag::is_read_only property of an array that is cast or converted to an xtd::collections::concurrent::concurrent_bag <type_t> object returns true, even though individual array elements can be modified.

◆ is_synchronized()

template<typename type_t>
auto xtd::collections::concurrent::concurrent_bag< type_t >::is_synchronized ( ) const -> bool
inlinenodiscardoverridevirtualnoexcept

Gets a value indicating whether access to the xtd::collections::concurrent::concurrent_bag <type_t> is synchronized (thread safe).

Returns
true if access to the ICollection is synchronized (thread safe); otherwise, false.
Remarks
xtd::collections::concurrent::iproducer_consumer_collection::sync_root returns an object, which can be used to synchronize access to the xtd::collections::generic::icollection.
Most collection classes in the xtd::collections namespace also implement a Synchronized method, which provides a synchronized wrapper around the underlying collection.
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.
The following code example shows how to lock the collection using the xtd::collections::concurrent::iproducer_consumer_collection::sync_root property during the entire enumeration.
icollection& my_collection = some_collection;
lock(my_collection.sync_root()) {
for (const auto& item : my_collection) {
// Insert your code here.
}
}
generic::icollection< xtd::any_object > icollection
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition icollection.hpp:32

Implements xtd::collections::concurrent::iproducer_consumer_collection< type_t >.

◆ add()

template<typename type_t>
auto xtd::collections::concurrent::concurrent_bag< type_t >::add ( const_reference item) -> void
inlineoverride

Adds an item to the xtd::collections::concurrent::concurrent_bag <type_t>.

Parameters
itemThe object to add to the xtd::collections::concurrent::concurrent_bag <type_t>.
Exceptions
xtd::not_supported_exceptionThe xtd::collections::concurrent::concurrent_bag <type_t> is read-only.

◆ copy_to()

template<typename type_t>
auto xtd::collections::concurrent::concurrent_bag< type_t >::copy_to ( xtd::array< value_type > & array,
xtd::usize array_index ) const -> void
inlineoverride

Copies the elements of the xtd::collections::concurrent::concurrent_bag <type_t> to an xtd::array, starting at a particular xtd::array index.

Parameters
arrayThe one-dimensional xtd::array that is the destination of the elements copied from xtd::collections::concurrent::concurrent_bag <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::concurrent::concurrent_bag <type_t> is greater than the available space from `array_index` to the end of the destination `array`.

◆ get_enumerator()

template<typename type_t>
auto xtd::collections::concurrent::concurrent_bag< type_t >::get_enumerator ( ) const -> xtd::collections::generic::enumerator< value_type >
inlinenodiscardoverridevirtual

Returns an enumerator that iterates through a collection.

Returns
An xtd::collections::generic::enumerator object that can be used to iterate through the collection.

Implements xtd::collections::concurrent::iproducer_consumer_collection< type_t >.

◆ to_array()

template<typename type_t>
auto xtd::collections::concurrent::concurrent_bag< type_t >::to_array ( ) const -> xtd::array< value_type >
inlinenodiscardoverridevirtual

Copies the elements contained in the xtd::collections::concurrent::concurrent_bag <type_t> to a new array.

Returns
A new array containing the elements copied from the xtd::collections::concurrent::iproducer_consumer_collection <type_t.
Remarks
The method provides a snapshot of the underlying collection. It is possible for other threads to add or remove items immediately after the array is made.

Implements xtd::collections::concurrent::iproducer_consumer_collection< type_t >.

◆ to_string()

template<typename type_t>
auto xtd::collections::concurrent::concurrent_bag< type_t >::to_string ( ) const -> xtd::string
inlinenodiscardoverridevirtualnoexcept

Returns a xtd::string that represents the current object.

Returns
A string that represents the current object.

Reimplemented from xtd::object.

◆ try_add()

template<typename type_t>
auto xtd::collections::concurrent::concurrent_bag< type_t >::try_add ( const_reference item) -> bool
inlineoverride

Attempts to add an object to the xtd::collections::concurrent::concurrent_bag <type_t>.

Parameters
itemThe object to add to the xtd::collections::concurrent::iproducer_consumer_collection <type_t>.
Returns
true if the object was added successfully; otherwise, false. @exceprion xtd::argument_exception The item was invalid for this collection.

◆ try_peek()

template<typename type_t>
auto xtd::collections::concurrent::concurrent_bag< type_t >::try_peek ( reference item) const -> bool
inline

Attempts to return an object from the xtd::collections::concurrent::concurrent_bag <type_t> without removing it.

Parameters
itemWhen this method returns, result contains an object from the ConcurrentBag<T> or the default value of T if the operation failed.
Returns
true if an object was returned successfully; otherwise, false.

◆ try_take()

template<typename type_t>
auto xtd::collections::concurrent::concurrent_bag< type_t >::try_take ( reference item) -> bool
inlineoverride

Attempts to remove and return an object from the xtd::collections::concurrent::concurrent <type_t>.

Parameters
itemWhen this method returns, if the object was removed and returned successfully, item contains the removed object. If no object was available to be removed, the value is unspecified.
Returns
true if an object was removed and returned successfully; otherwise, false.

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