Skip to content

iceoryx_hoofs/cxx/variant.hpp🔗

Namespaces🔗

Name
iox
building block to easily create free function for logging in a library context
iox::cxx

Classes🔗

Name
struct iox::cxx::in_place_index
helper struct to perform an emplacement at a predefined index in the constructor of a variant
struct iox::cxx::in_place_type
helper struct to perform an emplacement of a predefined type in in the constructor of a variant
class iox::cxx::variant
Variant implementation from the C++17 standard with C++11. The interface is inspired by the C++17 standard but it has changes in get and emplace since we are not allowed to throw exceptions.

Source code🔗

// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_HOOFS_CXX_VARIANT_HPP
#define IOX_HOOFS_CXX_VARIANT_HPP

#include "iceoryx_hoofs/cxx/algorithm.hpp"
#include "iceoryx_hoofs/internal/cxx/variant_internal.hpp"

#include <cstdint>
#include <iostream>
#include <limits>
#include <type_traits>

#include "iceoryx_hoofs/platform/platform_correction.hpp"

namespace iox
{
namespace cxx
{
template <uint64_t N>
struct in_place_index
{
    static constexpr uint64_t value = N;
};

template <typename T>
struct in_place_type
{
    using type = T;
};

static constexpr uint64_t INVALID_VARIANT_INDEX = std::numeric_limits<uint64_t>::max();

template <typename... Types>
class variant
{
  private:
    static constexpr uint64_t TYPE_SIZE = algorithm::max(sizeof(Types)...);

  public:
    constexpr variant() noexcept = default;

    template <uint64_t N, typename... CTorArguments>
    constexpr variant(const in_place_index<N>& index, CTorArguments&&... args) noexcept;

    template <typename T, typename... CTorArguments>
    constexpr variant(const in_place_type<T>& type, CTorArguments&&... args) noexcept;

    template <typename T,
              typename = std::enable_if_t<!std::is_same<std::decay_t<T>, variant>::value>,
              typename std::enable_if_t<!internal::is_in_place_index<std::decay_t<T>>::value, bool> = false,
              typename std::enable_if_t<!internal::is_in_place_type<std::decay_t<T>>::value, bool> = false>
    constexpr variant(T&& arg) noexcept;

    constexpr variant(const variant& rhs) noexcept;

    constexpr variant& operator=(const variant& rhs) noexcept;

    constexpr variant(variant&& rhs) noexcept;

    constexpr variant& operator=(variant&& rhs) noexcept;

    ~variant() noexcept;

    template <typename T>
    typename std::enable_if<!std::is_same<T, variant<Types...>&>::value, variant<Types...>>::type&
    operator=(T&& rhs) noexcept;

    template <uint64_t TypeIndex, typename... CTorArguments>
    bool emplace_at_index(CTorArguments&&... args) noexcept;

    template <typename T, typename... CTorArguments>
    bool emplace(CTorArguments&&... args) noexcept;

    template <uint64_t TypeIndex>
    typename internal::get_type_at_index<0, TypeIndex, Types...>::type* get_at_index() noexcept;

    template <uint64_t TypeIndex>
    const typename internal::get_type_at_index<0, TypeIndex, Types...>::type* get_at_index() const noexcept;

    template <typename T>
    const T* get() const noexcept;

    template <typename T>
    T* get() noexcept;

    template <typename T>
    T* get_if(T* defaultValue) noexcept;

    template <typename T>
    const T* get_if(const T* defaultValue) const noexcept;

    constexpr uint64_t index() const noexcept;

  private:
    alignas(algorithm::max(alignof(Types)...)) internal::byte_t m_storage[TYPE_SIZE]{0u};
    uint64_t m_type_index = INVALID_VARIANT_INDEX;

  private:
    template <typename T>
    bool has_bad_variant_element_access() const noexcept;
    static void error_message(const char* source, const char* msg) noexcept;

    void call_element_destructor() noexcept;
};

template <typename T, typename... Types>
constexpr bool holds_alternative(const variant<Types...>& variant) noexcept;

} // namespace cxx
} // namespace iox

#include "iceoryx_hoofs/internal/cxx/variant.inl"

#endif // IOX_HOOFS_CXX_VARIANT_HPP

Updated on 18 December 2023 at 13:11:42 CET