2006-08-22 15:57:13 +00:00
|
|
|
/*=============================================================================
|
|
|
|
Copyright (c) 2001-2006 Joel de Guzman
|
2007-06-11 07:01:05 +00:00
|
|
|
Copyright (c) 2007 Dan Marsden
|
2006-08-22 15:57:13 +00:00
|
|
|
|
2007-03-02 10:44:14 +00:00
|
|
|
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)
|
2006-08-22 15:57:13 +00:00
|
|
|
==============================================================================*/
|
|
|
|
#include <boost/detail/lightweight_test.hpp>
|
2007-10-20 22:21:43 +00:00
|
|
|
#include <boost/fusion/sequence/container/vector/vector.hpp>
|
|
|
|
#include <boost/fusion/sequence/adapted/mpl.hpp>
|
2006-08-22 15:57:13 +00:00
|
|
|
#include <boost/fusion/sequence/io/out.hpp>
|
|
|
|
#include <boost/fusion/sequence/intrinsic/at.hpp>
|
2007-06-13 20:30:30 +00:00
|
|
|
#include <boost/fusion/sequence/generation/make_vector.hpp>
|
2006-08-22 15:57:13 +00:00
|
|
|
#include <boost/fusion/algorithm/iteration/fold.hpp>
|
2007-05-28 23:13:46 +00:00
|
|
|
#include <boost/fusion/algorithm/iteration/accumulate.hpp>
|
2006-08-22 15:57:13 +00:00
|
|
|
#include <boost/type_traits/is_same.hpp>
|
|
|
|
#include <boost/mpl/if.hpp>
|
|
|
|
#include <boost/mpl/next.hpp>
|
|
|
|
#include <boost/mpl/int.hpp>
|
|
|
|
#include <boost/mpl/vector.hpp>
|
|
|
|
|
2007-06-19 21:30:06 +00:00
|
|
|
#include <boost/type_traits/remove_const.hpp>
|
|
|
|
#include <boost/type_traits/remove_reference.hpp>
|
|
|
|
#include <boost/type_traits/is_reference.hpp>
|
|
|
|
|
2007-06-13 20:30:30 +00:00
|
|
|
#include <string>
|
|
|
|
|
2006-08-22 15:57:13 +00:00
|
|
|
using boost::mpl::if_;
|
|
|
|
using boost::mpl::int_;
|
|
|
|
using boost::is_same;
|
|
|
|
|
|
|
|
struct add_ints_only
|
|
|
|
{
|
2007-06-11 07:01:05 +00:00
|
|
|
template<typename T>
|
|
|
|
struct result;
|
|
|
|
|
2006-08-22 15:57:13 +00:00
|
|
|
template <typename T, typename State>
|
2007-06-11 07:01:05 +00:00
|
|
|
struct result<add_ints_only(T,State)>
|
2006-08-22 15:57:13 +00:00
|
|
|
{
|
2007-06-19 21:30:06 +00:00
|
|
|
typedef typename boost::remove_const<
|
|
|
|
typename boost::remove_reference<State>::type>::type type;
|
2006-08-22 15:57:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename State>
|
|
|
|
State const&
|
|
|
|
operator()(T const& x, State const& state) const
|
|
|
|
{
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
operator()(int x, int state) const
|
|
|
|
{
|
|
|
|
return x + state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct count_ints
|
|
|
|
{
|
2007-06-11 07:01:05 +00:00
|
|
|
template<typename T>
|
|
|
|
struct result;
|
|
|
|
|
2006-08-22 15:57:13 +00:00
|
|
|
template <typename T, typename CountT>
|
2007-06-11 07:01:05 +00:00
|
|
|
struct result<count_ints(T,CountT)>
|
2006-08-22 15:57:13 +00:00
|
|
|
{
|
2007-06-19 21:30:06 +00:00
|
|
|
typedef typename boost::remove_const<
|
|
|
|
typename boost::remove_reference<T>::type>::type elem;
|
|
|
|
typedef typename boost::remove_const<
|
|
|
|
typename boost::remove_reference<CountT>::type>::type state;
|
|
|
|
|
2006-08-22 15:57:13 +00:00
|
|
|
typedef typename
|
|
|
|
if_<
|
2007-06-19 21:30:06 +00:00
|
|
|
is_same<elem, int>
|
|
|
|
, typename boost::mpl::next<state>::type
|
|
|
|
, state
|
2006-08-22 15:57:13 +00:00
|
|
|
>::type
|
|
|
|
type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename CountT>
|
2007-06-11 07:01:05 +00:00
|
|
|
typename result<count_ints(T, CountT)>::type
|
2006-08-22 15:57:13 +00:00
|
|
|
operator()(T const&, CountT const&) const
|
|
|
|
{
|
2007-06-11 07:01:05 +00:00
|
|
|
typedef typename result<count_ints(T, CountT)>::type result;
|
2006-08-22 15:57:13 +00:00
|
|
|
return result();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-06-13 20:30:30 +00:00
|
|
|
struct appender
|
|
|
|
{
|
|
|
|
typedef std::string result_type;
|
|
|
|
|
|
|
|
std::string operator()(char c, std::string const& str) const
|
|
|
|
{
|
|
|
|
return str + c;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-06-19 21:30:06 +00:00
|
|
|
struct lvalue_adder
|
|
|
|
{
|
|
|
|
template<typename Sig>
|
|
|
|
struct result;
|
|
|
|
|
|
|
|
template<typename T0, typename T1>
|
|
|
|
struct result<lvalue_adder(T0&, T1)>
|
|
|
|
{
|
|
|
|
// Second argument still needs to support rvalues - see definition of fusion::fold
|
|
|
|
typedef T0 type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T0, typename T1>
|
|
|
|
T0 operator()(T0& lhs, T1 const& rhs) const
|
|
|
|
{
|
|
|
|
return lhs + rhs;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-06-22 18:02:58 +00:00
|
|
|
int add(int lhs, int rhs)
|
|
|
|
{
|
|
|
|
return lhs + rhs;
|
|
|
|
}
|
|
|
|
|
2006-08-22 15:57:13 +00:00
|
|
|
int
|
|
|
|
main()
|
|
|
|
{
|
|
|
|
using namespace boost::fusion;
|
|
|
|
namespace fusion = boost::fusion;
|
|
|
|
|
|
|
|
{
|
|
|
|
typedef vector<int, char, int, double> vector_type;
|
|
|
|
vector_type v(12345, 'x', 678910, 3.36);
|
|
|
|
int result = fold(v, 0, add_ints_only());
|
|
|
|
std::cout << result << std::endl;
|
|
|
|
BOOST_TEST(result == 12345+678910);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
typedef vector<int> vector_type;
|
|
|
|
vector_type v(12345);
|
|
|
|
|
|
|
|
int n = fusion::fold(v, int_<0>(), count_ints());
|
|
|
|
std::cout << n << std::endl;
|
|
|
|
BOOST_TEST(n == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
typedef vector<int, char, int, double, int> vector_type;
|
|
|
|
vector_type v(12345, 'x', 678910, 3.36, 8756);
|
|
|
|
|
|
|
|
int n = fusion::fold(v, int_<0>(), count_ints());
|
|
|
|
std::cout << n << std::endl;
|
|
|
|
BOOST_TEST(n == 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
typedef boost::mpl::vector<int, char, int, double, int> mpl_vec;
|
|
|
|
int n = fusion::fold(mpl_vec(), int_<0>(), count_ints());
|
|
|
|
std::cout << n << std::endl;
|
|
|
|
BOOST_TEST(n == 3);
|
|
|
|
}
|
|
|
|
|
2007-06-13 20:30:30 +00:00
|
|
|
{
|
|
|
|
BOOST_TEST(fusion::fold(fusion::make_vector('a','b','c','d','e'), std::string(""), appender())
|
|
|
|
== "abcde");
|
|
|
|
}
|
|
|
|
|
2007-06-19 21:30:06 +00:00
|
|
|
{
|
|
|
|
vector<int, int> vec(1,2);
|
|
|
|
BOOST_TEST(fusion::fold(vec, 0, lvalue_adder()) == 3);
|
|
|
|
}
|
|
|
|
|
2007-06-22 18:02:58 +00:00
|
|
|
{
|
|
|
|
vector<int, int> vec(1,2);
|
|
|
|
BOOST_TEST(fusion::fold(vec, 0, add) == 3);
|
|
|
|
}
|
|
|
|
|
2007-05-28 23:13:46 +00:00
|
|
|
{
|
|
|
|
typedef vector<int, char, int, double> vector_type;
|
|
|
|
vector_type v(12345, 'x', 678910, 3.36);
|
|
|
|
int result = accumulate(v, 0, add_ints_only());
|
|
|
|
std::cout << result << std::endl;
|
|
|
|
BOOST_TEST(result == 12345+678910);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
typedef vector<int> vector_type;
|
|
|
|
vector_type v(12345);
|
|
|
|
|
|
|
|
int n = fusion::accumulate(v, int_<0>(), count_ints());
|
|
|
|
std::cout << n << std::endl;
|
|
|
|
BOOST_TEST(n == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
typedef vector<int, char, int, double, int> vector_type;
|
|
|
|
vector_type v(12345, 'x', 678910, 3.36, 8756);
|
|
|
|
|
|
|
|
int n = fusion::accumulate(v, int_<0>(), count_ints());
|
|
|
|
std::cout << n << std::endl;
|
|
|
|
BOOST_TEST(n == 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
typedef boost::mpl::vector<int, char, int, double, int> mpl_vec;
|
|
|
|
int n = fusion::accumulate(mpl_vec(), int_<0>(), count_ints());
|
|
|
|
std::cout << n << std::endl;
|
|
|
|
BOOST_TEST(n == 3);
|
|
|
|
}
|
|
|
|
|
2007-06-13 20:30:30 +00:00
|
|
|
{
|
|
|
|
BOOST_TEST(fusion::accumulate(fusion::make_vector('a','b','c','d','e'), std::string(""), appender())
|
|
|
|
== "abcde");
|
|
|
|
}
|
2007-05-28 23:13:46 +00:00
|
|
|
|
2007-06-22 18:02:58 +00:00
|
|
|
{
|
|
|
|
vector<int, int> vec(1,2);
|
|
|
|
BOOST_TEST(fusion::accumulate(vec, 0, add) == 3);
|
|
|
|
}
|
|
|
|
|
2006-08-22 15:57:13 +00:00
|
|
|
return boost::report_errors();
|
|
|
|
}
|
|
|
|
|