Chombo + EB  3.0
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 
87 // The self_type is there so we can restrict these to classes that define
88 // that; otherwise, this is just too promiscuous; when a class already has
89 // an operator+ member function, the compiler considers the existence of
90 // that one, and the one here, to be an intolerable ambiguity.
91 // C::self_type should just be C itself.
92 // C::scalar_type should be the kind of scalar that you hope to add, multiply
93 // etc against a C. For Chombo::IntVect, for example, scalar_type is int.
94 template<class C> typename C::self_type operator+( const C&, const C& );
95 template<class C> typename C::self_type operator+( const C&, const typename C::scalar_type& );
96 template<class C> typename C::self_type operator+( const typename C::scalar_type&, const C& );
97 
98 template<class C> typename C::self_type operator*( const C&, const C& );
99 template<class C> typename C::self_type operator*( const C&, const typename C::scalar_type& );
100 template<class C> typename C::self_type operator*( const typename C::scalar_type&, const C& );
101 
102 template<class C> typename C::self_type operator-( const C&, const C& );
103 template<class C> typename C::self_type operator-( const C&, const typename C::scalar_type& );
104 template<class C> typename C::self_type operator-( const typename C::scalar_type&, const C& );
105 
106 template<class C> typename C::self_type operator/( const C&, const C& );
107 template<class C> typename C::self_type operator/( const C&, const typename C::scalar_type& );
108 template<class C> typename C::self_type operator/( const typename C::scalar_type&, const C& );
109 
110 #include "BaseNamespaceFooter.H"
111 
112 #include "GenericArithmeticI.H"
113 
114 #endif // include guard
bool operator>(const SelfT &) const
Definition: GenericArithmeticI.H:32
bool operator<(const SelfT &) const
Definition: GenericArithmeticI.H:26
C::self_type operator-(const C &, const C &)
Definition: GenericArithmeticI.H:128
ScalarT scalar_type
Definition: GenericArithmetic.H:60
Definition: GenericArithmetic.H:54
bool operator>=(const SelfT &) const
Definition: GenericArithmeticI.H:44
GenericArithmeticable(SelfT *s)
Definition: GenericArithmetic.H:64
SelfT & operator+=(const SelfT &)
Definition: GenericArithmeticI.H:53
virtual ~GenericArithmeticable()
Definition: GenericArithmetic.H:57
C::self_type operator+(const C &, const C &)
Definition: GenericArithmeticI.H:124
C::self_type operator/(const C &, const C &)
Definition: GenericArithmeticI.H:136
SelfT self_type
Definition: GenericArithmetic.H:61
C::self_type operator*(const C &, const C &)
Definition: GenericArithmeticI.H:132
bool operator<=(const SelfT &) const
Definition: GenericArithmeticI.H:38
SelfT * m_child
Definition: GenericArithmetic.H:83
SelfT & operator*=(const SelfT &)
Definition: GenericArithmeticI.H:77
SelfT & operator/=(const SelfT &)
Definition: GenericArithmeticI.H:89
SelfT & operator-=(const SelfT &)
Definition: GenericArithmeticI.H:65