src/basics.h

changeset 218
63125c36de73
parent 214
8e1fe64ce4e3
child 235
7ef03c2b46ab
equal deleted inserted replaced
217:6d95c1a41e6e 218:63125c36de73
35 #include <QVariant> 35 #include <QVariant>
36 #include <QVector> 36 #include <QVector>
37 #include <glm/glm.hpp> 37 #include <glm/glm.hpp>
38 #include "geometry.h" 38 #include "geometry.h"
39 #include "functional.h" 39 #include "functional.h"
40 #include "typeconversions.h"
40 41
41 template<typename T> 42 template<typename T>
42 using opt = std::optional<T>; 43 using opt = std::optional<T>;
43 44
44 Q_DECLARE_METATYPE(glm::vec3) 45 Q_DECLARE_METATYPE(glm::vec3)
47 //! \brief count the amount of elements in a basic array 48 //! \brief count the amount of elements in a basic array
48 template<typename T, int N> 49 template<typename T, int N>
49 constexpr int countof(T(&)[N]) 50 constexpr int countof(T(&)[N])
50 { 51 {
51 return N; 52 return N;
52 }
53
54 //! \brief casts @c x to a suitable unsigned integer
55 template<typename T>
56 constexpr auto unsigned_cast(T x)
57 -> std::enable_if_t<std::is_integral_v<T>, std::make_unsigned_t<T>>
58 {
59 return static_cast<std::make_unsigned_t<T>>(x);
60 }
61
62 //! \brief casts @c x to a suitable signed integer
63 template<typename T>
64 constexpr auto signed_cast(T x)
65 -> std::enable_if_t<std::is_integral_v<T>, std::make_signed_t<T>>
66 {
67 return static_cast<std::make_signed_t<T>>(x);
68 }
69
70 //! \brief casts floating point values to float
71 template<typename T>
72 constexpr auto float_cast(T x)
73 -> std::enable_if_t<std::is_floating_point_v<T>, float>
74 {
75 return static_cast<float>(x);
76 }
77
78 //! \brief casts floating point values to double
79 template<typename T>
80 constexpr auto double_cast(T x)
81 -> std::enable_if_t<std::is_floating_point_v<T>, double>
82 {
83 return static_cast<double>(x);
84 }
85
86 //! \brief casts floating point values to qreal
87 template<typename T>
88 constexpr auto qreal_cast(T x)
89 -> std::enable_if_t<std::is_floating_point_v<T>, qreal>
90 {
91 return static_cast<qreal>(x);
92 } 53 }
93 54
94 template<int N, typename T, glm::qualifier Q> 55 template<int N, typename T, glm::qualifier Q>
95 constexpr QPointF toQPointF(const glm::vec<N, T, Q>& vec) 56 constexpr QPointF toQPointF(const glm::vec<N, T, Q>& vec)
96 { 57 {

mercurial