Mon, 26 Jul 2021 11:50:45 +0300
refactor
89 | 1 | #pragma once |
2 | #include "main.h" | |
3 | #include "colors.h" | |
4 | ||
5 | class Vec3Editor; | |
6 | class MatrixEditor; | |
7 | ||
8 | namespace ldraw | |
9 | { | |
10 | enum class Property; | |
11 | struct PropertyKeyValue; | |
12 | template<Property property> | |
13 | struct PropertyTraits | |
14 | { | |
15 | static constexpr bool defined = false; | |
16 | }; | |
17 | } | |
18 | ||
19 | /** | |
20 | * Different properties | |
21 | */ | |
22 | enum class ldraw::Property | |
23 | { | |
24 | Color, // Color of the object | |
25 | Text, // Text contained in a comment | |
26 | Point0, // First vertex in a polygon or edge line | |
27 | Point1, // Second vertex in a polygon or edge line | |
28 | Point2, // Third vertex in a polygon | |
29 | Point3, // Fourth vertex in a quadrilateral | |
30 | Transformation, // 4x4 transformation matrix of a subfile reference | |
31 | ReferenceName, // Subfile reference name | |
32 | IsInverted, // Whether or not the object has been inverted with BFC INVERTNEXT | |
33 | ErrorMessage // For error lines, why parsing failed | |
34 | }; | |
35 | ||
36 | Q_DECLARE_METATYPE(ldraw::Property) | |
37 | ||
38 | // Mapping of properties to types | |
39 | #define LDFORGE_DEFINE_PROPERTY_TYPE(PROPERTY, TYPE) \ | |
40 | namespace ldraw \ | |
41 | { \ | |
42 | template<> struct PropertyTraits<ldraw::Property::PROPERTY> \ | |
43 | { \ | |
44 | using type = TYPE; \ | |
45 | static constexpr std::array<char, 256> name{#PROPERTY}; \ | |
46 | static constexpr bool defined = true; \ | |
47 | }; \ | |
48 | } | |
49 | ||
50 | LDFORGE_DEFINE_PROPERTY_TYPE(Color, ldraw::Color) | |
51 | LDFORGE_DEFINE_PROPERTY_TYPE(Text, QString) | |
52 | LDFORGE_DEFINE_PROPERTY_TYPE(Point0, glm::vec3) | |
53 | LDFORGE_DEFINE_PROPERTY_TYPE(Point1, glm::vec3) | |
54 | LDFORGE_DEFINE_PROPERTY_TYPE(Point2, glm::vec3) | |
55 | LDFORGE_DEFINE_PROPERTY_TYPE(Point3, glm::vec3) | |
56 | LDFORGE_DEFINE_PROPERTY_TYPE(Transformation, glm::mat4) | |
57 | LDFORGE_DEFINE_PROPERTY_TYPE(ReferenceName, QString) | |
58 | LDFORGE_DEFINE_PROPERTY_TYPE(IsInverted, bool) | |
59 | LDFORGE_DEFINE_PROPERTY_TYPE(ErrorMessage, QString) | |
60 | ||
61 | #define LDRAW_OBJECT_HANDLE_SET_PROPERTY(PROPERTY, HANDLER) \ | |
62 | {this->handle<ldraw::Property::PROPERTY>(result, pair, \ | |
63 | [&](const ldraw::PropertyType<ldraw::Property::PROPERTY>& value) HANDLER);} | |
64 | ||
65 | // Generics | |
66 | namespace ldraw | |
67 | { | |
68 | template<ldraw::Property property> | |
69 | using PropertyType = typename PropertyTraits<property>::type; | |
70 | ||
71 | template<ldraw::Property property> | |
72 | inline const char* PROPERTY_NAME = PropertyTraits<property>::name; | |
73 | ||
74 | struct PropertyKeyValue | |
75 | { | |
76 | Property key; | |
77 | QVariant value; | |
78 | }; | |
79 | ||
80 | constexpr Property pointProperty(int n) | |
81 | { | |
82 | Q_ASSERT(n >= 0 and n < 4); | |
83 | return static_cast<Property>(static_cast<int>(Property::Point0) + n); | |
84 | } | |
85 | ||
86 | struct PropertyTrait | |
87 | { | |
88 | ldraw::Property property; | |
89 | std::array<char, 256> name; | |
90 | int type; | |
91 | }; | |
92 | ||
93 | namespace detail | |
94 | { | |
95 | template<int N> | |
96 | constexpr int propertyCountHelper() | |
97 | { | |
98 | if constexpr (ldraw::PropertyTraits<static_cast<Property>(N)>::defined) | |
99 | { | |
100 | return propertyCountHelper<N + 1>(); | |
101 | } | |
102 | else | |
103 | { | |
104 | return N; | |
105 | } | |
106 | } | |
107 | ||
108 | template<int k> | |
109 | constexpr PropertyTrait getPropertyTrait() | |
110 | { | |
111 | constexpr auto property = static_cast<ldraw::Property>(k); | |
112 | using trait = ldraw::PropertyTraits<property>; | |
113 | return PropertyTrait{ | |
114 | property, | |
115 | trait::name, | |
116 | qMetaTypeId<typename trait::type>() | |
117 | }; | |
118 | } | |
119 | ||
120 | template<int... Ints> | |
121 | auto getPropertyTraits(std::integer_sequence<int, Ints...>) | |
122 | { | |
123 | return std::array<PropertyTrait, sizeof...(Ints)>{getPropertyTrait<Ints>()...}; | |
124 | } | |
125 | } | |
126 | ||
127 | constexpr int NUM_PROPERTIES = detail::propertyCountHelper<0>(); | |
128 | inline const auto& traits() | |
129 | { | |
130 | static std::array<PropertyTrait, NUM_PROPERTIES> result = | |
131 | detail::getPropertyTraits(std::make_integer_sequence<int, NUM_PROPERTIES>()); | |
132 | return result; | |
133 | } | |
134 | ||
135 | inline const auto& traits(ldraw::Property property) | |
136 | { | |
137 | return traits()[static_cast<int>(property)]; | |
138 | } | |
139 | ||
140 | template<typename T, std::size_t... Ints> | |
141 | constexpr auto makeIndexArray(std::index_sequence<Ints...>) | |
142 | { | |
143 | return std::array{static_cast<T>(Ints)...}; | |
144 | } | |
145 | ||
146 | constexpr auto ALL_PROPERTIES = makeIndexArray<Property>(std::make_index_sequence<NUM_PROPERTIES>{}); | |
147 | ||
148 | template<typename T> | |
149 | bool testPropertyType(ldraw::Property property) | |
150 | { | |
151 | return qMetaTypeId<T>() == ldraw::traits(property).type; | |
152 | } | |
153 | } |