Skip to content

Commit

Permalink
add: change implementation of max_align_t from sizeof to alignment_of
Browse files Browse the repository at this point in the history
  • Loading branch information
cleeus committed Jul 31, 2012
1 parent b5080a4 commit d1006f6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
16 changes: 8 additions & 8 deletions arena_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ using boost::arena::obstack;
BOOST_AUTO_TEST_SUITE(arena_all)


BOOST_AUTO_TEST_CASE(max_sizeof_char_double) {
BOOST_AUTO_TEST_CASE(max_alignof_char_double) {
typedef char T1;
typedef double T2;
typedef typename boost::arena::detail::max_sizeof<T1,T2> Tmax;
typedef typename boost::arena::detail::max_alignof<T1,T2> Tmax;

BOOST_CHECK( sizeof(Tmax::type) == sizeof(T2) );
BOOST_CHECK( Tmax::value == sizeof(T2) );
}

BOOST_AUTO_TEST_CASE(max_sizeof_double_char) {
BOOST_AUTO_TEST_CASE(max_alignof_double_char) {
typedef double T1;
typedef char T2;
typedef typename boost::arena::detail::max_sizeof<T1,T2> Tmax;
typedef typename boost::arena::detail::max_alignof<T1,T2> Tmax;

BOOST_CHECK( sizeof(Tmax::type) == sizeof(T1) );
BOOST_CHECK( Tmax::value == sizeof(T1) );
}

BOOST_AUTO_TEST_CASE(max_sizeof_9_char_int) {
typedef typename boost::arena::detail::max_sizeof<
BOOST_AUTO_TEST_CASE(max_alignof_9_char_int) {
typedef typename boost::arena::detail::max_alignof<
char,
char,
char,
Expand All @@ -54,8 +54,8 @@ BOOST_AUTO_TEST_CASE(max_sizeof_9_char_int) {
BOOST_CHECK( Tmax::value == sizeof(int) );
}

BOOST_AUTO_TEST_CASE(max_sizeof_9_char_int_reverse) {
typedef typename boost::arena::detail::max_sizeof<
BOOST_AUTO_TEST_CASE(max_alignof_9_char_int_reverse) {
typedef typename boost::arena::detail::max_alignof<
int,
char,
char,
Expand Down
33 changes: 17 additions & 16 deletions max_alignment_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@
#define BOOST_MAXALIGNMENT_TYPE_HPP

#include <boost/cstdint.hpp>
#include <boost/type_traits/alignment_of.hpp>

namespace boost {
namespace arena {
namespace detail {

template<typename T1, typename T2, bool T1gtT2>
struct max_sizeof_impl {
struct max_alignof_impl {
typedef T1 type;
enum { value = sizeof(type) };
enum { value = alignment_of<type>::value };
};

//partial specialization on false
template<typename T1, typename T2>
struct max_sizeof_impl<T1,T2,false> {
struct max_alignof_impl<T1,T2,false> {
typedef T2 type;
enum { value = sizeof(type) };
enum { value = alignment_of<type>::value };
};

struct null_type {};
Expand All @@ -34,21 +35,21 @@ template<
typename T9 = null_type,
typename T10 = null_type
>
struct max_sizeof {
struct max_alignof {
private:
typedef typename max_sizeof<T2,T3,T4,T5,T6,T7,T8,T9,T10>::type end_max_type;
typedef max_sizeof<T1,end_max_type> total_max_sizeof;
typedef typename max_alignof<T2,T3,T4,T5,T6,T7,T8,T9,T10>::type back_type;
typedef max_alignof<T1,back_type> total_type;

typedef typename total_max_sizeof::type max_type;
enum { max_value = total_max_sizeof::value };
typedef typename total_type::type max_type;
enum { max_value = total_type::value };
public:
typedef max_type type;
enum { value = max_value };
};


template<typename T1>
struct max_sizeof<
struct max_alignof<
T1,
null_type,
null_type,
Expand All @@ -62,12 +63,12 @@ struct max_sizeof<
>
{
typedef T1 type;
enum { value = sizeof(type) };
enum { value = alignment_of<type>::value };
};


template<typename T1, typename T2>
struct max_sizeof<
struct max_alignof<
T1,
T2,
null_type,
Expand All @@ -81,9 +82,9 @@ struct max_sizeof<
>
{
private:
typedef max_sizeof_impl<T1,T2, (sizeof(T1)>sizeof(T2)) > total_max_sizeof;
typedef typename total_max_sizeof::type max_type;
enum { max_value = total_max_sizeof::value };
typedef max_alignof_impl<T1,T2, (alignment_of<T1>::value > alignment_of<T2>::value) > total_type;
typedef typename total_type::type max_type;
enum { max_value = total_type::value };
public:
typedef max_type type;
enum { value = max_value };
Expand All @@ -93,7 +94,7 @@ struct max_sizeof<
} //namespace detail


typedef detail::max_sizeof<
typedef detail::max_alignof<
char,
short,
int,
Expand Down

0 comments on commit d1006f6

Please sign in to comment.