Ticket #5905: new_switch.patch

File new_switch.patch, 7.3 KB (added by Pierre Esterie <pesterie@…>, 11 years ago)

patch_switch

  • boost/proto/proto_fwd.hpp

     
    269269
    270270    using namespace tagns_;
    271271
     272    template<typename Expr>
     273    struct tag_of;
     274
    272275    ////////////////////////////////////////////////////////////////////////////////////////////////
    273276    struct _;
    274277
     
    359362    template<typename Condition, typename Then = _, typename Else = not_<_> >
    360363    struct if_;
    361364
    362     template<typename Cases>
     365    template<typename Cases, typename Transform = tag_of<_>() >
    363366    struct switch_;
    364367
    365368    template<typename T>
     
    462465    struct is_sub_domain_of;
    463466
    464467    template<typename Expr>
    465     struct tag_of;
    466 
    467     template<typename Expr>
    468468    struct arity_of;
    469469
    470470    template<typename T, typename Void = void>
     
    741741    template<typename Grammar>
    742742    struct pass_through;
    743743
    744     template<typename Grammar = detail::_default>
     744    template<typename Grammar = detail::_default >
    745745    struct _default;
    746746
    747747    struct _expr;
  • boost/proto/matches.hpp

     
    1010#ifndef BOOST_PROTO_MATCHES_HPP_EAN_11_03_2006
    1111#define BOOST_PROTO_MATCHES_HPP_EAN_11_03_2006
    1212
     13
    1314#include <boost/config.hpp>
    1415#include <boost/detail/workaround.hpp>
    1516#include <boost/preprocessor/cat.hpp>
     
    402403        {};
    403404
    404405        // handle proto::switch_
    405         template<typename Expr, typename Tag, typename Args, long Arity, typename Cases>
    406         struct matches_<Expr, proto::basic_expr<Tag, Args, Arity>, switch_<Cases> >
    407           : matches_<
    408                 Expr
    409               , proto::basic_expr<Tag, Args, Arity>
    410               , typename Cases::template case_<Tag>::proto_grammar
    411             >
     406        template< typename Expr, typename Tag, typename Args, long Arity
     407                , typename Cases, typename Transform
     408                >
     409        struct matches_ < Expr, proto::basic_expr<Tag, Args, Arity>
     410                        , switch_<Cases,Transform>
     411                        >
     412        : matches_< Expr
     413                  , proto::basic_expr<Tag, Args, Arity>
     414                  , typename
     415                    Cases::template case_<typename
     416                                          when<_,Transform>::template
     417                                          impl<Expr,int,int>::result_type
     418                                         >::proto_grammar
     419                  >
    412420        {
    413             typedef typename Cases::template case_<Tag> which;
     421          typedef when<_,Transform> applicant;
     422          typedef typename applicant::template impl<Expr,int,int>::result_type selected_type;
     423          typedef typename Cases::template case_<selected_type> which;
    414424        };
     425
     426        template< typename Expr, typename Tag, typename Args, long Arity
     427                , typename Cases
     428                >
     429        struct matches_ < Expr, proto::basic_expr<Tag, Args, Arity>
     430                        , switch_<Cases>
     431                        >
     432        : matches_< Expr
     433                  , proto::basic_expr<Tag, Args, Arity>
     434                  , typename  Cases::template
     435                              case_<Tag>::proto_grammar
     436                  >
     437        {
     438          typedef typename Cases::template case_<Tag> which;
     439        };
    415440    }
    416441
    417442    /// \brief A Boolean metafunction that evaluates whether a given
     
    449474    ///     not match \c T.
    450475    /// \li An expression \c E matches <tt>switch_\<C\></tt> if
    451476    ///     \c E matches <tt>C::case_\<E::proto_tag\></tt>.
     477    /// \li An expression \c E matches <tt>switch_\<C,T\></tt> if
     478    ///     the result type of the transform \c T on \c E matches
     479    ///     <tt>C::case_\<boost::result_of\<T(E)\>::type\></tt>.
    452480    ///
    453481    /// A terminal expression <tt>expr\<AT,term\<A\> \></tt> matches
    454482    /// a grammar <tt>expr\<BT,term\<B\> \></tt> if \c BT is \c AT or
     
    784812    /// When applying <tt>switch_\<C\></tt> as a transform with an
    785813    /// expression \c e of type \c E, state \c s and data \c d, it is
    786814    /// equivalent to <tt>C::case_\<E::proto_tag\>()(e, s, d)</tt>.
     815    template<typename Cases, typename Transform>
     816    struct switch_ : transform< switch_<Cases,Transform> >
     817    {
     818      typedef switch_ proto_grammar;
     819      typedef when<_,Transform> applicant;
     820
     821      template<typename Expr, typename State, typename Data>
     822      struct impl
     823      : Cases::template
     824        case_ < typename applicant::template impl<Expr,State,Data>::result_type
     825              >::template impl<Expr, State, Data>
     826      {};
     827
     828      template<typename Expr, typename State, typename Data>
     829      struct impl<Expr &, State, Data>
     830      : Cases::template
     831        case_ <typename applicant::template impl<Expr,State,Data>::result_type
     832              >::template impl<Expr &, State, Data>
     833      {};
     834    };
     835
    787836    template<typename Cases>
    788     struct switch_ : transform<switch_<Cases> >
     837    struct switch_<Cases> : transform< switch_<Cases> >
    789838    {
    790         typedef switch_ proto_grammar;
     839      typedef switch_ proto_grammar;
     840      /// \param e An expression
     841      /// \param s The current state
     842      /// \param d A data of arbitrary type
     843      /// \pre <tt>matches\<Expr,switch_\>::value</tt> is \c true.
     844      /// \return <tt>which()(e, s, d)</tt>, where <tt>which</tt> is
     845      /// <tt>Cases::case_<typename Expr::proto_tag></tt>
    791846
    792         /// \param e An expression
    793         /// \param s The current state
    794         /// \param d A data of arbitrary type
    795         /// \pre <tt>matches\<Expr,switch_\>::value</tt> is \c true.
    796         /// \return <tt>which()(e, s, d)</tt>, where <tt>which</tt> is
    797         /// <tt>Cases::case_<typename Expr::proto_tag></tt>
     847      template<typename Expr, typename State, typename Data>
     848      struct impl
     849      : Cases::template case_<typename Expr::proto_tag>::template impl<Expr, State, Data>
     850      {};
    798851
    799         template<typename Expr, typename State, typename Data>
    800         struct impl
    801           : Cases::template case_<typename Expr::proto_tag>::template impl<Expr, State, Data>
    802         {};
    803 
    804         template<typename Expr, typename State, typename Data>
    805         struct impl<Expr &, State, Data>
    806           : Cases::template case_<typename Expr::proto_tag>::template impl<Expr &, State, Data>
    807         {};
     852      template<typename Expr, typename State, typename Data>
     853      struct impl<Expr &, State, Data>
     854      : Cases::template case_<typename Expr::proto_tag>::template impl<Expr &, State, Data>
     855      {};
    808856    };
    809857
    810858    /// \brief For forcing exact matches of terminal types.
     
    901949
    902950    /// INTERNAL ONLY
    903951    ///
    904     template<typename Cases>
    905     struct is_callable<switch_<Cases> >
     952    template<typename Cases, typename Transform>
     953    struct is_callable<switch_<Cases,Transform> >
    906954      : mpl::true_
    907955    {};
    908956
  • libs/proto/test/Jamfile.v2

     
    3131        [ run make_expr.cpp ]
    3232        [ run matches.cpp ]
    3333        [ run flatten.cpp ]
     34        [ run new_switch.cpp ]
    3435        [ run toy_spirit.cpp ]
    3536        [ run toy_spirit2.cpp ]
    3637        [ run make.cpp ]