diff --git a/src/vector.hpp b/src/vector.hpp new file mode 100644 index 0000000..b145afb --- /dev/null +++ b/src/vector.hpp @@ -0,0 +1,56 @@ +#ifndef idid0528646832E04CF08E9785B66CFE0BD1 +#define idid0528646832E04CF08E9785B66CFE0BD1 + +#include +#include +#include + +namespace cloonel { + template + class Vector { + template friend class Vector; + public: + Vector ( void ) = default; + explicit Vector ( T parValue ); + template 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} {} + + ~Vector ( void ) noexcept = default; + + enum { + Dimension = S + }; + + template = S> > T& x ( void ) { return m_mem[0]; } + template = S> > const T& x ( void ) const { return m_mem[0]; } + template = 2 and 4 >= S> > T& y ( void ) { return m_mem[1]; } + template = 2 and 4 >= S> > const T& y ( void ) const { return m_mem[1]; } + template = 3 and 4 >= S> > T& z ( void ) { return m_mem[2]; } + template = 3 and 4 >= S> > const T& z ( void ) const { return m_mem[2]; } + template = 4 and 4 >= S> > T& w ( void ) { return m_mem[3]; } + template = 4 and 4 >= S> > const T& w ( void ) const { return m_mem[3]; } + + 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/= ( const Vector& parOther ); + private: + T m_mem[S]; + }; + + template + 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* ( const Vector& parA, const Vector& parB ) __attribute__((pure)); + template + Vector::type, S> operator/ ( const Vector& parA, const Vector& parB ) __attribute__((pure)); + + typedef Vector float2; + typedef Vector ushort2; +} //namespace cloonel +#include "vector.inl" +#endif diff --git a/src/vector.inl b/src/vector.inl new file mode 100644 index 0000000..321cb31 --- /dev/null +++ b/src/vector.inl @@ -0,0 +1,89 @@ +namespace cloonel { + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + template + Vector::Vector (T parValue) : + m_mem {parValue} + { + } + + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + template + template + Vector::Vector (const Vector& parOther) { + for (uint32_t z = 0; z < S; ++z) { + m_mem[z] = parOther.m_mem[z]; + } + } + + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + template + template + const Vector& Vector::operator+= (const Vector& parOther) { + for (uint32_t z = 0; z < S; ++z) { + m_mem[z] += parOther.m_mem[z]; + } + return *this; + } + template + template + const Vector& Vector::operator-= (const Vector& parOther) { + for (uint32_t z = 0; z < S; ++z) { + m_mem[z] -= parOther.m_mem[z]; + } + return *this; + } + template + template + const Vector& Vector::operator*= (const Vector& parOther) { + for (uint32_t z = 0; z < S; ++z) { + m_mem[z] *= parOther.m_mem[z]; + } + return *this; + } + template + template + const Vector& Vector::operator/= (const Vector& parOther) { + for (uint32_t z = 0; z < S; ++z) { + m_mem[z] /= parOther.m_mem[z]; + } + return *this; + } + + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + template + inline + Vector::type, S> operator+ (const Vector& parA, const Vector& parB) { + typedef typename std::common_type::type RetType; + Vector retVal(parA); + parA += parB; + return retVal; + } + template + inline + Vector::type, S> operator- (const Vector& parA, const Vector& parB) { + typedef typename std::common_type::type RetType; + Vector retVal(parA); + parA -= parB; + return retVal; + } + template + inline + Vector::type, S> operator* (const Vector& parA, const Vector& parB) { + typedef typename std::common_type::type RetType; + Vector retVal(parA); + parA *= parB; + return retVal; + } + template + inline + Vector::type, S> operator/ (const Vector& parA, const Vector& parB) { + typedef typename std::common_type::type RetType; + Vector retVal(parA); + parA /= parB; + return retVal; + } +} //namespace cloonel