Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:10

0001 // Boost Lambda Library -- loops.hpp ----------------------------------------
0002 
0003 // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
0004 // Copyright (C) 2000 Gary Powell (powellg@amazon.com)
0005 // Copyright (c) 2001-2002 Joel de Guzman
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See
0008 // accompanying file LICENSE_1_0.txt or copy at
0009 // http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 // For more information, see www.boost.org
0012 
0013 // --------------------------------------------------------------------------
0014 
0015 #if !defined(BOOST_LAMBDA_LOOPS_HPP)
0016 #define BOOST_LAMBDA_LOOPS_HPP
0017 
0018 #include "boost/lambda/core.hpp"
0019 
0020 namespace boost { 
0021 namespace lambda {
0022 
0023 // -- loop control structure actions ----------------------
0024 
0025 class forloop_action {};
0026 class forloop_no_body_action {};
0027 class whileloop_action {};
0028 class whileloop_no_body_action {};
0029 class dowhileloop_action {};
0030 class dowhileloop_no_body_action {};
0031 
0032 
0033 // For loop
0034 template <class Arg1, class Arg2, class Arg3, class Arg4>
0035 inline const 
0036 lambda_functor<
0037   lambda_functor_base<
0038     forloop_action, 
0039     tuple<lambda_functor<Arg1>, lambda_functor<Arg2>, 
0040           lambda_functor<Arg3>, lambda_functor<Arg4> >
0041   > 
0042 >
0043 for_loop(const lambda_functor<Arg1>& a1, const lambda_functor<Arg2>& a2, 
0044          const lambda_functor<Arg3>& a3, const lambda_functor<Arg4>& a4) { 
0045   return 
0046       lambda_functor_base<
0047         forloop_action, 
0048         tuple<lambda_functor<Arg1>, lambda_functor<Arg2>, 
0049               lambda_functor<Arg3>, lambda_functor<Arg4> >
0050       > 
0051     ( tuple<lambda_functor<Arg1>, lambda_functor<Arg2>, 
0052             lambda_functor<Arg3>, lambda_functor<Arg4> >(a1, a2, a3, a4)
0053     );
0054 }
0055 
0056 // No body case.
0057 template <class Arg1, class Arg2, class Arg3>
0058 inline const 
0059 lambda_functor<
0060   lambda_functor_base<
0061     forloop_no_body_action, 
0062     tuple<lambda_functor<Arg1>, lambda_functor<Arg2>, lambda_functor<Arg3> >
0063   > 
0064 >
0065 for_loop(const lambda_functor<Arg1>& a1, const lambda_functor<Arg2>& a2, 
0066          const lambda_functor<Arg3>& a3) { 
0067   return 
0068       lambda_functor_base<
0069         forloop_no_body_action, 
0070         tuple<lambda_functor<Arg1>, lambda_functor<Arg2>, 
0071               lambda_functor<Arg3> >
0072       > 
0073       ( tuple<lambda_functor<Arg1>, lambda_functor<Arg2>, 
0074                lambda_functor<Arg3> >(a1, a2, a3) );
0075 }
0076 
0077 // While loop
0078 template <class Arg1, class Arg2>
0079 inline const 
0080 lambda_functor<
0081   lambda_functor_base<
0082     whileloop_action, 
0083     tuple<lambda_functor<Arg1>, lambda_functor<Arg2> >
0084   > 
0085 >
0086 while_loop(const lambda_functor<Arg1>& a1, const lambda_functor<Arg2>& a2) { 
0087   return 
0088       lambda_functor_base<
0089         whileloop_action, 
0090         tuple<lambda_functor<Arg1>, lambda_functor<Arg2> >
0091       > 
0092       ( tuple<lambda_functor<Arg1>, lambda_functor<Arg2> >(a1, a2));
0093 }
0094 
0095 // No body case.
0096 template <class Arg1>
0097 inline const 
0098 lambda_functor<
0099   lambda_functor_base<
0100     whileloop_no_body_action, 
0101     tuple<lambda_functor<Arg1> >
0102   > 
0103 >
0104 while_loop(const lambda_functor<Arg1>& a1) { 
0105   return 
0106       lambda_functor_base<
0107         whileloop_no_body_action, 
0108         tuple<lambda_functor<Arg1> >
0109       > 
0110       ( tuple<lambda_functor<Arg1> >(a1) );
0111 }
0112 
0113 
0114 // Do While loop
0115 template <class Arg1, class Arg2>
0116 inline const 
0117 lambda_functor<
0118   lambda_functor_base<
0119     dowhileloop_action, 
0120     tuple<lambda_functor<Arg1>, lambda_functor<Arg2> >
0121   > 
0122 >
0123 do_while_loop(const lambda_functor<Arg1>& a1, const lambda_functor<Arg2>& a2) {
0124   return 
0125       lambda_functor_base<
0126         dowhileloop_action, 
0127         tuple<lambda_functor<Arg1>, lambda_functor<Arg2> >
0128       > 
0129       ( tuple<lambda_functor<Arg1>, lambda_functor<Arg2> >(a1, a2));
0130 }
0131 
0132 // No body case.
0133 template <class Arg1>
0134 inline const 
0135 lambda_functor<
0136   lambda_functor_base<
0137     dowhileloop_no_body_action, 
0138     tuple<lambda_functor<Arg1> >
0139   > 
0140 >
0141 do_while_loop(const lambda_functor<Arg1>& a1) { 
0142   return 
0143       lambda_functor_base<
0144         dowhileloop_no_body_action, 
0145         tuple<lambda_functor<Arg1> >
0146       > 
0147       ( tuple<lambda_functor<Arg1> >(a1));
0148 }
0149 
0150 
0151 // Control loop lambda_functor_base specializations.
0152 
0153 // Specialization for for_loop.
0154 template<class Args>
0155 class 
0156 lambda_functor_base<forloop_action, Args> {
0157 public:
0158   Args args;
0159   template <class T> struct sig { typedef void type; };
0160 public:
0161   explicit lambda_functor_base(const Args& a) : args(a) {}
0162 
0163   template<class RET, CALL_TEMPLATE_ARGS>
0164   RET call(CALL_FORMAL_ARGS) const {
0165     for(detail::select(boost::tuples::get<0>(args), CALL_ACTUAL_ARGS); 
0166         detail::select(boost::tuples::get<1>(args), CALL_ACTUAL_ARGS); 
0167         detail::select(boost::tuples::get<2>(args), CALL_ACTUAL_ARGS))
0168       
0169       detail::select(boost::tuples::get<3>(args), CALL_ACTUAL_ARGS);
0170   }
0171 };
0172 
0173 // No body case
0174 template<class Args>
0175 class 
0176 lambda_functor_base<forloop_no_body_action, Args> {
0177 public:
0178   Args args;
0179   template <class T> struct sig { typedef void type; };
0180 public:
0181   explicit lambda_functor_base(const Args& a) : args(a) {}
0182 
0183   template<class RET, CALL_TEMPLATE_ARGS>
0184   RET call(CALL_FORMAL_ARGS) const {
0185     for(detail::select(boost::tuples::get<0>(args), CALL_ACTUAL_ARGS); 
0186         detail::select(boost::tuples::get<1>(args), CALL_ACTUAL_ARGS); 
0187         detail::select(boost::tuples::get<2>(args), CALL_ACTUAL_ARGS)) {}
0188    }
0189 };
0190 
0191 
0192 // Specialization for while_loop.
0193 template<class Args>
0194 class 
0195 lambda_functor_base<whileloop_action, Args> {
0196 public:
0197   Args args;
0198   template <class T> struct sig { typedef void type; };
0199 public:
0200   explicit lambda_functor_base(const Args& a) : args(a) {}
0201 
0202   template<class RET, CALL_TEMPLATE_ARGS>
0203   RET call(CALL_FORMAL_ARGS) const {
0204     while(detail::select(boost::tuples::get<0>(args), CALL_ACTUAL_ARGS))
0205       
0206       detail::select(boost::tuples::get<1>(args), CALL_ACTUAL_ARGS);
0207   }
0208 };
0209 
0210 // No body case
0211 template<class Args> 
0212 class 
0213 lambda_functor_base<whileloop_no_body_action, Args> {
0214 public:
0215   Args args;
0216   template <class T> struct sig { typedef void type; };
0217 public:
0218   explicit lambda_functor_base(const Args& a) : args(a) {}
0219 
0220   template<class RET, CALL_TEMPLATE_ARGS>
0221   RET call(CALL_FORMAL_ARGS) const {
0222           while(detail::select(boost::tuples::get<0>(args), CALL_ACTUAL_ARGS)) {}
0223   }
0224 };
0225 
0226 // Specialization for do_while_loop.
0227 // Note that the first argument is the condition.
0228 template<class Args>
0229 class 
0230 lambda_functor_base<dowhileloop_action, Args> {
0231 public:
0232   Args args;
0233   template <class T> struct sig { typedef void type; };
0234 public:
0235   explicit lambda_functor_base(const Args& a) : args(a) {}
0236 
0237   template<class RET, CALL_TEMPLATE_ARGS>
0238   RET call(CALL_FORMAL_ARGS) const {
0239     do {
0240       detail::select(boost::tuples::get<1>(args), CALL_ACTUAL_ARGS);      
0241     } while (detail::select(boost::tuples::get<0>(args), CALL_ACTUAL_ARGS) );
0242   }
0243 };
0244 
0245 // No body case
0246 template<class Args>
0247 class 
0248 lambda_functor_base<dowhileloop_no_body_action, Args> {
0249 public:
0250   Args args;
0251   template <class T> struct sig { typedef void type; };
0252 public:
0253   explicit lambda_functor_base(const Args& a) : args(a) {}
0254 
0255   template<class RET, CALL_TEMPLATE_ARGS>
0256   RET call(CALL_FORMAL_ARGS) const {
0257           do {} while (detail::select(boost::tuples::get<0>(args), CALL_ACTUAL_ARGS) );
0258   }
0259 };
0260 
0261   // The code below is from Joel de Guzman, some name changes etc. 
0262   // has been made.
0263 
0264 ///////////////////////////////////////////////////////////////////////////////
0265 //
0266 //  while_composite
0267 //
0268 //      This composite has the form:
0269 //
0270 //          while_(condition)
0271 //          [
0272 //              statement
0273 //          ]
0274 //
0275 //      While the condition (an lambda_functor) evaluates to true, statement
0276 //      (another lambda_functor) is executed. The result type of this is void.
0277 //      Note the trailing underscore after while_.
0278 //
0279 ///////////////////////////////////////////////////////////////////////////////
0280 template <typename CondT, typename DoT>
0281 struct while_composite {
0282 
0283     typedef while_composite<CondT, DoT> self_t;
0284 
0285     template <class SigArgs>
0286     struct sig { typedef void type; };
0287 
0288     while_composite(CondT const& cond_, DoT const& do__)
0289     :   cond(cond_), do_(do__) {}
0290 
0291     template <class Ret, CALL_TEMPLATE_ARGS>
0292     Ret call(CALL_FORMAL_ARGS) const
0293     {
0294         while (cond.internal_call(CALL_ACTUAL_ARGS))
0295             do_.internal_call(CALL_ACTUAL_ARGS);
0296     }
0297 
0298     CondT cond;
0299     DoT do_;
0300 };
0301 
0302 //////////////////////////////////
0303 template <typename CondT>
0304 struct while_gen {
0305 
0306     while_gen(CondT const& cond_)
0307     :   cond(cond_) {}
0308 
0309     template <typename DoT>
0310     lambda_functor<while_composite<
0311         typename as_lambda_functor<CondT>::type,
0312         typename as_lambda_functor<DoT>::type> >
0313     operator[](DoT const& do_) const
0314     {
0315         typedef while_composite<
0316             typename as_lambda_functor<CondT>::type,
0317             typename as_lambda_functor<DoT>::type>
0318         result;
0319 
0320         return result(
0321             to_lambda_functor(cond),
0322             to_lambda_functor(do_));
0323     }
0324 
0325     CondT cond;
0326 };
0327 
0328 //////////////////////////////////
0329 template <typename CondT>
0330 inline while_gen<CondT>
0331 while_(CondT const& cond)
0332 {
0333     return while_gen<CondT>(cond);
0334 }
0335 
0336 ///////////////////////////////////////////////////////////////////////////////
0337 //
0338 //  do_composite
0339 //
0340 //      This composite has the form:
0341 //
0342 //          do_
0343 //          [
0344 //              statement
0345 //          ]
0346 //          .while_(condition)
0347 //
0348 //      While the condition (an lambda_functor) evaluates to true, statement
0349 //      (another lambda_functor) is executed. The statement is executed at least
0350 //      once. The result type of this is void. Note the trailing
0351 //      underscore after do_ and the leading dot and the trailing
0352 //      underscore before and after .while_.
0353 //
0354 ///////////////////////////////////////////////////////////////////////////////
0355 template <typename DoT, typename CondT>
0356 struct do_composite {
0357 
0358     typedef do_composite<DoT, CondT> self_t;
0359 
0360     template <class SigArgs>
0361     struct sig { typedef void type; };
0362 
0363     do_composite(DoT const& do__, CondT const& cond_)
0364     :   do_(do__), cond(cond_) {}
0365 
0366     template <class Ret, CALL_TEMPLATE_ARGS>
0367     Ret call(CALL_FORMAL_ARGS) const
0368     {
0369         do
0370             do_.internal_call(CALL_ACTUAL_ARGS);
0371         while (cond.internal_call(CALL_ACTUAL_ARGS));
0372     }
0373 
0374     DoT do_;
0375     CondT cond;
0376 };
0377 
0378 ////////////////////////////////////
0379 template <typename DoT>
0380 struct do_gen2 {
0381 
0382     do_gen2(DoT const& do__)
0383     :   do_(do__) {}
0384 
0385     template <typename CondT>
0386     lambda_functor<do_composite<
0387         typename as_lambda_functor<DoT>::type,
0388         typename as_lambda_functor<CondT>::type> >
0389     while_(CondT const& cond) const
0390     {
0391         typedef do_composite<
0392             typename as_lambda_functor<DoT>::type,
0393             typename as_lambda_functor<CondT>::type>
0394         result;
0395 
0396         return result(
0397             to_lambda_functor(do_),
0398             to_lambda_functor(cond));
0399     }
0400 
0401     DoT do_;
0402 };
0403 
0404 ////////////////////////////////////
0405 struct do_gen {
0406 
0407     template <typename DoT>
0408     do_gen2<DoT>
0409     operator[](DoT const& do_) const
0410     {
0411         return do_gen2<DoT>(do_);
0412     }
0413 };
0414 
0415 do_gen const do_ = do_gen();
0416 
0417 ///////////////////////////////////////////////////////////////////////////////
0418 //
0419 //  for_composite
0420 //
0421 //      This statement has the form:
0422 //
0423 //          for_(init, condition, step)
0424 //          [
0425 //              statement
0426 //          ]
0427 //
0428 //      Where init, condition, step and statement are all lambda_functors. init
0429 //      is executed once before entering the for-loop. The for-loop
0430 //      exits once condition evaluates to false. At each loop iteration,
0431 //      step and statement is called. The result of this statement is
0432 //      void. Note the trailing underscore after for_.
0433 //
0434 ///////////////////////////////////////////////////////////////////////////////
0435 template <typename InitT, typename CondT, typename StepT, typename DoT>
0436 struct for_composite {
0437 
0438     template <class SigArgs>
0439     struct sig { typedef void type; };
0440 
0441     for_composite(
0442         InitT const& init_,
0443         CondT const& cond_,
0444         StepT const& step_,
0445         DoT const& do__)
0446     :   init(init_), cond(cond_), step(step_), do_(do__) {}
0447 
0448     template <class Ret, CALL_TEMPLATE_ARGS>
0449     Ret
0450     call(CALL_FORMAL_ARGS) const
0451     {
0452         for (init.internal_call(CALL_ACTUAL_ARGS); cond.internal_call(CALL_ACTUAL_ARGS); step.internal_call(CALL_ACTUAL_ARGS))
0453             do_.internal_call(CALL_ACTUAL_ARGS);
0454     }
0455 
0456     InitT init; CondT cond; StepT step; DoT do_; //  lambda_functors
0457 };
0458 
0459 //////////////////////////////////
0460 template <typename InitT, typename CondT, typename StepT>
0461 struct for_gen {
0462 
0463     for_gen(
0464         InitT const& init_,
0465         CondT const& cond_,
0466         StepT const& step_)
0467     :   init(init_), cond(cond_), step(step_) {}
0468 
0469     template <typename DoT>
0470     lambda_functor<for_composite<
0471         typename as_lambda_functor<InitT>::type,
0472         typename as_lambda_functor<CondT>::type,
0473         typename as_lambda_functor<StepT>::type,
0474         typename as_lambda_functor<DoT>::type> >
0475     operator[](DoT const& do_) const
0476     {
0477         typedef for_composite<
0478             typename as_lambda_functor<InitT>::type,
0479             typename as_lambda_functor<CondT>::type,
0480             typename as_lambda_functor<StepT>::type,
0481             typename as_lambda_functor<DoT>::type>
0482         result;
0483 
0484         return result(
0485             to_lambda_functor(init),
0486             to_lambda_functor(cond),
0487             to_lambda_functor(step),
0488             to_lambda_functor(do_));
0489     }
0490 
0491     InitT init; CondT cond; StepT step;
0492 };
0493 
0494 //////////////////////////////////
0495 template <typename InitT, typename CondT, typename StepT>
0496 inline for_gen<InitT, CondT, StepT>
0497 for_(InitT const& init, CondT const& cond, StepT const& step)
0498 {
0499     return for_gen<InitT, CondT, StepT>(init, cond, step);
0500 }
0501 
0502 } // lambda
0503 } // boost
0504 
0505 #endif // BOOST_LAMBDA_LOOPS_HPP