iceoryx_doc  1.0.1
fifo.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 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_CONCURRENT_FIFO_HPP
18 #define IOX_UTILS_CONCURRENT_FIFO_HPP
19 
20 #include "iceoryx_utils/cxx/optional.hpp"
21 
22 #include <atomic>
23 
24 namespace iox
25 {
26 namespace concurrent
27 {
29 template <typename ValueType, uint64_t Capacity>
30 class FiFo
31 {
32  public:
36  bool push(const ValueType& f_value) noexcept;
37 
41  cxx::optional<ValueType> pop() noexcept;
42 
44  bool empty() const noexcept;
45 
47  uint64_t size() const noexcept;
48 
50  static constexpr uint64_t capacity() noexcept;
51 
52  private:
53  bool is_full() const noexcept;
54 
55  private:
56  ValueType m_data[Capacity];
57  std::atomic<uint64_t> m_write_pos{0};
58  std::atomic<uint64_t> m_read_pos{0};
59 };
60 
61 } // namespace concurrent
62 } // namespace iox
63 
64 #include "iceoryx_utils/internal/concurrent/fifo.inl"
65 
66 #endif // IOX_UTILS_CONCURRENT_FIFO_HPP
single pusher single pop'er thread safe fifo
Definition: fifo.hpp:31
uint64_t size() const noexcept
returns the size of the fifo
Definition: fifo.inl:54
static constexpr uint64_t capacity() noexcept
returns the capacity of the fifo
Definition: fifo.inl:60
bool empty() const noexcept
returns true when the fifo is empty, otherwise false
Definition: fifo.inl:66
cxx::optional< ValueType > pop() noexcept
returns the oldest value from the fifo and removes it
Definition: fifo.inl:72
bool push(const ValueType &f_value) noexcept
pushes a value into the fifo
Definition: fifo.inl:27
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