value_init and swap work on nvidia gpu's

This commit is contained in:
Eric Niebler 2014-05-01 15:29:43 -07:00
parent de0e18ca0a
commit 379e2111e2
2 changed files with 32 additions and 2 deletions

View File

@ -21,12 +21,15 @@
// avoid ambiguity when swapping objects of a Boost type that does // avoid ambiguity when swapping objects of a Boost type that does
// not have its own boost::swap overload. // not have its own boost::swap overload.
#include <algorithm> //for std::swap #include <utility> //for std::swap (C++11)
#include <algorithm> //for std::swap (C++98)
#include <cstddef> //for std::size_t #include <cstddef> //for std::size_t
#include <boost/config.hpp>
namespace boost_swap_impl namespace boost_swap_impl
{ {
template<class T> template<class T>
BOOST_GPU_ENABLED
void swap_impl(T& left, T& right) void swap_impl(T& left, T& right)
{ {
using namespace std;//use std::swap if argument dependent lookup fails using namespace std;//use std::swap if argument dependent lookup fails
@ -34,6 +37,7 @@ namespace boost_swap_impl
} }
template<class T, std::size_t N> template<class T, std::size_t N>
BOOST_GPU_ENABLED
void swap_impl(T (& left)[N], T (& right)[N]) void swap_impl(T (& left)[N], T (& right)[N])
{ {
for (std::size_t i = 0; i < N; ++i) for (std::size_t i = 0; i < N; ++i)
@ -46,6 +50,7 @@ namespace boost_swap_impl
namespace boost namespace boost
{ {
template<class T1, class T2> template<class T1, class T2>
BOOST_GPU_ENABLED
void swap(T1& left, T2& right) void swap(T1& left, T2& right)
{ {
::boost_swap_impl::swap_impl(left, right); ::boost_swap_impl::swap_impl(left, right);

View File

@ -71,12 +71,14 @@ class initialized
#endif #endif
remove_const<T>::type data; remove_const<T>::type data;
BOOST_GPU_ENABLED
wrapper() wrapper()
: :
data() data()
{ {
} }
BOOST_GPU_ENABLED
wrapper(T const & arg) wrapper(T const & arg)
: :
data(arg) data(arg)
@ -90,6 +92,7 @@ class initialized
#endif #endif
aligned_storage<sizeof(wrapper), alignment_of<wrapper>::value>::type x; aligned_storage<sizeof(wrapper), alignment_of<wrapper>::value>::type x;
BOOST_GPU_ENABLED
wrapper * wrapper_address() const wrapper * wrapper_address() const
{ {
return static_cast<wrapper *>( static_cast<void*>(&x)); return static_cast<wrapper *>( static_cast<void*>(&x));
@ -97,6 +100,7 @@ class initialized
public : public :
BOOST_GPU_ENABLED
initialized() initialized()
{ {
#if BOOST_DETAIL_VALUE_INIT_WORKAROUND #if BOOST_DETAIL_VALUE_INIT_WORKAROUND
@ -105,16 +109,19 @@ class initialized
new (wrapper_address()) wrapper(); new (wrapper_address()) wrapper();
} }
BOOST_GPU_ENABLED
initialized(initialized const & arg) initialized(initialized const & arg)
{ {
new (wrapper_address()) wrapper( static_cast<wrapper const &>(*(arg.wrapper_address()))); new (wrapper_address()) wrapper( static_cast<wrapper const &>(*(arg.wrapper_address())));
} }
BOOST_GPU_ENABLED
explicit initialized(T const & arg) explicit initialized(T const & arg)
{ {
new (wrapper_address()) wrapper(arg); new (wrapper_address()) wrapper(arg);
} }
BOOST_GPU_ENABLED
initialized & operator=(initialized const & arg) initialized & operator=(initialized const & arg)
{ {
// Assignment is only allowed when T is non-const. // Assignment is only allowed when T is non-const.
@ -123,31 +130,37 @@ class initialized
return *this; return *this;
} }
BOOST_GPU_ENABLED
~initialized() ~initialized()
{ {
wrapper_address()->wrapper::~wrapper(); wrapper_address()->wrapper::~wrapper();
} }
BOOST_GPU_ENABLED
T const & data() const T const & data() const
{ {
return wrapper_address()->data; return wrapper_address()->data;
} }
BOOST_GPU_ENABLED
T& data() T& data()
{ {
return wrapper_address()->data; return wrapper_address()->data;
} }
BOOST_GPU_ENABLED
void swap(initialized & arg) void swap(initialized & arg)
{ {
::boost::swap( this->data(), arg.data() ); ::boost::swap( this->data(), arg.data() );
} }
BOOST_GPU_ENABLED
operator T const &() const operator T const &() const
{ {
return wrapper_address()->data; return wrapper_address()->data;
} }
BOOST_GPU_ENABLED
operator T&() operator T&()
{ {
return wrapper_address()->data; return wrapper_address()->data;
@ -156,18 +169,21 @@ class initialized
} ; } ;
template<class T> template<class T>
BOOST_GPU_ENABLED
T const& get ( initialized<T> const& x ) T const& get ( initialized<T> const& x )
{ {
return x.data() ; return x.data() ;
} }
template<class T> template<class T>
BOOST_GPU_ENABLED
T& get ( initialized<T>& x ) T& get ( initialized<T>& x )
{ {
return x.data() ; return x.data() ;
} }
template<class T> template<class T>
BOOST_GPU_ENABLED
void swap ( initialized<T> & lhs, initialized<T> & rhs ) void swap ( initialized<T> & lhs, initialized<T> & rhs )
{ {
lhs.swap(rhs) ; lhs.swap(rhs) ;
@ -183,31 +199,37 @@ class value_initialized
public : public :
BOOST_GPU_ENABLED
value_initialized() value_initialized()
: :
m_data() m_data()
{ } { }
BOOST_GPU_ENABLED
T const & data() const T const & data() const
{ {
return m_data.data(); return m_data.data();
} }
BOOST_GPU_ENABLED
T& data() T& data()
{ {
return m_data.data(); return m_data.data();
} }
BOOST_GPU_ENABLED
void swap(value_initialized & arg) void swap(value_initialized & arg)
{ {
m_data.swap(arg.m_data); m_data.swap(arg.m_data);
} }
BOOST_GPU_ENABLED
operator T const &() const operator T const &() const
{ {
return m_data; return m_data;
} }
BOOST_GPU_ENABLED
operator T&() operator T&()
{ {
return m_data; return m_data;
@ -216,18 +238,21 @@ class value_initialized
template<class T> template<class T>
BOOST_GPU_ENABLED
T const& get ( value_initialized<T> const& x ) T const& get ( value_initialized<T> const& x )
{ {
return x.data() ; return x.data() ;
} }
template<class T> template<class T>
BOOST_GPU_ENABLED
T& get ( value_initialized<T>& x ) T& get ( value_initialized<T>& x )
{ {
return x.data() ; return x.data() ;
} }
template<class T> template<class T>
BOOST_GPU_ENABLED
void swap ( value_initialized<T> & lhs, value_initialized<T> & rhs ) void swap ( value_initialized<T> & lhs, value_initialized<T> & rhs )
{ {
lhs.swap(rhs) ; lhs.swap(rhs) ;
@ -238,7 +263,7 @@ class initialized_value_t
{ {
public : public :
template <class T> operator T() const template <class T> BOOST_GPU_ENABLED operator T() const
{ {
return initialized<T>().data(); return initialized<T>().data();
} }