iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
variant.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) 2023 by ekxide IO GmbH. All rights reserved.
4// Copyright (c) 2025 Contributors to the Eclipse Foundation
5//
6// See the NOTICE file(s) distributed with this work for additional
7// information regarding copyright ownership.
8//
9// This program and the accompanying materials are made available under the
10// terms of the Apache Software License 2.0 which is available at
11// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
12// which is available at https://opensource.org/licenses/MIT.
13//
14// SPDX-License-Identifier: Apache-2.0 OR MIT
15
16#ifndef IOX2_BB_VOCABULARY_VARIANT_HPP
17#define IOX2_BB_VOCABULARY_VARIANT_HPP
18
20
22
23#include <algorithm>
24#include <cstdint>
25#include <iostream>
26#include <limits>
27#include <type_traits>
28
29namespace iox2 {
30namespace legacy {
37template <uint64_t N>
39 static constexpr uint64_t value { N };
40};
41
48template <typename T>
50 using type = T;
51};
52
65static constexpr uint64_t INVALID_VARIANT_INDEX { std::numeric_limits<uint64_t>::max() };
66
100template <typename... Types>
102 private:
104 static constexpr uint64_t TYPE_SIZE { std::max({ sizeof(Types)... }) };
106 static constexpr uint64_t TYPE_ALIGNMENT { std::max({ alignof(Types)... }) };
107
108 public:
111 constexpr variant() noexcept = default;
112
123
133
138 typename = std::enable_if_t<!std::is_same<std::decay_t<T>, variant>::value>,
139 typename std::enable_if_t<!internal::is_in_place_index<std::decay_t<T>>::value, bool> = false,
140 typename std::enable_if_t<!internal::is_in_place_type<std::decay_t<T>>::value, bool> = false>
142
147
153
160
168
172
180 // NOLINTJUSTIFICATION Correct return type is used through enable_if
181 // NOLINTNEXTLINE(cppcoreguidelines-c-copy-assignment-signature)
182 typename std::enable_if<!std::is_same<T, variant<Types...>&>::value, variant<Types...>>::type&
184
192
199
210 typename internal::get_type_at_index<0, TypeIndex, Types...>::type* get_at_index() noexcept;
211
219 typename internal::get_type_at_index<0, TypeIndex, Types...>::type* unsafe_get_at_index_unchecked() noexcept;
220
231 const typename internal::get_type_at_index<0, TypeIndex, Types...>::type* get_at_index() const noexcept;
232
240 const typename internal::get_type_at_index<0, TypeIndex, Types...>::type*
242
250
258
264
272
277
278 private:
279 // AXIVION Next Construct AutosarC++19_03-A9.6.1 : false positive. internal::byte_t is a type alias for uint8_t
280 struct alignas(TYPE_ALIGNMENT) storage_t {
281 // AXIVION Next Construct AutosarC++19_03-M0.1.3 : data provides the actual storage and is accessed via m_storage since &m_storage.data = &m_storage
282 // AXIVION Next Construct AutosarC++19_03-A18.1.1 : safe access is guaranteed since the c-array is wrapped inside the variant class
283 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays)
284 char data[TYPE_SIZE];
285 };
286 storage_t m_storage {};
287 uint64_t m_type_index { INVALID_VARIANT_INDEX };
288
289 private:
290 template <typename T>
291 bool has_bad_variant_element_access() const noexcept;
292 // AXIVION Next Construct AutosarC++19_03-A3.9.1 : internal convenience function to easily use IOX2_LOG
293 static void error_message(const char* source, const char* msg) noexcept;
294
295 void call_element_destructor() noexcept;
296
297 template <typename... Ts>
299 template <typename... Ts>
301};
302
306
314
322
323} // namespace legacy
324} // namespace iox2
325
326#include "iox2/legacy/detail/variant.inl"
327
328#endif // IOX2_BB_VOCABULARY_VARIANT_HPP
Variant implementation from the C++17 standard with C++11. The interface is inspired by the C++17 sta...
Definition variant.hpp:101
const T * get() const noexcept
returns a pointer to the type T stored in the variant. (not stl compliant)
void emplace(CTorArguments &&... args) noexcept
calls the constructor of the type T and perfectly forwards the arguments to the constructor of T.
T * get_if(T *defaultValue) noexcept
returns a pointer to the type T if its stored in the variant otherwise it returns the provided defaul...
constexpr uint64_t index() const noexcept
returns the index of the stored type in the variant. if the variant does not contain any type it retu...
void emplace_at_index(CTorArguments &&... args) noexcept
calls the constructor of the type at index TypeIndex and perfectly forwards the arguments to this con...
internal::get_type_at_index< 0, TypeIndex, Types... >::type * unsafe_get_at_index_unchecked() noexcept
returns a pointer to the type stored at index TypeIndex. (not stl compliant)
internal::get_type_at_index< 0, TypeIndex, Types... >::type * get_at_index() noexcept
returns a pointer to the type stored at index TypeIndex. (not stl compliant)
constexpr variant() noexcept=default
the default constructor constructs a variant which does not contain an element and returns INVALID_VA...
constexpr bool holds_alternative(const variant< Types... > &variant) noexcept
returns true if the variant holds a given type T, otherwise false
constexpr bool always_false_v
Helper value to bind a static_assert to a type.
static constexpr uint64_t INVALID_VARIANT_INDEX
value which an invalid variant index occupies
Definition variant.hpp:65
helper struct to perform an emplacement at a predefined index in the constructor of a variant
Definition variant.hpp:38
static constexpr uint64_t value
Definition variant.hpp:39
helper struct to perform an emplacement of a predefined type in in the constructor of a variant
Definition variant.hpp:49