mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 15:04:00 +00:00
Use a base class to apply the memset workaround to avoid dependency on TypeTraits
This commit is contained in:
parent
0ae5cebc7f
commit
25cb7aa122
@ -1,4 +1,5 @@
|
|||||||
// (C) Copyright 2002-2008, Fernando Luis Cacciola Carballal.
|
// (C) Copyright 2002-2008, Fernando Luis Cacciola Carballal.
|
||||||
|
// Copyright 2020 Peter Dimov
|
||||||
//
|
//
|
||||||
// Distributed under the Boost Software License, Version 1.0. (See
|
// Distributed under the Boost Software License, Version 1.0. (See
|
||||||
// accompanying file LICENSE_1_0.txt or copy at
|
// 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
|
// 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
|
// 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/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 <boost/swap.hpp>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <new>
|
#include <cstddef>
|
||||||
|
|
||||||
#ifdef BOOST_MSVC
|
#ifdef BOOST_MSVC
|
||||||
#pragma warning(push)
|
#pragma warning(push)
|
||||||
@ -39,6 +35,8 @@
|
|||||||
// It is safe to ignore the following MSVC warning, which may pop up when T is
|
// 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".
|
// a const type: "warning C4512: assignment operator could not be generated".
|
||||||
#pragma warning(disable: 4512)
|
#pragma warning(disable: 4512)
|
||||||
|
// C4355: 'this' : used in base member initializer list
|
||||||
|
#pragma warning(disable: 4355)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef BOOST_NO_COMPLETE_VALUE_INITIALIZATION
|
#ifdef BOOST_NO_COMPLETE_VALUE_INITIALIZATION
|
||||||
@ -60,92 +58,58 @@
|
|||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
struct zero_init
|
||||||
|
{
|
||||||
|
zero_init()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
zero_init( void * p, std::size_t n )
|
||||||
|
{
|
||||||
|
std::memset( p, 0, n );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
class initialized
|
class initialized
|
||||||
|
#if BOOST_DETAIL_VALUE_INIT_WORKAROUND
|
||||||
|
: detail::zero_init
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
private :
|
private:
|
||||||
struct wrapper
|
|
||||||
{
|
|
||||||
#if !BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x592))
|
|
||||||
typename
|
|
||||||
#endif
|
|
||||||
remove_const<T>::type data;
|
|
||||||
|
|
||||||
BOOST_GPU_ENABLED
|
T data_;
|
||||||
wrapper()
|
|
||||||
:
|
|
||||||
data()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_GPU_ENABLED
|
|
||||||
wrapper(T const & arg)
|
|
||||||
:
|
|
||||||
data(arg)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
mutable
|
|
||||||
#if !BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x592))
|
|
||||||
typename
|
|
||||||
#endif
|
|
||||||
aligned_storage<sizeof(wrapper), alignment_of<wrapper>::value>::type x;
|
|
||||||
|
|
||||||
BOOST_GPU_ENABLED
|
|
||||||
wrapper * wrapper_address() const
|
|
||||||
{
|
|
||||||
return static_cast<wrapper *>( static_cast<void*>(&x));
|
|
||||||
}
|
|
||||||
|
|
||||||
public :
|
public :
|
||||||
|
|
||||||
BOOST_GPU_ENABLED
|
BOOST_GPU_ENABLED
|
||||||
initialized()
|
initialized():
|
||||||
{
|
|
||||||
#if BOOST_DETAIL_VALUE_INIT_WORKAROUND
|
#if BOOST_DETAIL_VALUE_INIT_WORKAROUND
|
||||||
std::memset(&x, 0, sizeof(x));
|
zero_init( this, sizeof(*this) ),
|
||||||
#endif
|
#endif
|
||||||
new (wrapper_address()) wrapper();
|
data_()
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_GPU_ENABLED
|
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
|
BOOST_GPU_ENABLED
|
||||||
T const & data() const
|
T const & data() const
|
||||||
{
|
{
|
||||||
return wrapper_address()->data;
|
return data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_GPU_ENABLED
|
BOOST_GPU_ENABLED
|
||||||
T& data()
|
T& data()
|
||||||
{
|
{
|
||||||
return wrapper_address()->data;
|
return data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_GPU_ENABLED
|
BOOST_GPU_ENABLED
|
||||||
@ -157,13 +121,13 @@ class initialized
|
|||||||
BOOST_GPU_ENABLED
|
BOOST_GPU_ENABLED
|
||||||
operator T const &() const
|
operator T const &() const
|
||||||
{
|
{
|
||||||
return wrapper_address()->data;
|
return data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_GPU_ENABLED
|
BOOST_GPU_ENABLED
|
||||||
operator T&()
|
operator T&()
|
||||||
{
|
{
|
||||||
return wrapper_address()->data;
|
return data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
} ;
|
} ;
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <boost/core/lightweight_test.hpp>
|
#include <boost/core/lightweight_test.hpp>
|
||||||
|
#include <boost/config/workaround.hpp>
|
||||||
|
|
||||||
//
|
//
|
||||||
// Sample POD type
|
// Sample POD type
|
||||||
|
Loading…
x
Reference in New Issue
Block a user