Loading...
Searching...
No Matches
copyableoptional.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_COMMON_COPYABLE_OPTIONAL_HH
#define DUNE_COMMON_COPYABLE_OPTIONAL_HH
#include <cassert>
#include <iostream>
#include <memory>
#include <optional>
#include <type_traits>
#include <dune/common/typeutilities.hh>
namespace Dune {
template <class Type>
class CopyableOptional
: public std::optional<Type>
{
static_assert(std::is_copy_constructible_v<Type>);
static_assert(std::is_object_v<Type>);
using Base = std::optional<Type>;
public:
template <class T = Type,
std::enable_if_t<std::is_default_constructible_v<T>, int> = 0>
constexpr CopyableOptional ()
noexcept(std::is_nothrow_default_constructible_v<T>)
: Base{std::in_place}
{}
template <class T = Type,
disableCopyMove<CopyableOptional,T> = 0,
std::enable_if_t<std::is_constructible_v<Type,T&&>, int> = 0,
std::enable_if_t<std::is_convertible_v<T&&,Type>, int> = 0>
constexpr CopyableOptional (T&& value)
noexcept(std::is_nothrow_constructible_v<Type,T&&>)
: Base{std::in_place, std::forward<T>(value)}
{}
template <class T = Type,
disableCopyMove<CopyableOptional,T> = 0,
std::enable_if_t<std::is_constructible_v<Type,T&&>, int> = 0,
std::enable_if_t<not std::is_convertible_v<T&&,Type>, int> = 0>
explicit constexpr CopyableOptional (T&& value)
noexcept(std::is_nothrow_constructible_v<Type,T&&>)
: Base{std::in_place, std::forward<T>(value)}
{}
template <class... Args,
disableCopyMove<CopyableOptional, Args...> = 0,
std::enable_if_t<(sizeof...(Args) > 1), int> = 0,
std::enable_if_t<std::is_constructible_v<Type,Args&&...>, int> = 0>
constexpr CopyableOptional (Args&&... args)
noexcept(std::is_nothrow_constructible_v<Type,Args&&...>)
: Base{std::in_place, std::forward<Args>(args)...}
{}
constexpr CopyableOptional (const CopyableOptional&) = default;
constexpr CopyableOptional (CopyableOptional&&) = default;
~CopyableOptional () = default;
constexpr CopyableOptional& operator= (const CopyableOptional& that)
noexcept(std::is_nothrow_copy_assignable_v<Type> ||
(!std::is_copy_assignable_v<Type> && std::is_nothrow_copy_constructible_v<Type>))
{
if constexpr(std::is_copy_assignable_v<Type>)
Base::operator=(that);
else {
// no self-assignment
if (this != std::addressof(that)) {
if (that)
Base::emplace(*that);
else
Base::reset();
}
}
return *this;
}
template <class T = Type,
std::enable_if_t<std::is_move_constructible_v<T>, int> = 0>
constexpr CopyableOptional& operator= (CopyableOptional&& that)
noexcept(std::is_nothrow_move_assignable_v<Type> ||
(!std::is_move_assignable_v<Type> && std::is_nothrow_move_constructible_v<Type>))
{
if constexpr(std::is_move_assignable_v<Type>)
Base::operator=(std::move(that));
else {
// no self-assignment
if (this != std::addressof(that)) {
if (that)
Base::emplace(std::move(*that));
else
Base::reset();
}
}
return *this;
}
template <class T = Type,
std::enable_if_t<not std::is_same_v<std::decay_t<T>, CopyableOptional>, int> = 0,
std::enable_if_t<(std::is_assignable_v<Type&,T> || std::is_constructible_v<Type,T>), int> = 0>
constexpr CopyableOptional& operator= (T&& value)
noexcept(std::is_nothrow_assignable_v<Type&,T> ||
(!std::is_assignable_v<Type&,T> && std::is_nothrow_constructible_v<Type,T>))
{
if constexpr(std::is_assignable_v<Type&,T>)
Base::operator=(std::forward<T>(value));
else
Base::emplace(std::forward<T>(value));
return *this;
}
};
} // end namespace Dune
#endif // DUNE_COMMON_COPYABLE_OPTIONAL_HH
typeutilities.hh
Utilities for type computations, constraining overloads, ...
Dune::disableCopyMove
std::enable_if_t< not Impl::disableCopyMoveHelper< This, T... >::value, int > disableCopyMove
Helper to disable constructor as copy and move constructor.
Definition typeutilities.hh:45
std
STL namespace.
Dune
Dune namespace
Definition alignedallocator.hh:13
Dune::CopyableOptional
A copyable type wrapper that provides copy/move assignment operations for types that are only copy/mo...
Definition copyableoptional.hh:35
Dune::CopyableOptional::CopyableOptional
constexpr CopyableOptional(CopyableOptional &&)=default
Move construct the contained value.
Dune::CopyableOptional::CopyableOptional
constexpr CopyableOptional(T &&value) noexcept(std::is_nothrow_constructible_v< Type, T && >)
Construct the internal data from perfect forwarding of the passed arguments. Participates in overload...
Definition copyableoptional.hh:62
Dune::CopyableOptional::operator=
constexpr CopyableOptional & operator=(const CopyableOptional &that) noexcept(std::is_nothrow_copy_assignable_v< Type >||(!std::is_copy_assignable_v< Type > &&std::is_nothrow_copy_constructible_v< Type >))
Copy assignment in terms of copy constructor.
Definition copyableoptional.hh:100
Dune::CopyableOptional::CopyableOptional
constexpr CopyableOptional() noexcept(std::is_nothrow_default_constructible_v< T >)
Implementation of a default constructor, if the Type is itself default constructible....
Definition copyableoptional.hh:49
Dune::CopyableOptional::CopyableOptional
constexpr CopyableOptional(const CopyableOptional &)=default
Copy construct the contained value.
Dune::CopyableOptional::CopyableOptional
constexpr CopyableOptional(Args &&... args) noexcept(std::is_nothrow_constructible_v< Type, Args &&... >)
Construct the internal data from perfect forwarding of the passed arguments.
Definition copyableoptional.hh:85
Dune::CopyableOptional::~CopyableOptional
~CopyableOptional()=default
Default destructor.