Chombo + EB  3.0
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 //
13 // Author: Ted
14 //
15 #ifndef _GENERICARITHMETICI_H_
16 #define _GENERICARITHMETICI_H_
17 
18 #include <functional>
19 #include "BaseNamespaceHeader.H"
20 
21 //
22 // Comparison operators
23 //
24 
25 template<typename ScalarT, typename SelfT> bool
27 {
28  return m_child->operatorCompare( rhs, std::less<ScalarT>() );
29 }
30 
31 template<typename ScalarT, typename SelfT> bool
33 {
34  return m_child->operatorCompare( rhs, std::greater<ScalarT>() );
35 }
36 
37 template<typename ScalarT, typename SelfT> bool
39 {
40  return m_child->operatorCompare( rhs, std::less_equal<ScalarT>() );
41 }
42 
43 template<typename ScalarT, typename SelfT> bool
45 {
46  return m_child->operatorCompare( rhs, std::greater_equal<ScalarT>() );
47 }
48 
49 //
50 // In-place arithmetic operators
51 //
52 template<typename ScalarT, typename SelfT> SelfT&
54 {
55  return m_child->operatorOpEquals( rhs, std::plus<ScalarT>() );
56 }
57 
58 template<typename ScalarT, typename SelfT> SelfT&
60 {
61  return m_child->operatorOpEquals( a, std::plus<ScalarT>() );
62 }
63 
64 template<typename ScalarT, typename SelfT> SelfT&
66 {
67  return m_child->operatorOpEquals( rhs, std::minus<ScalarT>() );
68 }
69 
70 template<typename ScalarT, typename SelfT> SelfT&
72 {
73  return m_child->operatorOpEquals( a, std::minus<ScalarT>() );
74 }
75 
76 template<typename ScalarT, typename SelfT> SelfT&
78 {
79  return m_child->operatorOpEquals( rhs, std::multiplies<ScalarT>() );
80 }
81 
82 template<typename ScalarT, typename SelfT> SelfT&
84 {
85  return m_child->operatorOpEquals( a, std::multiplies<ScalarT>() );
86 }
87 
88 template<typename ScalarT, typename SelfT> SelfT&
90 {
91  return m_child->operatorOpEquals( rhs, std::divides<ScalarT>() );
92 }
93 
94 template<typename ScalarT, typename SelfT> SelfT&
96 {
97  return m_child->operatorOpEquals( a, std::divides<ScalarT>() );
98 }
99 
100 
101 
102 template< class C >
103 C operatorOp( const C& c1, const C& c2, C& (C::*op)(const C&) )
104 {
105  C result( c1 );
106  (result.*op)( c2 );
107  return result;
108 }
109 
110 // Same, but for scalar second arg.
111 template< class C >
112 C operatorOpScalar( const C& c1, const typename C::scalar_type& x,
113  C& (C::*op)(const typename C::scalar_type&) )
114 {
115  C result( c1 );
116  (result.*op)( x );
117  return result;
118 }
119 
120 
121 //
122 // Args C and C
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 template<class C> typename C::self_type operator/( const C& c1, const C& c2 )
137 {
138  return operatorOp< C >( c1, c2, &C::operator/= );
139 }
140 
141 
142 //
143 // Args C and const C::scalar_type&
144 //
145 template<class C> typename C::self_type operator+( const C& c, const typename C::scalar_type& x )
146 {
147  return operatorOpScalar<C>( c, x, &C::operator+= );
148 }
149 template<class C> typename C::self_type operator+( const typename C::scalar_type& x, const C& c )
150 {
151  return c + x;
152 }
153 
154 template<class C> typename C::self_type operator*( const C& c, const typename C::scalar_type& x )
155 {
156  return operatorOpScalar<C>( c, x, &C::operator*= );
157 }
158 template<class C> typename C::self_type operator*( const typename C::scalar_type& x, const C& c )
159 {
160  return c * x;
161 }
162 
163 template<class C> typename C::self_type operator-( const C& c, const typename C::scalar_type& x )
164 {
165  return operatorOpScalar<C>( c, x, &C::operator-= );
166 }
167 template<class C> typename C::self_type operator-( const typename C::scalar_type& x, const C& c )
168 {
169  C temp( c );
170  return (-temp) + x;
171 }
172 
173 template<class C> typename C::self_type operator/( const C& c, const typename C::scalar_type& x )
174 {
175  return operatorOpScalar<C>( c, x, &C::operator/= );
176 }
177 template<class C> typename C::self_type operator/( const typename C::scalar_type& x, const C& c )
178 {
179  C temp( c );
180  return temp.reciprocal() * x;
181  // Warning: if x is an int, then temp.reciprocal() will always be 0
182  // and the return value from this function will also always be 0.
183 }
184 
185 #include "BaseNamespaceFooter.H"
186 
187 #endif // include guard
bool operator>(const SelfT &) const
Definition: GenericArithmeticI.H:32
bool operator<(const SelfT &) const
Definition: GenericArithmeticI.H:26
bool operator>=(const SelfT &) const
Definition: GenericArithmeticI.H:44
SelfT & operator+=(const SelfT &)
Definition: GenericArithmeticI.H:53
C::self_type operator/(const C &c1, const C &c2)
Definition: GenericArithmeticI.H:136
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 operatorOpScalar(const C &c1, const typename C::scalar_type &x, C &(C::*op)(const typename C::scalar_type &))
Definition: GenericArithmeticI.H:112
bool operator<=(const SelfT &) const
Definition: GenericArithmeticI.H:38
SelfT & operator*=(const SelfT &)
Definition: GenericArithmeticI.H:77
SelfT & operator/=(const SelfT &)
Definition: GenericArithmeticI.H:89
SelfT & operator-=(const SelfT &)
Definition: GenericArithmeticI.H:65
C operatorOp(const C &c1, const C &c2, C &(C::*op)(const C &))
Definition: GenericArithmeticI.H:103