Chombo + EB + MF  3.2
GenericArithmeticI.H
Go to the documentation of this file.
1 #ifdef CH_LANG_CC
2 /*
3  * _______ __
4  * / ___/ / ___ __ _ / / ___
5  * / /__/ _ \/ _ \/ V \/ _ \/ _ \
6  * \___/_//_/\___/_/_/_/_.__/\___/
7  * Please refer to Copyright.txt, in Chombo's root directory.
8  */
9 #endif
10 
11 //
12 // Author: Ted
13 //
14 #ifndef _GENERICARITHMETICI_H_
15 #define _GENERICARITHMETICI_H_
16 
17 #include <functional>
18 #include "BaseNamespaceHeader.H"
19 
20 //
21 // Comparison operators
22 //
23 
24 template<typename ScalarT, typename SelfT> bool
26 {
27  return m_child->operatorCompare( rhs, std::less<ScalarT>() );
28 }
29 
30 template<typename ScalarT, typename SelfT> bool
32 {
33  return m_child->operatorCompare( rhs, std::greater<ScalarT>() );
34 }
35 
36 template<typename ScalarT, typename SelfT> bool
38 {
39  return m_child->operatorCompare( rhs, std::less_equal<ScalarT>() );
40 }
41 
42 template<typename ScalarT, typename SelfT> bool
44 {
45  return m_child->operatorCompare( rhs, std::greater_equal<ScalarT>() );
46 }
47 
48 //
49 // In-place arithmetic operators
50 //
51 template<typename ScalarT, typename SelfT> SelfT&
53 {
54  return m_child->operatorOpEquals( rhs, std::plus<ScalarT>() );
55 }
56 
57 template<typename ScalarT, typename SelfT> SelfT&
59 {
60  return m_child->operatorOpEquals( a, std::plus<ScalarT>() );
61 }
62 
63 template<typename ScalarT, typename SelfT> SelfT&
65 {
66  return m_child->operatorOpEquals( rhs, std::minus<ScalarT>() );
67 }
68 
69 template<typename ScalarT, typename SelfT> SelfT&
71 {
72  return m_child->operatorOpEquals( a, std::minus<ScalarT>() );
73 }
74 
75 template<typename ScalarT, typename SelfT> SelfT&
77 {
78  return m_child->operatorOpEquals( rhs, std::multiplies<ScalarT>() );
79 }
80 
81 template<typename ScalarT, typename SelfT> SelfT&
83 {
84  return m_child->operatorOpEquals( a, std::multiplies<ScalarT>() );
85 }
86 
87 template<typename ScalarT, typename SelfT> SelfT&
89 {
90  return m_child->operatorOpEquals( rhs, std::divides<ScalarT>() );
91 }
92 
93 template<typename ScalarT, typename SelfT> SelfT&
95 {
96  return m_child->operatorOpEquals( a, std::divides<ScalarT>() );
97 }
98 
99 template< class C >
100 C operatorOp( const C& c1, const C& c2, C& (C::*op)(const C&) )
101 {
102  C result( c1 );
103  (result.*op)( c2 );
104  return result;
105 }
106 
107 // Same, but for scalar second arg.
108 template< class C >
109 C operatorOpScalar( const C& c1, const typename C::scalar_type& x,
110  C& (C::*op)(const typename C::scalar_type&) )
111 {
112  C result( c1 );
113  (result.*op)( x );
114  return result;
115 }
116 
117 //
118 // Args C and C
119 //
120 template<class C> typename C::self_type operator+( const C& c1, const C& c2 )
121 {
122  return operatorOp< C >( c1, c2, &C::operator+= );
123 }
124 template<class C> typename C::self_type operator-( const C& c1, const C& c2 )
125 {
126  return operatorOp< C >( c1, c2, &C::operator-= );
127 }
128 template<class C> typename C::self_type operator*( const C& c1, const C& c2 )
129 {
130  return operatorOp< C >( c1, c2, &C::operator*= );
131 }
132 template<class C> typename C::self_type operator/( const C& c1, const C& c2 )
133 {
134  return operatorOp< C >( c1, c2, &C::operator/= );
135 }
136 
137 //
138 // Args C and const C::scalar_type&
139 //
140 template<class C> typename C::self_type operator+( const C& c, const typename C::scalar_type& x )
141 {
142  return operatorOpScalar<C>( c, x, &C::operator+= );
143 }
144 template<class C> typename C::self_type operator+( const typename C::scalar_type& x, const C& c )
145 {
146  return c + x;
147 }
148 
149 template<class C> typename C::self_type operator*( const C& c, const typename C::scalar_type& x )
150 {
151  return operatorOpScalar<C>( c, x, &C::operator*= );
152 }
153 template<class C> typename C::self_type operator*( const typename C::scalar_type& x, const C& c )
154 {
155  return c * x;
156 }
157 
158 template<class C> typename C::self_type operator-( const C& c, const typename C::scalar_type& x )
159 {
160  return operatorOpScalar<C>( c, x, &C::operator-= );
161 }
162 template<class C> typename C::self_type operator-( const typename C::scalar_type& x, const C& c )
163 {
164  C temp( c );
165  return (-temp) + x;
166 }
167 
168 template<class C> typename C::self_type operator/( const C& c, const typename C::scalar_type& x )
169 {
170  return operatorOpScalar<C>( c, x, &C::operator/= );
171 }
172 template<class C> typename C::self_type operator/( const typename C::scalar_type& x, const C& c )
173 {
174  C temp( c );
175  return temp.reciprocal() * x;
176  // Warning: if x is an int, then temp.reciprocal() will always be 0
177  // and the return value from this function will also always be 0.
178 }
179 
180 #include "BaseNamespaceFooter.H"
181 
182 #endif // include guard
bool operator>(const SelfT &) const
Definition: GenericArithmeticI.H:31
bool operator<(const SelfT &) const
Definition: GenericArithmeticI.H:25
bool operator>=(const SelfT &) const
Definition: GenericArithmeticI.H:43
void const char const int const int const int const Real const Real const int const Real const int const Real Real * C
Definition: Lapack.H:83
SelfT & operator+=(const SelfT &)
Definition: GenericArithmeticI.H:52
C::self_type operator/(const C &c1, const C &c2)
Definition: GenericArithmeticI.H:132
C::self_type operator*(const C &c1, const C &c2)
Definition: GenericArithmeticI.H:128
C::self_type operator-(const C &c1, const C &c2)
Definition: GenericArithmeticI.H:124
C::self_type operator+(const C &c1, const C &c2)
Definition: GenericArithmeticI.H:120
C operatorOpScalar(const C &c1, const typename C::scalar_type &x, C &(C::*op)(const typename C::scalar_type &))
Definition: GenericArithmeticI.H:109
bool operator<=(const SelfT &) const
Definition: GenericArithmeticI.H:37
SelfT & operator*=(const SelfT &)
Definition: GenericArithmeticI.H:76
SelfT & operator/=(const SelfT &)
Definition: GenericArithmeticI.H:88
SelfT & operator-=(const SelfT &)
Definition: GenericArithmeticI.H:64
C operatorOp(const C &c1, const C &c2, C &(C::*op)(const C &))
Definition: GenericArithmeticI.H:100