Debian Codegraph @

src/App/FeatureTest.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2002 Jürgen Riegel <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #include <boost/core/ignore_unused.hpp>
25 #include <sstream>
26
27 #include <Base/Console.h>
28 #include <Base/Exception.h>
29 #include <Base/Interpreter.h>
30 #include <Base/Unit.h>
31 #include <CXX/Objects.hxx>
32
33 #include "FeatureTest.h"
34 #include "Material.h"
35 #include "Range.h"
36
37 #ifdef _MSC_VER
38 #pragma warning(disable : 4700)
39 #pragma warning(disable : 4723)
40 #endif
41
42 using namespace App;
43
44
45 PROPERTY_SOURCE(App::FeatureTest, App::DocumentObject)
46
47 const char* enums[] = {"Zero", "One", "Two", "Three", "Four", nullptr};
48 const PropertyIntegerConstraint::Constraints intPercent = {0, 100, 1};
49 const PropertyFloatConstraint::Constraints floatPercent = {0.0, 100.0, 1.0};
50
51
52 FeatureTest::FeatureTest()
53 {
54 // clang-format off
55 ADD_PROPERTY(Integer, (4711));
56 ADD_PROPERTY(Float, (47.11f));
57 ADD_PROPERTY(Bool, (true));
58 ADD_PROPERTY(BoolList, (false));
59 ADD_PROPERTY(String, ("4711"));
60 ADD_PROPERTY(Path, ("c:\\temp"));
61 ADD_PROPERTY(StringList, ("4711"));
62
63 ADD_PROPERTY(Enum, (4));
64 Enum.setEnums(enums);
65 ADD_PROPERTY(ConstraintInt, (5));
66 ConstraintInt.setConstraints(&intPercent);
67 ADD_PROPERTY(ConstraintFloat, (5.0));
68 ConstraintFloat.setConstraints(&floatPercent);
69
70 Base::Color c;
71 App::Material mat(App::Material::GOLD);
72 ADD_PROPERTY(Colour, (c));
73 ADD_PROPERTY(ColourList, (c));
74 ADD_PROPERTY(Material, (mat));
75 ADD_PROPERTY(MaterialList, (mat));
76
77 ADD_PROPERTY(Distance, (47.11f));
78 ADD_PROPERTY(Angle, (3.0f));
79
80 ADD_PROPERTY(IntegerList, (4711));
81 ADD_PROPERTY(FloatList, (47.11f));
82
83 ADD_PROPERTY(Link, (nullptr));
84 ADD_PROPERTY(LinkSub, (nullptr));
85 ADD_PROPERTY(LinkList, (nullptr));
86 ADD_PROPERTY(LinkSubList, (nullptr));
87
88 ADD_PROPERTY(Vector, (1.0, 2.0, 3.0));
89 ADD_PROPERTY(VectorList, (3.0, 2.0, 1.0));
90 ADD_PROPERTY(Matrix,
91 (Base::Matrix4D( 1.0, 2.0, 3.0, 4.0,
92 5.0, 6.0, 7.0, 8.0,
93 9.0, 10.0, 11.0, 12.0,
94 13.0, 14.0, 15.0, 16.0)));
95 ADD_PROPERTY(Placement, (Base::Placement()));
96
97 // properties for recompute testing
98 static const char* group = "Feature Test";
99 ADD_PROPERTY_TYPE(Source1, (nullptr), group, Prop_None, "Source for testing links");
100 ADD_PROPERTY_TYPE(Source2, (nullptr), group, Prop_None, "Source for testing links");
101 ADD_PROPERTY_TYPE(SourceN, (nullptr), group, Prop_None, "Source for testing links");
102 ADD_PROPERTY_TYPE(ExecResult, ("empty"), group, Prop_None, "Result of the execution");
103 ADD_PROPERTY_TYPE(ExceptionType,
104 (0),
105 group,
106 Prop_None,
107 "The type of exception the execution method throws");
108 ADD_PROPERTY_TYPE(ExecCount, (0), group, Prop_None, "Number of executions");
109
110 // properties with types
111 ADD_PROPERTY_TYPE(TypeHidden,
112 (4711),
113 group,
114 Prop_Hidden,
115 "An example property which has the type 'Hidden'");
116 ADD_PROPERTY_TYPE(TypeReadOnly,
117 (4711),
118 group,
119 Prop_ReadOnly,
120 "An example property which has the type 'ReadOnly'");
121 ADD_PROPERTY_TYPE(TypeOutput,
122 (4711),
123 group,
124 Prop_Output,
125 "An example property which has the type 'Output'");
126 ADD_PROPERTY_TYPE(TypeTransient,
127 (4711),
128 group,
129 Prop_Transient,
130 "An example property which has the type 'Transient'");
131 ADD_PROPERTY_TYPE(TypeNoRecompute,
132 (4711),
133 group,
134 Prop_NoRecompute,
135 "An example property which has the type 'NoRecompute'");
136 ADD_PROPERTY_TYPE(TypeAll,
137 (4711),
138 group,
139 (App::PropertyType)(Prop_Output | Prop_ReadOnly | Prop_Hidden),
140 "An example property which has the types 'Output', 'ReadOnly' and 'Hidden'");
141
142 ADD_PROPERTY(QuantityLength, (1.0));
143 QuantityLength.setUnit(Base::Unit::Length);
144 ADD_PROPERTY(QuantityOther, (5.0));
145 QuantityOther.setUnit(Base::Unit::Density);
146 // clang-format on
147 }
148
149 FeatureTest::~FeatureTest() = default;
150
151 short FeatureTest::mustExecute() const
152 {
153 return DocumentObject::mustExecute();
154 }
155
156 DocumentObjectExecReturn* FeatureTest::execute()
157 {
158 // Enum handling
159 Enumeration enumObj1 = Enum.getEnum();
160 enumObj1.setValue(7, false);
161 enumObj1.setValue(4, true);
162
163 Enumeration enumObj2 = Enum.getEnum();
164 enumObj2.setValue(4, true);
165
166 Enumeration enumObj3(enumObj2);
167 const char* val = enumObj3.getCStr();
168 enumObj3.isValue(val);
169 enumObj3.getEnumVector();
170
171 Enumeration enumObj4("Single item");
172 enumObj4.setEnums(enums);
173 boost::ignore_unused(enumObj4 == enumObj2);
174 enumObj4.setEnums(nullptr);
175 enumObj4 = enumObj2;
176 boost::ignore_unused(enumObj4 == enumObj4.getCStr());
177
178 Enumeration enumObj5(enums, enums[3]);
179 enumObj5.isValue(enums[2]);
180 enumObj5.isValue(enums[3]);
181 enumObj5.contains(enums[1]);
182
183 Enumeration enumObj6;
184 enumObj6.setEnums(enums);
185 enumObj6.setValue(enums[1]);
186 std::vector<std::string> list;
187 list.emplace_back("Hello");
188 list.emplace_back("World");
189 enumObj6.setEnums(list);
190 enumObj6.setValue(list.back());
191
192 int *i = nullptr, j;
193 float f;
194 void* s;
195 std::string t;
196
197 // Code analyzers may complain about some errors. This can be ignored
198 // because this is done on purpose to test the error handling mechanism
199 switch (ExceptionType.getValue()) {
200 case 0:
201 break;
202 case 1:
203 throw std::runtime_error("Test Exception");
204 case 2:
205 throw Base::RuntimeError("FeatureTestException::execute(): Testexception");
206 default:
207 (void)i;
208 (void)j;
209 (void)f;
210 (void)s;
211 (void)t;
212 break;
213 }
214
215 ExecCount.setValue(ExecCount.getValue() + 1);
216
217 ExecResult.setValue("Exec");
218
219 return DocumentObject::StdReturn;
220 }
221
222 // ----------------------------------------------------------------------------
223
224 PROPERTY_SOURCE(App::FeatureTestException, App::FeatureTest)
225
226
227 FeatureTestException::FeatureTestException()
228 {
229 ADD_PROPERTY(ExceptionType, (Base::Exception::getClassTypeId().getKey()));
230 }
231
232 DocumentObjectExecReturn* FeatureTestException::execute()
233 {
234 // ExceptionType;
235 throw Base::RuntimeError("FeatureTestException::execute(): Testexception ;-)");
236
237 return nullptr;
238 }
239
240 // ----------------------------------------------------------------------------
241
242 PROPERTY_SOURCE(App::FeatureTestColumn, App::DocumentObject)
243
244
245 FeatureTestColumn::FeatureTestColumn()
246 {
247 ADD_PROPERTY_TYPE(Column, ("A"), "Test", App::Prop_None, "");
248 ADD_PROPERTY_TYPE(Silent, (false), "Test", App::Prop_None, "");
249 ADD_PROPERTY_TYPE(Value, (0L), "Test", App::Prop_Output, "");
250 }
251
252 DocumentObjectExecReturn* FeatureTestColumn::execute()
253 {
254 Value.setValue(decodeColumn(Column.getStrValue(), Silent.getValue()));
255 return nullptr;
256 }
257
258 // ----------------------------------------------------------------------------
259
260 PROPERTY_SOURCE(App::FeatureTestRow, App::DocumentObject)
261
262
263 FeatureTestRow::FeatureTestRow()
264 {
265 ADD_PROPERTY_TYPE(Row, ("1"), "Test", App::Prop_None, "");
266 ADD_PROPERTY_TYPE(Silent, (false), "Test", App::Prop_None, "");
267 ADD_PROPERTY_TYPE(Value, (0L), "Test", App::Prop_Output, "");
268 }
269
270 DocumentObjectExecReturn* FeatureTestRow::execute()
271 {
272 Value.setValue(decodeRow(Row.getStrValue(), Silent.getValue()));
273 return nullptr;
274 }
275
276 // ----------------------------------------------------------------------------
277
278 PROPERTY_SOURCE(App::FeatureTestAbsAddress, App::DocumentObject)
279
280
281 FeatureTestAbsAddress::FeatureTestAbsAddress()
282 {
283 ADD_PROPERTY_TYPE(Address, (""), "Test", Prop_None, "");
284 ADD_PROPERTY_TYPE(Valid, (false), "Test", PropertyType(Prop_Output | Prop_ReadOnly), "");
285 }
286
287 DocumentObjectExecReturn* FeatureTestAbsAddress::execute()
288 {
289 CellAddress address;
290 Valid.setValue(address.parseAbsoluteAddress(Address.getValue()));
291 return StdReturn;
292 }
293
294 // ----------------------------------------------------------------------------
295
296 PROPERTY_SOURCE(App::FeatureTestPlacement, App::DocumentObject)
297
298
299 FeatureTestPlacement::FeatureTestPlacement()
300 {
301 ADD_PROPERTY_TYPE(Input1, (Base::Placement()), "Test", Prop_None, "");
302 ADD_PROPERTY_TYPE(Input2, (Base::Placement()), "Test", Prop_None, "");
303 ADD_PROPERTY_TYPE(MultLeft, (Base::Placement()), "Test", Prop_Output, "");
304 ADD_PROPERTY_TYPE(MultRight, (Base::Placement()), "Test", Prop_Output, "");
305 }
306
307 DocumentObjectExecReturn* FeatureTestPlacement::execute()
308 {
309 Base::Placement p1 = Input1.getValue();
310 Base::Placement q1 = Input1.getValue();
311 Base::Placement p2 = Input2.getValue();
312 MultLeft.setValue(p1.multLeft(p2));
313 MultRight.setValue(q1.multRight(p2));
314 return nullptr;
315 }
316
317 // ----------------------------------------------------------------------------
318
319 PROPERTY_SOURCE(App::FeatureTestAttribute, App::DocumentObject)
320
321
322 FeatureTestAttribute::FeatureTestAttribute()
323 {
324 ADD_PROPERTY(Object, (Py::Object()));
325 ADD_PROPERTY(Attribute, ("Name"));
326 }
327
328 FeatureTestAttribute::~FeatureTestAttribute()
329 {
330 Base::PyGILStateLocker lock;
331 try {
332 Object.getValue().getAttr("Name");
333 #if PYCXX_VERSION_MAJOR >= 7
334 Py::ifPyErrorThrowCxxException();
335 #else
336 if (PyErr_Occurred()) {
337 throw Py::RuntimeError();
338 }
339 #endif
340 }
341 catch (Py::RuntimeError& e) {
342 e.clear();
343 }
344 catch (Py::Exception& e) {
345 e.clear();
346 Base::Console().error("Unexpected exception in ~FeatureTestRemoval()\n");
347 }
348 }
349
350 DocumentObjectExecReturn* FeatureTestAttribute::execute()
351 {
352 Base::PyGILStateLocker lock;
353 try {
354 Object.getValue().getAttr(Attribute.getValue());
355 #if PYCXX_VERSION_MAJOR >= 7
356 Py::ifPyErrorThrowCxxException();
357 #else
358 if (PyErr_Occurred()) {
359 throw Py::AttributeError();
360 }
361 #endif
362 }
363 catch (Py::AttributeError& e) {
364 e.clear();
365 std::stringstream str;
366 str << "No such attribute '" << Attribute.getValue() << "'";
367 throw Base::AttributeError(str.str());
368 }
369 return StdReturn;
370 }