OpenKalman
compressed_pair.hpp
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 #if !defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS)
22 # include "no_unique_address.hpp"
23 #endif
24 
25 namespace std {
26 namespace experimental {
27 namespace detail {
28 
29 // For no unique address emulation, this is the case taken when neither are empty.
30 // For real `[[no_unique_address]]`, this case is always taken.
31 template <class _T, class _U, class _Enable = void> struct __compressed_pair {
32  _MDSPAN_NO_UNIQUE_ADDRESS _T __t_val;
33  _MDSPAN_NO_UNIQUE_ADDRESS _U __u_val;
34  MDSPAN_FORCE_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 _T &__first() noexcept { return __t_val; }
35  MDSPAN_FORCE_INLINE_FUNCTION constexpr _T const &__first() const noexcept {
36  return __t_val;
37  }
38  MDSPAN_FORCE_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 _U &__second() noexcept { return __u_val; }
39  MDSPAN_FORCE_INLINE_FUNCTION constexpr _U const &__second() const noexcept {
40  return __u_val;
41  }
42 
43  MDSPAN_INLINE_FUNCTION_DEFAULTED
44  constexpr __compressed_pair() noexcept = default;
45  MDSPAN_INLINE_FUNCTION_DEFAULTED
46  constexpr __compressed_pair(__compressed_pair const &) noexcept = default;
47  MDSPAN_INLINE_FUNCTION_DEFAULTED
48  constexpr __compressed_pair(__compressed_pair &&) noexcept = default;
49  MDSPAN_INLINE_FUNCTION_DEFAULTED
50  _MDSPAN_CONSTEXPR_14_DEFAULTED __compressed_pair &
51  operator=(__compressed_pair const &) noexcept = default;
52  MDSPAN_INLINE_FUNCTION_DEFAULTED
53  _MDSPAN_CONSTEXPR_14_DEFAULTED __compressed_pair &
54  operator=(__compressed_pair &&) noexcept = default;
55  MDSPAN_INLINE_FUNCTION_DEFAULTED
56  ~__compressed_pair() noexcept = default;
57  template <class _TLike, class _ULike>
58  MDSPAN_INLINE_FUNCTION constexpr __compressed_pair(_TLike &&__t, _ULike &&__u)
59  : __t_val((_TLike &&) __t), __u_val((_ULike &&) __u) {}
60 };
61 
62 #if !defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS)
63 
64 // First empty.
65 template <class _T, class _U>
67  _T, _U,
68  enable_if_t<_MDSPAN_TRAIT(is_empty, _T) && !_MDSPAN_TRAIT(is_empty, _U)>>
69  : private _T {
70  _U __u_val;
71  MDSPAN_FORCE_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 _T &__first() noexcept {
72  return *static_cast<_T *>(this);
73  }
74  MDSPAN_FORCE_INLINE_FUNCTION constexpr _T const &__first() const noexcept {
75  return *static_cast<_T const *>(this);
76  }
77  MDSPAN_FORCE_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 _U &__second() noexcept { return __u_val; }
78  MDSPAN_FORCE_INLINE_FUNCTION constexpr _U const &__second() const noexcept {
79  return __u_val;
80  }
81 
82  MDSPAN_INLINE_FUNCTION_DEFAULTED
83  constexpr __compressed_pair() noexcept = default;
84  MDSPAN_INLINE_FUNCTION_DEFAULTED
85  constexpr __compressed_pair(__compressed_pair const &) noexcept = default;
86  MDSPAN_INLINE_FUNCTION_DEFAULTED
87  constexpr __compressed_pair(__compressed_pair &&) noexcept = default;
88  MDSPAN_INLINE_FUNCTION_DEFAULTED
89  _MDSPAN_CONSTEXPR_14_DEFAULTED __compressed_pair &
90  operator=(__compressed_pair const &) noexcept = default;
91  MDSPAN_INLINE_FUNCTION_DEFAULTED
92  _MDSPAN_CONSTEXPR_14_DEFAULTED __compressed_pair &
93  operator=(__compressed_pair &&) noexcept = default;
94  MDSPAN_INLINE_FUNCTION_DEFAULTED
95  ~__compressed_pair() noexcept = default;
96  template <class _TLike, class _ULike>
97  MDSPAN_INLINE_FUNCTION constexpr __compressed_pair(_TLike &&__t, _ULike &&__u)
98  : _T((_TLike &&) __t), __u_val((_ULike &&) __u) {}
99 };
100 
101 // Second empty.
102 template <class _T, class _U>
104  _T, _U,
105  enable_if_t<!_MDSPAN_TRAIT(is_empty, _T) && _MDSPAN_TRAIT(is_empty, _U)>>
106  : private _U {
107  _T __t_val;
108  MDSPAN_FORCE_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 _T &__first() noexcept { return __t_val; }
109  MDSPAN_FORCE_INLINE_FUNCTION constexpr _T const &__first() const noexcept {
110  return __t_val;
111  }
112  MDSPAN_FORCE_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 _U &__second() noexcept {
113  return *static_cast<_U *>(this);
114  }
115  MDSPAN_FORCE_INLINE_FUNCTION constexpr _U const &__second() const noexcept {
116  return *static_cast<_U const *>(this);
117  }
118 
119  MDSPAN_INLINE_FUNCTION_DEFAULTED
120  constexpr __compressed_pair() noexcept = default;
121  MDSPAN_INLINE_FUNCTION_DEFAULTED
122  constexpr __compressed_pair(__compressed_pair const &) noexcept = default;
123  MDSPAN_INLINE_FUNCTION_DEFAULTED
124  constexpr __compressed_pair(__compressed_pair &&) noexcept = default;
125  MDSPAN_INLINE_FUNCTION_DEFAULTED
126  _MDSPAN_CONSTEXPR_14_DEFAULTED __compressed_pair &
127  operator=(__compressed_pair const &) noexcept = default;
128  MDSPAN_INLINE_FUNCTION_DEFAULTED
129  _MDSPAN_CONSTEXPR_14_DEFAULTED __compressed_pair &
130  operator=(__compressed_pair &&) noexcept = default;
131  MDSPAN_INLINE_FUNCTION_DEFAULTED
132  ~__compressed_pair() noexcept = default;
133 
134  template <class _TLike, class _ULike>
135  MDSPAN_INLINE_FUNCTION constexpr __compressed_pair(_TLike &&__t, _ULike &&__u)
136  : _U((_ULike &&) __u), __t_val((_TLike &&) __t) {}
137 };
138 
139 // Both empty.
140 template <class _T, class _U>
142  _T, _U,
143  enable_if_t<_MDSPAN_TRAIT(is_empty, _T) && _MDSPAN_TRAIT(is_empty, _U)>>
144  // We need to use the __no_unique_address_emulation wrapper here to avoid
145  // base class ambiguities.
146 #ifdef _MDSPAN_COMPILER_MSVC
147 // MSVC doesn't allow you to access public static member functions of a type
148 // when you *happen* to privately inherit from that type.
151 #else
154 #endif
155 {
158 
159  MDSPAN_FORCE_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 _T &__first() noexcept {
160  return this->__first_base_t::__ref();
161  }
162  MDSPAN_FORCE_INLINE_FUNCTION constexpr _T const &__first() const noexcept {
163  return this->__first_base_t::__ref();
164  }
165  MDSPAN_FORCE_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 _U &__second() noexcept {
166  return this->__second_base_t::__ref();
167  }
168  MDSPAN_FORCE_INLINE_FUNCTION constexpr _U const &__second() const noexcept {
169  return this->__second_base_t::__ref();
170  }
171 
172  MDSPAN_INLINE_FUNCTION_DEFAULTED
173  constexpr __compressed_pair() noexcept = default;
174  MDSPAN_INLINE_FUNCTION_DEFAULTED
175  constexpr __compressed_pair(__compressed_pair const &) noexcept = default;
176  MDSPAN_INLINE_FUNCTION_DEFAULTED
177  constexpr __compressed_pair(__compressed_pair &&) noexcept = default;
178  MDSPAN_INLINE_FUNCTION_DEFAULTED
179  _MDSPAN_CONSTEXPR_14_DEFAULTED __compressed_pair &
180  operator=(__compressed_pair const &) noexcept = default;
181  MDSPAN_INLINE_FUNCTION_DEFAULTED
182  _MDSPAN_CONSTEXPR_14_DEFAULTED __compressed_pair &
183  operator=(__compressed_pair &&) noexcept = default;
184  MDSPAN_INLINE_FUNCTION_DEFAULTED
185  ~__compressed_pair() noexcept = default;
186  template <class _TLike, class _ULike>
187  MDSPAN_INLINE_FUNCTION constexpr __compressed_pair(_TLike &&__t, _ULike &&__u) noexcept
188  : __first_base_t(_T((_TLike &&) __t)),
189  __second_base_t(_U((_ULike &&) __u))
190  { }
191 };
192 
193 #endif // !defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS)
194 
195 } // end namespace detail
196 } // end namespace experimental
197 } // end namespace std
Definition: compressed_pair.hpp:31