Rheolef  7.2
an efficient C++ finite element environment
 
Loading...
Searching...
No Matches
expr_utilities.h
Go to the documentation of this file.
1#ifndef _RHEOLEF_EXPR_UTILITIES_H
2#define _RHEOLEF_EXPR_UTILITIES_H
23// utilities for expressions: used by vec<T,M> and field_basic<T,M>
24//
25// author: Pierre.Saramito@imag.fr
26//
27// date: 14 september 2015
28//
29#include <functional>
30
31namespace rheolef { namespace details {
32
33// -------------------------------------------
34// operators as functors
35// -------------------------------------------
36
37#define _RHEOLEF_generic_unary_syntax_functor(OP,NAME) \
38template <typename T = void> \
39struct NAME; \
40 \
41template <typename T> \
42struct NAME: public std::unary_function<T, T> { \
43 T operator() (const T& x) const { return OP x; } \
44}; \
45 \
46template<> \
47struct NAME<void> { \
48 template <typename T> \
49 auto operator() (T&& x) const \
50 noexcept (noexcept (OP std::forward<T>(x))) \
51 -> decltype(OP std::forward<T>(x)) \
52 { return OP std::forward<T>(x); } \
53};
54
55#define _RHEOLEF_generic_binary_syntax_functor(OP,NAME) \
56template <typename T = void> \
57struct NAME; \
58 \
59template<typename T> \
60struct NAME: public std::binary_function<T, T, T> { \
61 T operator() (const T& x, const T& y) const { return x OP y; } \
62}; \
63 \
64template<> \
65struct NAME<void> { \
66 template <typename T, typename U> \
67 auto operator() (T&& t, U&& y) const \
68 noexcept (noexcept (std::forward<T>(t) OP std::forward<U>(y))) \
69 -> decltype(std::forward<T>(t) OP std::forward<U>(y)) \
70 { return std::forward<T>(t) OP std::forward<U>(y); } \
71};
72
77_RHEOLEF_generic_binary_syntax_functor (*,generic_multiplies)
79#undef _RHEOLEF_generic_unary_syntax_functor
80#undef _RHEOLEF_generic_binary_syntax_functor
81
82template<class BinaryFunction, class A1>
83struct generic_binder1st {
84 generic_binder1st (const BinaryFunction& f, const A1& x1)
85 : _f(f), _x1(x1) {}
86 template<class A2>
87 auto operator() (A2&& x2) const
88 // TODO: assume that both BinaryFunction and A1 have a default constructor...
89 noexcept (noexcept (BinaryFunction() (A1(), std::forward<A2>(x2))))
90 -> decltype(BinaryFunction() (A1(), std::forward<A2>(x2)))
91 { return _f (_x1, std::forward<A2>(x2)); }
92protected:
93 BinaryFunction _f;
94 A1 _x1;
95};
96
97template<class BinaryFunction, class A2>
99 generic_binder2nd (const BinaryFunction& f, const A2& x2)
100 : _f(f), _x2(x2) {}
101 template<class A1>
102 auto operator() (A1&& x1) const
103 // TODO: assume that both BinaryFunction and A2 have a default constructor...
104 noexcept (noexcept (BinaryFunction() (std::forward<A1>(x1), A2())))
105 -> decltype(BinaryFunction() (std::forward<A1>(x1), A2()))
106 { return _f (std::forward<A1>(x1),_x2); }
107protected:
108 BinaryFunction _f;
109 A2 _x2;
110};
111// -------------------------------------------
112// computed assignement as functors
113// -------------------------------------------
114// for = += -= *= /= etc
115struct assign_op {
116 template<class T, class U>
117 void operator() (T &t, const U &u) const { t = u; }
118};
120 template<class T, class U>
121 void operator() (T &t, const U &u) const { t += u; }
122};
124 template<class T, class U>
125 void operator() (T &t, const U &u) const { t -= u; }
126};
128 template<class T, class U>
129 void operator() (T &t, const U &u) const { t *= u; }
130};
132 template<class T, class U>
133 void operator() (T &t, const U &u) const { t /= u; }
134};
135template<class ForwardIterator, class InputIterator, class OpAssign>
136void
137assign_with_operator (ForwardIterator first, ForwardIterator last, InputIterator iter_rhs, OpAssign op_assign) {
138 for (; first != last; ++first, ++iter_rhs) {
139 op_assign (*first, *iter_rhs);
140 }
141}
142// -------------------------------------------
143// misc
144// -------------------------------------------
145// for convenince: a dummy forward iterator that returns a constant
146template <class T>
148
149 typedef T value_type;
150 iterator_on_constant (const T& c) : _c(c) {}
152 value_type operator* () const { return _c; }
153protected:
155};
156
157}} // namespace rheolef::details
158#endif // _RHEOLEF_EXPR_UTILITIES_H
#define _RHEOLEF_generic_binary_syntax_functor(OP, NAME)
#define _RHEOLEF_generic_unary_syntax_functor(OP, NAME)
Expr1::float_type T
Definition field_expr.h:230
void assign_with_operator(ForwardIterator first, ForwardIterator last, InputIterator iter_rhs, OpAssign op_assign)
This file is part of Rheolef.
Definition cavity_dg.h:29
void operator()(T &t, const U &u) const
void operator()(T &t, const U &u) const
generic_binder2nd(const BinaryFunction &f, const A2 &x2)
auto operator()(A1 &&x1) const noexcept(noexcept(BinaryFunction()(std::forward< A1 >(x1), A2()))) -> decltype(BinaryFunction()(std::forward< A1 >(x1), A2()))
iterator_on_constant< T > & operator++()
void operator()(T &t, const U &u) const
void operator()(T &t, const U &u) const
void operator()(T &t, const U &u) const
Definition leveque.h:25