os/ossrv/ossrv_pub/boost_apis/boost/spirit/phoenix/statements.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*=============================================================================
     2     Phoenix V1.2.1
     3     Copyright (c) 2001-2002 Joel de Guzman
     4 
     5     Use, modification and distribution is subject to the Boost Software
     6     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
     7     http://www.boost.org/LICENSE_1_0.txt)
     8 ==============================================================================*/
     9 #ifndef PHOENIX_STATEMENTS_HPP
    10 #define PHOENIX_STATEMENTS_HPP
    11 
    12 ///////////////////////////////////////////////////////////////////////////////
    13 #include <boost/spirit/phoenix/composite.hpp>
    14 
    15 ///////////////////////////////////////////////////////////////////////////////
    16 namespace phoenix {
    17 
    18 ///////////////////////////////////////////////////////////////////////////////
    19 //
    20 //  sequential_composite
    21 //
    22 //      Two or more actors separated by the comma generates a
    23 //      sequential_composite which is a composite actor. Example:
    24 //
    25 //          actor,
    26 //          actor,
    27 //          actor
    28 //
    29 //      The actors are evaluated sequentially. The result type of this
    30 //      is void. Note that the last actor should not have a trailing
    31 //      comma.
    32 //
    33 ///////////////////////////////////////////////////////////////////////////////
    34 template <typename A0, typename A1>
    35 struct sequential_composite {
    36 
    37     typedef sequential_composite<A0, A1> self_t;
    38 
    39     template <typename TupleT>
    40     struct result { typedef void type; };
    41 
    42     sequential_composite(A0 const& _0, A1 const& _1)
    43     :   a0(_0), a1(_1) {}
    44 
    45     template <typename TupleT>
    46     void
    47     eval(TupleT const& args) const
    48     {
    49         a0.eval(args);
    50         a1.eval(args);
    51     }
    52 
    53     A0 a0; A1 a1; //  actors
    54 };
    55 
    56 //////////////////////////////////
    57 template <typename BaseT0, typename BaseT1>
    58 inline actor<sequential_composite<actor<BaseT0>, actor<BaseT1> > >
    59 operator,(actor<BaseT0> const& _0, actor<BaseT1> const& _1)
    60 {
    61     return sequential_composite<actor<BaseT0>, actor<BaseT1> >(_0, _1);
    62 }
    63 
    64 ///////////////////////////////////////////////////////////////////////////////
    65 //
    66 //  if_then_else_composite
    67 //
    68 //      This composite has two (2) forms:
    69 //
    70 //          if_(condition)
    71 //          [
    72 //              statement
    73 //          ]
    74 //
    75 //      and
    76 //
    77 //          if_(condition)
    78 //          [
    79 //              true_statement
    80 //          ]
    81 //          .else_
    82 //          [
    83 //              false_statement
    84 //          ]
    85 //
    86 //      where condition is an actor that evaluates to bool. If condition
    87 //      is true, the true_statement (again an actor) is executed
    88 //      otherwise, the false_statement (another actor) is executed. The
    89 //      result type of this is void. Note the trailing underscore after
    90 //      if_ and the the leading dot and the trailing underscore before
    91 //      and after .else_.
    92 //
    93 ///////////////////////////////////////////////////////////////////////////////
    94 template <typename CondT, typename ThenT, typename ElseT>
    95 struct if_then_else_composite {
    96 
    97     typedef if_then_else_composite<CondT, ThenT, ElseT> self_t;
    98 
    99     template <typename TupleT>
   100     struct result {
   101 
   102         typedef void type;
   103     };
   104 
   105     if_then_else_composite(
   106         CondT const& cond_,
   107         ThenT const& then_,
   108         ElseT const& else__)
   109     :   cond(cond_), then(then_), else_(else__) {}
   110 
   111     template <typename TupleT>
   112     void eval(TupleT const& args) const
   113     {
   114         if (cond.eval(args))
   115             then.eval(args);
   116         else
   117             else_.eval(args);
   118     }
   119 
   120     CondT cond; ThenT then; ElseT else_; //  actors
   121 };
   122 
   123 //////////////////////////////////
   124 template <typename CondT, typename ThenT>
   125 struct else_gen {
   126 
   127     else_gen(CondT const& cond_, ThenT const& then_)
   128     :   cond(cond_), then(then_) {}
   129 
   130     template <typename ElseT>
   131     actor<if_then_else_composite<CondT, ThenT,
   132         typename as_actor<ElseT>::type> >
   133     operator[](ElseT const& else_)
   134     {
   135         typedef if_then_else_composite<CondT, ThenT,
   136             typename as_actor<ElseT>::type>
   137         result;
   138 
   139         return result(cond, then, as_actor<ElseT>::convert(else_));
   140     }
   141 
   142     CondT cond; ThenT then;
   143 };
   144 
   145 //////////////////////////////////
   146 template <typename CondT, typename ThenT>
   147 struct if_then_composite {
   148 
   149     typedef if_then_composite<CondT, ThenT> self_t;
   150 
   151     template <typename TupleT>
   152     struct result { typedef void type; };
   153 
   154     if_then_composite(CondT const& cond_, ThenT const& then_)
   155     :   cond(cond_), then(then_), else_(cond, then) {}
   156 
   157     template <typename TupleT>
   158     void eval(TupleT const& args) const
   159     {
   160         if (cond.eval(args))
   161             then.eval(args);
   162     }
   163 
   164     CondT cond; ThenT then; //  actors
   165     else_gen<CondT, ThenT> else_;
   166 };
   167 
   168 //////////////////////////////////
   169 template <typename CondT>
   170 struct if_gen {
   171 
   172     if_gen(CondT const& cond_)
   173     :   cond(cond_) {}
   174 
   175     template <typename ThenT>
   176     actor<if_then_composite<
   177         typename as_actor<CondT>::type,
   178         typename as_actor<ThenT>::type> >
   179     operator[](ThenT const& then) const
   180     {
   181         typedef if_then_composite<
   182             typename as_actor<CondT>::type,
   183             typename as_actor<ThenT>::type>
   184         result;
   185 
   186         return result(
   187             as_actor<CondT>::convert(cond),
   188             as_actor<ThenT>::convert(then));
   189     }
   190 
   191     CondT cond;
   192 };
   193 
   194 //////////////////////////////////
   195 template <typename CondT>
   196 inline if_gen<CondT>
   197 if_(CondT const& cond)
   198 {
   199     return if_gen<CondT>(cond);
   200 }
   201 
   202 ///////////////////////////////////////////////////////////////////////////////
   203 //
   204 //  while_composite
   205 //
   206 //      This composite has the form:
   207 //
   208 //          while_(condition)
   209 //          [
   210 //              statement
   211 //          ]
   212 //
   213 //      While the condition (an actor) evaluates to true, statement
   214 //      (another actor) is executed. The result type of this is void.
   215 //      Note the trailing underscore after while_.
   216 //
   217 ///////////////////////////////////////////////////////////////////////////////
   218 template <typename CondT, typename DoT>
   219 struct while_composite {
   220 
   221     typedef while_composite<CondT, DoT> self_t;
   222 
   223     template <typename TupleT>
   224     struct result { typedef void type; };
   225 
   226     while_composite(CondT const& cond_, DoT const& do__)
   227     :   cond(cond_), do_(do__) {}
   228 
   229     template <typename TupleT>
   230     void eval(TupleT const& args) const
   231     {
   232         while (cond.eval(args))
   233             do_.eval(args);
   234     }
   235 
   236     CondT cond;
   237     DoT do_;
   238 };
   239 
   240 //////////////////////////////////
   241 template <typename CondT>
   242 struct while_gen {
   243 
   244     while_gen(CondT const& cond_)
   245     :   cond(cond_) {}
   246 
   247     template <typename DoT>
   248     actor<while_composite<
   249         typename as_actor<CondT>::type,
   250         typename as_actor<DoT>::type> >
   251     operator[](DoT const& do_) const
   252     {
   253         typedef while_composite<
   254             typename as_actor<CondT>::type,
   255             typename as_actor<DoT>::type>
   256         result;
   257 
   258         return result(
   259             as_actor<CondT>::convert(cond),
   260             as_actor<DoT>::convert(do_));
   261     }
   262 
   263     CondT cond;
   264 };
   265 
   266 //////////////////////////////////
   267 template <typename CondT>
   268 inline while_gen<CondT>
   269 while_(CondT const& cond)
   270 {
   271     return while_gen<CondT>(cond);
   272 }
   273 
   274 ///////////////////////////////////////////////////////////////////////////////
   275 //
   276 //  do_composite
   277 //
   278 //      This composite has the form:
   279 //
   280 //          do_
   281 //          [
   282 //              statement
   283 //          ]
   284 //          .while_(condition)
   285 //
   286 //      While the condition (an actor) evaluates to true, statement
   287 //      (another actor) is executed. The statement is executed at least
   288 //      once. The result type of this is void. Note the trailing
   289 //      underscore after do_ and the the leading dot and the trailing
   290 //      underscore before and after .while_.
   291 //
   292 ///////////////////////////////////////////////////////////////////////////////
   293 template <typename DoT, typename CondT>
   294 struct do_composite {
   295 
   296     typedef do_composite<DoT, CondT> self_t;
   297 
   298     template <typename TupleT>
   299     struct result { typedef void type; };
   300 
   301     do_composite(DoT const& do__, CondT const& cond_)
   302     :   do_(do__), cond(cond_) {}
   303 
   304     template <typename TupleT>
   305     void eval(TupleT const& args) const
   306     {
   307         do
   308             do_.eval(args);
   309         while (cond.eval(args));
   310     }
   311 
   312     DoT do_;
   313     CondT cond;
   314 };
   315 
   316 ////////////////////////////////////
   317 template <typename DoT>
   318 struct do_gen2 {
   319 
   320     do_gen2(DoT const& do__)
   321     :   do_(do__) {}
   322 
   323     template <typename CondT>
   324     actor<do_composite<
   325         typename as_actor<DoT>::type,
   326         typename as_actor<CondT>::type> >
   327     while_(CondT const& cond) const
   328     {
   329         typedef do_composite<
   330             typename as_actor<DoT>::type,
   331             typename as_actor<CondT>::type>
   332         result;
   333 
   334         return result(
   335             as_actor<DoT>::convert(do_),
   336             as_actor<CondT>::convert(cond));
   337     }
   338 
   339     DoT do_;
   340 };
   341 
   342 ////////////////////////////////////
   343 struct do_gen {
   344 
   345     template <typename DoT>
   346     do_gen2<DoT>
   347     operator[](DoT const& do_) const
   348     {
   349         return do_gen2<DoT>(do_);
   350     }
   351 };
   352 
   353 do_gen const do_ = do_gen();
   354 
   355 ///////////////////////////////////////////////////////////////////////////////
   356 //
   357 //  for_composite
   358 //
   359 //      This statement has the form:
   360 //
   361 //          for_(init, condition, step)
   362 //          [
   363 //              statement
   364 //          ]
   365 //
   366 //      Where init, condition, step and statement are all actors. init
   367 //      is executed once before entering the for-loop. The for-loop
   368 //      exits once condition evaluates to false. At each loop iteration,
   369 //      step and statement is called. The result of this statement is
   370 //      void. Note the trailing underscore after for_.
   371 //
   372 ///////////////////////////////////////////////////////////////////////////////
   373 template <typename InitT, typename CondT, typename StepT, typename DoT>
   374 struct for_composite {
   375 
   376     typedef composite<InitT, CondT, StepT, DoT> self_t;
   377 
   378     template <typename TupleT>
   379     struct result { typedef void type; };
   380 
   381     for_composite(
   382         InitT const& init_,
   383         CondT const& cond_,
   384         StepT const& step_,
   385         DoT const& do__)
   386     :   init(init_), cond(cond_), step(step_), do_(do__) {}
   387 
   388     template <typename TupleT>
   389     void
   390     eval(TupleT const& args) const
   391     {
   392         for (init.eval(args); cond.eval(args); step.eval(args))
   393             do_.eval(args);
   394     }
   395 
   396     InitT init; CondT cond; StepT step; DoT do_; //  actors
   397 };
   398 
   399 //////////////////////////////////
   400 template <typename InitT, typename CondT, typename StepT>
   401 struct for_gen {
   402 
   403     for_gen(
   404         InitT const& init_,
   405         CondT const& cond_,
   406         StepT const& step_)
   407     :   init(init_), cond(cond_), step(step_) {}
   408 
   409     template <typename DoT>
   410     actor<for_composite<
   411         typename as_actor<InitT>::type,
   412         typename as_actor<CondT>::type,
   413         typename as_actor<StepT>::type,
   414         typename as_actor<DoT>::type> >
   415     operator[](DoT const& do_) const
   416     {
   417         typedef for_composite<
   418             typename as_actor<InitT>::type,
   419             typename as_actor<CondT>::type,
   420             typename as_actor<StepT>::type,
   421             typename as_actor<DoT>::type>
   422         result;
   423 
   424         return result(
   425             as_actor<InitT>::convert(init),
   426             as_actor<CondT>::convert(cond),
   427             as_actor<StepT>::convert(step),
   428             as_actor<DoT>::convert(do_));
   429     }
   430 
   431     InitT init; CondT cond; StepT step;
   432 };
   433 
   434 //////////////////////////////////
   435 template <typename InitT, typename CondT, typename StepT>
   436 inline for_gen<InitT, CondT, StepT>
   437 for_(InitT const& init, CondT const& cond, StepT const& step)
   438 {
   439     return for_gen<InitT, CondT, StepT>(init, cond, step);
   440 }
   441 
   442 }   //  namespace phoenix
   443 
   444 #endif