Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: Common/Range.h
- * Author: ryden
- * Based on code found at: http://gcc.gnu.org/ml/gcc-patches/2010-07/msg01854.html
- *
- */
- #ifndef RANGE_H
- #define RANGE_H
- template < int iMin, int iMax >
- struct Range
- {
- struct iterator
- {
- iterator ( int val ) : v ( val ) {}
- iterator operator++ () { iterator it ( v ); ++v; return it; }
- iterator& operator++ (int) { ++v; return *this; }
- bool operator!= ( const iterator& other ) const { return other.v != v; }
- int operator* () const { return v; }
- private:
- int v;
- };
- iterator begin () const { return iterator ( iMin ); }
- iterator end () const { return iterator ( iMax + 1 ); }
- };
- #define range(min,max) Range<min,max>()
- #define until(max) Range<0,(max)-1>()
- #endif /* RANGE_H */
- /*
- * File: Common/Vector.h
- * Author: ryden
- *
- */
- #ifndef VECTOR_H
- #define VECTOR_H
- #include <string>
- #include <sstream>
- template < typename T, unsigned int uiNumComponents >
- struct VectorBase
- {
- typedef T __attribute__ ((vector_size(uiNumComponents*sizeof(T)))) vectorType;
- union
- {
- T v [ uiNumComponents ];
- vectorType vec;
- };
- };
- template < typename T, unsigned int uiNumComponents >
- class Vector : protected VectorBase < T, uiNumComponents >
- {
- public:
- Vector ( )
- {
- for ( auto i : until(uiNumComponents) )
- {
- Vector::v[i] = 0;
- }
- }
- Vector ( const T values[uiNumComponents] )
- : Vector::v ( values )
- {
- }
- Vector ( const std::initializer_list<T>& list )
- {
- for ( auto i : until(uiNumComponents) )
- {
- Vector::v[i] = list.begin ()[i];
- }
- }
- Vector& operator= ( const Vector& Right )
- {
- Vector::vec = Right.vec;
- return *this;
- }
- operator std::string () const
- {
- return *(*this);
- }
- std::string operator* () const
- {
- std::ostringstream ostr;
- ostr << "( ";
- for ( const T& elem : Vector::v )
- {
- ostr << elem << ", ";
- }
- ostr.seekp( -2, std::ios_base::cur );
- ostr << " )";
- return ostr.str();
- }
- Vector operator+ ( const Vector& Right ) const
- {
- Vector vecRet;
- vecRet.vec = Vector::vec + Right.vec;
- return vecRet;
- }
- Vector& operator+= ( const Vector& Right )
- {
- Vector::vec = Vector::vec + Right.vec;
- return *this;
- }
- };
- template < typename T >
- class Vector4 : public Vector < T, 4 >
- {
- typedef Vector < T, 4 > parent;
- public:
- Vector4 ()
- : parent ()
- {
- }
- Vector4 ( const T& x, const T& y = 0, const T& z = 0, const T& w = 0 )
- {
- parent::v [ 0 ] = x;
- parent::v [ 1 ] = y;
- parent::v [ 2 ] = z;
- parent::v [ 3 ] = w;
- }
- Vector4 ( const parent& Right )
- : parent ( Right )
- {
- }
- Vector4& operator= ( const parent& Right )
- {
- parent::operator= ( Right );
- return *this;
- }
- T& x () { return parent::v[0]; }
- const T& x () const { return parent::v[0]; }
- T& y () { return parent::v[1]; }
- const T& y () const { return parent::v[1]; }
- T& z () { return parent::v[2]; }
- const T& z () const { return parent::v[2]; }
- T& w () { return parent::v[3]; }
- const T& w () const { return parent::v[3]; }
- };
- typedef Vector4<short> Vector4s, vec4s;
- typedef Vector4<float> Vector4f, vec4f;
- typedef Vector4<double> Vector4d, vec4d;
- #endif /* VECTOR_H */
- #include <cstdio>
- #include <cstdlib>
- int main(int argc, char** argv)
- {
- vec4f v1 ( 1, 2, 3, 4 ), v2 ( 8, 7, 6, 5 ), v3;
- v3 = v1 + v2;
- // v3 += { 1, 0, 1, 0 };
- printf ( "Result: %f %f %f %f\n", v3.x(), v3.y(), v3.z(), v3.w() );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment