iceoryx_doc  1.0.1
variant.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. 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_UTILS_CXX_VARIANT_HPP
17 #define IOX_UTILS_CXX_VARIANT_HPP
18 
19 #include "iceoryx_utils/cxx/algorithm.hpp"
20 #include "iceoryx_utils/internal/cxx/variant_internal.hpp"
21 
22 #include <cstdint>
23 #include <iostream>
24 #include <limits>
25 
26 #include "iceoryx_utils/platform/platform_correction.hpp"
27 
28 namespace iox
29 {
30 namespace cxx
31 {
38 template <uint64_t N>
40 {
41  static constexpr uint64_t value = N;
42 };
43 
50 template <typename T>
52 {
53  using type = T;
54 };
55 
68 static constexpr uint64_t INVALID_VARIANT_INDEX = std::numeric_limits<uint64_t>::max();
69 
103 template <typename... Types>
104 class variant
105 {
106  private:
108  static constexpr uint64_t TYPE_SIZE = algorithm::max(sizeof(Types)...);
109 
110  public:
113  variant() = default;
114 
121  template <uint64_t N, typename... CTorArguments>
122  variant(const in_place_index<N>& index, CTorArguments&&... args) noexcept;
123 
129  template <typename T, typename... CTorArguments>
130  variant(const in_place_type<T>& type, CTorArguments&&... args) noexcept;
131 
135  variant(const variant& rhs) noexcept;
136 
141  variant& operator=(const variant& rhs) noexcept;
142 
146  variant(variant&& rhs) noexcept;
147 
152  variant& operator=(variant&& rhs) noexcept;
153 
156  ~variant() noexcept;
157 
163  template <typename T>
164  typename std::enable_if<!std::is_same<T, variant<Types...>&>::value, variant<Types...>>::type&
165  operator=(T&& rhs) noexcept;
166 
173  template <uint64_t TypeIndex, typename... CTorArguments>
174  bool emplace_at_index(CTorArguments&&... args) noexcept;
175 
180  template <typename T, typename... CTorArguments>
181  bool emplace(CTorArguments&&... args) noexcept;
182 
191  template <uint64_t TypeIndex>
192  typename internal::get_type_at_index<0, TypeIndex, Types...>::type* get_at_index() noexcept;
193 
202  template <uint64_t TypeIndex>
203  const typename internal::get_type_at_index<0, TypeIndex, Types...>::type* get_at_index() const noexcept;
204 
209  template <typename T>
210  const T* get() const noexcept;
211 
216  template <typename T>
217  T* get() noexcept;
218 
222  template <typename T>
223  T* get_if(T* defaultValue) noexcept;
224 
228  template <typename T>
229  const T* get_if(const T* defaultValue) const noexcept;
230 
234  constexpr size_t index() const noexcept;
235 
236  private:
237  alignas(algorithm::max(alignof(Types)...)) internal::byte_t m_storage[TYPE_SIZE]{0u};
238  uint64_t m_type_index = INVALID_VARIANT_INDEX;
239 
240  private:
241  template <typename T>
242  bool has_bad_variant_element_access() const noexcept;
243  static void error_message(const char* f_source, const char* f_msg) noexcept;
244 
245  void call_element_destructor() noexcept;
246 };
247 
249 template <typename T, typename... Types>
250 constexpr bool holds_alternative(const variant<Types...>& f_variant) noexcept;
251 
252 } // namespace cxx
253 } // namespace iox
254 
255 #include "iceoryx_utils/internal/cxx/variant.inl"
256 
257 #endif // IOX_UTILS_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:105
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)
Definition: variant.inl:194
bool emplace_at_index(CTorArguments &&... args) noexcept
calls the constructor of the type at index TypeIndex and perfectly forwards the arguments to this con...
Definition: variant.inl:156
variant()=default
the default constructor constructs a variant which does not contain an element and returns INVALID_VA...
~variant() noexcept
if the variant contains an element the elements destructor is called otherwise nothing happens
Definition: variant.inl:115
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...
Definition: variant.inl:238
bool emplace(CTorArguments &&... args) noexcept
calls the constructor of the type T and perfectly forwards the arguments to the constructor of T.
Definition: variant.inl:171
constexpr size_t index() const noexcept
returns the index of the stored type in the variant. if the variant does not contain any type it retu...
Definition: variant.inl:256
variant & operator=(const variant &rhs) noexcept
if the variant contains an element the elements copy assignment operator is called otherwise an empty...
Definition: variant.inl:52
const T * get() const noexcept
returns a pointer to the type T stored in the variant. (not stl compliant)
Definition: variant.inl:217
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28
helper struct to perform an emplacement at a predefined index in the constructor of a variant
Definition: variant.hpp:40
helper struct to perform an emplacement of a predefined type in in the constructor of a variant
Definition: variant.hpp:52