New operators and stuff in vector class.

This commit is contained in:
King_DuckZ 2014-02-10 01:19:45 +01:00
parent 091b498ff7
commit e2e932358b
2 changed files with 130 additions and 1 deletions

View file

@ -12,7 +12,7 @@ namespace cloonel {
public:
Vector ( void ) = default;
explicit Vector ( T parValue );
template <typename U> Vector ( const Vector<U, S>& parOther );
template <typename U> explicit Vector ( const Vector<U, S>& parOther );
template <typename = std::enable_if<S == 2> > Vector ( T parX, T parY ) : m_mem {parX, parY} {}
template <typename = std::enable_if<S == 3> > Vector ( T parX, T parY, T parZ ) : m_mem {parX, parY, parZ} {}
template <typename = std::enable_if<S == 4> > Vector ( T parX, T parY, T parZ, T parW ) : m_mem {parX, parY, parZ, parW} {}
@ -36,6 +36,17 @@ namespace cloonel {
template <typename U> const Vector& operator-= ( const Vector<U, S>& parOther );
template <typename U> const Vector& operator*= ( const Vector<U, S>& parOther );
template <typename U> const Vector& operator/= ( const Vector<U, S>& parOther );
template <typename U> const Vector& operator+= ( U parOther );
template <typename U> const Vector& operator-= ( U parOther );
template <typename U> const Vector& operator*= ( U parOther );
template <typename U> const Vector& operator/= ( U parOther );
template <typename U>
operator Vector<U, S> ( 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<typename std::common_type<T, U>::type, S> operator* ( const Vector<T, S>& parA, const Vector<U, S>& parB ) __attribute__((pure));
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator/ ( const Vector<T, S>& parA, const Vector<U, S>& parB ) __attribute__((pure));
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator+ ( U parA, const Vector<U, S>& parB ) __attribute__((pure));
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator- ( U parA, const Vector<U, S>& parB ) __attribute__((pure));
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator* ( U parA, const Vector<U, S>& parB ) __attribute__((pure));
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator/ ( U parA, const Vector<U, S>& parB ) __attribute__((pure));
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator+ ( const Vector<T, S>& parA, U parB ) __attribute__((pure));
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator- ( const Vector<T, S>& parA, U parB ) __attribute__((pure));
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator* ( const Vector<T, S>& parA, U parB ) __attribute__((pure));
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator/ ( const Vector<T, S>& parA, U parB ) __attribute__((pure));
typedef Vector<float, 2> float2;
typedef Vector<uint16_t, 2> ushort2;

View file

@ -52,6 +52,53 @@ namespace cloonel {
return *this;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, uint32_t S>
template <typename U>
const Vector<T, S>& Vector<T, S>::operator+= (U parOther) {
for (uint32_t z = 0; z < S; ++z) {
m_mem[z] += parOther;
}
return *this;
}
template <typename T, uint32_t S>
template <typename U>
const Vector<T, S>& Vector<T, S>::operator-= (U parOther) {
for (uint32_t z = 0; z < S; ++z) {
m_mem[z] -= parOther;
}
return *this;
}
template <typename T, uint32_t S>
template <typename U>
const Vector<T, S>& Vector<T, S>::operator*= (U parOther) {
for (uint32_t z = 0; z < S; ++z) {
m_mem[z] *= parOther;
}
return *this;
}
template <typename T, uint32_t S>
template <typename U>
const Vector<T, S>& Vector<T, S>::operator/= (U parOther) {
for (uint32_t z = 0; z < S; ++z) {
m_mem[z] /= parOther;
}
return *this;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, uint32_t S>
template <typename U>
Vector<T, S>::operator Vector<U, S>() {
Vector<U, S> retVal;
for (uint32_t z = 0; z < S; ++z) {
retVal[z] = m_mem[z];
}
return retVal;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, typename U, uint32_t S>
@ -86,4 +133,59 @@ namespace cloonel {
parA /= parB;
return retVal;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator+ (T parA, const Vector<U, S>& parB) {
return parB + parA;
}
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator- (T parA, const Vector<U, S>& parB) {
return Vector<T, S>(parA) - parB;
}
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator* (T parA, const Vector<U, S>& parB) {
return parB * parA;
}
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator/ (T parA, const Vector<U, S>& parB) {
return Vector<T, S>(parA) / parB;
}
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator+ (const Vector<T, S>& parA, U parB) {
typedef typename std::common_type<T, U>::type RetType;
Vector<RetType, S> retVal;
for (uint32_t z = 0; z < S; ++z) {
retVal[z] = parA[z] + parB;
}
return retVal;
}
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator- (const Vector<T, S>& parA, U parB) {
typedef typename std::common_type<T, U>::type RetType;
Vector<RetType, S> retVal;
for (uint32_t z = 0; z < S; ++z) {
retVal[z] = parA[z] - parB;
}
return retVal;
}
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator* (const Vector<T, S>& parA, U parB) {
typedef typename std::common_type<T, U>::type RetType;
Vector<RetType, S> retVal;
for (uint32_t z = 0; z < S; ++z) {
retVal[z] = parA[z] * parB;
}
return retVal;
}
template <typename T, typename U, uint32_t S>
Vector<typename std::common_type<T, U>::type, S> operator/ (const Vector<T, S>& parA, U parB) {
typedef typename std::common_type<T, U>::type RetType;
Vector<RetType, S> retVal;
for (uint32_t z = 0; z < S; ++z) {
retVal[z] = parA[z] / parB;
}
return retVal;
}
} //namespace cloonel