| 1 |
//@HEADER |
| 2 |
// ************************************************************************ |
| 3 |
// |
| 4 |
// Kokkos v. 4.0 |
| 5 |
// Copyright (2022) National Technology & Engineering |
| 6 |
// Solutions of Sandia, LLC (NTESS). |
| 7 |
// |
| 8 |
// Under the terms of Contract DE-NA0003525 with NTESS, |
| 9 |
// the U.S. Government retains certain rights in this software. |
| 10 |
// |
| 11 |
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. |
| 12 |
// See https://kokkos.org/LICENSE for license information. |
| 13 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 14 |
// |
| 15 |
//@HEADER |
| 16 |
#pragma once |
| 17 |
|
| 18 |
#include "macros.hpp" |
| 19 |
#include "trait_backports.hpp" |
| 20 |
|
| 21 |
namespace MDSPAN_IMPL_STANDARD_NAMESPACE { |
| 22 |
namespace detail { |
| 23 |
|
| 24 |
//============================================================================== |
| 25 |
|
| 26 |
template <class _T, size_t _Disambiguator = 0, class _Enable = void> |
| 27 |
struct __no_unique_address_emulation { |
| 28 |
using __stored_type = _T; |
| 29 |
_T __v; |
| 30 |
MDSPAN_FORCE_INLINE_FUNCTION constexpr _T const &__ref() const noexcept { |
| 31 |
return __v; |
| 32 |
} |
| 33 |
MDSPAN_FORCE_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 _T &__ref() noexcept { |
| 34 |
return __v; |
| 35 |
} |
| 36 |
}; |
| 37 |
|
| 38 |
// Empty case |
| 39 |
// This doesn't work if _T is final, of course, but we're not using anything |
| 40 |
// like that currently. That kind of thing could be added pretty easily though |
| 41 |
template <class _T, size_t _Disambiguator> |
| 42 |
struct __no_unique_address_emulation< |
| 43 |
_T, _Disambiguator, |
| 44 |
std::enable_if_t<_MDSPAN_TRAIT(std::is_empty, _T) && |
| 45 |
// If the type isn't trivially destructible, its destructor |
| 46 |
// won't be called at the right time, so don't use this |
| 47 |
// specialization |
| 48 |
_MDSPAN_TRAIT(std::is_trivially_destructible, _T)>> : |
| 49 |
#ifdef _MDSPAN_COMPILER_MSVC |
| 50 |
// MSVC doesn't allow you to access public static member functions of a type |
| 51 |
// when you *happen* to privately inherit from that type. |
| 52 |
protected |
| 53 |
#else |
| 54 |
// But we still want this to be private if possible so that we don't accidentally |
| 55 |
// access members of _T directly rather than calling __ref() first, which wouldn't |
| 56 |
// work if _T happens to be stateful and thus we're using the unspecialized definition |
| 57 |
// of __no_unique_address_emulation above. |
| 58 |
private |
| 59 |
#endif |
| 60 |
_T { |
| 61 |
using __stored_type = _T; |
| 62 |
MDSPAN_FORCE_INLINE_FUNCTION constexpr _T const &__ref() const noexcept { |
| 63 |
return *static_cast<_T const *>(this); |
| 64 |
} |
| 65 |
MDSPAN_FORCE_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 _T &__ref() noexcept { |
| 66 |
return *static_cast<_T *>(this); |
| 67 |
} |
| 68 |
|
| 69 |
MDSPAN_INLINE_FUNCTION_DEFAULTED |
| 70 |
constexpr __no_unique_address_emulation() noexcept = default; |
| 71 |
MDSPAN_INLINE_FUNCTION_DEFAULTED |
| 72 |
constexpr __no_unique_address_emulation( |
| 73 |
__no_unique_address_emulation const &) noexcept = default; |
| 74 |
MDSPAN_INLINE_FUNCTION_DEFAULTED |
| 75 |
constexpr __no_unique_address_emulation( |
| 76 |
__no_unique_address_emulation &&) noexcept = default; |
| 77 |
MDSPAN_INLINE_FUNCTION_DEFAULTED |
| 78 |
_MDSPAN_CONSTEXPR_14_DEFAULTED __no_unique_address_emulation & |
| 79 |
operator=(__no_unique_address_emulation const &) noexcept = default; |
| 80 |
MDSPAN_INLINE_FUNCTION_DEFAULTED |
| 81 |
_MDSPAN_CONSTEXPR_14_DEFAULTED __no_unique_address_emulation & |
| 82 |
operator=(__no_unique_address_emulation &&) noexcept = default; |
| 83 |
MDSPAN_INLINE_FUNCTION_DEFAULTED |
| 84 |
~__no_unique_address_emulation() noexcept = default; |
| 85 |
|
| 86 |
// Explicitly make this not a reference so that the copy or move |
| 87 |
// constructor still gets called. |
| 88 |
MDSPAN_INLINE_FUNCTION |
| 89 |
explicit constexpr __no_unique_address_emulation(_T const& __v) noexcept : _T(__v) {} |
| 90 |
MDSPAN_INLINE_FUNCTION |
| 91 |
explicit constexpr __no_unique_address_emulation(_T&& __v) noexcept : _T(::std::move(__v)) {} |
| 92 |
}; |
| 93 |
|
| 94 |
//============================================================================== |
| 95 |
|
| 96 |
} // end namespace detail |
| 97 |
} // end namespace MDSPAN_IMPL_STANDARD_NAMESPACE |