Skip to content

iceoryx_utils/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.
//
// 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_UTILS_CXX_VARIANT_HPP
#define IOX_UTILS_CXX_VARIANT_HPP

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

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

#include "iceoryx_utils/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:
    variant() = default;

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

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

    variant(const variant& rhs) noexcept;

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

    variant(variant&& rhs) noexcept;

    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 size_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* f_source, const char* f_msg) noexcept;

    void call_element_destructor() noexcept;
};

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

} // namespace cxx
} // namespace iox

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

#endif // IOX_UTILS_CXX_VARIANT_HPP

Updated on 31 May 2022 at 15:29:15 CEST