iceoryx_doc  1.0.1
sofi.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // SPDX-License-Identifier: Apache-2.0
16 #ifndef IOX_UTILS_CONCURRENT_SOFI_HPP
17 #define IOX_UTILS_CONCURRENT_SOFI_HPP
18 
19 #include "iceoryx_utils/platform/platform_correction.hpp"
20 
21 #include <atomic>
22 #include <cstdint>
23 #include <cstring>
24 #include <functional>
25 
26 namespace iox
27 {
28 namespace concurrent
29 {
44 template <class ValueType, uint64_t CapacityValue>
45 class SoFi
46 {
47  static_assert(std::is_trivially_copyable<ValueType>::value, "SoFi can handle only trivially copyable data types");
52  static_assert(2 <= ATOMIC_INT_LOCK_FREE, "SoFi is not able to run lock free on this data type");
53 
56  static constexpr uint32_t INTERNAL_SIZE_ADD_ON = 1;
57 
59  static constexpr uint32_t INTERNAL_SOFI_SIZE = CapacityValue + INTERNAL_SIZE_ADD_ON;
60 
61  public:
63  SoFi() noexcept;
64 
100  bool push(const ValueType& valueOut, ValueType& f_paramOut_r) noexcept;
101 
107  bool pop(ValueType& valueOut) noexcept;
108 
127  template <typename Verificator_T>
128  bool popIf(ValueType& valueOut, const Verificator_T& verificator) noexcept;
129 
135  bool empty() const noexcept;
136 
142  bool setCapacity(const uint64_t newSize) noexcept;
143 
146  uint64_t capacity() const noexcept;
147 
150  uint64_t size() const noexcept;
151 
152  private:
153  ValueType m_data[INTERNAL_SOFI_SIZE];
154  uint64_t m_size = INTERNAL_SOFI_SIZE;
155 
158  std::atomic<uint64_t> m_readPosition{0u};
159  std::atomic<uint64_t> m_writePosition{0u};
160 };
161 
162 } // namespace concurrent
163 } // namespace iox
164 
165 #include "iceoryx_utils/internal/concurrent/sofi.inl"
166 
167 #endif // IOX_UTILS_CONCURRENT_SOFI_HPP
Thread safe producer and consumer queue with a safe overflowing behavior. SoFi is designed in a FIFO ...
Definition: sofi.hpp:46
bool empty() const noexcept
returns true if sofi is empty, otherwise false
Definition: sofi.inl:67
bool pop(ValueType &valueOut) noexcept
pop the oldest element
Definition: sofi.inl:87
SoFi() noexcept
default constructor which constructs an empty sofi
Definition: sofi.inl:24
uint64_t size() const noexcept
returns the current size of sofi
Definition: sofi.inl:35
bool popIf(ValueType &valueOut, const Verificator_T &verificator) noexcept
conditional pop call to provide an alternative for a peek and pop approach. If the verificator return...
Definition: sofi.inl:94
bool push(const ValueType &valueOut, ValueType &f_paramOut_r) noexcept
pushs an element into sofi. if sofi is full the oldest data will be returned and the pushed element i...
Definition: sofi.inl:144
uint64_t capacity() const noexcept
returns the capacity of sofi
Definition: sofi.inl:29
bool setCapacity(const uint64_t newSize) noexcept
resizes sofi
Definition: sofi.inl:50
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28