OSDN Git Service

CUDA
[eos/hostdependX86LINUX64.git] / util / X86LINUX64 / cuda-6.5 / include / thrust / system / cuda / detail / detail / uninitialized.h
1 /*
2  *  Copyright 2008-2013 NVIDIA Corporation
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 #pragma once
18
19 #include <thrust/detail/config.h>
20 #include <thrust/system/cuda/detail/detail/alignment.h>
21 #include <cstddef>
22 #include <new>
23
24 namespace thrust
25 {
26 namespace system
27 {
28 namespace cuda
29 {
30 namespace detail
31 {
32 namespace detail
33 {
34
35
36 template<typename T>
37   class uninitialized
38 {
39   private:
40     typename aligned_storage<
41       sizeof(T),
42       alignment_of<T>::value
43     >::type storage;
44
45     __device__ __thrust_forceinline__ const T* ptr() const
46     {
47       return reinterpret_cast<const T*>(storage.data);
48     }
49
50     __device__ __thrust_forceinline__ T* ptr()
51     {
52       return reinterpret_cast<T*>(storage.data);
53     }
54
55   public:
56     // copy assignment
57     __device__ __thrust_forceinline__ uninitialized<T> &operator=(const T &other)
58     {
59       T& self = *this;
60       self = other;
61       return *this;
62     }
63
64     __device__ __thrust_forceinline__ T& get()
65     {
66       return *ptr();
67     }
68
69     __device__ __thrust_forceinline__ const T& get() const
70     {
71       return *ptr();
72     }
73
74     __device__ __thrust_forceinline__ operator T& ()
75     {
76       return get();
77     }
78
79     __device__ __thrust_forceinline__ operator const T&() const
80     {
81       return get();
82     }
83
84     __thrust_forceinline__ __device__ void construct()
85     {
86       ::new(ptr()) T();
87     }
88
89     template<typename Arg>
90     __thrust_forceinline__ __device__ void construct(const Arg &a)
91     {
92       ::new(ptr()) T(a);
93     }
94
95     template<typename Arg1, typename Arg2>
96     __thrust_forceinline__ __device__ void construct(const Arg1 &a1, const Arg2 &a2)
97     {
98       ::new(ptr()) T(a1,a2);
99     }
100
101     template<typename Arg1, typename Arg2, typename Arg3>
102     __thrust_forceinline__ __device__ void construct(const Arg1 &a1, const Arg2 &a2, const Arg3 &a3)
103     {
104       ::new(ptr()) T(a1,a2,a3);
105     }
106
107     template<typename Arg1, typename Arg2, typename Arg3, typename Arg4>
108     __thrust_forceinline__ __device__ void construct(const Arg1 &a1, const Arg2 &a2, const Arg3 &a3, const Arg4 &a4)
109     {
110       ::new(ptr()) T(a1,a2,a3,a4);
111     }
112
113     template<typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
114     __thrust_forceinline__ __device__ void construct(const Arg1 &a1, const Arg2 &a2, const Arg3 &a3, const Arg4 &a4, const Arg5 &a5)
115     {
116       ::new(ptr()) T(a1,a2,a3,a4,a5);
117     }
118
119     template<typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5, typename Arg6>
120     __thrust_forceinline__ __device__ void construct(const Arg1 &a1, const Arg2 &a2, const Arg3 &a3, const Arg4 &a4, const Arg5 &a5, const Arg6 &a6)
121     {
122       ::new(ptr()) T(a1,a2,a3,a4,a5,a6);
123     }
124
125     template<typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5, typename Arg6, typename Arg7>
126     __thrust_forceinline__ __device__ void construct(const Arg1 &a1, const Arg2 &a2, const Arg3 &a3, const Arg4 &a4, const Arg5 &a5, const Arg6 &a6, const Arg7 &a7)
127     {
128       ::new(ptr()) T(a1,a2,a3,a4,a5,a6,a7);
129     }
130
131     template<typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5, typename Arg6, typename Arg7, typename Arg8>
132     __thrust_forceinline__ __device__ void construct(const Arg1 &a1, const Arg2 &a2, const Arg3 &a3, const Arg4 &a4, const Arg5 &a5, const Arg6 &a6, const Arg7 &a7, const Arg8 &a8)
133     {
134       ::new(ptr()) T(a1,a2,a3,a4,a5,a6,a7,a8);
135     }
136
137     template<typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5, typename Arg6, typename Arg7, typename Arg8, typename Arg9>
138     __thrust_forceinline__ __device__ void construct(const Arg1 &a1, const Arg2 &a2, const Arg3 &a3, const Arg4 &a4, const Arg5 &a5, const Arg6 &a6, const Arg7 &a7, const Arg8 &a8, const Arg9 &a9)
139     {
140       ::new(ptr()) T(a1,a2,a3,a4,a5,a6,a7,a8,a9);
141     }
142
143     template<typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5, typename Arg6, typename Arg7, typename Arg8, typename Arg9, typename Arg10>
144     __thrust_forceinline__ __device__ void construct(const Arg1 &a1, const Arg2 &a2, const Arg3 &a3, const Arg4 &a4, const Arg5 &a5, const Arg6 &a6, const Arg7 &a7, const Arg8 &a8, const Arg9 &a9, const Arg10 &a10)
145     {
146       ::new(ptr()) T(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10);
147     }
148
149     __thrust_forceinline__ __device__ void destroy()
150     {
151       T& self = *this;
152       self.~T();
153     }
154 };
155
156
157 template<typename T, std::size_t N>
158   class uninitialized_array
159 {
160   public:
161     typedef T             value_type; 
162     typedef T&            reference;
163     typedef const T&      const_reference;
164     typedef T*            pointer;
165     typedef const T*      const_pointer;
166     typedef pointer       iterator;
167     typedef const_pointer const_iterator;
168     typedef std::size_t   size_type;
169
170     __thrust_forceinline__ __device__ iterator begin()
171     {
172       return data();
173     }
174
175     __thrust_forceinline__ __device__ const_iterator begin() const
176     {
177       return data();
178     }
179
180     __thrust_forceinline__ __device__ iterator end()
181     {
182       return begin() + size();
183     }
184
185     __thrust_forceinline__ __device__ const_iterator end() const
186     {
187       return begin() + size();
188     }
189
190     __thrust_forceinline__ __device__ const_iterator cbegin() const
191     {
192       return begin();
193     }
194
195     __thrust_forceinline__ __device__ const_iterator cend() const
196     {
197       return end();
198     }
199
200     __thrust_forceinline__ __device__ size_type size() const
201     {
202       return N;
203     }
204
205     __thrust_forceinline__ __device__ bool empty() const
206     {
207       return false;
208     }
209
210     __thrust_forceinline__ __device__ T* data()
211     {
212       return impl.get();
213     }
214
215     __thrust_forceinline__ __device__ const T* data() const
216     {
217       return impl.get();
218     }
219
220     // element access
221     __thrust_forceinline__ __device__ reference operator[](size_type n)
222     {
223       return data()[n];
224     }
225
226     __thrust_forceinline__ __device__ const_reference operator[](size_type n) const
227     {
228       return data()[n];
229     }
230
231     __thrust_forceinline__ __device__ reference front()
232     {
233       return *data();
234     }
235
236     __thrust_forceinline__ __device__ const_reference front() const
237     {
238       return *data();
239     }
240
241     __thrust_forceinline__ __device__ reference back()
242     {
243       return data()[size() - size_type(1)];
244     }
245
246     __thrust_forceinline__ __device__ const_reference back() const
247     {
248       return data()[size() - size_type(1)];
249     }
250
251   private:
252     uninitialized<T[N]> impl;
253 };
254
255
256 } // end detail
257 } // end detail
258 } // end cuda
259 } // end system
260 } // end thrust
261