iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
variant_internal.hpp
Go to the documentation of this file.
1// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
3// Copyright (c) 2025 Contributors to the Eclipse Foundation
4//
5// See the NOTICE file(s) distributed with this work for additional
6// information regarding copyright ownership.
7//
8// This program and the accompanying materials are made available under the
9// terms of the Apache Software License 2.0 which is available at
10// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
11// which is available at https://opensource.org/licenses/MIT.
12//
13// SPDX-License-Identifier: Apache-2.0 OR MIT
14
15#ifndef IOX2_BB_VOCABULARY_VARIANT_INTERNAL_HPP
16#define IOX2_BB_VOCABULARY_VARIANT_INTERNAL_HPP
17
19
20#include <cstdint>
21#include <type_traits>
22#include <utility>
23
24namespace iox2 {
25namespace legacy {
26template <uint64_t N>
27struct in_place_index;
28
29template <typename T>
30struct in_place_type;
31namespace internal {
32template <typename N>
33struct is_in_place_index : std::false_type { };
34
35template <uint64_t N>
36struct is_in_place_index<in_place_index<N>> : std::true_type { };
37
38template <typename T>
39struct is_in_place_type : std::false_type { };
40
41template <typename T>
42struct is_in_place_type<in_place_type<T>> : std::true_type { };
43
44template <typename TypeToCheck, typename T, typename... Targs>
46 static constexpr bool value { std::is_same<TypeToCheck, T>::value
48};
49
50template <typename TypeToCheck, typename T>
52 static constexpr bool value { std::is_same<TypeToCheck, T>::value };
53};
54
55template <uint64_t N, typename Type, typename T, typename... Targs>
57 static constexpr uint64_t index { get_index_of_type<N + 1, Type, Targs...>::index };
58};
59
60template <uint64_t N, typename Type, typename... Targs>
62 static constexpr uint64_t index { N };
63};
64
65template <uint64_t N, uint64_t Index, typename T, typename... Targs>
67 using type = typename get_type_at_index<N + 1, Index, Targs...>::type;
68};
69
70template <uint64_t N, typename T, typename... Targs>
71struct get_type_at_index<N, N, T, Targs...> {
72 using type = T;
73};
74
75// AXIVION DISABLE STYLE AutosarC++19_03-M5.2.8 : conversion to typed pointer is intentional, it is correctly aligned and points to sufficient memory for a T by design of variant; note that this is an internal class used by variant
76// AXIVION DISABLE STYLE AutosarC++19_03-A5.2.4 : conversion to typed pointer is intentional, it is correctly aligned and points to sufficient memory for a T by design of variant; note that this is an internal class used by variant
77// AXIVION DISABLE STYLE AutosarC++19_03-A18.5.10 : destination is aligned to the maximum alignment of Types, see variant.hpp
78// AXIVION DISABLE STYLE FaultDetection-IndirectAssignmentOverflow : destination is aligned to the maximum alignment of Types, see variant.hpp
79// AXIVION DISABLE STYLE AutosarC++19_03-A5.3.2 : this is an internal struct used only by variant; all pointer arguments are checked in the variant class before the static functions are called
80// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast)
81template <uint64_t N, typename T, typename... Targs>
83 static void destructor(const uint64_t index, void* ptr) noexcept {
84 if (N == index) {
85 reinterpret_cast<T*>(ptr)->~T();
86 } else {
88 }
89 }
90
91 static void move(const uint64_t index, void* source, void* const destination) noexcept {
92 if (N == index) {
93 *reinterpret_cast<T*>(destination) = std::move(*reinterpret_cast<T*>(source));
94 } else {
96 }
97 }
98
99 static void moveConstructor(const uint64_t index, void* source, void* const destination) noexcept {
100 if (N == index) {
101 new (destination) T(std::move(*reinterpret_cast<T*>(source)));
102 } else {
104 }
105 }
106
107 static void copy(const uint64_t index, const void* const source, void* const destination) noexcept {
108 if (N == index) {
109 *reinterpret_cast<T*>(destination) = *reinterpret_cast<const T*>(source);
110 } else {
112 }
113 }
114
115 static void copyConstructor(const uint64_t index, const void* const source, void* const destination) noexcept {
116 if (N == index) {
117 new (destination) T(*reinterpret_cast<const T*>(source));
118 } else {
120 }
121 }
122
123 static bool equality(const uint64_t index, const void* const lhs, const void* const rhs) noexcept {
124 if (N == index) {
125 return *reinterpret_cast<const T*>(lhs) == *reinterpret_cast<const T*>(rhs);
126 }
128 }
129};
130
131template <uint64_t N, typename T>
132struct call_at_index<N, T> {
133 static void destructor(const uint64_t index, void* ptr) noexcept {
134 if (N == index) {
135 reinterpret_cast<T*>(ptr)->~T();
136 } else {
137 IOX2_PANIC("Could not call destructor for variant element");
138 }
139 }
140
141 static void move(const uint64_t index, void* source, void* const destination) noexcept {
142 if (N == index) {
143 *reinterpret_cast<T*>(destination) = std::move(*reinterpret_cast<T*>(source));
144 } else {
145 IOX2_PANIC("Could not call move assignment for variant element");
146 }
147 }
148
149 static void moveConstructor(const uint64_t index, void* source, void* const destination) noexcept {
150 if (N == index) {
151 new (destination) T(std::move(*reinterpret_cast<T*>(source)));
152 } else {
153 IOX2_PANIC("Could not call move constructor for variant element");
154 }
155 }
156
157 static void copy(const uint64_t index, const void* const source, void* const destination) noexcept {
158 if (N == index) {
159 *reinterpret_cast<T*>(destination) = *reinterpret_cast<const T*>(source);
160 } else {
161 IOX2_PANIC("Could not call copy assignment for variant element");
162 }
163 }
164
165 static void copyConstructor(const uint64_t index, const void* const source, void* const destination) noexcept {
166 if (N == index) {
167 new (destination) T(*reinterpret_cast<const T*>(source));
168 } else {
169 IOX2_PANIC("Could not call copy constructor for variant element");
170 }
171 }
172
173 static bool equality(const uint64_t index, const void* const lhs, const void* const rhs) noexcept {
174 if (N == index) {
175 return *reinterpret_cast<const T*>(lhs) == *reinterpret_cast<const T*>(rhs);
176 }
177 IOX2_PANIC("Could not call equality operator for variant element");
178 return false;
179 }
180};
181// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
182// AXIVION ENABLE STYLE AutosarC++19_03-A5.3.2
183// AXIVION ENABLE STYLE FaultDetection-IndirectAssignmentOverflow
184// AXIVION ENABLE STYLE AutosarC++19_03-A18.5.10
185// AXIVION ENABLE STYLE AutosarC++19_03-A5.2.4
186// AXIVION ENABLE STYLE AutosarC++19_03-M5.2.8
187
188} // namespace internal
189} // namespace legacy
190} // namespace iox2
191
192#endif // IOX2_BB_VOCABULARY_VARIANT_INTERNAL_HPP
#define IOX2_PANIC(message)
calls panic handler and does not return
constexpr bool always_false_v
Helper value to bind a static_assert to a type.
helper struct to perform an emplacement at a predefined index in the constructor of a variant
Definition variant.hpp:38
helper struct to perform an emplacement of a predefined type in in the constructor of a variant
Definition variant.hpp:49
static bool equality(const uint64_t index, const void *const lhs, const void *const rhs) noexcept
static void move(const uint64_t index, void *source, void *const destination) noexcept
static void copyConstructor(const uint64_t index, const void *const source, void *const destination) noexcept
static void copy(const uint64_t index, const void *const source, void *const destination) noexcept
static void moveConstructor(const uint64_t index, void *source, void *const destination) noexcept
static void destructor(const uint64_t index, void *ptr) noexcept
static void copy(const uint64_t index, const void *const source, void *const destination) noexcept
static void move(const uint64_t index, void *source, void *const destination) noexcept
static bool equality(const uint64_t index, const void *const lhs, const void *const rhs) noexcept
static void moveConstructor(const uint64_t index, void *source, void *const destination) noexcept
static void destructor(const uint64_t index, void *ptr) noexcept
static void copyConstructor(const uint64_t index, const void *const source, void *const destination) noexcept
typename get_type_at_index< N+1, Index, Targs... >::type type