iceoryx_doc  1.0.1
cyclic_index.hpp
1 // Copyright (c) 2019, 2020 by Robert Bosch GmbH, Apex.AI Inc. 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 
17 #ifndef IOX_UTILS_LOCKFREE_QUEUE_CYCLIC_INDEX_HPP
18 #define IOX_UTILS_LOCKFREE_QUEUE_CYCLIC_INDEX_HPP
19 
20 #include <limits>
21 #include <stdint.h>
22 #include <type_traits>
23 
24 
25 namespace iox
26 {
27 namespace concurrent
28 {
31 template <uint64_t CycleLength, typename ValueType = uint64_t>
33 {
34  public:
35  static_assert(std::is_unsigned<ValueType>::value, "ValueType must be an unsigned integral type");
36  static_assert(CycleLength >= 1U, "CycleLength must be >= 1");
37 
38  using value_t = ValueType;
39 
40  static constexpr ValueType MAX_INDEX = CycleLength - 1U;
41  static constexpr ValueType MAX_VALUE = std::numeric_limits<ValueType>::max();
42 
43  // assumes MAX_VALUE >= CycleLength, otherwise we could not fit in even one cycle
44  static constexpr ValueType MAX_CYCLE = MAX_VALUE / CycleLength;
45 
46  static constexpr ValueType INDEX_AT_MAX_VALUE = MAX_VALUE % CycleLength;
47  static constexpr ValueType OVERFLOW_START_INDEX = (INDEX_AT_MAX_VALUE + 1U) % CycleLength;
48 
49  static_assert(CycleLength < MAX_VALUE / 2U, "CycleLength is too large, need at least one bit for cycle");
50  static_assert(CycleLength > 0, "CycleLength must be > 0");
51 
52  explicit CyclicIndex(ValueType value = 0U) noexcept;
53 
54  CyclicIndex(ValueType index, ValueType cycle) noexcept;
55 
56  CyclicIndex(const CyclicIndex&) = default;
57  CyclicIndex(CyclicIndex&&) = default;
58  CyclicIndex& operator=(const CyclicIndex&) = default;
59  CyclicIndex& operator=(CyclicIndex&&) = default;
60 
61  ValueType getIndex() const noexcept;
62 
63  ValueType getCycle() const noexcept;
64 
65  ValueType getValue() const noexcept;
66 
67  CyclicIndex operator+(const ValueType value) const noexcept;
68 
69  CyclicIndex next() const noexcept;
70 
71  bool isOneCycleBehind(const CyclicIndex& other) const noexcept;
72 
82  int64_t operator-(const CyclicIndex<CycleLength, ValueType>& rhs) const;
83 
84  private:
85  ValueType m_value{0U};
86 };
87 
88 } // namespace concurrent
89 } // namespace iox
90 
91 #include "cyclic_index.inl"
92 
93 #endif // IOX_UTILS_LOCKFREE_QUEUE_CYCLIC_INDEX_HPP
index structure that can contain logical values 0, ..., CycleLength-1 but also stores an internal cyc...
Definition: cyclic_index.hpp:33
int64_t operator-(const CyclicIndex< CycleLength, ValueType > &rhs) const
Definition: cyclic_index.inl:94
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28