src/PropertyMacro.h

changeset 642
751a8df42842
parent 641
425b169a82aa
child 643
a79277000830
equal deleted inserted replaced
641:425b169a82aa 642:751a8df42842
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013, 2014 Santeri Piippo
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #pragma once
20
21 // =============================================================================
22 //
23 // Identifier names
24 //
25 #define PROPERTY_SET_ACCESSOR(NAME) set##NAME
26 #define PROPERTY_GET_ACCESSOR(NAME) get##NAME
27 #define PROPERTY_IS_ACCESSOR(NAME) is##NAME // for bool types
28 #define PROPERTY_MEMBER_NAME(NAME) m_##NAME
29
30 // Names of operations
31 #define PROPERTY_APPEND_OPERATION(NAME) appendTo##NAME
32 #define PROPERTY_PREPEND_OPERATION(NAME) prependTo##NAME
33 #define PROPERTY_REPLACE_OPERATION(NAME) replaceIn##NAME
34 #define PROPERTY_INCREASE_OPERATION(NAME) increase##NAME
35 #define PROPERTY_DECREASE_OPERATION(NAME) decrease##NAME
36 #define PROPERTY_TOGGLE_OPERATION(NAME) toggle##NAME
37 #define PROPERTY_PUSH_OPERATION(NAME) pushTo##NAME
38 #define PROPERTY_REMOVE_OPERATION(NAME) removeFrom##NAME
39 #define PROPERTY_CLEAR_OPERATION(NAME) clear##NAME
40 #define PROPERTY_COUNT_OPERATION(NAME) count##NAME
41
42 // Operation definitions
43 // These are the methods of the list type that are called in the operations.
44 #define PROPERTY_APPEND_METHOD_NAME append // QString::append
45 #define PROPERTY_PREPEND_METHOD_NAME prepend // QString::prepend
46 #define PROPERTY_REPLACE_METHOD_NAME replace // QString::replace
47 #define PROPERTY_PUSH_METHOD_NAME append // QList<T>::append
48 #define PROPERTY_REMOVE_METHOD_NAME removeOne // QList<T>::removeOne
49 #define PROPERTY_CLEAR_METHOD_NAME clear // QList<T>::clear
50
51 // =============================================================================
52 //
53 // Main PROPERTY macro
54 //
55 #define PROPERTY(ACCESS, TYPE, NAME, OPS, WRITETYPE) \
56 private: \
57 TYPE PROPERTY_MEMBER_NAME(NAME); \
58 \
59 public: \
60 inline TYPE const& PROPERTY_GET_READ_METHOD (NAME, OPS) const \
61 { \
62 return PROPERTY_MEMBER_NAME(NAME); \
63 } \
64 \
65 ACCESS: \
66 PROPERTY_MAKE_WRITE (TYPE, NAME, WRITETYPE) \
67 PROPERTY_DEFINE_OPERATIONS (TYPE, NAME, OPS)
68
69 // =============================================================================
70 //
71 // PROPERTY_GET_READ_METHOD
72 //
73 // This macro uses the OPS argument to construct the name of the actual
74 // macro which returns the name of the get accessor. This is so that the
75 // bool properties get is<NAME>() accessors while non-bools get get<NAME>()
76 //
77 #define PROPERTY_GET_READ_METHOD(NAME, OPS) \
78 PROPERTY_GET_READ_METHOD_##OPS (NAME)
79
80 #define PROPERTY_GET_READ_METHOD_BOOL_OPS(NAME) PROPERTY_IS_ACCESSOR (NAME)()
81 #define PROPERTY_GET_READ_METHOD_NO_OPS(NAME) PROPERTY_GET_ACCESSOR (NAME)()
82 #define PROPERTY_GET_READ_METHOD_STR_OPS(NAME) PROPERTY_GET_ACCESSOR (NAME)()
83 #define PROPERTY_GET_READ_METHOD_NUM_OPS(NAME) PROPERTY_GET_ACCESSOR (NAME)()
84 #define PROPERTY_GET_READ_METHOD_LIST_OPS(NAME) PROPERTY_GET_ACCESSOR (NAME)()
85
86 // =============================================================================
87 //
88 // PROPERTY_MAKE_WRITE
89 //
90 // This macro uses the WRITETYPE argument to construct the set accessor of the
91 // property. If WRITETYPE is STOCK_WRITE, an inline method is defined to just
92 // set the new value of the property. If WRITETYPE is CUSTOM_WRITE, the accessor
93 // is merely declared and is left for the user to define.
94 //
95 #define PROPERTY_MAKE_WRITE(TYPE, NAME, WRITETYPE) \
96 PROPERTY_MAKE_WRITE_##WRITETYPE (TYPE, NAME)
97
98 #define PROPERTY_MAKE_WRITE_STOCK_WRITE(TYPE, NAME) \
99 inline void set##NAME (TYPE const& new##NAME) \
100 { \
101 PROPERTY_MEMBER_NAME(NAME) = new##NAME; \
102 }
103
104 #define PROPERTY_MAKE_WRITE_CUSTOM_WRITE(TYPE, NAME) \
105 void set##NAME (TYPE const& new##NAME); \
106
107 // =============================================================================
108 //
109 // PROPERTY_DEFINE_OPERATIONS
110 //
111 // This macro may expand into methods defining additional operations for the
112 // method.
113
114 #define PROPERTY_DEFINE_OPERATIONS(TYPE, NAME, OPS) \
115 DEFINE_PROPERTY_##OPS (TYPE, NAME)
116
117 // =============================================================================
118 //
119 // DEFINE_PROPERTY_NO_OPS
120 //
121 // Obviously NO_OPS expands into no operations.
122 //
123 #define DEFINE_PROPERTY_NO_OPS(TYPE, NAME)
124
125 // =============================================================================
126 //
127 // DEFINE_PROPERTY_STR_OPS
128 //
129 #define DEFINE_PROPERTY_STR_OPS(TYPE, NAME) \
130 void PROPERTY_APPEND_OPERATION(NAME) (const TYPE& a) \
131 { \
132 TYPE tmp (PROPERTY_MEMBER_NAME(NAME)); \
133 tmp.PROPERTY_APPEND_METHOD_NAME (a); \
134 set##NAME (tmp); \
135 } \
136 \
137 void PROPERTY_PREPEND_OPERATION(NAME) (const TYPE& a) \
138 { \
139 TYPE tmp (PROPERTY_MEMBER_NAME(NAME)); \
140 tmp.PROPERTY_PREPEND_METHOD_NAME (a); \
141 set##NAME (tmp); \
142 } \
143 \
144 void PROPERTY_REPLACE_OPERATION(NAME) (const TYPE& a, const TYPE& b) \
145 { \
146 TYPE tmp (PROPERTY_MEMBER_NAME(NAME)); \
147 tmp.PROPERTY_REPLACE_METHOD_NAME (a, b); \
148 set##NAME (tmp); \
149 }
150
151 // =============================================================================
152 //
153 // DEFINE_PROPERTY_NUM_OPS
154 //
155 #define DEFINE_PROPERTY_NUM_OPS(TYPE, NAME) \
156 inline void PROPERTY_INCREASE_OPERATION(NAME) (TYPE a = 1) \
157 { \
158 set##NAME (PROPERTY_MEMBER_NAME(NAME) + a); \
159 } \
160 \
161 inline void PROPERTY_DECREASE_OPERATION(NAME) (TYPE a = 1) \
162 { \
163 set##NAME (PROPERTY_MEMBER_NAME(NAME) - a); \
164 }
165
166 // =============================================================================
167 //
168 // DEFINE_PROPERTY_BOOL_OPS
169 //
170 #define DEFINE_PROPERTY_BOOL_OPS(TYPE, NAME) \
171 inline void PROPERTY_TOGGLE_OPERATION(NAME)() \
172 { \
173 set##NAME (!PROPERTY_MEMBER_NAME(NAME)); \
174 }
175
176 // =============================================================================
177 //
178 // DEFINE_PROPERTY_LIST_OPS
179 //
180 #define DEFINE_PROPERTY_LIST_OPS(TYPE, NAME) \
181 void PROPERTY_PUSH_OPERATION(NAME) (const TYPE::value_type& a) \
182 { \
183 PROPERTY_MEMBER_NAME(NAME).PROPERTY_PUSH_METHOD_NAME (a); \
184 } \
185 \
186 void PROPERTY_REMOVE_OPERATION(NAME) (const TYPE::value_type& a) \
187 { \
188 PROPERTY_MEMBER_NAME(NAME).PROPERTY_REMOVE_METHOD_NAME (a); \
189 } \
190 \
191 inline void PROPERTY_CLEAR_OPERATION(NAME)() \
192 { \
193 PROPERTY_MEMBER_NAME(NAME).PROPERTY_CLEAR_METHOD_NAME(); \
194 } \
195 \
196 public: \
197 inline int PROPERTY_COUNT_OPERATION(NAME)() const \
198 { \
199 return PROPERTY_GET_ACCESSOR (NAME)().size(); \
200 }

mercurial