SPH
OperatorTemplate.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "common/Assert.h"
9 
11 
26 template <typename TDerived>
28 public:
29  TDerived operator+(const TDerived& other) const {
30  TDerived sum(derived());
31  sum += other;
32  return sum;
33  }
34  friend TDerived operator-(const TDerived& value1, const TDerived& value2) {
35  // this has to be a friend operator as the unary operator would hide the binary one (we would have to
36  // add using OperatorTemplate<...>::operator-, which is undesirable)
37  return value1 + (-value2);
38  }
39  TDerived& operator-=(const TDerived& other) {
40  return derived().operator+=(-other);
41  }
42  bool operator!=(const TDerived& other) const {
43  return !(derived() == other);
44  }
47  TDerived operator*(const Float value) const {
48  TDerived multiplied(derived());
49  multiplied *= value;
50  return multiplied;
51  }
52  friend TDerived operator*(const Float value, const TDerived& derived) {
53  return derived * value;
54  }
55  TDerived operator/(const Float value) const {
56  SPH_ASSERT(value != 0._f);
57  return derived() * (1._f / value);
58  }
59  TDerived& operator/=(const Float value) {
60  SPH_ASSERT(value != 0._f);
61  return derived().operator*=(1._f / value);
62  }
63 
64 private:
65  INLINE TDerived& derived() {
66  return static_cast<TDerived&>(*this);
67  }
68 
69  INLINE const TDerived& derived() const {
70  return static_cast<const TDerived&>(*this);
71  }
72 };
73 
Custom assertions.
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
constexpr Size sum()
Definition: Multipole.h:31
Class defining additional operators from existing ones.
TDerived operator+(const TDerived &other) const
TDerived & operator-=(const TDerived &other)
friend TDerived operator-(const TDerived &value1, const TDerived &value2)
TDerived & operator/=(const Float value)
bool operator!=(const TDerived &other) const
TDerived operator*(const Float value) const
friend TDerived operator*(const Float value, const TDerived &derived)
TDerived operator/(const Float value) const