NiHu  2.0
control.hpp
Go to the documentation of this file.
1 // This file is a part of NiHu, a C++ BEM template library.
2 //
3 // Copyright (C) 2012-2014 Peter Fiala <fiala@hit.bme.hu>
4 // Copyright (C) 2012-2014 Peter Rucz <rucz@hit.bme.hu>
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
24 #ifndef CONTROL_HPP_INCLUDED
25 #define CONTROL_HPP_INCLUDED
26 
27 #include "algorithm.hpp"
28 #include "lambda.hpp"
29 #include "sequence.hpp"
30 
31 #include <type_traits>
32 
34 namespace tmp
35 {
36 
38  namespace internal
39  {
47  template <class Begin, class End, class Transform, class...Args>
48  struct call_each_impl
49  {
50  static void eval(Args ...args)
51  {
52  // instantiate and call actual templated version of Transform
53  typedef typename apply<Transform, typename deref<Begin>::type>::type cur;
54  cur c;
55  c(args...);
56  // envoke call_each for the remaining part of the sequence
57  call_each_impl<
58  typename next<Begin>::type,
59  End,
60  Transform,
61  Args...
62  >::eval(args...);
63  }
64  };
65 
67  template <class End, class Transform, class...Args>
68  struct call_each_impl<End, End, Transform, Args...>
69  {
70  static void eval(Args...) { }
71  };
72  } // namespace internal
73 
80  template <class Seq, class Transform, class...Args>
81  static void call_each(Args...args)
82  {
83  internal::call_each_impl<
84  typename begin<Seq>::type,
85  typename end<Seq>::type,
86  Transform,
87  Args...
88  >::eval(args...);
89  }
90 
91  namespace internal
92  {
101  template <class Begin, class End, class SeqIn, class Transform, class...Args>
102  struct d_call_each_impl
103  {
104  // bind transform to the first argument
105  typedef typename lambda<Transform>::type::template apply<
106  typename deref<Begin>::type, _1
107  > partially_evaluated_transform;
108 
109  static void eval(Args...args)
110  {
111  // call call_each with partially evaluated Transform
112  call_each<
113  SeqIn,
114  partially_evaluated_transform,
115  Args...
116  >(args...);
117  // call d_call_each with the rest of the outer sequence
118  d_call_each_impl<
119  typename next<Begin>::type,
120  End,
121  SeqIn,
122  Transform,
123  Args...
124  >::eval(args...);
125  }
126  };
127 
128 
130  template <class End, class SeqIn, class Transform, class...Args>
131  struct d_call_each_impl<End, End, SeqIn, Transform, Args...>
132  {
133  static void eval(Args...) {}
134  };
135  }
136 
137 
145  template <class SeqOut, class SeqIn, class Transform, class...Args>
146  static void d_call_each(Args...args)
147  {
148  internal::d_call_each_impl<
149  typename begin<SeqOut>::type,
150  typename end<SeqOut>::type,
151  SeqIn,
152  Transform,
153  Args...
154  >::eval(args...);
155  }
156 
157  namespace internal
158  {
166  template <class Begin, class End, class Transform, class...Args>
167  struct call_until_impl
168  {
169  static bool eval(Args...args)
170  {
171  typedef typename apply<Transform, typename deref<Begin>::type>::type cur;
172  cur c;
173  if (!c(args...))
174  {
175  return call_until_impl<
176  typename next<Begin>::type,
177  End,
178  Transform,
179  Args...
180  >::eval(args...);
181  }
182  return true;
183  }
184  };
185 
187  template <class End, class Transform, class...Args>
188  struct call_until_impl<End, End, Transform, Args...>
189  {
190  static bool eval(Args...) { return false; }
191  };
192  }
193 
200  template <class Seq, class Transform, class...Args>
201  static bool call_until(Args...args)
202  {
203  return internal::call_until_impl<
204  typename begin<Seq>::type,
205  typename end<Seq>::type,
206  Transform,
207  Args...
208  >::eval(args...);
209  }
210 }
211 
212 #endif /* CONTROL_HPP_INCLUDED */
tmp
template metaprogramming functions
Definition: asymptotic_types.hpp:101
lambda.hpp
implementation of placeholders and lambda functions
algorithm.hpp
vectoralgorithms
tmp::_1
arg< 1 > _1
shorthand for selecting the 1st argument
Definition: lambda.hpp:55
sequence.hpp
implementation of compile time sequences