iceoryx_doc  1.0.1
variant_queue.hpp
1 // Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2020 - 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 #ifndef IOX_UTILS_CXX_VARIANT_QUEUE_HPP
18 #define IOX_UTILS_CXX_VARIANT_QUEUE_HPP
19 
20 #include "iceoryx_utils/concurrent/resizeable_lockfree_queue.hpp"
21 #include "iceoryx_utils/cxx/expected.hpp"
22 #include "iceoryx_utils/cxx/optional.hpp"
23 #include "iceoryx_utils/cxx/variant.hpp"
24 #include "iceoryx_utils/internal/concurrent/fifo.hpp"
25 #include "iceoryx_utils/internal/concurrent/sofi.hpp"
26 
27 #include <cstdint>
28 
29 namespace iox
30 {
31 namespace cxx
32 {
39 enum class VariantQueueTypes : uint64_t
40 {
41  FiFo_SingleProducerSingleConsumer = 0,
42  SoFi_SingleProducerSingleConsumer = 1,
43  FiFo_MultiProducerSingleConsumer = 2,
44  SoFi_MultiProducerSingleConsumer = 3
45 };
46 
47 // remark: we need to consider to support the non-resizable queue as well
48 // since it should have performance benefits if resize is not actually needed
49 // for now we just use the most general variant, which allows resizing
50 
69 template <typename ValueType, uint64_t Capacity>
71 {
72  public:
77 
80  VariantQueue(const VariantQueueTypes type) noexcept;
81 
87  optional<ValueType> push(const ValueType& value) noexcept;
88 
92  optional<ValueType> pop() noexcept;
93 
95  bool empty() const noexcept;
96 
100  uint64_t size() noexcept;
101 
110  bool setCapacity(const uint64_t newCapacity) noexcept;
111 
114  uint64_t capacity() const noexcept;
115 
124  fifo_t& getUnderlyingFiFo() noexcept;
125 
126  private:
127  VariantQueueTypes m_type;
128  fifo_t m_fifo;
129 };
130 } // namespace cxx
131 } // namespace iox
132 
133 #include "iceoryx_utils/internal/cxx/variant_queue.inl"
134 
135 #endif // IOX_UTILS_CXX_VARIANT_QUEUE_HPP
implements a lock free queue (i.e. container with FIFO order) of elements of type T with a maximum ca...
Definition: resizeable_lockfree_queue.hpp:48
Thread safe producer and consumer queue with a safe overflowing behavior. SoFi is designed in a FIFO ...
Definition: sofi.hpp:46
wrapper of multiple fifo's
Definition: variant_queue.hpp:71
fifo_t & getUnderlyingFiFo() noexcept
returns reference to the underlying fifo
Definition: variant_queue.inl:250
optional< ValueType > pop() noexcept
pops an element from the fifo
Definition: variant_queue.inl:94
uint64_t capacity() const noexcept
get the capacity of the queue.
Definition: variant_queue.inl:217
optional< ValueType > push(const ValueType &value) noexcept
pushs an element into the fifo
Definition: variant_queue.inl:53
bool setCapacity(const uint64_t newCapacity) noexcept
set the capacity of the queue
Definition: variant_queue.inl:188
VariantQueue(const VariantQueueTypes type) noexcept
Constructor of a VariantQueue.
Definition: variant_queue.inl:27
uint64_t size() noexcept
get the current size of the queue. Caution, another thread can have changed the size just after readi...
Definition: variant_queue.inl:155
bool empty() const noexcept
returns true if empty otherwise true
Definition: variant_queue.inl:126
Optional implementation from the C++17 standard with C++11. The interface is analog to the C++17 stan...
Definition: optional.hpp:63
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28