Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
unique_ptr.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 // ptr/unique_ptr.hpp
32 //
33 // A managed pointer to device memory.
34 //
35 // Author: Scott D. Zuyderduyn, Ph.D. (scott.zuyderduyn@utoronto.ca)
36 //----------------------------------------------------------------------------
37 
38 #ifndef ECUDA_PTR_UNIQUE_PTR_HPP
39 #define ECUDA_PTR_UNIQUE_PTR_HPP
40 
41 #include <iostream>
42 
43 #include "../global.hpp"
44 #include "common.hpp"
45 #include "../type_traits.hpp"
46 //#include "../algorithm.hpp"
47 
48 namespace ecuda {
49 
83 template< typename T, class Deleter=default_device_delete<T> >
85 {
86 
87  /* SFINAE strat used in c++ stdlib to get T::pointer if it exists - unfortunately
88  * relies on decltype which we don't have prior to C++11 but we can do without
89  * this for now.
90  class _Pointer {
91  template<typename U> static typename U::pointer test(typename U::pointer*);
92  template<typename U> static T* test(...);
93  typedef typename std::remove_reference<Deleter>::type _Del;
94  public:
95  typedef decltype(test<Deleter>(0)) type;
96  };
97  */
98 
99 public:
100  typedef T element_type;
101  typedef typename ecuda::add_pointer<T>::type pointer;
102  typedef Deleter deleter_type;
103 
104 private:
105  pointer current_ptr;
106  deleter_type deleter;
107 
108 private:
109  __HOST__ __DEVICE__ unique_ptr( const unique_ptr& ); // disabled
110 
111 public:
113  __HOST__ __DEVICE__ explicit unique_ptr( T* ptr ) __NOEXCEPT__ : current_ptr(ptr) {}
114  __HOST__ __DEVICE__ unique_ptr( T* ptr, Deleter deleter ) __NOEXCEPT__ : current_ptr(ptr), deleter(deleter) {}
115 
116  #ifdef ECUDA_CPP11_AVAILABLE
117  __HOST__ __DEVICE__ unique_ptr( unique_ptr&& src ) __NOEXCEPT__ : current_ptr(src.release()) {}
118  template<typename U,class E> __HOST__ __DEVICE__ unique_ptr( unique_ptr<U,E>&& src ) __NOEXCEPT__ : current_ptr(src.release()), deleter_type(src.get_deleter()) {}
119  #endif
120 
121  __HOST__ __DEVICE__ ~unique_ptr() { deleter(current_ptr); }
122 
123  #ifdef ECUDA_CPP11_AVAILABLE
124  //TODO: review this block
125  __HOST__ __DEVICE__ inline unique_ptr& operator=( unique_ptr&& src ) __NOEXCEPT__ {
126  current_ptr = std::move(src.ptr);
127  deleter = std::move(src.deleter);
128  return *this;
129  }
130  template<typename U,class E> __HOST__ __DEVICE__ inline unique_ptr& operator=( unique_ptr<U,E>&& src ) __NOEXCEPT__ {
131  current_ptr = std::move(src.ptr);
132  deleter = std::move(src.deleter);
133  return *this;
134  }
135  #endif
136 
137  //template<typename U>
138  //__HOST__ __DEVICE__ inline unique_ptr& operator=( U* ptr ) {
139  // reset(release());
140  // current_ptr = ptr;
141  // return *this;
142  //}
143 
145  pointer old_ptr = current_ptr;
146  current_ptr = NULL;
147  return old_ptr;
148  }
149 
151  pointer old_ptr = current_ptr;
152  current_ptr = ptr;
153  if( old_ptr ) get_deleter()( old_ptr );
154  }
155 
156  __HOST__ __DEVICE__ inline void swap( unique_ptr& other ) __NOEXCEPT__ { ::ecuda::swap( current_ptr, other.current_ptr ); }
157 
158  __HOST__ __DEVICE__ inline pointer get() const { return current_ptr; }
159 
160  __HOST__ __DEVICE__ inline deleter_type& get_deleter() { return deleter; }
161  __HOST__ __DEVICE__ inline const deleter_type& get_deleter() const { return deleter; }
162 
163  #ifdef ECUDA_CPP11_AVAILABLE
164  __HOST__ __DEVICE__ explicit operator bool() const { return get() != NULL; }
165  #else
166  __HOST__ __DEVICE__ operator bool() const { return get() != NULL; }
167  #endif
168 
169  __DEVICE__ inline typename ecuda::add_lvalue_reference<T>::type operator*() const __NOEXCEPT__ { return *current_ptr; }
170 
171  __HOST__ __DEVICE__ inline pointer operator->() const __NOEXCEPT__ { return current_ptr; }
172 
173  __DEVICE__ inline typename ecuda::add_lvalue_reference<T>::type operator[]( std::size_t i ) const {
174  //return *pointer_traits<pointer>().increment( current_ptr, i );
175  return *(current_ptr+i);
176  }
177 
178  template<typename T2,class D2> __HOST__ __DEVICE__ bool operator==( const unique_ptr<T2,D2>& other ) const { return get() == other.get(); }
179  template<typename T2,class D2> __HOST__ __DEVICE__ bool operator!=( const unique_ptr<T2,D2>& other ) const { return get() != other.get(); }
180  template<typename T2,class D2> __HOST__ __DEVICE__ bool operator< ( const unique_ptr<T2,D2>& other ) const { return get() < other.get(); }
181  template<typename T2,class D2> __HOST__ __DEVICE__ bool operator> ( const unique_ptr<T2,D2>& other ) const { return get() > other.get(); }
182  template<typename T2,class D2> __HOST__ __DEVICE__ bool operator<=( const unique_ptr<T2,D2>& other ) const { return get() <= other.get(); }
183  template<typename T2,class D2> __HOST__ __DEVICE__ bool operator>=( const unique_ptr<T2,D2>& other ) const { return get() >= other.get(); }
184 
185 };
186 
187 } // namespace ecuda
188 
189 #endif
__DEVICE__ ecuda::add_lvalue_reference< T >::type operator*() const __NOEXCEPT__
Definition: unique_ptr.hpp:169
__HOST__ __DEVICE__ pointer release() __NOEXCEPT__
Definition: unique_ptr.hpp:144
#define __CONSTEXPR__
Definition: global.hpp:141
A smart pointer that retains sole ownership of an object.
Definition: unique_ptr.hpp:84
__HOST__ __DEVICE__ bool operator==(const unique_ptr< T2, D2 > &other) const
Definition: unique_ptr.hpp:178
#define __NOEXCEPT__
Definition: global.hpp:140
__HOST__ __DEVICE__ void reset(pointer ptr=pointer()) __NOEXCEPT__
Definition: unique_ptr.hpp:150
#define __HOST__
Definition: global.hpp:150
__HOST__ __DEVICE__ __CONSTEXPR__ unique_ptr() __NOEXCEPT__
Definition: unique_ptr.hpp:112
__HOST__ __DEVICE__ void swap(T &a, T &b) __NOEXCEPT__
Definition: algorithm.hpp:54
__HOST__ __DEVICE__ ~unique_ptr()
Definition: unique_ptr.hpp:121
__HOST__ __DEVICE__ unique_ptr(T *ptr) __NOEXCEPT__
Definition: unique_ptr.hpp:113
__HOST__ __DEVICE__ bool operator>=(const unique_ptr< T2, D2 > &other) const
Definition: unique_ptr.hpp:183
__HOST__ __DEVICE__ bool operator!=(const unique_ptr< T2, D2 > &other) const
Definition: unique_ptr.hpp:179
__HOST__ __DEVICE__ void swap(unique_ptr &other) __NOEXCEPT__
Definition: unique_ptr.hpp:156
__HOST__ __DEVICE__ unique_ptr(T *ptr, Deleter deleter) __NOEXCEPT__
Definition: unique_ptr.hpp:114
__DEVICE__ ecuda::add_lvalue_reference< T >::type operator[](std::size_t i) const
Definition: unique_ptr.hpp:173
__HOST__ __DEVICE__ pointer get() const
Definition: unique_ptr.hpp:158
__HOST__ __DEVICE__ bool operator>(const unique_ptr< T2, D2 > &other) const
Definition: unique_ptr.hpp:181
__HOST__ __DEVICE__ const deleter_type & get_deleter() const
Definition: unique_ptr.hpp:161
__HOST__ __DEVICE__ deleter_type & get_deleter()
Definition: unique_ptr.hpp:160
#define __DEVICE__
Definition: global.hpp:151
__HOST__ __DEVICE__ pointer operator->() const __NOEXCEPT__
Definition: unique_ptr.hpp:171
ecuda::add_pointer< T >::type pointer
Definition: unique_ptr.hpp:101