xtd 1.0.0
Loading...
Searching...
No Matches
concurrent_queue.hpp
Go to the documentation of this file.
1
4#pragma once
8#include "../../object.hpp"
9#include "../../unused.hpp"
10
12namespace xtd {
14 namespace collections {
17 namespace concurrent {
33 template<typename type_t>
35 public:
37
48
50
53 concurrent_queue() = default;
56 concurrent_queue(std::initializer_list<type_t> items) {
57 for (const auto& item : items)
58 enqueue(item);
59 }
60
62 for (const auto& item : collection)
63 enqueue(item);
64 }
65
67 template<xtd::iterable iterable_t>
68 concurrent_queue(iterable_t&& items) {
69 for (const auto& item : items)
70 enqueue(item);
71 }
72
73
75
79 [[nodiscard]] auto count() const noexcept -> xtd::usize override {
80 lock_guard_(items_)
81 return items_.count();
82 return {};
83 }
84
88 [[nodiscard]] auto is_read_only() const noexcept -> bool override {return false;}
89
104 [[nodiscard]] auto is_synchronized() const noexcept -> bool override {return true;}
106
108
110 auto clear() -> void override {
111 lock_guard_(items_)
112 items_.clear();
113 }
114
119 auto copy_to(xtd::array<value_type>& array, xtd::usize array_index) const -> void override {
120 lock_guard_(items_)
121 items_.copy_to(array, array_index);
122 }
123
127 virtual auto enqueue(const_reference item) -> void {
128 lock_guard_(items_)
129 items_.enqueue(item);
130 }
131
135 [[nodiscard]] auto get_enumerator() const -> xtd::collections::generic::enumerator<value_type> override {
136 static thread_local auto items = xtd::array<value_type> {};
137 items = to_array();
138 return items.get_enumerator();
139 }
140
144 [[nodiscard]] auto to_array() const -> xtd::array<value_type> override {
145 lock_guard_(items_)
146 return items_.to_array();
147 return {};
148 }
149
152 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {
153 return to_array().to_string();
154 }
155
159 virtual auto try_dequeue(reference result) -> bool {
160 lock_guard_(items_)
161 return items_.try_dequeue(result);
162 return false;
163 }
164
168 auto try_peek(reference result) const -> bool {
169 lock_guard_(items_)
170 return items_.try_peek(result);
171 return false;
172 }
173
174
175 private:
176 auto add(const_reference item) -> void override {enqueue(item);}
177 [[nodiscard]] auto contains(const_reference item) const noexcept -> bool override {return false;}
178 auto remove(const_reference item) -> bool override {return false;}
179 [[nodiscard]] auto sync_root() const noexcept -> const object& override {return items_;}
180 auto try_add(const_reference item) -> bool override {return false;}
181 auto try_take(reference item) -> bool override {return try_dequeue(item);}
182
183 collection_type items_;
184 };
185
187 // Deduction guides for xtd::collections::generic::hash_set
188 // {
189 template<typename type_t>
190 concurrent_queue(xtd::collections::generic::ienumerable<type_t>) -> concurrent_queue<type_t>;
191
192 template<xtd::iterable iterable_t>
194
195 template<typename type_t>
196 concurrent_queue(std::initializer_list<type_t>) -> concurrent_queue<type_t>;
197 // }
199 }
200 }
201}
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
Represents a thread-safe first in-first out (FIFO) collection.
Definition concurrent_queue.hpp:34
auto is_synchronized() const noexcept -> bool override
Gets a value indicating whether access to the xtd::collections::concurrent::concurrent_queue <type_t>...
Definition concurrent_queue.hpp:104
auto try_peek(reference result) const -> bool
Tries to return an object from the beginning of the xtd::collections::concurrent::concurrent_queue <t...
Definition concurrent_queue.hpp:168
const value_type & const_reference
Represents the const reference of list value type.
Definition concurrent_queue.hpp:44
value_type & reference
Represents the reference of list value type.
Definition concurrent_queue.hpp:42
auto is_read_only() const noexcept -> bool override
Gets a value indicating whether the xtd::collections::concurrent::concurrent_queue <type_t> is read-o...
Definition concurrent_queue.hpp:88
typename iproducer_consumer_collection< type_t >::value_type value_type
Represents the concurrent bag value type.
Definition concurrent_queue.hpp:40
auto count() const noexcept -> xtd::usize override
Gets the number of elements contained in the xtd::collections::generic::icollection.
Definition concurrent_queue.hpp:79
xtd::collections::generic::queue< value_type > collection_type
Represents the concurrent bag collection type.
Definition concurrent_queue.hpp:46
auto get_enumerator() const -> xtd::collections::generic::enumerator< value_type > override
Returns an enumerator that iterates through a collection.
Definition concurrent_queue.hpp:135
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current object.
Definition concurrent_queue.hpp:152
concurrent_queue(std::initializer_list< type_t > items)
Constructs the container with the contents of the specified initializer list, and allocator.
Definition concurrent_queue.hpp:56
concurrent_queue(const xtd::collections::generic::ienumerable< value_type > &collection)
Initializes a new instance of the xtd::collections::concurrent::concurrent_queue <type_t> class that ...
Definition concurrent_queue.hpp:61
auto to_array() const -> xtd::array< value_type > override
Copies the elements contained in the xtd::collections::concurrent::concurrent_queue <type_t> to a new...
Definition concurrent_queue.hpp:144
virtual auto enqueue(const_reference item) -> void
Adds an item to the end of the xtd::collections::concurrent::concurrent_queue <type_t>.
Definition concurrent_queue.hpp:127
concurrent_queue(iterable_t &&items)
Initializes a new instance of the xtd::iterable that contains elements copied from the specified coll...
Definition concurrent_queue.hpp:68
virtual auto try_dequeue(reference result) -> bool
Tries to remove and return the object at the beginning of the concurrent queue.
Definition concurrent_queue.hpp:159
auto clear() -> void override
Removes all items from the xtd::collections::generic::icollection <type_t>.
Definition concurrent_queue.hpp:110
auto copy_to(xtd::array< value_type > &array, xtd::usize array_index) const -> void override
Copies the elements of the xtd::collections::concurrent::concurrent_queue <type_t> to an xtd::array,...
Definition concurrent_queue.hpp:119
concurrent_queue()=default
Initializes a new instance of the xtd::collections::concurrent::concurrent_queue <type_t> class.
Defines methods to manipulate thread-safe collections intended for producer/consumer usage....
Definition iproducer_consumer_collection.hpp:30
virtual auto sync_root() const noexcept -> const object &=0
Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollecti...
virtual auto to_array() const -> xtd::array< type_t >=0
Copies the elements contained in the xtd::collections::concurrent::iproducer_consumer_collection <typ...
typename xtd::collections::generic::ienumerable< type_t >::value_type value_type
Represents the xtd::collections::generic::icollection value type.
Definition icollection.hpp:51
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:40
Represents a first-in, first-out collection of objects.
Definition queue.hpp:47
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
Contains xtd::collections::generic::queue <value_t> class.
generic::enumerator< xtd::any_object > enumerator
Supports a simple iteration over a non-generic collection.
Definition enumerator.hpp:28
#define lock_guard_(object)
The lock_guard_ keyword marks a statement block_guard as a critical section by obtaining the mutual-e...
Definition lock_guard.hpp:65
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
Contains xtd::collections::concurrent::iproducer_consumer_collection <type_t> interface.
Provides several thread-safe collection classes that should be used in place of the corresponding typ...
Definition concurrent_bag.hpp:20
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
The xtd::collections namespace contains interfaces and classes that define various collections of obj...
Definition any_pair.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
const type_t & const_reference
Represents the read_only_span const reference type.
Definition read_only_span.hpp:68
Contains xtd::object class.
Contains xtd::threading::lock_guard class.
Contains __ and unused_ keywords.