1 /*
2 //@HEADER
3 // ************************************************************************
4 //
5 //                        Kokkos v. 3.0
6 //       Copyright (2020) National Technology & Engineering
7 //               Solutions of Sandia, LLC (NTESS).
8 //
9 // Under the terms of Contract DE-NA0003525 with NTESS,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Christian R. Trott (crtrott@sandia.gov)
40 //
41 // ************************************************************************
42 //@HEADER
43 */
44 
45 #ifndef KOKKOS_CORE_IMPL_UTILITIES_HPP
46 #define KOKKOS_CORE_IMPL_UTILITIES_HPP
47 
48 #include <Kokkos_Macros.hpp>
49 #include <cstdint>
50 #include <type_traits>
51 #include <initializer_list>  // in-order comma operator fold emulation
52 #include <utility>           // integer_sequence and friends
53 
54 //----------------------------------------------------------------------------
55 //----------------------------------------------------------------------------
56 
57 namespace Kokkos {
58 namespace Impl {
59 
60 template <typename T>
61 struct identity {
62   using type = T;
63 };
64 
65 template <typename T>
66 using identity_t = typename identity<T>::type;
67 
68 struct not_a_type {
69   not_a_type()                  = delete;
70   ~not_a_type()                 = delete;
71   not_a_type(not_a_type const&) = delete;
72   void operator=(not_a_type const&) = delete;
73 };
74 
75 #if defined(__cpp_lib_void_t)
76 // since C++17
77 using std::void_t;
78 #else
79 template <class...>
80 using void_t = void;
81 #endif
82 
83 //==============================================================================
84 // <editor-fold desc="remove_cvref_t"> {{{1
85 
86 #if defined(__cpp_lib_remove_cvref)
87 // since C++20
88 using std::remove_cvref;
89 using std::remove_cvref_t;
90 #else
91 template <class T>
92 struct remove_cvref {
93   using type = std::remove_cv_t<std::remove_reference_t<T>>;
94 };
95 
96 template <class T>
97 using remove_cvref_t = typename remove_cvref<T>::type;
98 #endif
99 
100 // </editor-fold> end remove_cvref_t }}}1
101 //==============================================================================
102 
103 //==============================================================================
104 // <editor-fold desc="is_specialization_of"> {{{1
105 
106 template <class Type, template <class...> class Template, class Enable = void>
107 struct is_specialization_of : std::false_type {};
108 
109 template <template <class...> class Template, class... Args>
110 struct is_specialization_of<Template<Args...>, Template> : std::true_type {};
111 
112 // </editor-fold> end is_specialization_of }}}1
113 //==============================================================================
114 
115 //==============================================================================
116 // <editor-fold desc="Folding emulation"> {{{1
117 
118 // acts like void for comma fold emulation
119 struct _fold_comma_emulation_return {};
120 
121 template <class... Ts>
122 constexpr KOKKOS_INLINE_FUNCTION _fold_comma_emulation_return
emulate_fold_comma_operator(Ts &&...)123 emulate_fold_comma_operator(Ts&&...) noexcept {
124   return _fold_comma_emulation_return{};
125 }
126 
127 #define KOKKOS_IMPL_FOLD_COMMA_OPERATOR(expr)                                \
128   ::Kokkos::Impl::emulate_fold_comma_operator(                               \
129       ::std::initializer_list<::Kokkos::Impl::_fold_comma_emulation_return>{ \
130           ((expr), ::Kokkos::Impl::_fold_comma_emulation_return{})...})
131 
132 // </editor-fold> end Folding emulation }}}1
133 //==============================================================================
134 
135 //==============================================================================
136 // destruct_delete is a unique_ptr deleter for objects
137 // created by placement new into already allocated memory
138 // by only calling the destructor on the object.
139 //
140 // Because unique_ptr never calls its deleter with a nullptr value,
141 // no need to check if p == nullptr.
142 //
143 // Note:  This differs in interface from std::default_delete in that the
144 // function call operator is templated instead of the class, to make
145 // it easier to use and disallow specialization.
146 struct destruct_delete {
147   template <typename T>
operator ()Kokkos::Impl::destruct_delete148   KOKKOS_INLINE_FUNCTION constexpr void operator()(T* p) const noexcept {
149     p->~T();
150   }
151 };
152 //==============================================================================
153 
154 //==============================================================================
155 // <editor-fold desc="type_list"> {{{1
156 
157 // An intentionally uninstantiateable type_list for metaprogramming purposes
158 template <class...>
159 struct type_list;
160 
161 // </editor-fold> end type_list }}}1
162 //==============================================================================
163 
164 }  // namespace Impl
165 }  // namespace Kokkos
166 
167 #endif  // KOKKOS_CORE_IMPL_UTILITIES_HPP
168