functor_expr.hpp 9.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/



/** \file functor_expr.hpp */

#ifndef AMDIS_FUNCTOR_EXPRESSION_HPP
#define AMDIS_FUNCTOR_EXPRESSION_HPP

#include "AMDiS_fwd.h"
#include "LazyOperatorTerm.h"
#include "Functors.h"
#include "operations/functors.hpp"

#include <boost/static_assert.hpp>

namespace AMDiS 
{
  namespace traits
  {
    template<class T>
    struct void_{ typedef void type; };

    template<typename F, typename Enable = void>
    struct functor_result_type {};
    
    template<typename F>
    struct functor_result_type<F, typename void_<typename F::value_type>::type>
    {
      typedef typename F::value_type type;
    };
    
    template<typename F>
    struct functor_result_type<F, typename void_<typename F::result_type>::type>
    {
      typedef typename F::result_type type;
    };

  } // end namespace traits

  namespace expressions 
  {
    /// Expressions for a functor with one argument
    template<typename F, typename Term>
    struct Function1 : public LazyOperatorTerm1<Term>
    {
      typedef LazyOperatorTerm1<Term> super;
      BOOST_STATIC_ASSERT_MSG( (boost::is_base_of<FunctorBase, F>::value), "********** ERROR: Only functors with base FunctorBase allowed **********" );
      
      typedef typename traits::functor_result_type<F>::type value_type;    
      BOOST_STATIC_ASSERT_MSG( (!boost::is_same<value_type, traits::no_valid_type>::value), "********** ERROR: You have to define a result_type for your Functor **********" );
      
      F f;    
      Function1(const F& f_, const Term& term_) : super(term_), f(f_) {}
      
      int getDegree() const
      {
	return f.getDegree(super::term.getDegree());
      }

      inline value_type operator()(const int& iq) const { return f(super::term(iq)); }
    };
    

    /// Expressions for a functor with two arguments
    template<typename F, typename Term1, typename Term2>
    struct Function2 : public LazyOperatorTerm2<Term1, Term2>
    {
      typedef LazyOperatorTerm2<Term1, Term2> super;
      BOOST_STATIC_ASSERT_MSG( (boost::is_base_of<FunctorBase, F>::value), "********** ERROR: Only functors with base FunctorBase allowed **********" );
      
      typedef typename traits::functor_result_type<F>::type value_type;    
      BOOST_STATIC_ASSERT_MSG( (!boost::is_same<value_type, traits::no_valid_type>::value), "********** ERROR: You have to define a result_type for your Functor **********" );
      
      F f;      
      Function2(const F& f_, const Term1& term1_, const Term2& term2_) : super(term1_, term2_), f(f_) {}
      
      int getDegree() const
      {
	return f.getDegree(super::term1.getDegree(), super::term2.getDegree());
      }

      inline value_type operator()(const int& iq) const { return f(super::term1(iq), super::term2(iq)); }
    };
    

    /// Expressions for a functor with 3 arguments
    template<typename F, typename Term1, typename Term2, typename Term3>
    struct Function3 : public LazyOperatorTerm3<Term1, Term2, Term3>
    {
      typedef LazyOperatorTerm3<Term1, Term2, Term3> super;
      BOOST_STATIC_ASSERT_MSG( (boost::is_base_of<FunctorBase, F>::value), "********** ERROR: Only functors with base FunctorBase allowed **********" );
      
      typedef typename traits::functor_result_type<F>::type value_type;    
      BOOST_STATIC_ASSERT_MSG( (!boost::is_same<value_type, traits::no_valid_type>::value), "********** ERROR: You have to define a result_type for your Functor **********" );
      
      F f;      
      Function3(const F& f_, const Term1& term1_, const Term2& term2_, const Term3& term3_)
	: super(term1_, term2_, term3_), f(f_) {}
      
      int getDegree() const
      {
	return f.getDegree(super::term1.getDegree(), super::term2.getDegree(), super::term3.getDegree());
      }

      inline value_type operator()(const int& iq) const { return f(super::term1(iq), super::term2(iq), super::term3(iq)); }
    };
    
    
    /// A wrapper functor for AMDiS::AbstractFunctions
    template<typename TOut, typename TIn>
    struct Wrapper : public FunctorBase
    {
      typedef TOut result_type;
      Wrapper(AbstractFunction<TOut, TIn>* fct_) : fct(fct_) {}
      int getDegree(int degree) const { return fct->getDegree(); }
      
      TOut operator()(const TIn& x) const 
      {
	return (*fct)(x);
      }
      
    protected:
      AbstractFunction<TOut, TIn>* fct;
    };
    
  } // end namespace expressions


