xtd 1.0.0
Loading...
Searching...
No Matches
concurrent_stack.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_stack() = default;
56 concurrent_stack(std::initializer_list<type_t> items) {
57 for (const auto& item : items)
58 push(item);
59 }
60
62 for (const auto& item : collection)
63 push(item);
64 }
65
67 template<xtd::iterable iterable_t>
68 concurrent_stack(iterable_t&& items) {
69 for (const auto& item : items)
70 push(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_empty() const noexcept -> bool {
89 lock_guard_(items_)
90 return !items_.count();
91 return {};
92 }
93
97 [[nodiscard]] auto is_read_only() const noexcept -> bool override {return false;}
98
113 [[nodiscard]] auto is_synchronized() const noexcept -> bool override {return true;}
115
117
119 auto clear() -> void override {
120 lock_guard_(items_)
121 items_.clear();
122 }
123
128 auto copy_to(xtd::array<value_type>& array, xtd::usize array_index) const -> void override {
129 lock_guard_(items_)
130 items_.copy_to(array, array_index);
131 }
132
136 virtual auto push(const_reference item) -> void {
137 lock_guard_(items_)
138 items_.push(item);
139 }
140
144 template <xtd::iterable iterable_t>
145 auto push_range(const iterable_t& items) -> void {
146 push_range(items, 0, items.size());
147 }
148
155 template <xtd::iterable iterable_t>
156 auto push_range(const iterable_t& items, xtd::usize start_index, xtd::usize count) -> void {
158 lock_guard_(items_)
159 std::for_each(items.begin()+start_index, items.begin()+start_index+count, [this](auto&& item) {items_.push(item);});
160 }
161
165 [[nodiscard]] auto get_enumerator() const -> xtd::collections::generic::enumerator<value_type> override {
166 static thread_local auto items = xtd::array<value_type> {};
167 items = to_array();
168 return items.get_enumerator();
169 }
170
174 [[nodiscard]] auto to_array() const -> xtd::array<value_type> override {
175 lock_guard_(items_)
176 return items_.to_array();
177 return {};
178 }
179
182 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {
183 return to_array().to_string();
184 }
185
189 virtual auto try_pop(reference result) -> bool {
190 lock_guard_(items_)
191 return items_.try_pop(result);
192 return false;
193 }
194
199 template <xtd::iterable iterable_t>
200 auto try_pop_range(iterable_t& items) -> xtd::usize {
201 return try_pop_range(items, 0, items.size());
202 }
203
211 template <xtd::iterable iterable_t>
212 auto try_pop_range(iterable_t& items, xtd::usize start_index, xtd::usize count) -> xtd::usize {
214 auto result = xtd::usize {0};
215 lock_guard_(items_)
216 for (auto index = xtd::usize {0}; index < count; ++index) {
217 if (!items_.try_pop(*(items.begin() + start_index + index))) return result;
218 ++result;
219 }
220 return result;
221 }
222
226 auto try_peek(reference result) const -> bool {
227 lock_guard_(items_)
228 return items_.try_peek(result);
229 return false;
230 }
231
232
233 private:
234 auto add(const_reference item) -> void override {push(item);}
235 [[nodiscard]] auto contains(const_reference item) const noexcept -> bool override {return false;}
236 auto remove(const_reference item) -> bool override {return false;}
237 [[nodiscard]] auto sync_root() const noexcept -> const object& override {return items_;}
238 auto try_add(const_reference item) -> bool override {return false;}
239 auto try_take(reference item) -> bool override {return try_pop(item);}
240
241 collection_type items_;
242 };
243
245 // Deduction guides for xtd::collections::concurrent::concurrent_stack
246 // {
247 template<typename type_t>
248 concurrent_stack(xtd::collections::generic::ienumerable<type_t>) -> concurrent_stack<type_t>;
249
250 template<xtd::iterable iterable_t>
252
253 template<typename type_t>
254 concurrent_stack(std::initializer_list<type_t>) -> concurrent_stack<type_t>;
255 // }
257 }
258 }
259}
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
Represents a thread-safe last in-first out (LIFO) collection.
Definition concurrent_stack.hpp:34
auto try_pop_range(iterable_t &items) -> xtd::usize
Attempts to pop and return multiple objects from the top of the xtd::collections::concurrent::concurr...
Definition concurrent_stack.hpp:200
auto try_peek(reference result) const -> bool
Tries to return an object from the beginning of the xtd::collections::concurrent::concurrent_stack <t...
Definition concurrent_stack.hpp:226
value_type & reference
Represents the reference of list value type.
Definition concurrent_stack.hpp:42
auto to_array() const -> xtd::array< value_type > override
Copies the elements contained in the xtd::collections::concurrent::concurrent_stack <type_t> to a new...
Definition concurrent_stack.hpp:174
concurrent_stack(std::initializer_list< type_t > items)
Constructs the container with the contents of the specified initializer list, and allocator.
Definition concurrent_stack.hpp:56
auto is_read_only() const noexcept -> bool override
Gets a value indicating whether the xtd::collections::concurrent::concurrent_stack <type_t> is read-o...
Definition concurrent_stack.hpp:97
virtual auto try_pop(reference result) -> bool
Tries to remove and return the object at the beginning of the concurrent stack.
Definition concurrent_stack.hpp:189
auto is_empty() const noexcept -> bool
Gets a value that indicates whether the xtd::collections::concurrent::concurrent_stack <type_t> is em...
Definition concurrent_stack.hpp:88
xtd::collections::generic::stack< value_type > collection_type
Represents the concurrent bag collection type.
Definition concurrent_stack.hpp:46
typename iproducer_consumer_collection< type_t >::value_type value_type
Represents the concurrent bag value type.
Definition concurrent_stack.hpp:40
concurrent_stack()=default
Initializes a new instance of the xtd::collections::concurrent::concurrent_stack <type_t> class.
auto count() const noexcept -> xtd::usize override
Gets the number of elements contained in the xtd::collections::generic::icollection.
Definition concurrent_stack.hpp:79
auto try_pop_range(iterable_t &items, xtd::usize start_index, xtd::usize count) -> xtd::usize
Attempts to pop and return multiple objects from the top of the xtd::collections::concurrent::concurr...
Definition concurrent_stack.hpp:212
concurrent_stack(const xtd::collections::generic::ienumerable< value_type > &collection)
Initializes a new instance of the xtd::collections::concurrent::concurrent_stack <type_t> class that ...
Definition concurrent_stack.hpp:61
auto is_synchronized() const noexcept -> bool override
Gets a value indicating whether access to the xtd::collections::concurrent::concurrent_stack <type_t>...
Definition concurrent_stack.hpp:113
auto copy_to(xtd::array< value_type > &array, xtd::usize array_index) const -> void override
Copies the elements of the xtd::collections::concurrent::concurrent_stack <type_t> to an xtd::array,...
Definition concurrent_stack.hpp:128
auto clear() -> void override
Removes all items from the xtd::collections::generic::icollection <type_t>.
Definition concurrent_stack.hpp:119
concurrent_stack(iterable_t &&items)
Initializes a new instance of the xtd::iterable that contains elements copied from the specified coll...
Definition concurrent_stack.hpp:68
const value_type & const_reference
Represents the const reference of list value type.
Definition concurrent_stack.hpp:44
auto push_range(const iterable_t &items, xtd::usize start_index, xtd::usize count) -> void
Inserts multiple objects at the top of the xtd::collections::concurrent::concurrent_stack <type_t> at...
Definition concurrent_stack.hpp:156
auto push_range(const iterable_t &items) -> void
Inserts multiple objects at the top of the xtd::collections::concurrent::concurrent_stack <type_t> at...
Definition concurrent_stack.hpp:145
virtual auto push(const_reference item) -> void
Adds an item to the end of the xtd::collections::concurrent::concurrent_stack <type_t>.
Definition concurrent_stack.hpp:136
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current object.
Definition concurrent_stack.hpp:182
auto get_enumerator() const -> xtd::collections::generic::enumerator< value_type > override
Returns an enumerator that iterates through a collection.
Definition concurrent_stack.hpp:165
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 variable size last-in-first-out (LIFO) collection of instances of the same specified typ...
Definition stack.hpp:47
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:40
Contains xtd::collections::generic::stack <value_t> class.
generic::enumerator< xtd::any_object > enumerator
Supports a simple iteration over a non-generic collection.
Definition enumerator.hpp:28
@ argument_out_of_range
The argument is out of range.
Definition exception_case.hpp:35
#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:75
Contains xtd::object class.
Represents a type that can be used to index a collection either from the beginning or the end.
Definition index.hpp:38
Contains xtd::threading::lock_guard class.
Contains __ and unused_ keywords.