Chombo + EB  3.2
GenericArithmetic.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 // For any class that has operator+=, defines a non-member operator+. Ditto
13 // operator-=, *=, /=.
14 // To use these functions, your class must derive from
15 // class GenericArithmeticable (defined below), and furthermore define
16 // a unary operator-(), and a reciprocal() function.
17 //
18 // All of this is done more elegantly and completely in boost/operators.hpp;
19 // let's keep that in mind if we ever decide to use Boost.
20 //
21 // Author: Ted
22 //
23 
24 #ifndef _GENERICARITHMETIC_H_
25 #define _GENERICARITHMETIC_H_
26 
27 #include "BaseNamespaceHeader.H"
28 
29 /** Class that you need to derive from, if you want the *global* operators below
30  * to be available for your class.
31  * For example:
32  * class Foo : public GenericArithmeticable<double,Foo>
33  * {
34  * public:
35  * Foo() : GenericArithmeticable<double,Foo>(this)
36  * {
37  * }
38  * Foo& operator+=( const Foo& );
39  * Foo& operator+=( const double& );
40  * [etc]
41  * };
42 
43  *
44  * If, in addition, you want the member-function operators, you'll need to
45  * implement some generic operator-appliers in your derived class: see, in
46  * GenericArithmeticI.H, operatorCompare() and operatorOpEquals().
47  * If you implement operator<, operator+= etc in your derived class, then that'll
48  * override the ones here (regardless of whether you've defined operatorCompare()
49  * and operatorOpEquals() in your derived class, though it would then be weird if
50  * you did).
51  *
52 */
53 template<typename ScalarT, typename SelfT>
55 {
56  // Intel 8.1 icpc wants this
58  {
59  }
60  typedef ScalarT scalar_type;
61  typedef SelfT self_type;
62 
63  public:
64  GenericArithmeticable( SelfT * s ) : m_child( s )
65  {
66  }
67 
68  bool operator<(const SelfT&) const;
69  bool operator>(const SelfT&) const;
70  bool operator<=(const SelfT&) const;
71  bool operator>=(const SelfT&) const;
72 
73  SelfT& operator+=(const SelfT&);
74  SelfT& operator+=(const ScalarT&);
75  SelfT& operator-=(const SelfT&);
76  SelfT& operator-=(const ScalarT&);
77  SelfT& operator*=(const SelfT&);
78  SelfT& operator*=(const ScalarT&);
79  SelfT& operator/=(const SelfT&);
80  SelfT& operator/=(const ScalarT&);
81 
82  private:
83  SelfT * m_child;
84 };
85 
86 // The self_type is there so we can restrict these to classes that define
87 // that; otherwise, this is just too promiscuous; when a class already has
88 // an operator+ member function, the compiler considers the existence of
89 // that one, and the one here, to be an intolerable ambiguity.
90 // C::self_type should just be C itself.
91 // C::scalar_type should be the kind of scalar that you hope to add, multiply
92 // etc against a C. For Chombo::IntVect, for example, scalar_type is int.
93 template<class C> typename C::self_type operator+( const C&, const C& );
94 template<class C> typename C::self_type operator+( const C&, const typename C::scalar_type& );
95 template<class C> typename C::self_type operator+( const typename C::scalar_type&, const C& );
96 
97 template<class C> typename C::self_type operator*( const C&, const C& );
98 template<class C> typename C::self_type operator*( const C&, const typename C::scalar_type& );
99 template<class C> typename C::self_type operator*( const typename C::scalar_type&, const C& );
100 
101 template<class C> typename C::self_type operator-( const C&, const C& );
102 template<class C> typename C::self_type operator-( const C&, const typename C::scalar_type& );
103 template<class C> typename C::self_type operator-( const typename C::scalar_type&, const C& );
104 
105 template<class C> typename C::self_type operator/( const C&, const C& );
106 template<class C> typename C::self_type operator/( const C&, const typename C::scalar_type& );
107 template<class C> typename C::self_type operator/( const typename C::scalar_type&, const C& );
108 
109 #include "BaseNamespaceFooter.H"
110 
111 #include "GenericArithmeticI.H"
112 
113 #endif // include guard
bool operator>(const SelfT &) const
Definition: GenericArithmeticI.H:31
bool operator<(const SelfT &) const
Definition: GenericArithmeticI.H:25
C::self_type operator-(const C &, const C &)
Definition: GenericArithmeticI.H:124
ScalarT scalar_type
Definition: GenericArithmetic.H:60
Definition: GenericArithmetic.H:54
bool operator>=(const SelfT &) const
Definition: GenericArithmeticI.H:43
GenericArithmeticable(SelfT *s)
Definition: GenericArithmetic.H:64
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
virtual ~GenericArithmeticable()
Definition: GenericArithmetic.H:57
C::self_type operator+(const C &, const C &)
Definition: GenericArithmeticI.H:120
C::self_type operator/(const C &, const C &)
Definition: GenericArithmeticI.H:132
SelfT self_type
Definition: GenericArithmetic.H:61
C::self_type operator*(const C &, const C &)
Definition: GenericArithmeticI.H:128
bool operator<=(const SelfT &) const
Definition: GenericArithmeticI.H:37
SelfT * m_child
Definition: GenericArithmetic.H:83
SelfT & operator*=(const SelfT &)
Definition: GenericArithmeticI.H:76
SelfT & operator/=(const SelfT &)
Definition: GenericArithmeticI.H:88
SelfT & operator-=(const SelfT &)
Definition: GenericArithmeticI.H:64