iceoryx_hoofs 2.0.5
Loading...
Searching...
No Matches
variant.hpp
1// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// SPDX-License-Identifier: Apache-2.0
17#ifndef IOX_HOOFS_CXX_VARIANT_HPP
18#define IOX_HOOFS_CXX_VARIANT_HPP
19
20#include "iceoryx_hoofs/cxx/algorithm.hpp"
21#include "iceoryx_hoofs/internal/cxx/variant_internal.hpp"
22
23#include <cstdint>
24#include <iostream>
25#include <limits>
26#include <type_traits>
27
28#include "iceoryx_hoofs/platform/platform_correction.hpp"
29
30namespace iox
31{
32namespace cxx
33{
40template <uint64_t N>
42{
43 static constexpr uint64_t value = N;
44};
45
52template <typename T>
54{
55 using type = T;
56};
57
70static constexpr uint64_t INVALID_VARIANT_INDEX = std::numeric_limits<uint64_t>::max();
71
105template <typename... Types>
107{
108 private:
110 static constexpr uint64_t TYPE_SIZE = algorithm::max(sizeof(Types)...);
111
112 public:
115 constexpr variant() noexcept = default;
116
125 template <uint64_t N, typename... CTorArguments>
126 constexpr variant(const in_place_index<N>& index, CTorArguments&&... args) noexcept;
127
135 template <typename T, typename... CTorArguments>
136 constexpr variant(const in_place_type<T>& type, CTorArguments&&... args) noexcept;
137
141 template <typename T,
142 typename = std::enable_if_t<!std::is_same<std::decay_t<T>, variant>::value>,
143 typename std::enable_if_t<!internal::is_in_place_index<std::decay_t<T>>::value, bool> = false,
144 typename std::enable_if_t<!internal::is_in_place_type<std::decay_t<T>>::value, bool> = false>
145 constexpr variant(T&& arg) noexcept;
146
150 constexpr variant(const variant& rhs) noexcept;
151
156 constexpr variant& operator=(const variant& rhs) noexcept;
157
161 constexpr variant(variant&& rhs) noexcept;
162
167 constexpr variant& operator=(variant&& rhs) noexcept;
168
171 ~variant() noexcept;
172
179 template <typename T>
180 typename std::enable_if<!std::is_same<T, variant<Types...>&>::value, variant<Types...>>::type&
181 operator=(T&& rhs) noexcept;
182
190 template <uint64_t TypeIndex, typename... CTorArguments>
191 bool emplace_at_index(CTorArguments&&... args) noexcept;
192
199 template <typename T, typename... CTorArguments>
200 bool emplace(CTorArguments&&... args) noexcept;
201
211 template <uint64_t TypeIndex>
212 typename internal::get_type_at_index<0, TypeIndex, Types...>::type* get_at_index() noexcept;
213
223 template <uint64_t TypeIndex>
224 const typename internal::get_type_at_index<0, TypeIndex, Types...>::type* get_at_index() const noexcept;
225
231 template <typename T>
232 const T* get() const noexcept;
233
239 template <typename T>
240 T* get() noexcept;
241
245 template <typename T>
246 T* get_if(T* defaultValue) noexcept;
247
253 template <typename T>
254 const T* get_if(const T* defaultValue) const noexcept;
255
259 constexpr uint64_t index() const noexcept;
260
261 private:
262 alignas(algorithm::max(alignof(Types)...)) internal::byte_t m_storage[TYPE_SIZE]{0u};
263 uint64_t m_type_index = INVALID_VARIANT_INDEX;
264
265 private:
266 template <typename T>
267 bool has_bad_variant_element_access() const noexcept;
268 static void error_message(const char* source, const char* msg) noexcept;
269
270 void call_element_destructor() noexcept;
271};
272
274template <typename T, typename... Types>
275constexpr bool holds_alternative(const variant<Types...>& variant) noexcept;
276
277} // namespace cxx
278} // namespace iox
279
280#include "iceoryx_hoofs/internal/cxx/variant.inl"
281
282#endif // IOX_HOOFS_CXX_VARIANT_HPP
Variant implementation from the C++17 standard with C++11. The interface is inspired by the C++17 sta...
Definition variant.hpp:107
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)
bool emplace_at_index(CTorArguments &&... args) noexcept
calls the constructor of the type at index TypeIndex and perfectly forwards the arguments to this con...
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...
constexpr variant() noexcept=default
the default constructor constructs a variant which does not contain an element and returns INVALID_VA...
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...
bool emplace(CTorArguments &&... args) noexcept
calls the constructor of the type T and perfectly forwards the arguments to the constructor of T.
const T * get() const noexcept
returns a pointer to the type T stored in the variant. (not stl compliant)
building block to easily create free function for logging in a library context
Definition lockfree_queue.hpp:29
helper struct to perform an emplacement at a predefined index in the constructor of a variant
Definition variant.hpp:42
helper struct to perform an emplacement of a predefined type in in the constructor of a variant
Definition variant.hpp:54