  namespace result_of
  {
    // function with one argument
    template<typename F, typename Term>
    struct Function1 : boost::enable_if
      <
	typename traits::is_valid_arg<Term>::type,
	expressions::Function1
	<
	  F, typename traits::to_expr<Term>::type
	> 
      > {};
      
    // function with two arguments
    template<typename F, typename Term1, typename Term2>
    struct Function2 : boost::enable_if
      <
	typename boost::mpl::and_
	<
	  typename traits::is_valid_arg<Term1>::type,
	  typename traits::is_valid_arg<Term2>::type
	>::type,
	expressions::Function2
	<
	  F, 
	  typename traits::to_expr<Term1>::type,
	  typename traits::to_expr<Term2>::type
	> 
      > {};
      
      
    // function with three arguments
    template<typename F, typename Term1, typename Term2, typename Term3>
    struct Function3 : boost::enable_if
      <
	typename boost::mpl::and_
	<
	  typename traits::is_valid_arg<Term1>::type,
	  typename traits::is_valid_arg<Term2>::type,
	  typename traits::is_valid_arg<Term3>::type
	>::type,
	expressions::Function3
	<
	  F, 
	  typename traits::to_expr<Term1>::type,
	  typename traits::to_expr<Term2>::type,
	  typename traits::to_expr<Term3>::type
	> 
      > {};
      
  } // end namespace result_of


  // call a function with 1 argument
  // _____________________________________________________________________________
  template<typename F, typename Term>
  inline typename result_of::Function1<F, Term>::type
  function_(const F& f, const Term& t)
  { 
    typedef typename traits::to_expr<Term>::to Expr;
    return expressions::Function1<F, typename Expr::type>(f, Expr::get(t)); 
  }


  template<typename F, typename Term>
  inline typename result_of::Function1<F, Term>::type
  function_(const Term& t) 
  { 
    typedef typename traits::to_expr<Term>::to Expr;
    return expressions::Function1<F, typename Expr::type>(F(), Expr::get(t)); 
  }


  // call a function with 2 arguments
  // _____________________________________________________________________________
  template<typename F, typename Term1, typename Term2>
  inline typename result_of::Function2<F, Term1, Term2>::type
  function_(const F& f, const Term1& t1, const Term2& t2) 
  { 
    typedef typename traits::to_expr<Term1>::to Expr1;
    typedef typename traits::to_expr<Term2>::to Expr2;
    return expressions::Function2<F, typename Expr1::type, typename Expr2::type>
	    (f, Expr1::get(t1), Expr2::get(t2)); 
  }


  template<typename F, typename Term1, typename Term2>
  inline typename result_of::Function2<F, Term1, Term2>::type
  function_(const Term1& t1, const Term2& t2) 
  { 
    typedef typename traits::to_expr<Term1>::to Expr1;
    typedef typename traits::to_expr<Term2>::to Expr2;
    return expressions::Function2<F, typename Expr1::type, typename Expr2::type>
	    (F(), Expr1::get(t1), Expr2::get(t2)); 
  }


  // call a function with 3 arguments
  // _____________________________________________________________________________
  template<typename F, typename Term1, typename Term2, typename Term3>
248
  inline typename result_of::Function3<F, Term1, Term2, Term3>::type
249
250
251
252
253
254
255
256
257
258
259
  function_(const F& f, const Term1& t1, const Term2& t2, const Term3& t3) 
  { 
    typedef typename traits::to_expr<Term1>::to Expr1;
    typedef typename traits::to_expr<Term2>::to Expr2;
    typedef typename traits::to_expr<Term3>::to Expr3;
    return expressions::Function3<F, typename Expr1::type, typename Expr2::type, typename Expr3::type>
	    (f, Expr1::get(t1), Expr2::get(t2), Expr3::get(t3)); 
  }


  template<typename F, typename Term1, typename Term2, typename Term3>
260
  inline typename result_of::Function3<F, Term1, Term2, Term3>::type
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  function_(const Term1& t1, const Term2& t2, const Term3& t3) 
  { 
    typedef typename traits::to_expr<Term1>::to Expr1;
    typedef typename traits::to_expr<Term2>::to Expr2;
    typedef typename traits::to_expr<Term3>::to Expr3;
    return expressions::Function3<F, typename Expr1::type, typename Expr2::type, typename Expr3::type>
	    (F(), Expr1::get(t1), Expr2::get(t2), Expr3::get(t3)); 
  }


  // function wrapper for abstract functions
  // _____________________________________________________________________________
  template<typename TOut, typename TIn>
  inline expressions::Wrapper<TOut,TIn> wrap(AbstractFunction<TOut, TIn>* fct) 
  { return expressions::Wrapper<TOut,TIn>(fct); }

} // end namespace AMDiS

#endif // AMDIS_FUNCTOR_EXPRESSION_HPP