summaryrefslogtreecommitdiffstats
path: root/include/ui/TVecHelpers.h
diff options
context:
space:
mode:
authorMathias Agopian <mathias@google.com>2013-09-01 21:35:36 -0700
committerMathias Agopian <mathias@google.com>2013-09-03 16:38:49 -0700
commit1d4d8f94e2989b7c8667602304df9059d2701653 (patch)
treea0a7ab12d583a454f08af3f21a01b7bd7dabc07a /include/ui/TVecHelpers.h
parent9b5534b0e5e1510f56e6a2c58ad0816167603ebd (diff)
downloadframeworks_native-1d4d8f94e2989b7c8667602304df9059d2701653.zip
frameworks_native-1d4d8f94e2989b7c8667602304df9059d2701653.tar.gz
frameworks_native-1d4d8f94e2989b7c8667602304df9059d2701653.tar.bz2
improve mat44 implementation
this will make it easier to create matrices of different sizes Change-Id: I2c1771ba0823c42d737762e2dfc2cd47eb302767
Diffstat (limited to 'include/ui/TVecHelpers.h')
-rw-r--r--include/ui/TVecHelpers.h103
1 files changed, 68 insertions, 35 deletions
diff --git a/include/ui/TVecHelpers.h b/include/ui/TVecHelpers.h
index 081c69c..bb7dbfc 100644
--- a/include/ui/TVecHelpers.h
+++ b/include/ui/TVecHelpers.h
@@ -57,16 +57,16 @@ struct Impersonator {
};
/*
- * TVecArithmeticOperators implements basic arithmetic and basic compound assignments
+ * TVec{Add|Product}Operators implements basic arithmetic and basic compound assignments
* operators on a vector of type BASE<T>.
*
* BASE only needs to implement operator[] and size().
- * By simply inheriting from TVecArithmeticOperators<BASE, T> BASE will automatically
+ * By simply inheriting from TVec{Add|Product}Operators<BASE, T> BASE will automatically
* get all the functionality here.
*/
template <template<typename T> class BASE, typename T>
-class TVecArithmeticOperators {
+class TVecAddOperators {
public:
/* compound assignment from a another vector of the same size but different
* element type.
@@ -87,42 +87,93 @@ public:
}
return rhs;
}
- template <typename OTHER>
- BASE<T>& operator *= (const BASE<OTHER>& v) {
+
+ /* compound assignment from a another vector of the same type.
+ * These operators can be used for implicit conversion and handle operations
+ * like "vector *= scalar" by letting the compiler implicitly convert a scalar
+ * to a vector (assuming the BASE<T> allows it).
+ */
+ BASE<T>& operator += (const BASE<T>& v) {
BASE<T>& rhs = static_cast<BASE<T>&>(*this);
for (size_t i=0 ; i<BASE<T>::size() ; i++) {
- rhs[i] *= v[i];
+ rhs[i] += v[i];
}
return rhs;
}
- template <typename OTHER>
- BASE<T>& operator /= (const BASE<OTHER>& v) {
+ BASE<T>& operator -= (const BASE<T>& v) {
BASE<T>& rhs = static_cast<BASE<T>&>(*this);
for (size_t i=0 ; i<BASE<T>::size() ; i++) {
- rhs[i] /= v[i];
+ rhs[i] -= v[i];
}
return rhs;
}
- /* compound assignment from a another vector of the same type.
- * These operators can be used for implicit conversion and handle operations
- * like "vector *= scalar" by letting the compiler implicitly convert a scalar
- * to a vector (assuming the BASE<T> allows it).
+ /*
+ * NOTE: the functions below ARE NOT member methods. They are friend functions
+ * with they definition inlined with their declaration. This makes these
+ * template functions available to the compiler when (and only when) this class
+ * is instantiated, at which point they're only templated on the 2nd parameter
+ * (the first one, BASE<T> being known).
*/
- BASE<T>& operator += (const BASE<T>& v) {
+
+ /* The operators below handle operation between vectors of the same side
+ * but of a different element type.
+ */
+ template<typename RT>
+ friend inline
+ BASE<T> PURE operator +(const BASE<T>& lv, const BASE<RT>& rv) {
+ return BASE<T>(lv) += rv;
+ }
+ template<typename RT>
+ friend inline
+ BASE<T> PURE operator -(const BASE<T>& lv, const BASE<RT>& rv) {
+ return BASE<T>(lv) -= rv;
+ }
+
+ /* The operators below (which are not templates once this class is instanced,
+ * i.e.: BASE<T> is known) can be used for implicit conversion on both sides.
+ * These handle operations like "vector * scalar" and "scalar * vector" by
+ * letting the compiler implicitly convert a scalar to a vector (assuming
+ * the BASE<T> allows it).
+ */
+ friend inline
+ BASE<T> PURE operator +(const BASE<T>& lv, const BASE<T>& rv) {
+ return BASE<T>(lv) += rv;
+ }
+ friend inline
+ BASE<T> PURE operator -(const BASE<T>& lv, const BASE<T>& rv) {
+ return BASE<T>(lv) -= rv;
+ }
+};
+
+template <template<typename T> class BASE, typename T>
+class TVecProductOperators {
+public:
+ /* compound assignment from a another vector of the same size but different
+ * element type.
+ */
+ template <typename OTHER>
+ BASE<T>& operator *= (const BASE<OTHER>& v) {
BASE<T>& rhs = static_cast<BASE<T>&>(*this);
for (size_t i=0 ; i<BASE<T>::size() ; i++) {
- rhs[i] += v[i];
+ rhs[i] *= v[i];
}
return rhs;
}
- BASE<T>& operator -= (const BASE<T>& v) {
+ template <typename OTHER>
+ BASE<T>& operator /= (const BASE<OTHER>& v) {
BASE<T>& rhs = static_cast<BASE<T>&>(*this);
for (size_t i=0 ; i<BASE<T>::size() ; i++) {
- rhs[i] -= v[i];
+ rhs[i] /= v[i];
}
return rhs;
}
+
+ /* compound assignment from a another vector of the same type.
+ * These operators can be used for implicit conversion and handle operations
+ * like "vector *= scalar" by letting the compiler implicitly convert a scalar
+ * to a vector (assuming the BASE<T> allows it).
+ */
BASE<T>& operator *= (const BASE<T>& v) {
BASE<T>& rhs = static_cast<BASE<T>&>(*this);
for (size_t i=0 ; i<BASE<T>::size() ; i++) {
@@ -151,16 +202,6 @@ public:
*/
template<typename RT>
friend inline
- BASE<T> PURE operator +(const BASE<T>& lv, const BASE<RT>& rv) {
- return BASE<T>(lv) += rv;
- }
- template<typename RT>
- friend inline
- BASE<T> PURE operator -(const BASE<T>& lv, const BASE<RT>& rv) {
- return BASE<T>(lv) -= rv;
- }
- template<typename RT>
- friend inline
BASE<T> PURE operator *(const BASE<T>& lv, const BASE<RT>& rv) {
return BASE<T>(lv) *= rv;
}
@@ -177,14 +218,6 @@ public:
* the BASE<T> allows it).
*/
friend inline
- BASE<T> PURE operator +(const BASE<T>& lv, const BASE<T>& rv) {
- return BASE<T>(lv) += rv;
- }
- friend inline
- BASE<T> PURE operator -(const BASE<T>& lv, const BASE<T>& rv) {
- return BASE<T>(lv) -= rv;
- }
- friend inline
BASE<T> PURE operator *(const BASE<T>& lv, const BASE<T>& rv) {
return BASE<T>(lv) *= rv;
}