1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2007 Dan Marsden
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)
==============================================================================*/
#include <boost/detail/lightweight_test.hpp>
#include <boost/fusion/container/vector/vector.hpp>
#include <boost/fusion/adapted/mpl.hpp>
#include <boost/fusion/sequence/io/out.hpp>
#include <boost/fusion/sequence/intrinsic/at.hpp>
#include <boost/fusion/container/generation/make_vector.hpp>
#include <boost/fusion/algorithm/iteration/fold.hpp>
#include <boost/fusion/algorithm/iteration/accumulate.hpp>
#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>
#include <boost/type_traits/remove_const.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/type_traits/is_reference.hpp>
#include <string>
using boost::mpl::if_;
using boost::mpl::int_;
using boost::is_same;
struct add_ints_only
{
template<typename T>
struct result;
template <typename State, typename T>
struct result<add_ints_only(State, T)>
{
typedef typename boost::remove_const<
typename boost::remove_reference<State>::type>::type type;
};
template <typename State, typename T>
State
operator()(State const& state, T const& /*x*/) const
{
return state;
}
int
operator()(int state, int x) const
{
return x + state;
}
};
struct count_ints
{
template<typename T>
struct result;
template <typename CountT, typename T>
struct result<count_ints(CountT, T)>
{
typedef typename boost::remove_const<
typename boost::remove_reference<CountT>::type>::type state;
typedef typename boost::remove_const<
typename boost::remove_reference<T>::type>::type elem;
typedef typename
if_<
is_same<elem, int>
, typename boost::mpl::next<state>::type
, state
>::type
type;
};
template <typename CountT, typename T>
typename result<count_ints(CountT, T)>::type
operator()(CountT const&, T const&) const
{
typedef typename result<count_ints(CountT, T)>::type result_;
return result_();
}
};
struct appender
{
typedef std::string result_type;
std::string operator()(std::string const& str, char c) const
{
return str + c;
}
};
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 T1 type;
};
template<typename T0, typename T1>
T1 operator()(T0 const& lhs, T1& rhs) const
{
return lhs + rhs;
}
};
int add(int lhs, int rhs)
{
return lhs + rhs;
}
struct functor
{
template<typename T>
int
operator() (int hitherho, T const& cur) const
{
return int(hitherho + cur);
}
};
struct visitor
{
typedef int result_type;
int operator()(int sum, long&)
{
return sum;
}
};
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);
}
{
BOOST_TEST(fusion::fold(fusion::make_vector('a','b','c','d','e'), std::string(""), appender())
== "abcde");
}
{
vector<int, int> vec(1,2);
BOOST_TEST(fusion::fold(vec, 0, lvalue_adder()) == 3);
}
{
vector<int, int> vec(1,2);
BOOST_TEST(fusion::fold(vec, 0, add) == 3);
}
{
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);
}
{
BOOST_TEST(fusion::accumulate(fusion::make_vector('a','b','c','d','e'), std::string(""), appender())
== "abcde");
}
{
vector<int, int> vec(1,2);
BOOST_TEST(fusion::accumulate(vec, 0, add) == 3);
}
{
#if defined(BOOST_RESULT_OF_USE_DECLTYPE)
{
boost::fusion::vector<int, double, long> container{1, 2, 3};
functor f;
boost::fusion::fold(container, 0, f);
}
#endif
{
boost::fusion::vector<long> vec;
visitor v;
boost::fusion::fold(vec, 0, v);
}
}
return boost::report_errors();
}
|