Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
type_traits.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2014-2016, Scott Zuyderduyn
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 The views and conclusions contained in the software and documentation are those
26 of the authors and should not be interpreted as representing official policies,
27 either expressed or implied, of the FreeBSD Project.
28 */
29 
30 //----------------------------------------------------------------------------
31 // type_traits.hpp
32 //
33 // Metaprogramming templates to get type traits at compile-time.
34 //
35 // Author: Scott D. Zuyderduyn, Ph.D. (scott.zuyderduyn@utoronto.ca)
36 //----------------------------------------------------------------------------
37 
38 #pragma once
39 #ifndef ECUDA_TYPE_TRAITS_HPP
40 #define ECUDA_TYPE_TRAITS_HPP
41 
42 #include "global.hpp"
43 
45 
46 #ifdef ECUDA_CPP11_AVAILABLE
47 #include <type_traits>
48 
49 namespace ecuda {
50 
51 template<typename T> using add_const = std::add_const<T>;
52 template<typename T> using remove_const = std::remove_const<T>;
53 template<typename T,typename U> using is_same = std::is_same<T,U>;
54 template<typename T> using add_lvalue_reference = std::add_lvalue_reference<T>;
55 
56 //template<typename T> struct add_const : std::add_const<T> { typedef std::add_const<T>::type type; };
57 //template<typename T> struct add_const<T&> : std::add_const<T&> { typedef std::add_const<T&>::type type; };
58 //template<typename T> struct remove_const : std::remove_const<T> { typedef std::remove_const<T>::type type; };
59 //template<typename T> struct remove_const<const T> : std::remove_const<const T> { typedef std::remove_const<const T>::type type; };
60 //template<typename T,typename U> struct is_same { enum { value = 0 }; };
61 //template<typename T> struct is_same<T,T> { enum { value = 1 }; };
62 
63 template<bool B,typename T> using enable_if = std::enable_if<B,T>;
64 
65 typedef std::true_type true_type;
66 typedef std::false_type false_type;
67 
68 template<typename T> using is_integral = std::is_integral<T>;
69 /*
70 template<typename T> struct is_integral { typedef false_type type; };
71 template<> struct is_integral<bool> { typedef true_type type; };
72 template<> struct is_integral<char> { typedef true_type type; };
73 template<> struct is_integral<signed char> { typedef true_type type; };
74 template<> struct is_integral<unsigned char> { typedef true_type type; };
75 #ifdef _GLIBCXX_USE_WCHAR_T
76 template<> struct is_integral<wchar_t> { typedef true_type type; };
77 #endif
78 template<> struct is_integral<char16_t> { typedef true_type type; };
79 template<> struct is_integral<char32_t> { typedef true_type type; };
80 template<> struct is_integral<short> { typedef true_type type; };
81 template<> struct is_integral<unsigned short> { typedef true_type type; };
82 template<> struct is_integral<int> { typedef true_type type; };
83 template<> struct is_integral<unsigned int> { typedef true_type type; };
84 template<> struct is_integral<long> { typedef true_type type; };
85 template<> struct is_integral<unsigned long> { typedef true_type type; };
86 template<> struct is_integral<long long> { typedef true_type type; };
87 template<> struct is_integral<unsigned long long> { typedef true_type type; };
88 */
89 
90 template<typename T> using is_const = std::is_const<T>;
91 
92 template<typename T> using add_pointer = std::add_pointer<T>;
93 
94 //template<typename T> struct add_pointer : std::add_pointer<T> { typedef std::add_pointer<T>::type; };
95 //template<typename T> struct add_pointer<const T> : std::add_pointer<const T> { typedef std::add_pointer<const T>::type; };
96 
97 } // namespace ecuda
98 
99 #else
100 namespace ecuda {
106 
107 template<typename T> struct add_const { typedef const T type; };
108 template<typename T> struct add_const<T&> { typedef const T& type; };
109 
110 template<typename T> struct remove_const { typedef T type; };
111 template<typename T> struct remove_const<const T> { typedef T type; };
112 
113 template<typename T,typename U> struct is_same { enum { value = 0 }; };
114 template<typename T> struct is_same<T,T> { enum { value = 1 }; };
115 
116 template<typename T> struct remove_reference { typedef T type; };
117 template<typename T> struct remove_reference<T&> { typedef T type; };
118 #ifdef ECUDA_CPP11_AVAILABLE
119 template<typename T> struct remove_reference<T&&> { typedef T type; };
120 #endif
121 
122 template<typename T> struct add_lvalue_reference { typedef T& type; };
123 template<typename T> struct add_lvalue_reference<T&> { typedef T& type; };
124 
125 template<bool B,typename T,typename F> struct conditional { typedef T type; };
126 template<typename T,typename F> struct conditional<false,T,F> { typedef F type; };
127 
128 template<bool B,typename T=void> struct enable_if {};
129 template<typename T> struct enable_if<true,T> { typedef T type; };
130 
131 template<typename T,T v>
132 struct integral_constant {
133  static const/*constexpr*/ T value = v;
134  typedef T value_type;
135  typedef integral_constant<T,v> type;
136  /*constexpr*/ operator T() { return v; }
137 };
138 
139 typedef integral_constant<bool,true> true_type;
140 typedef integral_constant<bool,false> false_type;
141 
142 template<typename T> struct is_integral { typedef false_type type; };
143 template<> struct is_integral<bool> { typedef true_type type; };
144 template<> struct is_integral<char> { typedef true_type type; };
145 template<> struct is_integral<signed char> { typedef true_type type; };
146 template<> struct is_integral<unsigned char> { typedef true_type type; };
147 #ifdef _GLIBCXX_USE_WCHAR_T
148 template<> struct is_integral<wchar_t> { typedef true_type type; };
149 #endif
150 #ifdef ECUDA_CPP11_AVAILABLE
151 template<> struct is_integral<char16_t> { typedef true_type type; };
152 template<> struct is_integral<char32_t> { typedef true_type type; };
153 #endif
154 template<> struct is_integral<short> { typedef true_type type; };
155 template<> struct is_integral<unsigned short> { typedef true_type type; };
156 template<> struct is_integral<int> { typedef true_type type; };
157 template<> struct is_integral<unsigned int> { typedef true_type type; };
158 template<> struct is_integral<long> { typedef true_type type; };
159 template<> struct is_integral<unsigned long> { typedef true_type type; };
160 template<> struct is_integral<long long> { typedef true_type type; };
161 template<> struct is_integral<unsigned long long> { typedef true_type type; };
162 
163 template<typename T> struct is_const : false_type {};
164 template<typename T> struct is_const<const T> : true_type {};
165 
166 template<typename T> struct add_pointer { typedef typename remove_reference<T>::type* type; };
167 //template<typename T> struct add_pointer<const T> { typedef const T* type; }; // confirm this is needed
168 
169 template<typename T> struct remove_pointer { typedef T type; };
170 template<typename T> struct remove_pointer<T*> { typedef T type; };
171 template<typename T> struct remove_pointer<T* const> { typedef T type; };
172 template<typename T> struct remove_pointer<T* volatile> { typedef T type; };
173 template<typename T> struct remove_pointer<T* const volatile> { typedef T type; };
174 
175 } // namespace std
176 #endif
177 
178 namespace ecuda {
179 
183 //template<typename T> class naked_ptr; // forward declaration // deprecated
184 template<typename T,typename P> class padded_ptr; // forward declaration
185 template<typename T> class shared_ptr; // forward declaration
186 template<typename T,typename P> class striding_ptr; // forward declaration
187 template<typename T,typename P> class unique_ptr; // forward declaration
188 template<typename T,typename P> class striding_padded_ptr; // forward declaration
189 
201 template<typename T,typename U> __HOST__ __DEVICE__ T naked_cast( U* ptr ) { return reinterpret_cast<T>(ptr); }
202 template<typename T> __HOST__ __DEVICE__ T naked_cast( T* ptr ) { return ptr; }
203 //template<typename T,typename U> __HOST__ __DEVICE__ T naked_cast( const naked_ptr<U>& ptr ) { return naked_cast<T>(ptr.get()); }
204 template<typename T,typename U,typename V> __HOST__ __DEVICE__ T naked_cast( const unique_ptr<U,V>& ptr ) { return naked_cast<T>(ptr.get()); }
205 template<typename T,typename U> __HOST__ __DEVICE__ T naked_cast( const shared_ptr<U>& ptr ) { return naked_cast<T>(ptr.get()); }
206 template<typename T,typename U,typename V> __HOST__ __DEVICE__ T naked_cast( const padded_ptr<U,V>& ptr ) { return naked_cast<T>(ptr.get()); }
207 template<typename T,typename U,typename V> __HOST__ __DEVICE__ T naked_cast( const striding_ptr<U,V>& ptr ) { return naked_cast<T>(ptr.get()); }
208 
224 template<typename T> struct make_unmanaged;
225 template<typename T> struct make_unmanaged< T* > { typedef T* type; };
226 template<typename T> struct make_unmanaged< const T* > { typedef const T* type; };
227 //template<typename T> struct make_unmanaged< naked_ptr<T> > { typedef naked_ptr<T> type; };
228 //template<typename T> struct make_unmanaged< const naked_ptr<T> > { typedef naked_ptr<T> type; };
229 template<typename T,typename U> struct make_unmanaged< unique_ptr<T,U> > { typedef typename unique_ptr<T,U>::pointer type; };
230 template<typename T,typename U> struct make_unmanaged< const unique_ptr<T,U> > { typedef typename unique_ptr<T,U>::pointer type; };
231 template<typename T> struct make_unmanaged< shared_ptr<T> > { typedef typename ecuda::add_pointer<T>::type type; };
232 template<typename T> struct make_unmanaged< const shared_ptr<T> > { typedef typename ecuda::add_pointer<T>::type type; };
233 template<typename T,typename U> struct make_unmanaged< padded_ptr<T,U> > { typedef padded_ptr<T,typename make_unmanaged<U>::type> type; };
234 template<typename T,typename U> struct make_unmanaged< const padded_ptr<T,U> > { typedef padded_ptr<T,typename make_unmanaged<U>::type> type; };
235 template<typename T,typename U> struct make_unmanaged< striding_ptr<T,U> > { typedef striding_ptr<T,typename make_unmanaged<U>::type> type; };
236 template<typename T,typename U> struct make_unmanaged< const striding_ptr<T,U> > { typedef striding_ptr<T,typename make_unmanaged<U>::type> type; };
237 template<typename T,typename U> struct make_unmanaged< striding_padded_ptr<T,U> > { typedef striding_padded_ptr<T,typename make_unmanaged<U>::type> type; };
238 template<typename T,typename U> struct make_unmanaged< const striding_padded_ptr<T,U> > { typedef striding_padded_ptr<T,typename make_unmanaged<U>::type> type; };
239 
240 
256 template<typename T> __HOST__ __DEVICE__ inline typename make_unmanaged<T*>::type unmanaged_cast( T* ptr ) { return ptr; }
257 
258 //template<typename T> __HOST__ __DEVICE__ inline typename make_unmanaged< naked_ptr<T> >::type unmanaged_cast( const naked_ptr<T>& ptr ) { return naked_ptr<T>(ptr); }
259 
260 template<typename T,typename U>
261 __HOST__ __DEVICE__ inline
262 typename make_unmanaged<U>::type unmanaged_cast( const unique_ptr<T,U>& ptr )
263 {
264  return typename make_unmanaged<U>::type( ptr.get() );
265 }
266 
267 template<typename T>
268 __HOST__ __DEVICE__ inline
269 typename make_unmanaged< shared_ptr<T> >::type
270 unmanaged_cast( const shared_ptr<T>& ptr )
271 {
272  return typename make_unmanaged< shared_ptr<T> >::type( ptr.get() );
273 }
274 
275 template<typename T,typename U>
276 __HOST__ __DEVICE__ inline
277 padded_ptr<T,typename make_unmanaged<U>::type>
278 unmanaged_cast( const padded_ptr<T,U>& ptr )
279 {
280  //typename make_unmanaged<U>::type mp1 = unmanaged_cast( ptr.get_edge() );
281  typename make_unmanaged<U>::type mp = unmanaged_cast( ptr.get() );
282  return padded_ptr<T,typename make_unmanaged<U>::type>( mp, ptr.get_pitch() ); //, ptr.get_width(), mp2 );
283 }
284 
285 template<typename T,typename U>
286 __HOST__ __DEVICE__ inline
287 striding_ptr<T,typename make_unmanaged<U>::type>
288 unmanaged_cast( const striding_ptr<T,U>& ptr )
289 {
290  typename make_unmanaged<U>::type mp = unmanaged_cast( ptr.get() );
291  return striding_ptr<T,typename make_unmanaged<U>::type>( mp, ptr.get_stride() );
292 }
293 
294 template<typename T,typename U>
295 __HOST__ __DEVICE__ inline
296 striding_padded_ptr<T,typename make_unmanaged<U>::type>
297 unmanaged_cast( const striding_padded_ptr<T,U>& ptr )
298 {
299  typename make_unmanaged<U>::type mp = unmanaged_cast( ptr.get() );
300  return striding_padded_ptr<T,typename make_unmanaged<U>::type>( mp, ptr.get_stride() );
301 }
302 
315 template<typename T> struct make_const;
316 template<typename T> struct make_const< T* > { typedef const T* type; };
317 template<typename T> struct make_const< const T* > { typedef const T* type; };
318 //template<typename T> struct make_const< naked_ptr<T> > { typedef naked_ptr<const T> type; };
319 //template<typename T> struct make_const< naked_ptr<const T> > { typedef naked_ptr<const T> type; };
320 template<typename T,typename U> struct make_const< unique_ptr<T,U> > { typedef unique_ptr<const T,typename make_const<U>::type> type; };
321 template<typename T,typename U> struct make_const< unique_ptr<const T,U> > { typedef unique_ptr<const T,typename make_const<U>::type> type; };
322 template<typename T> struct make_const< shared_ptr<T> > { typedef shared_ptr<const T> type; };
323 template<typename T> struct make_const< shared_ptr<const T> > { typedef shared_ptr<const T> type; };
324 template<typename T,typename U> struct make_const< padded_ptr<T,U> > { typedef padded_ptr<const T,typename make_const<U>::type> type; };
325 template<typename T,typename U> struct make_const< padded_ptr<const T,U> > { typedef padded_ptr<const T,typename make_const<U>::type> type; };
326 template<typename T,typename U> struct make_const< striding_ptr<T,U> > { typedef striding_ptr<const T,typename make_const<U>::type> type; };
327 template<typename T,typename U> struct make_const< striding_ptr<const T,U> > { typedef striding_ptr<const T,typename make_const<U>::type> type; };
328 
329 template<typename T,typename U> struct make_const< striding_padded_ptr<T,U> > { typedef striding_padded_ptr<const T,typename make_const<U>::type> type; };
330 template<typename T,typename U> struct make_const< striding_padded_ptr<const T,U> > { typedef striding_padded_ptr<const T,typename make_const<U>::type> type; };
331 
337 template<typename T> struct make_unmanaged_const { typedef typename make_unmanaged<typename make_const<T>::type>::type type; };
338 
339 } // namespace ecuda
340 
342 
343 #endif
#define __HOST__
Definition: global.hpp:150
#define __DEVICE__
Definition: global.hpp:151