iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
slice.hpp
Go to the documentation of this file.
1// Copyright (c) 2024 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13#ifndef IOX2_BB_SLICE_HPP
14#define IOX2_BB_SLICE_HPP
15
17
18#include <cstdint>
19#include <type_traits>
20
21namespace iox2 {
22namespace bb {
23
30template <typename T>
31class Slice {
32 public:
33 using Iterator = T*;
34 using ConstIterator = const T*;
35 using ValueType = std::remove_const_t<T>;
36
40 Slice(T* data, uint64_t number_of_elements);
41
44 auto number_of_bytes() const -> uint64_t;
45
48 auto number_of_elements() const -> uint64_t;
49
54 auto operator[](uint64_t n) const -> const ValueType&;
55
60 auto operator[](uint64_t n) -> std::conditional_t<std::is_const<T>::value, const ValueType&, ValueType&>;
61
64 auto begin() const -> ConstIterator;
65
68 auto begin() -> Iterator;
69
72 auto end() const -> ConstIterator;
73
76 auto end() -> Iterator;
77
80 auto data() const -> ConstIterator;
81
84 auto data() -> Iterator;
85
86 private:
87 T* m_data;
88 uint64_t m_number_of_elements;
89};
90
91template <typename T>
93
94template <typename T>
95using ImmutableSlice = Slice<const T>;
96
97template <typename T>
99 : m_data { data }
100 , m_number_of_elements { number_of_elements } {
101 static_assert(!std::is_same<T, void>::value, "Slice<void> is not allowed");
102}
103
104template <typename T>
105auto Slice<T>::number_of_bytes() const -> uint64_t {
106 return ((sizeof(ValueType) * m_number_of_elements) + alignof(ValueType) - 1) & ~(alignof(ValueType) - 1);
107}
108
109template <typename T>
110auto Slice<T>::number_of_elements() const -> uint64_t {
111 return m_number_of_elements;
112}
113
114template <typename T>
115auto Slice<T>::operator[](const uint64_t n) const -> const ValueType& {
116 IOX2_ASSERT(n < m_number_of_elements, "Index out of bounds");
117 return *(m_data + n);
118}
119
120template <typename T>
121auto Slice<T>::operator[](const uint64_t n)
122 -> std::conditional_t<std::is_const<T>::value, const ValueType&, ValueType&> {
123 IOX2_ASSERT(n < m_number_of_elements, "Index out of bounds");
124 return *(m_data + n);
125}
126
127template <typename T>
129 return m_data;
130}
131
132template <typename T>
134 return m_data;
135}
136
137template <typename T>
139 return m_data + m_number_of_elements;
140}
141
142template <typename T>
144 return m_data + m_number_of_elements;
145}
146
147template <typename T>
149 return m_data;
150}
151
152template <typename T>
154 return m_data;
155}
156
157template <typename>
158struct IsSlice {
159 static constexpr bool VALUE = false;
160};
161
162template <typename T>
163struct IsSlice<Slice<T>> {
164 static constexpr bool VALUE = true;
165};
166
167} // namespace bb
168} // namespace iox2
169
170#endif // IOX2_BB_SLICE_HPP
#define IOX2_ASSERT(condition, message)
only for debug builds: report fatal assert violation if expression evaluates to false
A class representing a slice of contiguous elements of type T.
Definition slice.hpp:31
auto number_of_bytes() const -> uint64_t
Returns the total number of bytes occupied by the slice.
Definition slice.hpp:105
const T * ConstIterator
Definition slice.hpp:34
auto operator[](uint64_t n) const -> const ValueType &
Accesses the element at the specified index (const version).
Definition slice.hpp:115
std::remove_const_t< T > ValueType
Definition slice.hpp:35
auto number_of_elements() const -> uint64_t
Returns the number of elements in the slice.
Definition slice.hpp:110
auto end() const -> ConstIterator
Returns an iterator to the end of the slice (const version).
Definition slice.hpp:143
auto begin() const -> ConstIterator
Returns an iterator to the beginning of the slice (const version).
Definition slice.hpp:133
auto data() const -> ConstIterator
Returns a pointer to the underlying data of the slice (const version).
Definition slice.hpp:153