Chris@16: /*============================================================================== Chris@16: Copyright (c) 2001-2010 Joel de Guzman Chris@16: Copyright (c) 2010 Eric Niebler Chris@16: Chris@16: Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: ==============================================================================*/ Chris@16: #ifndef BOOST_PHOENIX_STATEMENT_IF_HPP Chris@16: #define BOOST_PHOENIX_STATEMENT_IF_HPP Chris@16: Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable: 4355) // 'this' used in base member initializer list Chris@16: #endif Chris@16: Chris@16: namespace boost { namespace phoenix Chris@16: { Chris@16: template struct if_actor; Chris@16: }} Chris@16: Chris@16: BOOST_PHOENIX_DEFINE_EXPRESSION_EXT( Chris@16: if_actor Chris@16: , (boost)(phoenix)(if_) Chris@16: , (meta_grammar) // Cond Chris@16: (meta_grammar) // Then Chris@16: ) Chris@16: Chris@16: BOOST_PHOENIX_DEFINE_EXPRESSION( Chris@16: (boost)(phoenix)(if_else_statement) Chris@16: , (meta_grammar) // Cond Chris@16: (meta_grammar) // Then Chris@16: (meta_grammar) // Else Chris@16: ) Chris@16: Chris@16: namespace boost { namespace phoenix Chris@16: { Chris@16: //////////////////////////////////////////////////////////////////////////// Chris@16: // If-Else statements Chris@16: //////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: // Function for evaluating lambdas like: Chris@16: // if_( foo )[ bar ] Chris@16: // and Chris@16: // if_( foo )[ bar ].else_[ baz ] Chris@16: struct if_else_eval Chris@16: { Chris@16: typedef void result_type; Chris@16: Chris@16: template Chris@16: result_type Chris@16: operator()(Cond const & cond, Then const & then, Context const & ctx) const Chris@16: { Chris@16: if(boost::phoenix::eval(cond, ctx)) Chris@16: boost::phoenix::eval(then, ctx); Chris@16: } Chris@16: Chris@16: template Chris@16: result_type Chris@16: operator()( Chris@16: Cond const & cond Chris@16: , Then const & then Chris@16: , Else const & else_ Chris@16: , Context const & ctx Chris@16: ) const Chris@16: { Chris@16: if(boost::phoenix::eval(cond, ctx)) Chris@16: boost::phoenix::eval(then, ctx); Chris@16: else Chris@16: boost::phoenix::eval(else_, ctx); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct default_actions::when Chris@16: : call Chris@16: {}; Chris@16: Chris@16: template Chris@16: struct default_actions::when Chris@16: : call Chris@16: {}; Chris@16: Chris@16: Chris@16: // Generator for .else_[ expr ] branch. Chris@16: template Chris@16: struct else_gen Chris@16: { Chris@101: else_gen(Cond const & cond_, Then const & then_) Chris@101: : cond(cond_) Chris@101: , then(then_) {} Chris@16: Chris@16: template Chris@16: typename expression::if_else_statement::type const Chris@16: operator[](Else const & else_) const Chris@16: { Chris@16: return expression::if_else_statement::make(cond, then, else_); Chris@16: } Chris@16: Chris@16: Cond cond; Chris@16: Then then; Chris@16: }; Chris@16: Chris@16: // We subclass actor so we can provide the member else_ (which is an Chris@16: // else_gen responsible for the .else_[ expr ] branch). Chris@16: template Chris@16: struct if_actor : actor Chris@16: { Chris@16: typedef actor base_type; Chris@16: Chris@16: if_actor(base_type const & base) Chris@16: : base_type(base) Chris@16: , else_(proto::child_c<0>(*this), proto::child_c<1>(*this)) Chris@16: {} Chris@16: Chris@16: typedef typename proto::result_of::child_c::type cond_type; Chris@16: typedef typename proto::result_of::child_c::type then_type; Chris@16: Chris@16: else_gen else_; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct is_actor > Chris@16: : mpl::true_ Chris@16: {}; Chris@16: Chris@16: // Generator for if( cond )[ then ] branch. Chris@16: template Chris@16: struct if_gen Chris@16: { Chris@101: if_gen(Cond const & cond_) Chris@101: : cond(cond_) {} Chris@16: Chris@16: template Chris@16: typename expression::if_::type const Chris@16: operator[](Then const & then) const Chris@16: { Chris@16: return expression::if_::make(cond, then); Chris@16: } Chris@16: Chris@16: Cond cond; Chris@16: }; Chris@16: Chris@16: template Chris@16: inline Chris@16: if_gen const Chris@16: if_(Cond const & cond) Chris@16: { Chris@16: return if_gen(cond); Chris@16: } Chris@16: Chris@16: }} Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: #endif