mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 15:04:00 +00:00
Code refactoring: removed private base classes of value_initialized, as suggested by Fernando Cacciola.
[SVN r41216]
This commit is contained in:
parent
b3a971e7e9
commit
ec46e40809
@ -6,6 +6,7 @@
|
||||
//
|
||||
// 21 Ago 2002 (Created) Fernando Cacciola
|
||||
// 07 Set 2007 (Worked around MSVC++ bug) Fernando Cacciola, Niels Dekker
|
||||
// 16 Nov 2007 (Refactoring: removed private base classes) Fernando Cacciola, Niels Dekker
|
||||
//
|
||||
#ifndef BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
|
||||
#define BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
|
||||
@ -14,7 +15,6 @@
|
||||
#include <boost/type_traits/cv_traits.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
|
||||
#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500) )
|
||||
// Microsoft Visual C++ does not correctly support value initialization, as reported by
|
||||
// Pavel Kuznetsov (MetaCommunications Engineering), 7/28/2005, Feedback ID 100744,
|
||||
// Feedback Title: Value-initialization in new-expression
|
||||
@ -22,10 +22,12 @@
|
||||
// The report was closed at 11/14/2006, and its status was set to "Closed (Won't Fix)".
|
||||
// Luckily, even in the presence of this compiler bug, boost::value_initialized will still
|
||||
// do its job correctly, when using the following workaround:
|
||||
#define BOOST_UTILITY_VALUE_INIT_WORKAROUND
|
||||
#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500))
|
||||
# define BOOST_UTILITY_VALUE_INIT_WORKAROUND
|
||||
#endif
|
||||
|
||||
#ifdef BOOST_UTILITY_VALUE_INIT_WORKAROUND
|
||||
|
||||
#include <boost/aligned_storage.hpp>
|
||||
#include <boost/type_traits/alignment_of.hpp>
|
||||
#include <cstring>
|
||||
@ -43,60 +45,34 @@
|
||||
|
||||
namespace boost {
|
||||
|
||||
namespace vinit_detail {
|
||||
|
||||
template<class T>
|
||||
class const_T_base
|
||||
class value_initialized
|
||||
{
|
||||
protected :
|
||||
|
||||
const_T_base()
|
||||
{
|
||||
std::memset(&x, 0, sizeof(x));
|
||||
new (&x) T();
|
||||
}
|
||||
|
||||
~const_T_base()
|
||||
{
|
||||
void const * ptr = &x;
|
||||
static_cast<T*>(ptr)->T::~T();
|
||||
}
|
||||
|
||||
T & get() const
|
||||
{
|
||||
void const * ptr = &x;
|
||||
return *static_cast<T*>(ptr);
|
||||
}
|
||||
|
||||
private :
|
||||
typename ::boost::aligned_storage<sizeof(T), ::boost::alignment_of<T>::value>::type x;
|
||||
} ;
|
||||
|
||||
template<class T>
|
||||
class non_const_T_base
|
||||
{
|
||||
protected :
|
||||
|
||||
non_const_T_base()
|
||||
{
|
||||
std::memset(&x, 0, sizeof(x));
|
||||
new (&x) T();
|
||||
}
|
||||
|
||||
~non_const_T_base()
|
||||
{
|
||||
void * ptr = &x;
|
||||
static_cast<T*>(ptr)->T::~T();
|
||||
}
|
||||
|
||||
T & get() const
|
||||
{
|
||||
void * ptr = &x;
|
||||
return *static_cast<T*>(ptr);
|
||||
}
|
||||
|
||||
private :
|
||||
mutable typename ::boost::aligned_storage<sizeof(T), ::boost::alignment_of<T>::value>::type x;
|
||||
|
||||
public :
|
||||
|
||||
value_initialized()
|
||||
{
|
||||
std::memset(&x, 0, sizeof(x));
|
||||
new (&x) T();
|
||||
}
|
||||
|
||||
~value_initialized()
|
||||
{
|
||||
void * ptr = &x;
|
||||
static_cast<T*>(ptr)->T::~T();
|
||||
}
|
||||
|
||||
T& data() const
|
||||
{
|
||||
void * ptr = &x;
|
||||
return *static_cast<T*>(ptr);
|
||||
}
|
||||
|
||||
operator T&() const { return this->data(); }
|
||||
|
||||
} ;
|
||||
|
||||
#ifdef BOOST_MSVC
|
||||
@ -108,59 +84,27 @@ class non_const_T_base
|
||||
|
||||
namespace boost {
|
||||
|
||||
namespace vinit_detail {
|
||||
|
||||
template<class T>
|
||||
class const_T_base
|
||||
{
|
||||
protected :
|
||||
|
||||
const_T_base() : x() {}
|
||||
T & get() const { return x; }
|
||||
|
||||
private :
|
||||
T x ;
|
||||
} ;
|
||||
|
||||
template<class T>
|
||||
class non_const_T_base
|
||||
{
|
||||
protected :
|
||||
|
||||
non_const_T_base() : x() {}
|
||||
T & get() const { return x; }
|
||||
|
||||
private :
|
||||
mutable T x ;
|
||||
} ;
|
||||
|
||||
#endif
|
||||
|
||||
template<class T>
|
||||
struct select_base
|
||||
{
|
||||
typedef
|
||||
#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
|
||||
typename
|
||||
#endif
|
||||
::boost::detail::if_true< ::boost::is_const<T>::value >
|
||||
::template then< const_T_base<T>, non_const_T_base<T> >::type type ;
|
||||
} ;
|
||||
|
||||
} // namespace vinit_detail
|
||||
|
||||
template<class T>
|
||||
class value_initialized : private vinit_detail::select_base<T>::type
|
||||
class value_initialized
|
||||
{
|
||||
public :
|
||||
|
||||
value_initialized() {}
|
||||
value_initialized() : x() {}
|
||||
|
||||
operator T&() const { return this->get(); }
|
||||
T& data() const { return x; }
|
||||
|
||||
T& data() const { return this->get(); }
|
||||
operator T&() const { return this->data(); }
|
||||
|
||||
mutable
|
||||
#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
|
||||
typename
|
||||
#endif
|
||||
::boost::remove_const<T>::type x ;
|
||||
|
||||
} ;
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
template<class T>
|
||||
T const& get ( value_initialized<T> const& x )
|
||||
@ -177,4 +121,3 @@ T& get ( value_initialized<T>& x )
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user