1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/*
* Copyright 2008-2021 NVIDIA Corporation
*
* 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.
*/
/*! \file
* \brief A pointer to a variable which resides in memory associated with a
* system.
*/
#pragma once
#include <thrust/detail/config.h>
#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
# pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
# pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header
#include <thrust/iterator/iterator_adaptor.h>
#include <thrust/iterator/detail/iterator_traversal_tags.h>
#include <thrust/type_traits/remove_cvref.h>
#include <thrust/detail/type_traits/pointer_traits.h>
#include <thrust/detail/type_traits.h>
#include <thrust/detail/reference_forward_declaration.h>
#include <ostream>
#include <cstddef>
THRUST_NAMESPACE_BEGIN
template <typename Element, typename Tag, typename Reference = use_default, typename Derived = use_default>
class pointer;
// Specialize `thrust::iterator_traits` to avoid problems with the name of
// pointer's constructor shadowing its nested pointer type. We do this before
// pointer is defined so the specialization is correctly used inside the
// definition.
template <typename Element, typename Tag, typename Reference, typename Derived>
struct iterator_traits<thrust::pointer<Element, Tag, Reference, Derived>>
{
using pointer = thrust::pointer<Element, Tag, Reference, Derived>;
using iterator_category = typename pointer::iterator_category;
using value_type = typename pointer::value_type;
using difference_type = typename pointer::difference_type;
using reference = typename pointer::reference;
};
THRUST_NAMESPACE_END
namespace std
{
template <typename Element, typename Tag, typename Reference, typename Derived>
struct iterator_traits<THRUST_NS_QUALIFIER::pointer<Element, Tag, Reference, Derived>>
{
using pointer = THRUST_NS_QUALIFIER::pointer<Element, Tag, Reference, Derived>;
using iterator_category = typename pointer::iterator_category;
using value_type = typename pointer::value_type;
using difference_type = typename pointer::difference_type;
using reference = typename pointer::reference;
};
} // namespace std
THRUST_NAMESPACE_BEGIN
namespace detail
{
// this metafunction computes the type of iterator_adaptor thrust::pointer should inherit from
template<typename Element, typename Tag, typename Reference, typename Derived>
struct pointer_base
{
// void pointers should have no element type
// note that we remove_cv from the Element type to get the value_type
typedef typename thrust::detail::eval_if<
thrust::detail::is_void<typename thrust::remove_cvref<Element>::type>::value,
thrust::detail::identity_<void>,
thrust::detail::remove_cv<Element>
>::type value_type;
// if no Derived type is given, just use pointer
typedef typename thrust::detail::eval_if<
thrust::detail::is_same<Derived,use_default>::value,
thrust::detail::identity_<pointer<Element,Tag,Reference,Derived> >,
thrust::detail::identity_<Derived>
>::type derived_type;
// void pointers should have no reference type
// if no Reference type is given, just use reference
typedef typename thrust::detail::eval_if<
thrust::detail::is_void<typename thrust::remove_cvref<Element>::type>::value,
thrust::detail::identity_<void>,
thrust::detail::eval_if<
thrust::detail::is_same<Reference,use_default>::value,
thrust::detail::identity_<reference<Element,derived_type> >,
thrust::detail::identity_<Reference>
>
>::type reference_type;
typedef thrust::iterator_adaptor<
derived_type, // pass along the type of our Derived class to iterator_adaptor
Element *, // we adapt a raw pointer
value_type, // the value type
Tag, // system tag
thrust::random_access_traversal_tag, // pointers have random access traversal
reference_type, // pass along our Reference type
std::ptrdiff_t
> type;
}; // end pointer_base
} // end detail
// the base type for all of thrust's tagged pointers.
// for reasonable pointer-like semantics, derived types should reimplement the following:
// 1. no-argument constructor
// 2. constructor from OtherElement *
// 3. constructor from OtherPointer related by convertibility
// 4. constructor from OtherPointer to void
// 5. assignment from OtherPointer related by convertibility
// These should just call the corresponding members of pointer.
template<typename Element, typename Tag, typename Reference, typename Derived>
class pointer
: public thrust::detail::pointer_base<Element,Tag,Reference,Derived>::type
{
private:
typedef typename thrust::detail::pointer_base<Element,Tag,Reference,Derived>::type super_t;
typedef typename thrust::detail::pointer_base<Element,Tag,Reference,Derived>::derived_type derived_type;
// friend iterator_core_access to give it access to dereference
friend class thrust::iterator_core_access;
__host__ __device__
typename super_t::reference dereference() const;
// don't provide access to this part of super_t's interface
using super_t::base;
using typename super_t::base_type;
public:
typedef typename super_t::base_type raw_pointer;
// constructors
__host__ __device__
pointer();
// NOTE: This is needed so that Thrust smart pointers can be used in
// `std::unique_ptr`.
__host__ __device__
pointer(std::nullptr_t);
// OtherValue shall be convertible to Value
// XXX consider making the pointer implementation a template parameter which defaults to Element *
template<typename OtherElement>
__host__ __device__
explicit pointer(OtherElement *ptr);
// OtherPointer's element_type shall be convertible to Element
// OtherPointer's system shall be convertible to Tag
template<typename OtherPointer>
__host__ __device__
pointer(const OtherPointer &other,
typename thrust::detail::enable_if_pointer_is_convertible<
OtherPointer,
pointer<Element,Tag,Reference,Derived>
>::type * = 0);
// OtherPointer's element_type shall be void
// OtherPointer's system shall be convertible to Tag
template<typename OtherPointer>
__host__ __device__
explicit
pointer(const OtherPointer &other,
typename thrust::detail::enable_if_void_pointer_is_system_convertible<
OtherPointer,
pointer<Element,Tag,Reference,Derived>
>::type * = 0);
// assignment
// NOTE: This is needed so that Thrust smart pointers can be used in
// `std::unique_ptr`.
__host__ __device__
derived_type& operator=(std::nullptr_t);
// OtherPointer's element_type shall be convertible to Element
// OtherPointer's system shall be convertible to Tag
template<typename OtherPointer>
__host__ __device__
typename thrust::detail::enable_if_pointer_is_convertible<
OtherPointer,
pointer,
derived_type &
>::type
operator=(const OtherPointer &other);
// observers
__host__ __device__
Element *get() const;
__host__ __device__
Element *operator->() const;
// NOTE: This is needed so that Thrust smart pointers can be used in
// `std::unique_ptr`.
__host__ __device__
explicit operator bool() const;
__host__ __device__
static derived_type pointer_to(typename thrust::detail::pointer_traits_detail::pointer_to_param<Element>::type r)
{
return thrust::detail::pointer_traits<derived_type>::pointer_to(r);
}
}; // end pointer
// Output stream operator
template<typename Element, typename Tag, typename Reference, typename Derived,
typename charT, typename traits>
__host__
std::basic_ostream<charT, traits> &
operator<<(std::basic_ostream<charT, traits> &os,
const pointer<Element, Tag, Reference, Derived> &p);
// NOTE: This is needed so that Thrust smart pointers can be used in
// `std::unique_ptr`.
template <typename Element, typename Tag, typename Reference, typename Derived>
__host__ __device__
bool operator==(std::nullptr_t, pointer<Element, Tag, Reference, Derived> p);
template <typename Element, typename Tag, typename Reference, typename Derived>
__host__ __device__
bool operator==(pointer<Element, Tag, Reference, Derived> p, std::nullptr_t);
template <typename Element, typename Tag, typename Reference, typename Derived>
__host__ __device__
bool operator!=(std::nullptr_t, pointer<Element, Tag, Reference, Derived> p);
template <typename Element, typename Tag, typename Reference, typename Derived>
__host__ __device__
bool operator!=(pointer<Element, Tag, Reference, Derived> p, std::nullptr_t);
THRUST_NAMESPACE_END
#include <thrust/detail/pointer.inl>
|