Use a base class to apply the memset workaround to avoid dependency on TypeTraits

This commit is contained in:
Peter Dimov 2020-05-25 05:09:36 +03:00
parent 0ae5cebc7f
commit 25cb7aa122
2 changed files with 35 additions and 70 deletions

View File

@ -1,4 +1,5 @@
// (C) Copyright 2002-2008, Fernando Luis Cacciola Carballal.
// Copyright 2020 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
@ -21,15 +22,10 @@
// issues, by clearing the bytes of T, before constructing the T object it
// contains. More details on these issues are at libs/utility/value_init.htm
#include <boost/aligned_storage.hpp>
#include <boost/config.hpp> // For BOOST_NO_COMPLETE_VALUE_INITIALIZATION.
#include <boost/detail/workaround.hpp>
#include <boost/static_assert.hpp>
#include <boost/type_traits/cv_traits.hpp>
#include <boost/type_traits/alignment_of.hpp>
#include <boost/swap.hpp>
#include <cstring>
#include <new>
#include <cstddef>
#ifdef BOOST_MSVC
#pragma warning(push)
@ -39,6 +35,8 @@
// It is safe to ignore the following MSVC warning, which may pop up when T is
// a const type: "warning C4512: assignment operator could not be generated".
#pragma warning(disable: 4512)
// C4355: 'this' : used in base member initializer list
#pragma warning(disable: 4355)
#endif
#ifdef BOOST_NO_COMPLETE_VALUE_INITIALIZATION
@ -60,92 +58,58 @@
namespace boost {
template<class T>
class initialized
{
private :
struct wrapper
{
#if !BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x592))
typename
#endif
remove_const<T>::type data;
namespace detail {
BOOST_GPU_ENABLED
wrapper()
:
data()
struct zero_init
{
zero_init()
{
}
BOOST_GPU_ENABLED
wrapper(T const & arg)
:
data(arg)
zero_init( void * p, std::size_t n )
{
std::memset( p, 0, n );
}
};
mutable
#if !BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x592))
typename
#endif
aligned_storage<sizeof(wrapper), alignment_of<wrapper>::value>::type x;
} // namespace detail
BOOST_GPU_ENABLED
wrapper * wrapper_address() const
{
return static_cast<wrapper *>( static_cast<void*>(&x));
}
template<class T>
class initialized
#if BOOST_DETAIL_VALUE_INIT_WORKAROUND
: detail::zero_init
#endif
{
private:
T data_;
public :
BOOST_GPU_ENABLED
initialized()
{
initialized():
#if BOOST_DETAIL_VALUE_INIT_WORKAROUND
std::memset(&x, 0, sizeof(x));
zero_init( this, sizeof(*this) ),
#endif
new (wrapper_address()) wrapper();
data_()
{
}
BOOST_GPU_ENABLED
initialized(initialized const & arg)
explicit initialized(T const & arg): data_( arg )
{
new (wrapper_address()) wrapper( static_cast<wrapper const &>(*(arg.wrapper_address())));
}
BOOST_GPU_ENABLED
explicit initialized(T const & arg)
{
new (wrapper_address()) wrapper(arg);
}
BOOST_GPU_ENABLED
initialized & operator=(initialized const & arg)
{
// Assignment is only allowed when T is non-const.
BOOST_STATIC_ASSERT( ! is_const<T>::value );
*wrapper_address() = static_cast<wrapper const &>(*(arg.wrapper_address()));
return *this;
}
BOOST_GPU_ENABLED
~initialized()
{
wrapper_address()->wrapper::~wrapper();
}
BOOST_GPU_ENABLED
T const & data() const
{
return wrapper_address()->data;
return data_;
}
BOOST_GPU_ENABLED
T& data()
{
return wrapper_address()->data;
return data_;
}
BOOST_GPU_ENABLED
@ -157,13 +121,13 @@ class initialized
BOOST_GPU_ENABLED
operator T const &() const
{
return wrapper_address()->data;
return data_;
}
BOOST_GPU_ENABLED
operator T&()
{
return wrapper_address()->data;
return data_;
}
} ;

View File

@ -22,6 +22,7 @@
#endif
#include <boost/core/lightweight_test.hpp>
#include <boost/config/workaround.hpp>
//
// Sample POD type