Allium  0.1.0
vector_trait.hpp
1 // Copyright 2020 Hannah Rittich
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ALLIUM_LA_VECTOR_TRAIT_HPP
16 #define ALLIUM_LA_VECTOR_TRAIT_HPP
17 
18 #include <allium/util/crtp.hpp>
19 #include <allium/util/numeric.hpp>
20 
21 namespace allium {
22 
29  template <typename Derived, typename N>
31  : public CrtpTrait<Derived> {
32  public:
33  using Number = N;
34  using Real = real_part_t<Number>;
35 
36  template <typename Other>
37  Derived& operator-= (const Other& other) {
38  derived(this) += (-1.0 * other);
39  return derived(this);
40  }
41 
42  Derived& operator/= (Number divisor) {
43  derived(this) *= (Number(1.0) / divisor);
44  return derived(this);
45  }
46 
47  template <typename Other>
48  Derived operator+ (const Other& rhs) const {
49  Derived result(derived(this));
50  result += rhs;
51  return result;
52  }
53 
54  template <typename Other>
55  Derived operator- (const Other& rhs) const {
56  Derived result(derived(this));
57  result -= rhs;
58  return result;
59  }
60 
61  Derived operator* (Number factor) const {
62  Derived result(derived(this));
63  result *= factor;
64  return result;
65  }
66 
67  Derived operator/ (Number divisor) const {
68  Derived result(derived(this));
69  result /= divisor;
70  return result;
71  }
72  };
73 
74  template <typename T, typename R = void>
75  using enable_if_vector
76  = std::enable_if_t<
77  std::is_base_of<
79 
81  template <typename T>
82  enable_if_vector<T, T>
83  operator* (typename T::Number factor, const T& rhs) {
84  return rhs * factor;
85  }
86 
87  //template <typename T>
88  //enable_if_vector<T, T>
89  //T operator* (typename T::Real factor, const T& rhs) {
90  // return rhs * factor;
91  //}
92 
93 }
94 
95 #endif
Generates common vector operations automatically.
Definition: vector_trait.hpp:31
Trait that implements the self helper routine for the curiously recurring template pattern.
Definition: crtp.hpp:36