TeaPacket v0.0.1
Multiplatform Game Engine
Loading...
Searching...
No Matches
Vector.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <stdexcept>
5#include <array>
6#include <string>
7
8#define TP_VEC_ALIAS(name, index) \
9T& name() { return _values[index]; static_assert(index <= i, "Attempt to use an alias that doesn't exist for this TeaPacket Vector type! Alias index: " #index "." ); }; \
10const T& name() const { return _values[index]; static_assert(index <= i, "Attempt to use an alias that doesn't exist for this TeaPacket Vector type! Alias index: " #index "."); }; \
11
12#define TP_VEC_MATH_OPERATOR(OP) \
13template<unsigned char s> \
14Vector<T,i>& operator OP##= (const Vector<T,s>& other) \
15{ \
16 static_assert(i >= s, "Left side Vector must have a larger or equal size to the Right side Vector when performing math operations."); \
17 for(unsigned char j = 0; j < s; j++) \
18 { \
19 this[j] OP##= other[j]; \
20 }\
21 return *this; \
22} \
23template<unsigned char s> \
24Vector<T,i> operator OP (const Vector<T,s>& other) const \
25{ \
26 static_assert(i >= s, "Left side Vector must have a larger or equal size to the Right side Vector when performing math operations."); \
27 Vector<T,i> vec = this; \
28 return vec OP##= other; \
29}
30
33{
35 template<typename T, unsigned char i>
36 class Vector
37 {
38 public:
39 static_assert(std::is_arithmetic_v<T>, "Provided Vector type is not an arithmetic type.");
40
45
50
53
55
67
71 bool operator==(Vector<T,i> const& other) const
72 {
73 for (unsigned char j = 0; j < i; j++)
74 {
75 if (other[j] != this[j])
76 {
77 return false;
78 }
79 }
80 return true;
81 }
82
86 bool operator!=(Vector<T,i> const& other) const
87 {
88 return !(this==other);
89 }
90
92 template<typename O, unsigned char s>
93 operator Vector<O,s>()
94 {
95 Vector<O,s> vec;
96 constexpr unsigned char minsize = (std::min)(s,i);
97 for (unsigned char j = 0; j < minsize; j++)
98 {
99 vec[j] = this[j];
100 }
101 }
102
104 std::array<T,i> GetAsArray() const
105 {
106 return std::array<T,i>(_values);
107 }
108
110 operator std::string() const
111 {
112 std::string str = "{";
113 for (unsigned char j = 0; j < i; j++)
114 {
115 str += std::to_string(_values[j]) + ",";
116 }
117 str.back() = '}';
118 return str;
119 }
120
123 {
124 _values = {};
125 }
126
128 Vector(std::initializer_list<T> il)
129 {
130 std::copy(il.begin(), il.end(), _values);
131 }
132
133 private:
135 T _values[i];
136
137 };
138}
139
140#define TP_VECTOR_TYPE_GEN_SOLO(size, type, typeInitial) \
141typedef TeaPacket::Math::Vector<type, size> Vector##size##typeInitial;
142
143#define TP_VECTOR_TYPE_GEN(vecsize) \
144TP_VECTOR_TYPE_GEN_SOLO(vecsize, float, ) \
145TP_VECTOR_TYPE_GEN_SOLO(vecsize, float, f) \
146TP_VECTOR_TYPE_GEN_SOLO(vecsize, double, d) \
147\
148TP_VECTOR_TYPE_GEN_SOLO(vecsize, signed char, sb) \
149TP_VECTOR_TYPE_GEN_SOLO(vecsize, char, b) \
150TP_VECTOR_TYPE_GEN_SOLO(vecsize, unsigned char, ub) \
151TP_VECTOR_TYPE_GEN_SOLO(vecsize, short, s) \
152TP_VECTOR_TYPE_GEN_SOLO(vecsize, signed short, ss) \
153TP_VECTOR_TYPE_GEN_SOLO(vecsize, unsigned short, us) \
154TP_VECTOR_TYPE_GEN_SOLO(vecsize, int, i) \
155TP_VECTOR_TYPE_GEN_SOLO(vecsize, signed int, si) \
156TP_VECTOR_TYPE_GEN_SOLO(vecsize, unsigned int, ui) \
157TP_VECTOR_TYPE_GEN_SOLO(vecsize, long, l) \
158TP_VECTOR_TYPE_GEN_SOLO(vecsize, signed long, sl) \
159TP_VECTOR_TYPE_GEN_SOLO(vecsize, unsigned long, ul) \
160TP_VECTOR_TYPE_GEN_SOLO(vecsize, long long, ll) \
161TP_VECTOR_TYPE_GEN_SOLO(vecsize, signed long long, sll) \
162TP_VECTOR_TYPE_GEN_SOLO(vecsize, unsigned long long, ull)
#define TP_VEC_MATH_OPERATOR(OP)
Definition Vector.hpp:12
#define TP_VEC_ALIAS(name, index)
Definition Vector.hpp:8
T & v()
Definition Vector.hpp:52
Vector()
Default initializer, setting all values to garbage data.
Definition Vector.hpp:122
T & y()
Definition Vector.hpp:42
T & g()
Definition Vector.hpp:47
T & a()
Definition Vector.hpp:49
T & r()
Definition Vector.hpp:46
bool operator!=(Vector< T, i > const &other) const
Definition Vector.hpp:86
T & b()
Definition Vector.hpp:48
std::array< T, i > GetAsArray() const
Get a copy of the Vector as a plain std::array.
Definition Vector.hpp:104
T & w()
Definition Vector.hpp:44
T & u()
Definition Vector.hpp:51
Vector(std::initializer_list< T > il)
Initializer list, sets all values to the given values in the list.
Definition Vector.hpp:128
T & z()
Definition Vector.hpp:43
T & x()
Definition Vector.hpp:41
Math related functions and classes for TeaPacket.
Definition Vector.hpp:33