initial commitment

[SVN r1945]
This commit is contained in:
Jonathan Turkanis
2004-02-08 04:04:13 +00:00
parent 3c7ead0c8e
commit 60030c86c8
2 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
// (C) Copyright Jonathan Turkanis 2004.
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies. This
// software is provided "as is" without express or implied warranty, and
// with no claim as to its suitability for any purpose.
#ifndef BOOST_UTILITY_SELECT_BY_SIZE_HPP_INCLUDED
#define BOOST_UTILITY_SELECT_BY_SIZE_HPP_INCLUDED
#include <boost/config.hpp> // BOOST_STATIC_CONSTANT.
#include <boost/mpl/int.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#ifndef BOOST_SELECT_BY_SIZE_MAX_CASE
#define BOOST_SELECT_BY_SIZE_MAX_CASE 30
#endif
namespace boost { namespace utility {
//--------------Definition of case_-------------------------------------------//
template<unsigned N> struct case_;
template<> struct case_<0> { char c; };
template<unsigned N> struct case_ { char c1; case_<N - 1> c2; };
typedef case_<true> yes_type;
typedef case_<false> no_type;
//--------------Definition of select_by_size----------------------------------//
template<unsigned Size> struct select_by_size;
// Specialize select_by_size for values sizeof(case_<0>), sizeof(case_<1>),
// ..., sizeof(case_(BOOST_SELECT_BY_SIZE_MAX_CASE>).
#define SELECT_BY_SIZE_SPEC(z, n, text) \
namespace detail { \
static const int BOOST_PP_CAT(sizeof_case_, n) = sizeof(case_<n>); \
} \
template<> \
struct select_by_size< detail::BOOST_PP_CAT(sizeof_case_, n) > \
: mpl::int_<n> { }; \
/**/
BOOST_PP_REPEAT(BOOST_SELECT_BY_SIZE_MAX_CASE, SELECT_BY_SIZE_SPEC, _);
#undef SELECT_BY_SIZE_SPEC
} } // End namespaces utility, boost.
//--------------Definition of SELECT_BY_SIZE----------------------------------//
#define BOOST_SELECT_BY_SIZE_ASSIGN(lhs, rhs) lhs = rhs
#define BOOST_SELECT_BY_SIZE(type_, name, expr) \
BOOST_STATIC_CONSTANT( \
unsigned, \
BOOST_PP_CAT(boost_select_by_size_temp_, name) = sizeof(expr) \
); \
BOOST_STATIC_CONSTANT( \
type_, \
name = \
( boost::utility::select_by_size< \
BOOST_PP_CAT(boost_select_by_size_temp_, name) \
>::type::value ) \
) \
/**/
#undef BOOST_SELECT_BY_SIZE_MAX_CASE
#endif // #ifndef BOOST_UTILITY_SELECT_BY_SIZE_HPP_INCLUDED

56
select_by_size_test.cpp Normal file
View File

@@ -0,0 +1,56 @@
#include <boost/static_assert.hpp>
#include <boost/utility/select_by_size.hpp>
using boost::utility::case_;
case_<0> helper(bool);
case_<1> helper(int);
case_<2> helper(unsigned);
case_<3> helper(long);
case_<4> helper(unsigned long);
case_<5> helper(float);
case_<6> helper(double);
case_<7> helper(const char*);
struct test {
BOOST_SELECT_BY_SIZE(int, v0, helper(true));
BOOST_SELECT_BY_SIZE(int, v1, helper(0));
BOOST_SELECT_BY_SIZE(int, v2, helper(0U));
BOOST_SELECT_BY_SIZE(int, v3, helper(0L));
BOOST_SELECT_BY_SIZE(int, v4, helper(0UL));
BOOST_SELECT_BY_SIZE(int, v5, helper(0.0F));
BOOST_SELECT_BY_SIZE(int, v6, helper(0.0));
BOOST_SELECT_BY_SIZE(int, v7, helper("hello"));
};
#include <iostream>
int main()
{
using namespace boost::utility;
BOOST_STATIC_ASSERT( select_by_size< sizeof(helper(true)) >::value == 0 );
BOOST_STATIC_ASSERT( select_by_size< sizeof(helper(0)) >::value == 1 );
BOOST_STATIC_ASSERT( select_by_size< sizeof(helper(0U)) >::value == 2 );
BOOST_STATIC_ASSERT( select_by_size< sizeof(helper(0L)) >::value == 3 );
BOOST_STATIC_ASSERT( select_by_size< sizeof(helper(0UL)) >::value == 4 );
BOOST_STATIC_ASSERT( select_by_size< sizeof(helper(0.0F)) >::value == 5 );
BOOST_STATIC_ASSERT( select_by_size< sizeof(helper(0.0)) >::value == 6 );
BOOST_STATIC_ASSERT( select_by_size< sizeof(helper("hello")) >::value == 7 );
BOOST_STATIC_ASSERT(test::v0 == 0);
BOOST_STATIC_ASSERT(test::v1 == 1);
BOOST_STATIC_ASSERT(test::v2 == 2);
BOOST_STATIC_ASSERT(test::v3 == 3);
BOOST_STATIC_ASSERT(test::v4 == 4);
BOOST_STATIC_ASSERT(test::v5 == 5);
BOOST_STATIC_ASSERT(test::v6 == 6);
BOOST_STATIC_ASSERT(test::v7 == 7);
return 0;
}