integer.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright Aleksey Gurtovoy 2001-2004
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/mpl for documentation.
  8. // $Id$
  9. // $Date$
  10. // $Revision$
  11. #include <boost/mpl/multiplies.hpp>
  12. #include <boost/mpl/list.hpp>
  13. #include <boost/mpl/lower_bound.hpp>
  14. #include <boost/mpl/transform_view.hpp>
  15. #include <boost/mpl/sizeof.hpp>
  16. #include <boost/mpl/int.hpp>
  17. #include <boost/mpl/identity.hpp>
  18. #include <boost/mpl/base.hpp>
  19. #include <boost/mpl/eval_if.hpp>
  20. #include <boost/mpl/deref.hpp>
  21. #include <boost/mpl/begin_end.hpp>
  22. #include <boost/mpl/assert.hpp>
  23. #include <boost/type_traits/is_same.hpp>
  24. namespace mpl = boost::mpl;
  25. using namespace mpl::placeholders;
  26. template< int bit_size >
  27. class big_int
  28. {
  29. // ...
  30. };
  31. template< int bit_size >
  32. struct integer
  33. {
  34. typedef mpl::list<char,short,int,long> builtins_;
  35. typedef typename mpl::base< typename mpl::lower_bound<
  36. mpl::transform_view< builtins_
  37. , mpl::multiplies< mpl::sizeof_<_1>, mpl::int_<8> >
  38. >
  39. , mpl::int_<bit_size>
  40. >::type >::type iter_;
  41. typedef typename mpl::end<builtins_>::type last_;
  42. typedef typename mpl::eval_if<
  43. boost::is_same<iter_,last_>
  44. , mpl::identity< big_int<bit_size> >
  45. , mpl::deref<iter_>
  46. >::type type;
  47. };
  48. typedef integer<1>::type int1;
  49. typedef integer<5>::type int5;
  50. typedef integer<15>::type int15;
  51. typedef integer<32>::type int32;
  52. typedef integer<100>::type int100;
  53. BOOST_MPL_ASSERT(( boost::is_same< int1, char > ));
  54. BOOST_MPL_ASSERT(( boost::is_same< int5, char > ));
  55. BOOST_MPL_ASSERT(( boost::is_same< int15, short > ));
  56. BOOST_MPL_ASSERT(( boost::is_same< int32, int > ));
  57. BOOST_MPL_ASSERT(( boost::is_same< int100, big_int<100> > ));