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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
//[ Mixed
///////////////////////////////////////////////////////////////////////////////
// Copyright 2008 Eric Niebler. 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)
//
// This is an example of using BOOST_PROTO_DEFINE_OPERATORS to Protofy
// expressions using std::vector<> and std::list, non-proto types. It is a port
// of the Mixed example from PETE.
// (http://www.codesourcery.com/pooma/download.html).
#include <list>
#include <cmath>
#include <vector>
#include <complex>
#include <iostream>
#include <stdexcept>
#include <boost/proto/core.hpp>
#include <boost/proto/debug.hpp>
#include <boost/proto/context.hpp>
#include <boost/proto/transform.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/typeof/std/list.hpp>
#include <boost/typeof/std/vector.hpp>
#include <boost/typeof/std/complex.hpp>
#include <boost/type_traits/remove_reference.hpp>
namespace proto = boost::proto;
namespace mpl = boost::mpl;
using proto::_;
template<typename Expr>
struct MixedExpr;
template<typename Iter>
struct iterator_wrapper
{
typedef Iter iterator;
explicit iterator_wrapper(Iter iter)
: it(iter)
{}
mutable Iter it;
};
struct begin : proto::callable
{
template<class Sig>
struct result;
template<class This, class Cont>
struct result<This(Cont)>
: proto::result_of::as_expr<
iterator_wrapper<typename boost::remove_reference<Cont>::type::const_iterator>
>
{};
template<typename Cont>
typename result<begin(Cont const &)>::type
operator ()(Cont const &cont) const
{
iterator_wrapper<typename Cont::const_iterator> it(cont.begin());
return proto::as_expr(it);
}
};
// Here is a grammar that replaces vector and list terminals with their
// begin iterators
struct Begin
: proto::or_<
proto::when< proto::terminal< std::vector<_, _> >, begin(proto::_value) >
, proto::when< proto::terminal< std::list<_, _> >, begin(proto::_value) >
, proto::when< proto::terminal<_> >
, proto::when< proto::nary_expr<_, proto::vararg<Begin> > >
>
{};
// Here is an evaluation context that dereferences iterator
// terminals.
struct DereferenceCtx
{
// Unless this is an iterator terminal, use the
// default evaluation context
template<typename Expr, typename EnableIf = void>
struct eval
: proto::default_eval<Expr, DereferenceCtx const>
{};
// Dereference iterator terminals.
template<typename Expr>
struct eval<
Expr
, typename boost::enable_if<
proto::matches<Expr, proto::terminal<iterator_wrapper<_> > >
>::type
>
{
typedef typename proto::result_of::value<Expr>::type IteratorWrapper;
typedef typename IteratorWrapper::iterator iterator;
typedef typename std::iterator_traits<iterator>::reference result_type;
result_type operator ()(Expr &expr, DereferenceCtx const &) const
{
return *proto::value(expr).it;
}
};
};
// Here is an evaluation context that increments iterator
// terminals.
struct IncrementCtx
{
// Unless this is an iterator terminal, use the
// default evaluation context
template<typename Expr, typename EnableIf = void>
struct eval
: proto::null_eval<Expr, IncrementCtx const>
{};
// advance iterator terminals.
template<typename Expr>
struct eval<
Expr
, typename boost::enable_if<
proto::matches<Expr, proto::terminal<iterator_wrapper<_> > >
>::type
>
{
typedef void result_type;
result_type operator ()(Expr &expr, IncrementCtx const &) const
{
++proto::value(expr).it;
}
};
};
// A grammar which matches all the assignment operators,
// so we can easily disable them.
struct AssignOps
: proto::switch_<struct AssignOpsCases>
{};
// Here are the cases used by the switch_ above.
struct AssignOpsCases
{
template<typename Tag, int D = 0> struct case_ : proto::not_<_> {};
template<int D> struct case_< proto::tag::plus_assign, D > : _ {};
template<int D> struct case_< proto::tag::minus_assign, D > : _ {};
template<int D> struct case_< proto::tag::multiplies_assign, D > : _ {};
template<int D> struct case_< proto::tag::divides_assign, D > : _ {};
template<int D> struct case_< proto::tag::modulus_assign, D > : _ {};
template<int D> struct case_< proto::tag::shift_left_assign, D > : _ {};
template<int D> struct case_< proto::tag::shift_right_assign, D > : _ {};
template<int D> struct case_< proto::tag::bitwise_and_assign, D > : _ {};
template<int D> struct case_< proto::tag::bitwise_or_assign, D > : _ {};
template<int D> struct case_< proto::tag::bitwise_xor_assign, D > : _ {};
};
// An expression conforms to the MixedGrammar if it is a terminal or some
// op that is not an assignment op. (Assignment will be handled specially.)
struct MixedGrammar
: proto::or_<
proto::terminal<_>
, proto::and_<
proto::nary_expr<_, proto::vararg<MixedGrammar> >
, proto::not_<AssignOps>
>
>
{};
// Expressions in the MixedDomain will be wrapped in MixedExpr<>
// and must conform to the MixedGrammar
struct MixedDomain
: proto::domain<proto::generator<MixedExpr>, MixedGrammar>
{};
// Here is MixedExpr, a wrapper for expression types in the MixedDomain.
template<typename Expr>
struct MixedExpr
: proto::extends<Expr, MixedExpr<Expr>, MixedDomain>
{
explicit MixedExpr(Expr const &expr)
: MixedExpr::proto_extends(expr)
{}
private:
// hide this:
using proto::extends<Expr, MixedExpr<Expr>, MixedDomain>::operator [];
};
// Define a trait type for detecting vector and list terminals, to
// be used by the BOOST_PROTO_DEFINE_OPERATORS macro below.
template<typename T>
struct IsMixed
: mpl::false_
{};
template<typename T, typename A>
struct IsMixed<std::list<T, A> >
: mpl::true_
{};
template<typename T, typename A>
struct IsMixed<std::vector<T, A> >
: mpl::true_
{};
namespace MixedOps
{
// This defines all the overloads to make expressions involving
// std::vector to build expression templates.
BOOST_PROTO_DEFINE_OPERATORS(IsMixed, MixedDomain)
struct assign_op
{
template<typename T, typename U>
void operator ()(T &t, U const &u) const
{
t = u;
}
};
struct plus_assign_op
{
template<typename T, typename U>
void operator ()(T &t, U const &u) const
{
t += u;
}
};
struct minus_assign_op
{
template<typename T, typename U>
void operator ()(T &t, U const &u) const
{
t -= u;
}
};
struct sin_
{
template<typename Sig>
struct result;
template<typename This, typename Arg>
struct result<This(Arg)>
: boost::remove_const<typename boost::remove_reference<Arg>::type>
{};
template<typename Arg>
Arg operator ()(Arg const &a) const
{
return std::sin(a);
}
};
template<typename A>
typename proto::result_of::make_expr<
proto::tag::function
, MixedDomain
, sin_ const
, A const &
>::type sin(A const &a)
{
return proto::make_expr<proto::tag::function, MixedDomain>(sin_(), boost::ref(a));
}
template<typename FwdIter, typename Expr, typename Op>
void evaluate(FwdIter begin, FwdIter end, Expr const &expr, Op op)
{
IncrementCtx const inc = {};
DereferenceCtx const deref = {};
typename boost::result_of<Begin(Expr const &)>::type expr2 = Begin()(expr);
for(; begin != end; ++begin)
{
op(*begin, proto::eval(expr2, deref));
proto::eval(expr2, inc);
}
}
// Add-assign to a vector from some expression.
template<typename T, typename A, typename Expr>
std::vector<T, A> &assign(std::vector<T, A> &arr, Expr const &expr)
{
evaluate(arr.begin(), arr.end(), proto::as_expr<MixedDomain>(expr), assign_op());
return arr;
}
// Add-assign to a list from some expression.
template<typename T, typename A, typename Expr>
std::list<T, A> &assign(std::list<T, A> &arr, Expr const &expr)
{
evaluate(arr.begin(), arr.end(), proto::as_expr<MixedDomain>(expr), assign_op());
return arr;
}
// Add-assign to a vector from some expression.
template<typename T, typename A, typename Expr>
std::vector<T, A> &operator +=(std::vector<T, A> &arr, Expr const &expr)
{
evaluate(arr.begin(), arr.end(), proto::as_expr<MixedDomain>(expr), plus_assign_op());
return arr;
}
// Add-assign to a list from some expression.
template<typename T, typename A, typename Expr>
std::list<T, A> &operator +=(std::list<T, A> &arr, Expr const &expr)
{
evaluate(arr.begin(), arr.end(), proto::as_expr<MixedDomain>(expr), plus_assign_op());
return arr;
}
// Minus-assign to a vector from some expression.
template<typename T, typename A, typename Expr>
std::vector<T, A> &operator -=(std::vector<T, A> &arr, Expr const &expr)
{
evaluate(arr.begin(), arr.end(), proto::as_expr<MixedDomain>(expr), minus_assign_op());
return arr;
}
// Minus-assign to a list from some expression.
template<typename T, typename A, typename Expr>
std::list<T, A> &operator -=(std::list<T, A> &arr, Expr const &expr)
{
evaluate(arr.begin(), arr.end(), proto::as_expr<MixedDomain>(expr), minus_assign_op());
return arr;
}
}
int main()
{
using namespace MixedOps;
int n = 10;
std::vector<int> a,b,c,d;
std::list<double> e;
std::list<std::complex<double> > f;
int i;
for(i = 0;i < n; ++i)
{
a.push_back(i);
b.push_back(2*i);
c.push_back(3*i);
d.push_back(i);
e.push_back(0.0);
f.push_back(std::complex<double>(1.0, 1.0));
}
MixedOps::assign(b, 2);
MixedOps::assign(d, a + b * c);
a += if_else(d < 30, b, c);
MixedOps::assign(e, c);
e += e - 4 / (c + 1);
f -= sin(0.1 * e * std::complex<double>(0.2, 1.2));
std::list<double>::const_iterator ei = e.begin();
std::list<std::complex<double> >::const_iterator fi = f.begin();
for (i = 0; i < n; ++i)
{
std::cout
<< "a(" << i << ") = " << a[i]
<< " b(" << i << ") = " << b[i]
<< " c(" << i << ") = " << c[i]
<< " d(" << i << ") = " << d[i]
<< " e(" << i << ") = " << *ei++
<< " f(" << i << ") = " << *fi++
<< std::endl;
}
}
//]
|