sl@0: /*============================================================================= sl@0: Phoenix V1.2.1 sl@0: Copyright (c) 2001-2002 Joel de Guzman sl@0: sl@0: Use, modification and distribution is subject to the Boost Software sl@0: License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at sl@0: http://www.boost.org/LICENSE_1_0.txt) sl@0: ==============================================================================*/ sl@0: #ifndef PHOENIX_STATEMENTS_HPP sl@0: #define PHOENIX_STATEMENTS_HPP sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: #include sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: namespace phoenix { sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // sl@0: // sequential_composite sl@0: // sl@0: // Two or more actors separated by the comma generates a sl@0: // sequential_composite which is a composite actor. Example: sl@0: // sl@0: // actor, sl@0: // actor, sl@0: // actor sl@0: // sl@0: // The actors are evaluated sequentially. The result type of this sl@0: // is void. Note that the last actor should not have a trailing sl@0: // comma. sl@0: // sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: template sl@0: struct sequential_composite { sl@0: sl@0: typedef sequential_composite self_t; sl@0: sl@0: template sl@0: struct result { typedef void type; }; sl@0: sl@0: sequential_composite(A0 const& _0, A1 const& _1) sl@0: : a0(_0), a1(_1) {} sl@0: sl@0: template sl@0: void sl@0: eval(TupleT const& args) const sl@0: { sl@0: a0.eval(args); sl@0: a1.eval(args); sl@0: } sl@0: sl@0: A0 a0; A1 a1; // actors sl@0: }; sl@0: sl@0: ////////////////////////////////// sl@0: template sl@0: inline actor, actor > > sl@0: operator,(actor const& _0, actor const& _1) sl@0: { sl@0: return sequential_composite, actor >(_0, _1); sl@0: } sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // sl@0: // if_then_else_composite sl@0: // sl@0: // This composite has two (2) forms: sl@0: // sl@0: // if_(condition) sl@0: // [ sl@0: // statement sl@0: // ] sl@0: // sl@0: // and sl@0: // sl@0: // if_(condition) sl@0: // [ sl@0: // true_statement sl@0: // ] sl@0: // .else_ sl@0: // [ sl@0: // false_statement sl@0: // ] sl@0: // sl@0: // where condition is an actor that evaluates to bool. If condition sl@0: // is true, the true_statement (again an actor) is executed sl@0: // otherwise, the false_statement (another actor) is executed. The sl@0: // result type of this is void. Note the trailing underscore after sl@0: // if_ and the the leading dot and the trailing underscore before sl@0: // and after .else_. sl@0: // sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: template sl@0: struct if_then_else_composite { sl@0: sl@0: typedef if_then_else_composite self_t; sl@0: sl@0: template sl@0: struct result { sl@0: sl@0: typedef void type; sl@0: }; sl@0: sl@0: if_then_else_composite( sl@0: CondT const& cond_, sl@0: ThenT const& then_, sl@0: ElseT const& else__) sl@0: : cond(cond_), then(then_), else_(else__) {} sl@0: sl@0: template sl@0: void eval(TupleT const& args) const sl@0: { sl@0: if (cond.eval(args)) sl@0: then.eval(args); sl@0: else sl@0: else_.eval(args); sl@0: } sl@0: sl@0: CondT cond; ThenT then; ElseT else_; // actors sl@0: }; sl@0: sl@0: ////////////////////////////////// sl@0: template sl@0: struct else_gen { sl@0: sl@0: else_gen(CondT const& cond_, ThenT const& then_) sl@0: : cond(cond_), then(then_) {} sl@0: sl@0: template sl@0: actor::type> > sl@0: operator[](ElseT const& else_) sl@0: { sl@0: typedef if_then_else_composite::type> sl@0: result; sl@0: sl@0: return result(cond, then, as_actor::convert(else_)); sl@0: } sl@0: sl@0: CondT cond; ThenT then; sl@0: }; sl@0: sl@0: ////////////////////////////////// sl@0: template sl@0: struct if_then_composite { sl@0: sl@0: typedef if_then_composite self_t; sl@0: sl@0: template sl@0: struct result { typedef void type; }; sl@0: sl@0: if_then_composite(CondT const& cond_, ThenT const& then_) sl@0: : cond(cond_), then(then_), else_(cond, then) {} sl@0: sl@0: template sl@0: void eval(TupleT const& args) const sl@0: { sl@0: if (cond.eval(args)) sl@0: then.eval(args); sl@0: } sl@0: sl@0: CondT cond; ThenT then; // actors sl@0: else_gen else_; sl@0: }; sl@0: sl@0: ////////////////////////////////// sl@0: template sl@0: struct if_gen { sl@0: sl@0: if_gen(CondT const& cond_) sl@0: : cond(cond_) {} sl@0: sl@0: template sl@0: actor::type, sl@0: typename as_actor::type> > sl@0: operator[](ThenT const& then) const sl@0: { sl@0: typedef if_then_composite< sl@0: typename as_actor::type, sl@0: typename as_actor::type> sl@0: result; sl@0: sl@0: return result( sl@0: as_actor::convert(cond), sl@0: as_actor::convert(then)); sl@0: } sl@0: sl@0: CondT cond; sl@0: }; sl@0: sl@0: ////////////////////////////////// sl@0: template sl@0: inline if_gen sl@0: if_(CondT const& cond) sl@0: { sl@0: return if_gen(cond); sl@0: } sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // sl@0: // while_composite sl@0: // sl@0: // This composite has the form: sl@0: // sl@0: // while_(condition) sl@0: // [ sl@0: // statement sl@0: // ] sl@0: // sl@0: // While the condition (an actor) evaluates to true, statement sl@0: // (another actor) is executed. The result type of this is void. sl@0: // Note the trailing underscore after while_. sl@0: // sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: template sl@0: struct while_composite { sl@0: sl@0: typedef while_composite self_t; sl@0: sl@0: template sl@0: struct result { typedef void type; }; sl@0: sl@0: while_composite(CondT const& cond_, DoT const& do__) sl@0: : cond(cond_), do_(do__) {} sl@0: sl@0: template sl@0: void eval(TupleT const& args) const sl@0: { sl@0: while (cond.eval(args)) sl@0: do_.eval(args); sl@0: } sl@0: sl@0: CondT cond; sl@0: DoT do_; sl@0: }; sl@0: sl@0: ////////////////////////////////// sl@0: template sl@0: struct while_gen { sl@0: sl@0: while_gen(CondT const& cond_) sl@0: : cond(cond_) {} sl@0: sl@0: template sl@0: actor::type, sl@0: typename as_actor::type> > sl@0: operator[](DoT const& do_) const sl@0: { sl@0: typedef while_composite< sl@0: typename as_actor::type, sl@0: typename as_actor::type> sl@0: result; sl@0: sl@0: return result( sl@0: as_actor::convert(cond), sl@0: as_actor::convert(do_)); sl@0: } sl@0: sl@0: CondT cond; sl@0: }; sl@0: sl@0: ////////////////////////////////// sl@0: template sl@0: inline while_gen sl@0: while_(CondT const& cond) sl@0: { sl@0: return while_gen(cond); sl@0: } sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // sl@0: // do_composite sl@0: // sl@0: // This composite has the form: sl@0: // sl@0: // do_ sl@0: // [ sl@0: // statement sl@0: // ] sl@0: // .while_(condition) sl@0: // sl@0: // While the condition (an actor) evaluates to true, statement sl@0: // (another actor) is executed. The statement is executed at least sl@0: // once. The result type of this is void. Note the trailing sl@0: // underscore after do_ and the the leading dot and the trailing sl@0: // underscore before and after .while_. sl@0: // sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: template sl@0: struct do_composite { sl@0: sl@0: typedef do_composite self_t; sl@0: sl@0: template sl@0: struct result { typedef void type; }; sl@0: sl@0: do_composite(DoT const& do__, CondT const& cond_) sl@0: : do_(do__), cond(cond_) {} sl@0: sl@0: template sl@0: void eval(TupleT const& args) const sl@0: { sl@0: do sl@0: do_.eval(args); sl@0: while (cond.eval(args)); sl@0: } sl@0: sl@0: DoT do_; sl@0: CondT cond; sl@0: }; sl@0: sl@0: //////////////////////////////////// sl@0: template sl@0: struct do_gen2 { sl@0: sl@0: do_gen2(DoT const& do__) sl@0: : do_(do__) {} sl@0: sl@0: template sl@0: actor::type, sl@0: typename as_actor::type> > sl@0: while_(CondT const& cond) const sl@0: { sl@0: typedef do_composite< sl@0: typename as_actor::type, sl@0: typename as_actor::type> sl@0: result; sl@0: sl@0: return result( sl@0: as_actor::convert(do_), sl@0: as_actor::convert(cond)); sl@0: } sl@0: sl@0: DoT do_; sl@0: }; sl@0: sl@0: //////////////////////////////////// sl@0: struct do_gen { sl@0: sl@0: template sl@0: do_gen2 sl@0: operator[](DoT const& do_) const sl@0: { sl@0: return do_gen2(do_); sl@0: } sl@0: }; sl@0: sl@0: do_gen const do_ = do_gen(); sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // sl@0: // for_composite sl@0: // sl@0: // This statement has the form: sl@0: // sl@0: // for_(init, condition, step) sl@0: // [ sl@0: // statement sl@0: // ] sl@0: // sl@0: // Where init, condition, step and statement are all actors. init sl@0: // is executed once before entering the for-loop. The for-loop sl@0: // exits once condition evaluates to false. At each loop iteration, sl@0: // step and statement is called. The result of this statement is sl@0: // void. Note the trailing underscore after for_. sl@0: // sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: template sl@0: struct for_composite { sl@0: sl@0: typedef composite self_t; sl@0: sl@0: template sl@0: struct result { typedef void type; }; sl@0: sl@0: for_composite( sl@0: InitT const& init_, sl@0: CondT const& cond_, sl@0: StepT const& step_, sl@0: DoT const& do__) sl@0: : init(init_), cond(cond_), step(step_), do_(do__) {} sl@0: sl@0: template sl@0: void sl@0: eval(TupleT const& args) const sl@0: { sl@0: for (init.eval(args); cond.eval(args); step.eval(args)) sl@0: do_.eval(args); sl@0: } sl@0: sl@0: InitT init; CondT cond; StepT step; DoT do_; // actors sl@0: }; sl@0: sl@0: ////////////////////////////////// sl@0: template sl@0: struct for_gen { sl@0: sl@0: for_gen( sl@0: InitT const& init_, sl@0: CondT const& cond_, sl@0: StepT const& step_) sl@0: : init(init_), cond(cond_), step(step_) {} sl@0: sl@0: template sl@0: actor::type, sl@0: typename as_actor::type, sl@0: typename as_actor::type, sl@0: typename as_actor::type> > sl@0: operator[](DoT const& do_) const sl@0: { sl@0: typedef for_composite< sl@0: typename as_actor::type, sl@0: typename as_actor::type, sl@0: typename as_actor::type, sl@0: typename as_actor::type> sl@0: result; sl@0: sl@0: return result( sl@0: as_actor::convert(init), sl@0: as_actor::convert(cond), sl@0: as_actor::convert(step), sl@0: as_actor::convert(do_)); sl@0: } sl@0: sl@0: InitT init; CondT cond; StepT step; sl@0: }; sl@0: sl@0: ////////////////////////////////// sl@0: template sl@0: inline for_gen sl@0: for_(InitT const& init, CondT const& cond, StepT const& step) sl@0: { sl@0: return for_gen(init, cond, step); sl@0: } sl@0: sl@0: } // namespace phoenix sl@0: sl@0: #endif