From e2e932358b29e5c78f507f25a7bfc1ebcdb78af4 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Mon, 10 Feb 2014 01:19:45 +0100 Subject: [PATCH] New operators and stuff in vector class. --- src/vector.hpp | 29 +++++++++++++- src/vector.inl | 102 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 1 deletion(-) diff --git a/src/vector.hpp b/src/vector.hpp index b145afb..66c4bdc 100644 --- a/src/vector.hpp +++ b/src/vector.hpp @@ -12,7 +12,7 @@ namespace cloonel { public: Vector ( void ) = default; explicit Vector ( T parValue ); - template Vector ( const Vector& parOther ); + template explicit Vector ( const Vector& parOther ); template > Vector ( T parX, T parY ) : m_mem {parX, parY} {} template > Vector ( T parX, T parY, T parZ ) : m_mem {parX, parY, parZ} {} template > Vector ( T parX, T parY, T parZ, T parW ) : m_mem {parX, parY, parZ, parW} {} @@ -36,6 +36,17 @@ namespace cloonel { template const Vector& operator-= ( const Vector& parOther ); template const Vector& operator*= ( const Vector& parOther ); template const Vector& operator/= ( const Vector& parOther ); + template const Vector& operator+= ( U parOther ); + template const Vector& operator-= ( U parOther ); + template const Vector& operator*= ( U parOther ); + template const Vector& operator/= ( U parOther ); + + template + operator Vector ( void ); + + T& operator[] ( uint32_t parIndex ) { return m_mem[parIndex]; } + const T& operator[] ( uint32_t parIndex ) const { return m_mem[parIndex]; } + private: T m_mem[S]; }; @@ -48,6 +59,22 @@ namespace cloonel { Vector::type, S> operator* ( const Vector& parA, const Vector& parB ) __attribute__((pure)); template Vector::type, S> operator/ ( const Vector& parA, const Vector& parB ) __attribute__((pure)); + template + Vector::type, S> operator+ ( U parA, const Vector& parB ) __attribute__((pure)); + template + Vector::type, S> operator- ( U parA, const Vector& parB ) __attribute__((pure)); + template + Vector::type, S> operator* ( U parA, const Vector& parB ) __attribute__((pure)); + template + Vector::type, S> operator/ ( U parA, const Vector& parB ) __attribute__((pure)); + template + Vector::type, S> operator+ ( const Vector& parA, U parB ) __attribute__((pure)); + template + Vector::type, S> operator- ( const Vector& parA, U parB ) __attribute__((pure)); + template + Vector::type, S> operator* ( const Vector& parA, U parB ) __attribute__((pure)); + template + Vector::type, S> operator/ ( const Vector& parA, U parB ) __attribute__((pure)); typedef Vector float2; typedef Vector ushort2; diff --git a/src/vector.inl b/src/vector.inl index 321cb31..8cd3c39 100644 --- a/src/vector.inl +++ b/src/vector.inl @@ -52,6 +52,53 @@ namespace cloonel { return *this; } + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + template + template + const Vector& Vector::operator+= (U parOther) { + for (uint32_t z = 0; z < S; ++z) { + m_mem[z] += parOther; + } + return *this; + } + template + template + const Vector& Vector::operator-= (U parOther) { + for (uint32_t z = 0; z < S; ++z) { + m_mem[z] -= parOther; + } + return *this; + } + template + template + const Vector& Vector::operator*= (U parOther) { + for (uint32_t z = 0; z < S; ++z) { + m_mem[z] *= parOther; + } + return *this; + } + template + template + const Vector& Vector::operator/= (U parOther) { + for (uint32_t z = 0; z < S; ++z) { + m_mem[z] /= parOther; + } + return *this; + } + + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + template + template + Vector::operator Vector() { + Vector retVal; + for (uint32_t z = 0; z < S; ++z) { + retVal[z] = m_mem[z]; + } + return retVal; + } + ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- template @@ -86,4 +133,59 @@ namespace cloonel { parA /= parB; return retVal; } + + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + template + Vector::type, S> operator+ (T parA, const Vector& parB) { + return parB + parA; + } + template + Vector::type, S> operator- (T parA, const Vector& parB) { + return Vector(parA) - parB; + } + template + Vector::type, S> operator* (T parA, const Vector& parB) { + return parB * parA; + } + template + Vector::type, S> operator/ (T parA, const Vector& parB) { + return Vector(parA) / parB; + } + template + Vector::type, S> operator+ (const Vector& parA, U parB) { + typedef typename std::common_type::type RetType; + Vector retVal; + for (uint32_t z = 0; z < S; ++z) { + retVal[z] = parA[z] + parB; + } + return retVal; + } + template + Vector::type, S> operator- (const Vector& parA, U parB) { + typedef typename std::common_type::type RetType; + Vector retVal; + for (uint32_t z = 0; z < S; ++z) { + retVal[z] = parA[z] - parB; + } + return retVal; + } + template + Vector::type, S> operator* (const Vector& parA, U parB) { + typedef typename std::common_type::type RetType; + Vector retVal; + for (uint32_t z = 0; z < S; ++z) { + retVal[z] = parA[z] * parB; + } + return retVal; + } + template + Vector::type, S> operator/ (const Vector& parA, U parB) { + typedef typename std::common_type::type RetType; + Vector retVal; + for (uint32_t z = 0; z < S; ++z) { + retVal[z] = parA[z] / parB; + } + return retVal; + } } //namespace cloonel