forked from boostorg/type_traits
Add new versions of alignment traits.
This commit is contained in:
138
include/boost/type_traits/aligned_storage.hpp
Normal file
138
include/boost/type_traits/aligned_storage.hpp
Normal file
@ -0,0 +1,138 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// boost aligned_storage.hpp header file
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2002-2003
|
||||
// Eric Friedman, Itay Maman
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_ALIGNED_STORAGE_HPP
|
||||
#define BOOST_ALIGNED_STORAGE_HPP
|
||||
|
||||
#include <cstddef> // for std::size_t
|
||||
|
||||
#include "boost/config.hpp"
|
||||
#include "boost/detail/workaround.hpp"
|
||||
#include "boost/type_traits/alignment_of.hpp"
|
||||
#include "boost/type_traits/type_with_alignment.hpp"
|
||||
#include "boost/type_traits/is_pod.hpp"
|
||||
#include "boost/type_traits/conditional.hpp"
|
||||
|
||||
namespace boost {
|
||||
|
||||
namespace detail { namespace aligned_storage {
|
||||
|
||||
BOOST_STATIC_CONSTANT(
|
||||
std::size_t
|
||||
, alignment_of_max_align = ::boost::alignment_of<boost::detail::max_align>::value
|
||||
);
|
||||
|
||||
//
|
||||
// To be TR1 conforming this must be a POD type:
|
||||
//
|
||||
template <
|
||||
std::size_t size_
|
||||
, std::size_t alignment_
|
||||
>
|
||||
struct aligned_storage_imp
|
||||
{
|
||||
union data_t
|
||||
{
|
||||
char buf[size_];
|
||||
|
||||
typename ::boost::type_with_alignment<alignment_>::type align_;
|
||||
} data_;
|
||||
void* address() const { return const_cast<aligned_storage_imp*>(this); }
|
||||
};
|
||||
template <std::size_t alignment_>
|
||||
struct aligned_storage_imp<std::size_t(-1), alignment_>
|
||||
{
|
||||
union data_t
|
||||
{
|
||||
char buf[1];
|
||||
::boost::detail::max_align align_;
|
||||
} data_;
|
||||
void* address() const { return const_cast<aligned_storage_imp*>(this); }
|
||||
};
|
||||
|
||||
template< std::size_t alignment_ >
|
||||
struct aligned_storage_imp<0u,alignment_>
|
||||
{
|
||||
/* intentionally empty */
|
||||
void* address() const { return 0; }
|
||||
};
|
||||
|
||||
}} // namespace detail::aligned_storage
|
||||
|
||||
template <
|
||||
std::size_t size_
|
||||
, std::size_t alignment_ = std::size_t(-1)
|
||||
>
|
||||
class aligned_storage :
|
||||
#ifndef __BORLANDC__
|
||||
private
|
||||
#else
|
||||
public
|
||||
#endif
|
||||
::boost::detail::aligned_storage::aligned_storage_imp<size_, alignment_>
|
||||
{
|
||||
|
||||
public: // constants
|
||||
|
||||
typedef ::boost::detail::aligned_storage::aligned_storage_imp<size_, alignment_> type;
|
||||
|
||||
BOOST_STATIC_CONSTANT(
|
||||
std::size_t
|
||||
, size = size_
|
||||
);
|
||||
BOOST_STATIC_CONSTANT(
|
||||
std::size_t
|
||||
, alignment = (
|
||||
alignment_ == std::size_t(-1)
|
||||
? ::boost::detail::aligned_storage::alignment_of_max_align
|
||||
: alignment_
|
||||
)
|
||||
);
|
||||
|
||||
private: // noncopyable
|
||||
|
||||
aligned_storage(const aligned_storage&);
|
||||
aligned_storage& operator=(const aligned_storage&);
|
||||
|
||||
public: // structors
|
||||
|
||||
aligned_storage()
|
||||
{
|
||||
}
|
||||
|
||||
~aligned_storage()
|
||||
{
|
||||
}
|
||||
|
||||
public: // accessors
|
||||
|
||||
void* address()
|
||||
{
|
||||
return static_cast<type*>(this)->address();
|
||||
}
|
||||
|
||||
const void* address() const
|
||||
{
|
||||
return static_cast<const type*>(this)->address();
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// Make sure that is_pod recognises aligned_storage<>::type
|
||||
// as a POD (Note that aligned_storage<> itself is not a POD):
|
||||
//
|
||||
template <std::size_t size_, std::size_t alignment_>
|
||||
struct is_pod< ::boost::detail::aligned_storage::aligned_storage_imp<size_, alignment_> > : public true_type{};
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_ALIGNED_STORAGE_HPP
|
20
include/boost/type_traits/conditional.hpp
Normal file
20
include/boost/type_traits/conditional.hpp
Normal file
@ -0,0 +1,20 @@
|
||||
// (C) Copyright John Maddock 2010.
|
||||
// Use, modification and distribution are subject to the Boost Software License,
|
||||
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt).
|
||||
//
|
||||
// See http://www.boost.org/libs/type_traits for most recent version including documentation.
|
||||
|
||||
|
||||
#ifndef BOOST_TT_CONDITIONAL_HPP_INCLUDED
|
||||
#define BOOST_TT_CONDITIONAL_HPP_INCLUDED
|
||||
|
||||
namespace boost {
|
||||
|
||||
template <bool b, class T, class U> struct conditional { typedef T type; };
|
||||
template <class T, class U> struct conditional<false, T, U> { typedef U type; };
|
||||
|
||||
} // namespace boost
|
||||
|
||||
|
||||
#endif // BOOST_TT_CONDITIONAL_HPP_INCLUDED
|
260
include/boost/type_traits/type_with_alignment.hpp
Normal file
260
include/boost/type_traits/type_with_alignment.hpp
Normal file
@ -0,0 +1,260 @@
|
||||
// (C) Copyright John Maddock 2000.
|
||||
// Use, modification and distribution are subject to the Boost Software License,
|
||||
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt).
|
||||
//
|
||||
// See http://www.boost.org/libs/type_traits for most recent version including documentation.
|
||||
|
||||
#ifndef BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED
|
||||
#define BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED
|
||||
|
||||
#include <boost/type_traits/alignment_of.hpp>
|
||||
#include <boost/type_traits/is_pod.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <cstddef>
|
||||
|
||||
#ifdef BOOST_MSVC
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable: 4121) // alignment is sensitive to packing
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <boost/type_traits/conditional.hpp>
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace detail{
|
||||
|
||||
#ifndef __BORLANDC__
|
||||
|
||||
union max_align
|
||||
{
|
||||
char c;
|
||||
short s;
|
||||
int i;
|
||||
long l;
|
||||
#ifndef BOOST_NO_LONG_LONG
|
||||
boost::long_long_type ll;
|
||||
#endif
|
||||
#ifdef BOOST_HAS_INT128
|
||||
boost::int128_type i128;
|
||||
#endif
|
||||
float f;
|
||||
double d;
|
||||
long double ld;
|
||||
#ifdef BOOST_HAS_FLOAT128
|
||||
__float128 f128;
|
||||
#endif
|
||||
};
|
||||
|
||||
template <std::size_t Target, bool check> struct long_double_alignment{ typedef long double type; };
|
||||
template <std::size_t Target> struct long_double_alignment<Target, false>{ typedef detail::max_align type; };
|
||||
|
||||
template <std::size_t Target, bool check> struct double_alignment{ typedef double type; };
|
||||
template <std::size_t Target> struct double_alignment<Target, false>{ typedef typename long_double_alignment<Target, boost::alignment_of<long double>::value >= Target>::type type; };
|
||||
|
||||
#ifndef BOOST_NO_LONG_LONG
|
||||
template <std::size_t Target, bool check> struct long_long_alignment{ typedef boost::long_long_type type; };
|
||||
template <std::size_t Target> struct long_long_alignment<Target, false>{ typedef typename double_alignment<Target, boost::alignment_of<double>::value >= Target>::type type; };
|
||||
#endif
|
||||
|
||||
template <std::size_t Target, bool check> struct long_alignment{ typedef long type; };
|
||||
#ifndef BOOST_NO_LONG_LONG
|
||||
template <std::size_t Target> struct long_alignment<Target, false>{ typedef typename long_long_alignment<Target, boost::alignment_of<boost::long_long_type>::value >= Target>::type type; };
|
||||
#else
|
||||
template <std::size_t Target> struct long_alignment<Target, false>{ typedef typename double_alignment<Target, boost::alignment_of<double>::value >= Target>::type type; };
|
||||
#endif
|
||||
|
||||
template <std::size_t Target, bool check> struct int_alignment{ typedef int type; };
|
||||
template <std::size_t Target> struct int_alignment<Target, false>{ typedef typename long_alignment<Target, boost::alignment_of<long>::value >= Target>::type type; };
|
||||
|
||||
template <std::size_t Target, bool check> struct short_alignment{ typedef short type; };
|
||||
template <std::size_t Target> struct short_alignment<Target, false>{ typedef typename int_alignment<Target, boost::alignment_of<int>::value >= Target>::type type; };
|
||||
|
||||
template <std::size_t Target, bool check> struct char_alignment{ typedef char type; };
|
||||
template <std::size_t Target> struct char_alignment<Target, false>{ typedef typename short_alignment<Target, boost::alignment_of<short>::value >= Target>::type type; };
|
||||
|
||||
}
|
||||
|
||||
template <std::size_t Align>
|
||||
struct type_with_alignment
|
||||
{
|
||||
typedef typename detail::char_alignment<Align, boost::alignment_of<char>::value >= Align>::type type;
|
||||
};
|
||||
|
||||
#if defined(__GNUC__) && !defined(BOOST_TT_DISABLE_INTRINSICS)
|
||||
namespace tt_align_ns {
|
||||
struct __attribute__((__aligned__(2))) a2 {};
|
||||
struct __attribute__((__aligned__(4))) a4 {};
|
||||
struct __attribute__((__aligned__(8))) a8 {};
|
||||
struct __attribute__((__aligned__(16))) a16 {};
|
||||
struct __attribute__((__aligned__(32))) a32 {};
|
||||
struct __attribute__((__aligned__(64))) a64 {};
|
||||
struct __attribute__((__aligned__(128))) a128 {};
|
||||
}
|
||||
|
||||
template<> struct type_with_alignment<1> { public: typedef char type; };
|
||||
template<> struct type_with_alignment<2> { public: typedef tt_align_ns::a2 type; };
|
||||
template<> struct type_with_alignment<4> { public: typedef tt_align_ns::a4 type; };
|
||||
template<> struct type_with_alignment<8> { public: typedef tt_align_ns::a8 type; };
|
||||
template<> struct type_with_alignment<16> { public: typedef tt_align_ns::a16 type; };
|
||||
template<> struct type_with_alignment<32> { public: typedef tt_align_ns::a32 type; };
|
||||
template<> struct type_with_alignment<64> { public: typedef tt_align_ns::a64 type; };
|
||||
template<> struct type_with_alignment<128> { public: typedef tt_align_ns::a128 type; };
|
||||
|
||||
template<> struct is_pod< ::boost::tt_align_ns::a2> : public true_type{};
|
||||
template<> struct is_pod< ::boost::tt_align_ns::a4> : public true_type{};
|
||||
template<> struct is_pod< ::boost::tt_align_ns::a8> : public true_type{};
|
||||
template<> struct is_pod< ::boost::tt_align_ns::a16> : public true_type{};
|
||||
template<> struct is_pod< ::boost::tt_align_ns::a32> : public true_type{};
|
||||
template<> struct is_pod< ::boost::tt_align_ns::a64> : public true_type{};
|
||||
template<> struct is_pod< ::boost::tt_align_ns::a128> : public true_type{};
|
||||
|
||||
#endif
|
||||
#if (defined(BOOST_MSVC) || (defined(BOOST_INTEL) && defined(_MSC_VER))) && !defined(BOOST_TT_DISABLE_INTRINSICS)
|
||||
//
|
||||
// MSVC supports types which have alignments greater than the normal
|
||||
// maximum: these are used for example in the types __m64 and __m128
|
||||
// to provide types with alignment requirements which match the SSE
|
||||
// registers. Therefore we extend type_with_alignment<> to support
|
||||
// such types, however, we have to be careful to use a builtin type
|
||||
// whenever possible otherwise we break previously working code:
|
||||
// see http://article.gmane.org/gmane.comp.lib.boost.devel/173011
|
||||
// for an example and test case. Thus types like a8 below will
|
||||
// be used *only* if the existing implementation can't provide a type
|
||||
// with suitable alignment. This does mean however, that type_with_alignment<>
|
||||
// may return a type which cannot be passed through a function call
|
||||
// by value (and neither can any type containing such a type like
|
||||
// Boost.Optional). However, this only happens when we have no choice
|
||||
// in the matter because no other "ordinary" type is available.
|
||||
//
|
||||
namespace tt_align_ns {
|
||||
struct __declspec(align(8)) a8 {
|
||||
char m[8];
|
||||
typedef a8 type;
|
||||
};
|
||||
struct __declspec(align(16)) a16 {
|
||||
char m[16];
|
||||
typedef a16 type;
|
||||
};
|
||||
struct __declspec(align(32)) a32 {
|
||||
char m[32];
|
||||
typedef a32 type;
|
||||
};
|
||||
struct __declspec(align(64)) a64
|
||||
{
|
||||
char m[64];
|
||||
typedef a64 type;
|
||||
};
|
||||
struct __declspec(align(128)) a128 {
|
||||
char m[128];
|
||||
typedef a128 type;
|
||||
};
|
||||
}
|
||||
|
||||
template<> struct type_with_alignment<8>
|
||||
{
|
||||
typedef boost::conditional<
|
||||
::boost::alignment_of<boost::detail::max_align>::value < 8,
|
||||
tt_align_ns::a8,
|
||||
detail::char_alignment<8, false> >::type t1;
|
||||
public:
|
||||
typedef t1::type type;
|
||||
};
|
||||
template<> struct type_with_alignment<16>
|
||||
{
|
||||
typedef boost::conditional<
|
||||
::boost::alignment_of<boost::detail::max_align>::value < 16,
|
||||
tt_align_ns::a16,
|
||||
detail::char_alignment<16, false> >::type t1;
|
||||
public:
|
||||
typedef t1::type type;
|
||||
};
|
||||
template<> struct type_with_alignment<32>
|
||||
{
|
||||
typedef boost::conditional<
|
||||
::boost::alignment_of<boost::detail::max_align>::value < 32,
|
||||
tt_align_ns::a32,
|
||||
detail::char_alignment<32, false> >::type t1;
|
||||
public:
|
||||
typedef t1::type type;
|
||||
};
|
||||
template<> struct type_with_alignment<64> {
|
||||
typedef boost::conditional<
|
||||
::boost::alignment_of<boost::detail::max_align>::value < 64,
|
||||
tt_align_ns::a64,
|
||||
detail::char_alignment<64, false> >::type t1;
|
||||
public:
|
||||
typedef t1::type type;
|
||||
};
|
||||
template<> struct type_with_alignment<128> {
|
||||
typedef boost::conditional<
|
||||
::boost::alignment_of<boost::detail::max_align>::value < 128,
|
||||
tt_align_ns::a128,
|
||||
detail::char_alignment<128, false> >::type t1;
|
||||
public:
|
||||
typedef t1::type type;
|
||||
};
|
||||
|
||||
template<> struct is_pod< ::boost::tt_align_ns::a8> : public true_type{};
|
||||
template<> struct is_pod< ::boost::tt_align_ns::a16> : public true_type{};
|
||||
template<> struct is_pod< ::boost::tt_align_ns::a32> : public true_type{};
|
||||
template<> struct is_pod< ::boost::tt_align_ns::a64> : public true_type{};
|
||||
template<> struct is_pod< ::boost::tt_align_ns::a128> : public true_type{};
|
||||
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
//
|
||||
// Borland specific version, we have this for two reasons:
|
||||
// 1) The version above doesn't always compile (with the new test cases for example)
|
||||
// 2) Because of Borlands #pragma option we can create types with alignments that are
|
||||
// greater that the largest aligned builtin type.
|
||||
|
||||
namespace tt_align_ns{
|
||||
#pragma option push -a16
|
||||
struct a2{ short s; };
|
||||
struct a4{ int s; };
|
||||
struct a8{ double s; };
|
||||
struct a16{ long double s; };
|
||||
#pragma option pop
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
|
||||
typedef ::boost::tt_align_ns::a16 max_align;
|
||||
|
||||
}
|
||||
//#if ! BOOST_WORKAROUND(__CODEGEARC__, BOOST_TESTED_AT(0x610))
|
||||
template <> struct is_pod< ::boost::tt_align_ns::a2> : public true_type{};
|
||||
template <> struct is_pod< ::boost::tt_align_ns::a4> : public true_type{};
|
||||
template <> struct is_pod< ::boost::tt_align_ns::a8> : public true_type{};
|
||||
template <> struct is_pod< ::boost::tt_align_ns::a16> : public true_type{};
|
||||
//#endif
|
||||
|
||||
template <std::size_t N> struct type_with_alignment
|
||||
{
|
||||
// We should never get to here, but if we do use the maximally
|
||||
// aligned type:
|
||||
// BOOST_STATIC_ASSERT(0);
|
||||
typedef tt_align_ns::a16 type;
|
||||
};
|
||||
template <> struct type_with_alignment<1>{ typedef char type; };
|
||||
template <> struct type_with_alignment<2>{ typedef tt_align_ns::a2 type; };
|
||||
template <> struct type_with_alignment<4>{ typedef tt_align_ns::a4 type; };
|
||||
template <> struct type_with_alignment<8>{ typedef tt_align_ns::a8 type; };
|
||||
template <> struct type_with_alignment<16>{ typedef tt_align_ns::a16 type; };
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#ifdef BOOST_MSVC
|
||||
# pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#endif // BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED
|
||||
|
||||
|
113
test/aligned_storage_a2_test.cpp
Normal file
113
test/aligned_storage_a2_test.cpp
Normal file
@ -0,0 +1,113 @@
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma pack(2)
|
||||
#endif
|
||||
|
||||
// (C) Copyright John Maddock 2000.
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include "test.hpp"
|
||||
#include "check_integral_constant.hpp"
|
||||
#ifdef TEST_STD
|
||||
# include <type_traits>
|
||||
# include <boost/type_traits/type_with_alignment.hpp> // max_align and long_long_type
|
||||
#else
|
||||
# include <boost/type_traits/alignment_of.hpp>
|
||||
# include <boost/type_traits/aligned_storage.hpp>
|
||||
# include <boost/type_traits/is_pod.hpp>
|
||||
#endif
|
||||
|
||||
template <class T>
|
||||
union must_be_pod
|
||||
{
|
||||
int i;
|
||||
T t;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
inline void no_unused_warning(const volatile T&)
|
||||
{
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void do_check(const T&)
|
||||
{
|
||||
typedef typename tt::aligned_storage<T::value,T::value>::type t1;
|
||||
t1 as1 = { 0, };
|
||||
must_be_pod<t1> pod1;
|
||||
no_unused_warning(as1);
|
||||
no_unused_warning(pod1);
|
||||
BOOST_TEST_MESSAGE(typeid(t1).name());
|
||||
BOOST_CHECK(::tt::alignment_of<t1>::value == T::value);
|
||||
BOOST_CHECK(sizeof(t1) == T::value);
|
||||
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
||||
BOOST_CHECK(::tt::is_pod<t1>::value == true);
|
||||
#endif
|
||||
typedef typename tt::aligned_storage<T::value*2,T::value>::type t2;
|
||||
t2 as2 = { 0, };
|
||||
must_be_pod<t2> pod2;
|
||||
no_unused_warning(as2);
|
||||
no_unused_warning(pod2);
|
||||
BOOST_TEST_MESSAGE(typeid(t2).name());
|
||||
BOOST_CHECK(::tt::alignment_of<t2>::value == T::value);
|
||||
BOOST_CHECK(sizeof(t2) == T::value*2);
|
||||
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
||||
BOOST_CHECK(::tt::is_pod<t2>::value == true);
|
||||
#endif
|
||||
|
||||
#ifndef TEST_STD
|
||||
// Non-Tr1 behaviour:
|
||||
typedef typename tt::aligned_storage<T::value,-1L>::type t3;
|
||||
t3 as3 = { 0, };
|
||||
must_be_pod<t3> pod3;
|
||||
no_unused_warning(as3);
|
||||
no_unused_warning(pod3);
|
||||
BOOST_TEST_MESSAGE(typeid(t3).name());
|
||||
BOOST_CHECK(::tt::alignment_of<t3>::value == ::tt::alignment_of< ::boost::detail::max_align>::value);
|
||||
BOOST_CHECK((sizeof(t3) % T::value) == 0);
|
||||
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
||||
BOOST_CHECK(::tt::is_pod<t3>::value == true);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
TT_TEST_BEGIN(type_with_alignment)
|
||||
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<char>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<short>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<int>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<long>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<float>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<double>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<long double>::value>());
|
||||
|
||||
#ifdef BOOST_HAS_LONG_LONG
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of< ::boost::long_long_type>::value>());
|
||||
#endif
|
||||
#ifdef BOOST_HAS_MS_INT64
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<__int64>::value>());
|
||||
#endif
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<int[4]>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<int(*)(int)>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<int*>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<VB>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<VD>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<enum_UDT>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<mf2>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<POD_UDT>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<empty_UDT>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<union_UDT>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<boost::detail::max_align>::value>());
|
||||
|
||||
TT_TEST_END
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
128
test/aligned_storage_empy_test.cpp
Normal file
128
test/aligned_storage_empy_test.cpp
Normal file
@ -0,0 +1,128 @@
|
||||
|
||||
// (C) Copyright Thorsten Ottosen 2009.
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include "test.hpp"
|
||||
#include "check_integral_constant.hpp"
|
||||
#ifdef TEST_STD
|
||||
# include <type_traits>
|
||||
# include <boost/type_traits/type_with_alignment.hpp> // max_align and long_long_type
|
||||
#else
|
||||
# include <boost/type_traits/alignment_of.hpp>
|
||||
# include <boost/type_traits/aligned_storage.hpp>
|
||||
# include <boost/type_traits/is_pod.hpp>
|
||||
#endif
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
template< unsigned N, unsigned Alignment >
|
||||
struct alignment_implementation1
|
||||
{
|
||||
boost::detail::aligned_storage::aligned_storage_imp<N,Alignment> type;
|
||||
const void* address() const { return type.address(); }
|
||||
};
|
||||
|
||||
template< unsigned N, unsigned Alignment >
|
||||
struct alignment_implementation2 :
|
||||
#ifndef __BORLANDC__
|
||||
private
|
||||
#else
|
||||
public
|
||||
#endif
|
||||
boost::detail::aligned_storage::aligned_storage_imp<N,Alignment>
|
||||
{
|
||||
typedef boost::detail::aligned_storage::aligned_storage_imp<N,Alignment> type;
|
||||
const void* address() const { return static_cast<const type*>(this)->address(); }
|
||||
};
|
||||
|
||||
template< unsigned N, class T >
|
||||
std::ptrdiff_t get_address1()
|
||||
{
|
||||
static alignment_implementation1<N*sizeof(T), tt::alignment_of<T>::value> imp1;
|
||||
return static_cast<const char*>(imp1.address()) - reinterpret_cast<const char*>(&imp1);
|
||||
}
|
||||
|
||||
template< unsigned N, class T >
|
||||
std::ptrdiff_t get_address2()
|
||||
{
|
||||
static alignment_implementation2<N*sizeof(T), tt::alignment_of<T>::value> imp2;
|
||||
return static_cast<const char*>(imp2.address()) - reinterpret_cast<const char*>(&imp2);
|
||||
}
|
||||
|
||||
template< class T >
|
||||
void do_check()
|
||||
{
|
||||
std::ptrdiff_t addr1 = get_address1<0,T>();
|
||||
std::ptrdiff_t addr2 = get_address2<0,T>();
|
||||
//
|
||||
// @remark: only the empty case differs
|
||||
//
|
||||
BOOST_CHECK( addr1 != addr2 );
|
||||
|
||||
addr1 = get_address1<1,T>();
|
||||
addr2 = get_address2<1,T>();
|
||||
BOOST_CHECK( addr1 == addr2 );
|
||||
|
||||
addr1 = get_address1<2,T>();
|
||||
addr2 = get_address2<2,T>();
|
||||
BOOST_CHECK( addr1 == addr2 );
|
||||
|
||||
addr1 = get_address1<3,T>();
|
||||
addr2 = get_address2<3,T>();
|
||||
BOOST_CHECK( addr1 == addr2 );
|
||||
|
||||
addr1 = get_address1<4,T>();
|
||||
addr2 = get_address2<4,T>();
|
||||
BOOST_CHECK( addr1 == addr2 );
|
||||
|
||||
addr1 = get_address1<17,T>();
|
||||
addr2 = get_address2<17,T>();
|
||||
BOOST_CHECK( addr1 == addr2 );
|
||||
|
||||
addr1 = get_address1<32,T>();
|
||||
addr2 = get_address2<32,T>();
|
||||
BOOST_CHECK( addr1 == addr2 );
|
||||
}
|
||||
}
|
||||
|
||||
TT_TEST_BEGIN(type_with_empty_alignment_buffer)
|
||||
|
||||
do_check<char>();
|
||||
do_check<short>();
|
||||
do_check<int>();
|
||||
do_check<long>();
|
||||
do_check<float>();
|
||||
do_check<double>();
|
||||
do_check<long double>();
|
||||
|
||||
#ifdef BOOST_HAS_MS_INT64
|
||||
do_check<__int64>();
|
||||
#endif
|
||||
#ifdef BOOST_HAS_LONG_LONG
|
||||
do_check<boost::long_long_type>();
|
||||
#endif
|
||||
|
||||
do_check<int(*)(int)>();
|
||||
do_check<int*>();
|
||||
do_check<VB>();
|
||||
do_check<VD>();
|
||||
do_check<enum_UDT>();
|
||||
do_check<mf2>();
|
||||
do_check<POD_UDT>();
|
||||
do_check<empty_UDT>();
|
||||
do_check<union_UDT>();
|
||||
do_check<boost::detail::max_align>();
|
||||
|
||||
TT_TEST_END
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
116
test/aligned_storage_test.cpp
Normal file
116
test/aligned_storage_test.cpp
Normal file
@ -0,0 +1,116 @@
|
||||
|
||||
// (C) Copyright John Maddock 2000.
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include "test.hpp"
|
||||
#include "check_integral_constant.hpp"
|
||||
#ifdef TEST_STD
|
||||
# include <type_traits>
|
||||
# include <boost/type_traits/type_with_alignment.hpp> // max_align and long_long_type
|
||||
#else
|
||||
# include <boost/type_traits/alignment_of.hpp>
|
||||
# include <boost/type_traits/aligned_storage.hpp>
|
||||
# include <boost/type_traits/is_pod.hpp>
|
||||
#endif
|
||||
|
||||
template <class T>
|
||||
union must_be_pod
|
||||
{
|
||||
int i;
|
||||
T t;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
inline void no_unused_warning(const volatile T&)
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(BOOST_INTEL)
|
||||
#pragma GCC diagnostic ignored "-Wmissing-braces"
|
||||
#endif
|
||||
|
||||
template <class T>
|
||||
void do_check(const T&)
|
||||
{
|
||||
typedef typename tt::aligned_storage<T::value,T::value>::type t1;
|
||||
t1 as1 = { 0, };
|
||||
must_be_pod<t1> pod1;
|
||||
no_unused_warning(as1);
|
||||
no_unused_warning(pod1);
|
||||
BOOST_TEST_MESSAGE(typeid(t1).name());
|
||||
BOOST_CHECK(::tt::alignment_of<t1>::value == T::value);
|
||||
BOOST_CHECK(sizeof(t1) == T::value);
|
||||
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
||||
BOOST_CHECK(::tt::is_pod<t1>::value == true);
|
||||
#endif
|
||||
typedef typename tt::aligned_storage<T::value*2,T::value>::type t2;
|
||||
t2 as2 = { 0, };
|
||||
must_be_pod<t2> pod2;
|
||||
no_unused_warning(as2);
|
||||
no_unused_warning(pod2);
|
||||
BOOST_TEST_MESSAGE(typeid(t2).name());
|
||||
BOOST_CHECK(::tt::alignment_of<t2>::value == T::value);
|
||||
BOOST_CHECK(sizeof(t2) == T::value*2);
|
||||
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
||||
BOOST_CHECK(::tt::is_pod<t2>::value == true);
|
||||
#endif
|
||||
|
||||
#ifndef TEST_STD
|
||||
// Non-Tr1 behaviour:
|
||||
typedef typename tt::aligned_storage<T::value, ~static_cast<std::size_t>(0UL)>::type t3;
|
||||
t3 as3 = { 0, };
|
||||
must_be_pod<t3> pod3;
|
||||
no_unused_warning(as3);
|
||||
no_unused_warning(pod3);
|
||||
BOOST_TEST_MESSAGE(typeid(t3).name());
|
||||
BOOST_CHECK(::tt::alignment_of<t3>::value == ::tt::alignment_of< ::boost::detail::max_align>::value);
|
||||
BOOST_CHECK((sizeof(t3) % T::value) == 0);
|
||||
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
||||
BOOST_CHECK(::tt::is_pod<t3>::value == true);
|
||||
#endif
|
||||
BOOST_CHECK(as3.address() == &as3);
|
||||
const t3 as4 = { 0, };
|
||||
BOOST_CHECK(as4.address() == static_cast<const void*>(&as4));
|
||||
#endif
|
||||
}
|
||||
|
||||
TT_TEST_BEGIN(type_with_alignment)
|
||||
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<char>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<short>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<int>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<long>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<float>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<double>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<long double>::value>());
|
||||
|
||||
#ifdef BOOST_HAS_LONG_LONG
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of< ::boost::long_long_type>::value>());
|
||||
#endif
|
||||
#ifdef BOOST_HAS_MS_INT64
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<__int64>::value>());
|
||||
#endif
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<int[4]>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<int(*)(int)>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<int*>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<VB>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<VD>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<enum_UDT>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<mf2>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<POD_UDT>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<empty_UDT>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<union_UDT>::value>());
|
||||
do_check(tt::integral_constant<std::size_t,::tt::alignment_of<boost::detail::max_align>::value>());
|
||||
|
||||
TT_TEST_END
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
31
test/conditional_test.cpp
Normal file
31
test/conditional_test.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
|
||||
// (C) Copyright John Maddock 2010.
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include "test.hpp"
|
||||
#include "check_integral_constant.hpp"
|
||||
#ifdef TEST_STD
|
||||
# include <type_traits>
|
||||
#else
|
||||
# include <boost/type_traits/conditional.hpp>
|
||||
#endif
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
|
||||
TT_TEST_BEGIN(conditional)
|
||||
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_same< ::tt::conditional<true, int, long>::type, int>::value), true);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_same< ::tt::conditional<false, int, long>::type, long>::value), true);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_same< ::tt::conditional<true, int, long>::type, long>::value), false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_same< ::tt::conditional<false, int, long>::type, int>::value), false);
|
||||
|
||||
TT_TEST_END
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
112
test/type_with_alignment_test.cpp
Normal file
112
test/type_with_alignment_test.cpp
Normal file
@ -0,0 +1,112 @@
|
||||
|
||||
// (C) Copyright John Maddock 2000.
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include "test.hpp"
|
||||
#include "check_integral_constant.hpp"
|
||||
#ifdef TEST_STD
|
||||
# include <type_traits>
|
||||
#else
|
||||
# include <boost/type_traits/alignment_of.hpp>
|
||||
# include <boost/type_traits/type_with_alignment.hpp>
|
||||
# include <boost/type_traits/is_pod.hpp>
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_MSVC) || (defined(BOOST_INTEL) && defined(_MSC_VER))
|
||||
#if (_MSC_VER >= 1400) && defined(_M_IX86)
|
||||
#include <xmmintrin.h>
|
||||
#endif
|
||||
struct __declspec(align(8)) a8 { char m[8]; };
|
||||
struct __declspec(align(16)) a16 { char m[16]; };
|
||||
struct __declspec(align(32)) a32 { char m[32]; };
|
||||
struct __declspec(align(64)) a64 { char m[64]; };
|
||||
struct __declspec(align(128)) a128 { char m[128]; };
|
||||
#endif
|
||||
|
||||
void check_call2(...){}
|
||||
|
||||
template <class T>
|
||||
void check_call(const T& v)
|
||||
{
|
||||
check_call2(v);
|
||||
}
|
||||
|
||||
#define TYPE_WITH_ALIGNMENT_TEST(T)\
|
||||
{\
|
||||
std::cout << "\ntesting type " << typeid(T).name() << std::endl;\
|
||||
std::cout << "Alignment of T is " << ::tt::alignment_of< T >::value << std::endl;\
|
||||
std::cout << "Aligned type is " << typeid(::tt::type_with_alignment< ::tt::alignment_of< T >::value>::type).name() << std::endl;\
|
||||
std::cout << "Alignment of aligned type is " << ::tt::alignment_of<\
|
||||
::tt::type_with_alignment<\
|
||||
::tt::alignment_of< T >::value\
|
||||
>::type\
|
||||
>::value << std::endl;\
|
||||
BOOST_CHECK(::tt::alignment_of<\
|
||||
::tt::type_with_alignment<\
|
||||
::tt::alignment_of< T >::value\
|
||||
>::type\
|
||||
>::value == ::boost::alignment_of< T >::value);\
|
||||
BOOST_CHECK(::tt::is_pod<\
|
||||
::tt::type_with_alignment<\
|
||||
::tt::alignment_of< T >::value>::type\
|
||||
>::value);\
|
||||
}
|
||||
#define TYPE_WITH_ALIGNMENT_TEST_EX(T)\
|
||||
TYPE_WITH_ALIGNMENT_TEST(T)\
|
||||
{\
|
||||
::tt::type_with_alignment<\
|
||||
::tt::alignment_of< T >::value\
|
||||
>::type val;\
|
||||
check_call(val);\
|
||||
}
|
||||
|
||||
|
||||
TT_TEST_BEGIN(type_with_alignment)
|
||||
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(char)
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(short)
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(int)
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(long)
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(float)
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(double)
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(long double)
|
||||
|
||||
#ifdef BOOST_HAS_LONG_LONG
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(::boost::long_long_type)
|
||||
#endif
|
||||
#ifdef BOOST_HAS_MS_INT64
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(__int64)
|
||||
#endif
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(int[4])
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(int(*)(int))
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(int*)
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(VB)
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(VD)
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(enum_UDT)
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(mf2)
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(POD_UDT)
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(empty_UDT)
|
||||
TYPE_WITH_ALIGNMENT_TEST_EX(union_UDT)
|
||||
|
||||
#if defined(BOOST_MSVC) || (defined(BOOST_INTEL) && defined(_MSC_VER))
|
||||
#if (_MSC_VER >= 1400) && defined(_M_IX86)
|
||||
TYPE_WITH_ALIGNMENT_TEST(__m128)
|
||||
TYPE_WITH_ALIGNMENT_TEST(__m64)
|
||||
#endif
|
||||
TYPE_WITH_ALIGNMENT_TEST(a8)
|
||||
TYPE_WITH_ALIGNMENT_TEST(a16)
|
||||
TYPE_WITH_ALIGNMENT_TEST(a32)
|
||||
#endif
|
||||
|
||||
TT_TEST_END
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user