src/typeconversions.h

Mon, 20 Jun 2022 19:49:56 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Mon, 20 Jun 2022 19:49:56 +0300
changeset 237
10a6298f636f
parent 218
63125c36de73
child 250
2837b549e616
permissions
-rw-r--r--

Add an option to log opengl messages

218
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
1 #ifndef TYPECONVERSIONS_H
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
2 #define TYPECONVERSIONS_H
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
3 #include <type_traits>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
4 #include <QtGlobal>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
5
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
6 template<typename T, typename R>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
7 struct transfer_cvref
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
8 {
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
9 using type = std::remove_reference_t<R>;
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
10 };
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
11
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
12 template<typename T, typename R>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
13 struct transfer_cvref<T&, R>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
14 {
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
15 using type = std::remove_reference_t<R>&;
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
16 };
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
17
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
18 template<typename T, typename R>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
19 struct transfer_cvref<const T&, R>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
20 {
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
21 using type = const std::remove_reference_t<R>&;
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
22 };
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
23
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
24 template<typename T, typename R>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
25 struct transfer_cvref<T&&, R>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
26 {
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
27 using type = std::remove_reference_t<R>&&;
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
28 };
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
29
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
30 //! \brief transfer l-value reference, r-value reference and const l-value
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
31 //! reference from T onto R
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
32 template<typename T, typename R>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
33 using transfer_cvref_t = typename transfer_cvref<T, R>::type;
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
34 static_assert(std::is_same_v<transfer_cvref_t<int, char>, char>);
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
35 static_assert(std::is_same_v<transfer_cvref_t<int&, char>, char&>);
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
36 static_assert(std::is_same_v<transfer_cvref_t<int&&, char>, char&&>);
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
37
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
38 //! \brief casts @c x to a suitable unsigned integer
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
39 template<typename T>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
40 constexpr auto unsigned_cast(T x)
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
41 -> std::enable_if_t<std::is_integral_v<T>, std::make_unsigned_t<T>>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
42 {
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
43 return static_cast<std::make_unsigned_t<T>>(x);
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
44 }
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
45
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
46 //! \brief casts @c x to a suitable signed integer
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
47 template<typename T>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
48 constexpr auto signed_cast(T x)
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
49 -> std::enable_if_t<std::is_integral_v<T>, std::make_signed_t<T>>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
50 {
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
51 return static_cast<std::make_signed_t<T>>(x);
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
52 }
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
53
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
54 //! \brief casts floating point values to float
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
55 template<typename T>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
56 constexpr auto float_cast(T x)
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
57 -> std::enable_if_t<std::is_floating_point_v<T>, float>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
58 {
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
59 return static_cast<float>(x);
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
60 }
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
61
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
62 //! \brief casts floating point values to double
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
63 template<typename T>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
64 constexpr auto double_cast(T x)
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
65 -> std::enable_if_t<std::is_floating_point_v<T>, double>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
66 {
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
67 return static_cast<double>(x);
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
68 }
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
69
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
70 //! \brief casts floating point values to qreal
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
71 template<typename T>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
72 constexpr auto qreal_cast(T x)
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
73 -> std::enable_if_t<std::is_floating_point_v<T>, qreal>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
74 {
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
75 return static_cast<qreal>(x);
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
76 }
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
77
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
78 //! \brief convert an enum value to its corresponding integer value (including
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
79 //! references)
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
80 template<typename T>
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
81 constexpr auto&& enum_value_cast(T&& enu)
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
82 {
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
83 using valuetype = std::underlying_type_t<std::remove_cvref_t<T>>;
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
84 using reftype = transfer_cvref_t<T&&, valuetype>;
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
85 return reinterpret_cast<reftype>(enu);
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
86 }
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
87
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff changeset
88 #endif // TYPECONVERSIONS_H

mercurial