16 #ifndef IOX_UTILS_CXX_VARIANT_HPP
17 #define IOX_UTILS_CXX_VARIANT_HPP
19 #include "iceoryx_utils/cxx/algorithm.hpp"
20 #include "iceoryx_utils/internal/cxx/variant_internal.hpp"
26 #include "iceoryx_utils/platform/platform_correction.hpp"
41 static constexpr uint64_t value = N;
68 static constexpr uint64_t INVALID_VARIANT_INDEX = std::numeric_limits<uint64_t>::max();
103 template <
typename... Types>
108 static constexpr uint64_t TYPE_SIZE = algorithm::max(
sizeof(Types)...);
121 template <uint64_t N,
typename... CTorArguments>
129 template <
typename T,
typename... CTorArguments>
163 template <typename T>
164 typename std::enable_if<!std::is_same<T,
variant<Types...>&>::value,
variant<Types...>>::type&
165 operator=(T&& rhs) noexcept;
173 template <uint64_t TypeIndex, typename... CTorArguments>
180 template <typename T, typename... CTorArguments>
181 bool emplace(CTorArguments&&... args) noexcept;
191 template <uint64_t TypeIndex>
192 typename internal::get_type_at_index<0, TypeIndex, Types...>::type*
get_at_index() noexcept;
202 template <uint64_t TypeIndex>
203 const typename internal::get_type_at_index<0, TypeIndex, Types...>::type*
get_at_index() const noexcept;
209 template <typename T>
210 const T*
get() const noexcept;
216 template <typename T>
222 template <typename T>
223 T*
get_if(T* defaultValue) noexcept;
228 template <typename T>
229 const T*
get_if(const T* defaultValue) const noexcept;
234 constexpr
size_t index() const noexcept;
237 alignas(algorithm::max(alignof(Types)...)) internal::byte_t m_storage[TYPE_SIZE]{0u};
238 uint64_t m_type_index = INVALID_VARIANT_INDEX;
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;
245 void call_element_destructor() noexcept;
249 template <typename T, typename... Types>
250 constexpr
bool holds_alternative(const
variant<Types...>& f_variant) noexcept;
255 #include "iceoryx_utils/internal/cxx/variant.inl"
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