|
Boost : |
From: Maurizio Vitale (mav_at_[hidden])
Date: 2007-05-07 20:47:21
Eric,
I can get the type of a proto expression (wrapped in a user-defined skin) using
BOOST_TYPEOF, but then I noticed BOOST_PROTO_TYPEOF in context.hpp.
Which one is supposed to be used?
BOOST_PROTO_TYPEOF fails on me when trying to emulate the way it is used inside context.hpp.
Here is code that shows what I'm trying. If BOOST_PROTO_TYPEOF is internal only, just let me know and forget
the code below.
Regards,
Maurizio
#include <iostream>
#define BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS
#define BOOST_PROTO_MAX_ARITY 8
#define BOOST_MPL_LIMIT_METAFUNCTION_ARITY 8 // GCC bug workaround, needs to be at least BOOST_PROTO_MAX_ARITY
#include <boost/xpressive/proto/proto.hpp>
#include <boost/xpressive/proto/context.hpp>
#include <boost/xpressive/proto/extends.hpp>
#include <boost/xpressive/proto/transform/arg.hpp>
#include <boost/xpressive/proto/transform/construct.hpp>
namespace proto=boost::proto;
namespace mpl=boost::mpl;
using proto::_;
struct my_domain : proto::domain<struct my_grammar> {};
//struct my_domain : proto::domain<> {};
template<typename> struct my_context;
template <typename Expr>
struct my_expr : proto::extends<Expr, my_expr<Expr>, my_domain> {
typedef proto::extends<Expr, my_expr<Expr>, my_domain> base_type;
my_expr (Expr const& expr = Expr()) : base_type (expr) {};
using base_type::operator =;
operator int () const {
return static_cast<int>(proto::eval(*this, my_context<Expr> ()));
}
operator unsigned int () const {
return static_cast<unsigned int>(proto::eval(*this, my_context<Expr> ()));
}
};
template<typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H>
struct number {
unsigned int m_data;
};
struct my_grammar : proto::or_ <
proto::terminal< number<_,_,_,_,_,_,_,_> > ,
proto::terminal<int>,
proto::terminal<unsigned int>,
proto::unary_expr<proto::_, my_grammar> ,
proto::binary_expr<proto::_, my_grammar, my_grammar>
>
{};
namespace boost { namespace proto {
template<typename Expr>
struct generate<my_domain, Expr> {
typedef my_expr<Expr> type;
static type make (Expr const& expr) {
return type (expr);
}
};
} } // end namespace boost::proto
template<typename Expr>
struct my_context : proto::callable_context<const my_context<Expr> > {
typedef unsigned int result_type;
template<typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H>
unsigned int operator () (proto::tag::terminal, number<A,B,C,D,E,F,G,H> n) const { return n.m_data; }
};
template<int N>
struct my_int : my_expr<typename proto::terminal< number<mpl::int_<0>,mpl::int_<0>, mpl::int_<0>, mpl::int_<0>, mpl::int_<0>, mpl::int_<0>, mpl::int_<0>, mpl::int_<0> > >::type>
{
typedef number<mpl::int_<0>,mpl::int_<0>, mpl::int_<0>, mpl::int_<0>, mpl::int_<0>, mpl::int_<0>, mpl::int_<0>, mpl::int_<0> > number_type;
typedef my_expr<typename proto::terminal<number_type>::type> expr_type;
my_int () {}
my_int (int i) : expr_type (expr_type::type::make (i)) {}
template<typename Expr>
my_int& operator = (const my_expr<Expr>& e) {
proto::arg (*this).m_data = static_cast<int>(proto::eval(e, my_context<Expr> ()));
return *this;
}
template<typename T>
my_int& operator = (T value) {
proto::arg (*this).m_data = value;
return *this;
}
};
template<typename T> struct dump;
#define DUMP(T) typedef dump<T>::type t
int main (int,char**) {
my_int<6> i4(-22);
int i;
unsigned int j;
i4 = 5;
i4 = 5*i4;
i = i4/i4+4;
j = i4/i4;
BOOST_PROTO_TYPEOF (i4+j+2, a_type);
DUMP (a_type);
// typedef BOOST_TYPEOF (i4*2) another_type;
// DUMP(another_type);
}
/// Local Variables:
/// mode:c++
/// comment-column:80
/// fill-column:160
/// compilation-read-command:nil
/// compile-command:"g++ -I. -ope1 pe1.cpp"
/// End:
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk