src/linetypes/propertygenerics.h

Sat, 05 Mar 2022 13:18:28 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Mar 2022 13:18:28 +0200
changeset 160
536efae7137e
parent 117
121a40d5e34c
child 186
922662adb72a
permissions
-rw-r--r--

Add document closing

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

mercurial