Loading...
Searching...
No Matches
expr_utilities.h
Go to the documentation of this file.
#ifndef _RHEOLEF_EXPR_UTILITIES_H
#define _RHEOLEF_EXPR_UTILITIES_H
// utilities for expressions: used by vec<T,M> and field_basic<T,M>
//
// author: Pierre.Saramito@imag.fr
//
// date: 14 september 2015
//
#include <functional>
namespace rheolef { namespace details {
// -------------------------------------------
// operators as functors
// -------------------------------------------
#define _RHEOLEF_generic_unary_syntax_functor(OP,NAME) \
template <typename T = void> \
struct NAME; \
\
template <typename T> \
struct NAME: public std::unary_function<T, T> { \
T operator() (const T& x) const { return OP x; } \
}; \
\
template<> \
struct NAME<void> { \
template <typename T> \
auto operator() (T&& x) const \
noexcept (noexcept (OP std::forward<T>(x))) \
-> decltype(OP std::forward<T>(x)) \
{ return OP std::forward<T>(x); } \
};
#define _RHEOLEF_generic_binary_syntax_functor(OP,NAME) \
template <typename T = void> \
struct NAME; \
\
template<typename T> \
struct NAME: public std::binary_function<T, T, T> { \
T operator() (const T& x, const T& y) const { return x OP y; } \
}; \
\
template<> \
struct NAME<void> { \
template <typename T, typename U> \
auto operator() (T&& t, U&& y) const \
noexcept (noexcept (std::forward<T>(t) OP std::forward<U>(y))) \
-> decltype(std::forward<T>(t) OP std::forward<U>(y)) \
{ return std::forward<T>(t) OP std::forward<U>(y); } \
};
_RHEOLEF_generic_unary_syntax_functor (+,generic_unary_plus)
_RHEOLEF_generic_unary_syntax_functor (-,generic_negate)
_RHEOLEF_generic_binary_syntax_functor (+,generic_plus)
_RHEOLEF_generic_binary_syntax_functor (-,generic_minus)
_RHEOLEF_generic_binary_syntax_functor (*,generic_multiplies)
_RHEOLEF_generic_binary_syntax_functor (/,generic_divides)
#undef _RHEOLEF_generic_unary_syntax_functor
#undef _RHEOLEF_generic_binary_syntax_functor
template<class BinaryFunction, class A1>
struct generic_binder1st {
generic_binder1st (const BinaryFunction& f, const A1& x1)
: _f(f), _x1(x1) {}
template<class A2>
auto operator() (A2&& x2) const
// TODO: assume that both BinaryFunction and A1 have a default constructor...
noexcept (noexcept (BinaryFunction() (A1(), std::forward<A2>(x2))))
-> decltype(BinaryFunction() (A1(), std::forward<A2>(x2)))
{ return _f (_x1, std::forward<A2>(x2)); }
protected:
BinaryFunction _f;
A1 _x1;
};
template<class BinaryFunction, class A2>
struct generic_binder2nd {
generic_binder2nd (const BinaryFunction& f, const A2& x2)
: _f(f), _x2(x2) {}
template<class A1>
auto operator() (A1&& x1) const
// TODO: assume that both BinaryFunction and A2 have a default constructor...
noexcept (noexcept (BinaryFunction() (std::forward<A1>(x1), A2())))
-> decltype(BinaryFunction() (std::forward<A1>(x1), A2()))
{ return _f (std::forward<A1>(x1),_x2); }
protected:
BinaryFunction _f;
A2 _x2;
};
// -------------------------------------------
// computed assignement as functors
// -------------------------------------------
// for = += -= *= /= etc
struct assign_op {
template<class T, class U>
void operator() (T &t, const U &u) const { t = u; }
};
struct plus_assign {
template<class T, class U>
void operator() (T &t, const U &u) const { t += u; }
};
struct minus_assign {
template<class T, class U>
void operator() (T &t, const U &u) const { t -= u; }
};
struct multiplies_assign {
template<class T, class U>
void operator() (T &t, const U &u) const { t *= u; }
};
struct divides_assign {
template<class T, class U>
void operator() (T &t, const U &u) const { t /= u; }
};
template<class ForwardIterator, class InputIterator, class OpAssign>
void
assign_with_operator (ForwardIterator first, ForwardIterator last, InputIterator iter_rhs, OpAssign op_assign) {
for (; first != last; ++first, ++iter_rhs) {
op_assign (*first, *iter_rhs);
}
}
// -------------------------------------------
// misc
// -------------------------------------------
// for convenince: a dummy forward iterator that returns a constant
template <class T>
struct iterator_on_constant {
typedef T value_type;
iterator_on_constant (const T& c) : _c(c) {}
iterator_on_constant<T>& operator++ () { return *this; }
value_type operator* () const { return _c; }
protected:
T _c;
};
}} // namespace rheolef::details
#endif // _RHEOLEF_EXPR_UTILITIES_H
rheolef::details::generic_binder1st
Definition expr_utilities.h:83
rheolef::details::generic_binder1st::_x1
A1 _x1
Definition expr_utilities.h:94
rheolef::details::generic_binder1st::_f
BinaryFunction _f
Definition expr_utilities.h:93
_RHEOLEF_generic_binary_syntax_functor
#define _RHEOLEF_generic_binary_syntax_functor(OP, NAME)
Definition expr_utilities.h:55
_RHEOLEF_generic_unary_syntax_functor
#define _RHEOLEF_generic_unary_syntax_functor(OP, NAME)
Definition expr_utilities.h:37
T
Expr1::float_type T
Definition field_expr.h:230
rheolef::details::assign_with_operator
void assign_with_operator(ForwardIterator first, ForwardIterator last, InputIterator iter_rhs, OpAssign op_assign)
Definition expr_utilities.h:137
rheolef
This file is part of Rheolef.
Definition compiler_eigen.h:39
f
Definition cavity_dg.h:29
rheolef::details::assign_op
Definition expr_utilities.h:115
rheolef::details::assign_op::operator()
void operator()(T &t, const U &u) const
Definition expr_utilities.h:117
rheolef::details::divides_assign
Definition expr_utilities.h:131
rheolef::details::divides_assign::operator()
void operator()(T &t, const U &u) const
Definition expr_utilities.h:133
rheolef::details::generic_binder2nd
Definition expr_utilities.h:98
rheolef::details::generic_binder2nd::_x2
A2 _x2
Definition expr_utilities.h:109
rheolef::details::generic_binder2nd::generic_binder2nd
generic_binder2nd(const BinaryFunction &f, const A2 &x2)
Definition expr_utilities.h:99
rheolef::details::generic_binder2nd::_f
BinaryFunction _f
Definition expr_utilities.h:108
rheolef::details::generic_binder2nd::operator()
auto operator()(A1 &&x1) const noexcept(noexcept(BinaryFunction()(std::forward< A1 >(x1), A2()))) -> decltype(BinaryFunction()(std::forward< A1 >(x1), A2()))
Definition expr_utilities.h:102
rheolef::details::iterator_on_constant
Definition expr_utilities.h:147
rheolef::details::iterator_on_constant::_c
T _c
Definition expr_utilities.h:154
rheolef::details::iterator_on_constant::value_type
T value_type
Definition expr_utilities.h:149
rheolef::details::iterator_on_constant::operator*
value_type operator*() const
Definition expr_utilities.h:152
rheolef::details::iterator_on_constant::operator++
iterator_on_constant< T > & operator++()
Definition expr_utilities.h:151
rheolef::details::iterator_on_constant::iterator_on_constant
iterator_on_constant(const T &c)
Definition expr_utilities.h:150
rheolef::details::minus_assign
Definition expr_utilities.h:123
rheolef::details::minus_assign::operator()
void operator()(T &t, const U &u) const
Definition expr_utilities.h:125
rheolef::details::multiplies_assign
Definition expr_utilities.h:127
rheolef::details::multiplies_assign::operator()
void operator()(T &t, const U &u) const
Definition expr_utilities.h:129
rheolef::details::plus_assign
Definition expr_utilities.h:119
rheolef::details::plus_assign::operator()
void operator()(T &t, const U &u) const
Definition expr_utilities.h:121
u
Definition leveque.h:25