basic types non-arithmetic T support

This commit is contained in:
BlackMATov
2021-02-21 00:50:38 +07:00
parent e3cefef78d
commit 347c721a7f
4 changed files with 111 additions and 82 deletions

View File

@@ -85,7 +85,8 @@ class vec_base;
template < typename T > template < typename T >
class vec_base<T, 2> { class vec_base<T, 2> {
public: public:
T x{}, y{}; T x = T{0};
T y = T{0};
constexpr vec_base() = default; constexpr vec_base() = default;
constexpr explicit vec_base(T v); constexpr explicit vec_base(T v);
@@ -97,7 +98,9 @@ public:
template < typename T > template < typename T >
class vec_base<T, 3> { class vec_base<T, 3> {
public: public:
T x{}, y{}, z{}; T x = T{0};
T y = T{0};
T z = T{0};
constexpr vec_base() = default; constexpr vec_base() = default;
constexpr explicit vec_base(T v); constexpr explicit vec_base(T v);
@@ -110,7 +113,10 @@ public:
template < typename T > template < typename T >
class vec_base<T, 4> { class vec_base<T, 4> {
public: public:
T x{}, y{}, z{}, w{}; T x = T{0};
T y = T{0};
T z = T{0};
T w = T{0};
constexpr vec_base() = default; constexpr vec_base() = default;
constexpr explicit vec_base(T v); constexpr explicit vec_base(T v);
@@ -142,6 +148,10 @@ public:
static constexpr size_t size = Size; static constexpr size_t size = Size;
constexpr vec() = default;
constexpr vec(const vec&) = default;
constexpr vec& operator=(const vec&) = default;
void swap(vec& other) noexcept(is_nothrow_swappable_v<T>); void swap(vec& other) noexcept(is_nothrow_swappable_v<T>);
iterator begin() noexcept; iterator begin() noexcept;
@@ -210,8 +220,8 @@ public:
using row_type = vec<T, 2>; using row_type = vec<T, 2>;
row_type rows[2] = { row_type rows[2] = {
{1, 0}, row_type{T{1}, T{0}},
{0, 1}}; row_type{T{0}, T{1}}};
constexpr mat_base() = default; constexpr mat_base() = default;
constexpr explicit mat_base(T d); constexpr explicit mat_base(T d);
@@ -235,9 +245,9 @@ public:
using row_type = vec<T, 3>; using row_type = vec<T, 3>;
row_type rows[3] = { row_type rows[3] = {
{1, 0, 0}, row_type{T{1}, T{0}, T{0}},
{0, 1, 0}, row_type{T{0}, T{1}, T{0}},
{0, 0, 1}}; row_type{T{0}, T{0}, T{1}}};
constexpr mat_base() = default; constexpr mat_base() = default;
constexpr explicit mat_base(T d); constexpr explicit mat_base(T d);
@@ -267,10 +277,10 @@ public:
using row_type = vec<T, 4>; using row_type = vec<T, 4>;
row_type rows[4] = { row_type rows[4] = {
{1, 0, 0, 0}, row_type{T{1}, T{0}, T{0}, T{0}},
{0, 1, 0, 0}, row_type{T{0}, T{1}, T{0}, T{0}},
{0, 0, 1, 0}, row_type{T{0}, T{0}, T{1}, T{0}},
{0, 0, 0, 1}}; row_type{T{0}, T{0}, T{0}, T{1}}};
constexpr mat_base() = default; constexpr mat_base() = default;
constexpr explicit mat_base(T d); constexpr explicit mat_base(T d);
@@ -315,6 +325,10 @@ public:
static constexpr size_t size = Size; static constexpr size_t size = Size;
constexpr mat() = default;
constexpr mat(const mat&) = default;
constexpr mat& operator=(const mat&) = default;
void swap(mat& other) noexcept(is_nothrow_swappable_v<T>); void swap(mat& other) noexcept(is_nothrow_swappable_v<T>);
iterator begin() noexcept; iterator begin() noexcept;
@@ -377,8 +391,11 @@ using ptrdiff4x4_t = mat<ptrdiff_t, 4>;
template < typename T > template < typename T >
class qua final { class qua final {
public: public:
vec<T, 3> v{0}; using imag_type = vec<T, 3>;
T s{1}; using real_type = T;
imag_type v = imag_type{T{0}};
real_type s = real_type{T{1}};
public: public:
using self_type = qua; using self_type = qua;
using component_type = T; using component_type = T;

View File

@@ -20,21 +20,22 @@ namespace vmath_hpp::detail
class mat_base<T, 2> { class mat_base<T, 2> {
public: public:
using row_type = vec<T, 2>; using row_type = vec<T, 2>;
row_type rows[2] = { row_type rows[2] = {
{1, 0}, row_type{T{1}, T{0}},
{0, 1}}; row_type{T{0}, T{1}}};
public: public:
constexpr mat_base() = default; constexpr mat_base() = default;
constexpr explicit mat_base(T d) constexpr explicit mat_base(T d)
: rows{ : rows{
row_type{d, 0}, row_type{d, T{0}},
row_type{0, d}} {} row_type{T{0}, d}} {}
constexpr explicit mat_base(const row_type& d) constexpr explicit mat_base(const row_type& d)
: rows{ : rows{
row_type{d[0], 0}, row_type{d[0], T{0}},
row_type{0, d[1]}} {} row_type{T{0}, d[1]}} {}
constexpr mat_base( constexpr mat_base(
T m11, T m12, T m11, T m12,
@@ -65,24 +66,25 @@ namespace vmath_hpp::detail
class mat_base<T, 3> { class mat_base<T, 3> {
public: public:
using row_type = vec<T, 3>; using row_type = vec<T, 3>;
row_type rows[3] = { row_type rows[3] = {
{1, 0, 0}, row_type{T{1}, T{0}, T{0}},
{0, 1, 0}, row_type{T{0}, T{1}, T{0}},
{0, 0, 1}}; row_type{T{0}, T{0}, T{1}}};
public: public:
constexpr mat_base() = default; constexpr mat_base() = default;
constexpr explicit mat_base(T d) constexpr explicit mat_base(T d)
: rows{ : rows{
row_type{d, 0, 0}, row_type{d, T{0}, T{0}},
row_type{0, d, 0}, row_type{T{0}, d, T{0}},
row_type{0, 0, d}} {} row_type{T{0}, T{0}, d}} {}
constexpr explicit mat_base(const row_type& d) constexpr explicit mat_base(const row_type& d)
: rows{ : rows{
row_type{d[0], 0, 0}, row_type{d[0], T{0}, T{0}},
row_type{0, d[1], 0}, row_type{T{0}, d[1], T{0}},
row_type{0, 0, d[2]}} {} row_type{T{0}, T{0}, d[2]}} {}
constexpr mat_base( constexpr mat_base(
T m11, T m12, T m13, T m11, T m12, T m13,
@@ -103,16 +105,16 @@ namespace vmath_hpp::detail
const mat_base<T, 2>& m, const mat_base<T, 2>& m,
const vec_base<T, 2>& v) const vec_base<T, 2>& v)
: rows{ : rows{
row_type{m.rows[0], 0}, row_type{m.rows[0], T{0}},
row_type{m.rows[1], 0}, row_type{m.rows[1], T{0}},
row_type{v, 1}} {} row_type{v, T{1}}} {}
constexpr explicit mat_base( constexpr explicit mat_base(
const mat_base<T, 2>& other) const mat_base<T, 2>& other)
: rows{ : rows{
row_type{other.rows[0], 0}, row_type{other.rows[0], T{0}},
row_type{other.rows[1], 0}, row_type{other.rows[1], T{0}},
row_type{0, 0, 1}} {} row_type{T{0}, T{0}, T{1}}} {}
constexpr explicit mat_base( constexpr explicit mat_base(
const mat_base<T, 4>& other) const mat_base<T, 4>& other)
@@ -126,27 +128,28 @@ namespace vmath_hpp::detail
class mat_base<T, 4> { class mat_base<T, 4> {
public: public:
using row_type = vec<T, 4>; using row_type = vec<T, 4>;
row_type rows[4] = { row_type rows[4] = {
{1, 0, 0, 0}, row_type{T{1}, T{0}, T{0}, T{0}},
{0, 1, 0, 0}, row_type{T{0}, T{1}, T{0}, T{0}},
{0, 0, 1, 0}, row_type{T{0}, T{0}, T{1}, T{0}},
{0, 0, 0, 1}}; row_type{T{0}, T{0}, T{0}, T{1}}};
public: public:
constexpr mat_base() = default; constexpr mat_base() = default;
constexpr explicit mat_base(T d) constexpr explicit mat_base(T d)
: rows{ : rows{
row_type{d, 0, 0, 0}, row_type{d, T{0}, T{0}, T{0}},
row_type{0, d, 0, 0}, row_type{T{0}, d, T{0}, T{0}},
row_type{0, 0, d, 0}, row_type{T{0}, T{0}, d, T{0}},
row_type{0, 0, 0, d}} {} row_type{T{0}, T{0}, T{0}, d}} {}
constexpr explicit mat_base(const row_type& d) constexpr explicit mat_base(const row_type& d)
: rows{ : rows{
row_type{d[0], 0, 0, 0}, row_type{d[0], T{0}, T{0}, T{0}},
row_type{0, d[1], 0, 0}, row_type{T{0}, d[1], T{0}, T{0}},
row_type{0, 0, d[2], 0}, row_type{T{0}, T{0}, d[2], T{0}},
row_type{0, 0, 0, d[3]}} {} row_type{T{0}, T{0}, T{0}, d[3]}} {}
constexpr mat_base( constexpr mat_base(
T m11, T m12, T m13, T m14, T m11, T m12, T m13, T m14,
@@ -170,26 +173,26 @@ namespace vmath_hpp::detail
const mat_base<T, 3>& m, const mat_base<T, 3>& m,
const vec_base<T, 3>& v) const vec_base<T, 3>& v)
: rows{ : rows{
row_type{m.rows[0], 0}, row_type{m.rows[0], T{0}},
row_type{m.rows[1], 0}, row_type{m.rows[1], T{0}},
row_type{m.rows[2], 0}, row_type{m.rows[2], T{0}},
row_type{v, 1}} {} row_type{v, T{1}}} {}
constexpr explicit mat_base( constexpr explicit mat_base(
const mat_base<T, 2>& other) const mat_base<T, 2>& other)
: rows{ : rows{
row_type{other.rows[0], 0, 0}, row_type{other.rows[0], T{0}, T{0}},
row_type{other.rows[1], 0, 0}, row_type{other.rows[1], T{0}, T{0}},
row_type{0, 0, 1, 0}, row_type{T{0}, T{0}, T{1}, T{0}},
row_type{0, 0, 0, 1}} {} row_type{T{0}, T{0}, T{0}, T{1}}} {}
constexpr explicit mat_base( constexpr explicit mat_base(
const mat_base<T, 3>& other) const mat_base<T, 3>& other)
: rows{ : rows{
row_type{other.rows[0], 0}, row_type{other.rows[0], T{0}},
row_type{other.rows[1], 0}, row_type{other.rows[1], T{0}},
row_type{other.rows[2], 0}, row_type{other.rows[2], T{0}},
row_type{0, 0, 0, 1}} {} row_type{T{0}, T{0}, T{0}, T{1}}} {}
}; };
} }
@@ -253,14 +256,6 @@ namespace vmath_hpp
return &rows[0]; return &rows[0];
} }
[[nodiscard]] constexpr reference operator[](std::size_t index) noexcept {
return rows[index];
}
[[nodiscard]] constexpr const_reference operator[](std::size_t index) const noexcept {
return rows[index];
}
[[nodiscard]] constexpr reference at(std::size_t index) { [[nodiscard]] constexpr reference at(std::size_t index) {
VMATH_HPP_THROW_IF(index >= size, std::out_of_range("mat::at")); VMATH_HPP_THROW_IF(index >= size, std::out_of_range("mat::at"));
return rows[index]; return rows[index];
@@ -270,6 +265,14 @@ namespace vmath_hpp
VMATH_HPP_THROW_IF(index >= size, std::out_of_range("mat::at")); VMATH_HPP_THROW_IF(index >= size, std::out_of_range("mat::at"));
return rows[index]; return rows[index];
} }
[[nodiscard]] constexpr reference operator[](std::size_t index) noexcept {
return rows[index];
}
[[nodiscard]] constexpr const_reference operator[](std::size_t index) const noexcept {
return rows[index];
}
}; };
} }

View File

@@ -16,8 +16,11 @@ namespace vmath_hpp
template < typename T > template < typename T >
class qua final { class qua final {
public: public:
vec<T, 3> v{0}; using imag_type = vec<T, 3>;
T s{1}; using real_type = T;
imag_type v = imag_type{T{0}};
real_type s = real_type{T{1}};
public: public:
using self_type = qua; using self_type = qua;
using component_type = T; using component_type = T;
@@ -85,6 +88,16 @@ namespace vmath_hpp
return &(*this)[0]; return &(*this)[0];
} }
[[nodiscard]] constexpr reference at(std::size_t index) {
VMATH_HPP_THROW_IF(index >= size, std::out_of_range("qua::at"));
return (*this)[index];
}
[[nodiscard]] constexpr const_reference at(std::size_t index) const {
VMATH_HPP_THROW_IF(index >= size, std::out_of_range("qua::at"));
return (*this)[index];
}
[[nodiscard]] constexpr reference operator[](std::size_t index) noexcept { [[nodiscard]] constexpr reference operator[](std::size_t index) noexcept {
switch ( index ) { switch ( index ) {
default: default:
@@ -104,16 +117,6 @@ namespace vmath_hpp
case 3: return s; case 3: return s;
} }
} }
[[nodiscard]] constexpr reference at(std::size_t index) {
VMATH_HPP_THROW_IF(index >= size, std::out_of_range("qua::at"));
return (*this)[index];
}
[[nodiscard]] constexpr const_reference at(std::size_t index) const {
VMATH_HPP_THROW_IF(index >= size, std::out_of_range("qua::at"));
return (*this)[index];
}
}; };
} }

View File

@@ -16,7 +16,8 @@ namespace vmath_hpp::detail
template < typename T > template < typename T >
class vec_base<T, 2> { class vec_base<T, 2> {
public: public:
T x{}, y{}; T x = T{0};
T y = T{0};
public: public:
constexpr vec_base() = default; constexpr vec_base() = default;
@@ -52,7 +53,9 @@ namespace vmath_hpp::detail
template < typename T > template < typename T >
class vec_base<T, 3> { class vec_base<T, 3> {
public: public:
T x{}, y{}, z{}; T x = T{0};
T y = T{0};
T z = T{0};
public: public:
constexpr vec_base() = default; constexpr vec_base() = default;
@@ -93,7 +96,10 @@ namespace vmath_hpp::detail
template < typename T > template < typename T >
class vec_base<T, 4> { class vec_base<T, 4> {
public: public:
T x{}, y{}, z{}, w{}; T x = T{0};
T y = T{0};
T z = T{0};
T w = T{0};
public: public:
constexpr vec_base() = default; constexpr vec_base() = default;