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