iceoryx_doc  1.0.1
fixed_size_container.hpp
1 // Copyright (c) 2019, 2021 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 #ifndef IOX_POSH_ROUDI_INTROSPECTION_FIXED_SIZE_CONTAINER_HPP
17 #define IOX_POSH_ROUDI_INTROSPECTION_FIXED_SIZE_CONTAINER_HPP
18 
19 #include "iceoryx_utils/cxx/vector.hpp"
20 
21 #include <cstdint>
22 
23 
24 namespace iox
25 {
26 namespace roudi
27 {
33 
37 template <typename T, uint32_t capacity = 1U>
39 {
40  public:
41  using Index_t = int32_t;
42  using Capacity_t = decltype(capacity);
43  static constexpr int32_t NOT_AN_ELEMENT = -1;
44 
46  : m_values(capacity)
47  {
48  }
49 
53  Index_t add(const T& element)
54  {
55  auto nextElement = nextFree();
56 
57  if (nextElement >= 0)
58  {
59  m_freeIndex = nextElement;
60  m_values[static_cast<Capacity_t>(m_freeIndex)].value = element;
61  m_values[static_cast<Capacity_t>(m_freeIndex)].isValid = true;
62  ++m_size;
63  }
64 
65  return nextElement;
66  }
67 
68  void remove(Index_t index)
69  {
70  if (m_values[static_cast<Capacity_t>(index)].isValid)
71  {
72  m_values[static_cast<Capacity_t>(index)].isValid = false;
73  --m_size;
74  }
75  }
76 
79  T& operator[](Index_t index)
80  {
81  return m_values[static_cast<Capacity_t>(index)].value;
82  }
83 
84  T* get(Index_t index)
85  {
86  return (m_values[static_cast<Capacity_t>(index)].isValid) ? &m_values[static_cast<uint32_t>(index)].value
87  : nullptr;
88  }
89 
90  size_t size()
91  {
92  return m_size;
93  }
94 
95  private:
96  Index_t nextFree()
97  {
98  if (m_size >= capacity)
99  return NOT_AN_ELEMENT; // container is full
100 
101  for (; m_values[static_cast<Capacity_t>(m_freeIndex)].isValid;
102  m_freeIndex = (m_freeIndex + 1) % static_cast<Index_t>(capacity))
103  ;
104 
105  return m_freeIndex;
106  }
107 
108  void setValid(Index_t index, bool value = true)
109  {
110  m_values[static_cast<Capacity_t>(index)].isValid = value;
111  }
112 
113  void setInvalid(Index_t index)
114  {
115  setValid(index, false);
116  }
117 
118  Index_t m_freeIndex{0};
119  size_t m_size{0U};
120 
121  struct entry_t
122  {
123  T value = T();
124  bool isValid = false;
125  };
126 
127  iox::cxx::vector<entry_t, capacity> m_values;
128 };
129 
130 } // namespace roudi
131 } // namespace iox
132 
133 #endif // IOX_POSH_ROUDI_INTROSPECTION_FIXED_SIZE_CONTAINER_HPP
Definition: fixed_size_container.hpp:39
Index_t add(const T &element)
Definition: fixed_size_container.hpp:53
T & operator[](Index_t index)
Definition: fixed_size_container.hpp:79
Definition: service_description.hpp:29