Debian Codegraph @

src/App/PropertyContainer.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 #include <map>
24 #include <vector>
25 #include <string>
26
27 #include <Base/Console.h>
28 #include <Base/Exception.h>
29 #include <Base/Reader.h>
30 #include <Base/Writer.h>
31
32 #include "Property.h"
33 #include "PropertyContainer.h"
34
35
36 FC_LOG_LEVEL_INIT("App",true,true)
37
38 using namespace App;
39 using namespace Base;
40 using namespace std;
41
42 TYPESYSTEM_SOURCE(App::PropertyContainer,Base::Persistence)
43
44
45 PropertyContainer::PropertyContainer()
46 {
47 propertyData.parentPropertyData = nullptr;
48 }
49
50 PropertyContainer::~PropertyContainer() = default;
51
52 unsigned int PropertyContainer::getMemSize () const
53 {
54 std::map<std::string,Property*> Map;
55 getPropertyMap(Map);
56 std::map<std::string,Property*>::const_iterator It;
57 unsigned int size = 0;
58 for (It = Map.begin(); It != Map.end();++It)
59 size += It->second->getMemSize();
60 return size;
61 }
62
63 App::Property* PropertyContainer::addDynamicProperty(
64 const char* type, const char* name, const char* group, const char* doc,
65 short attr, bool ro, bool hidden)
66 {
67 return dynamicProps.addDynamicProperty(*this,type,name,group,doc,attr,ro,hidden);
68 }
69
70 Property *PropertyContainer::getPropertyByName(const char* name) const
71 {
72 auto prop = dynamicProps.getDynamicPropertyByName(name);
73 if (prop) {
74 return prop;
75 }
76 return getPropertyData().getPropertyByName(this,name);
77 }
78
79 void PropertyContainer::getPropertyMap(std::map<std::string,Property*> &Map) const
80 {
81 dynamicProps.getPropertyMap(Map);
82 getPropertyData().getPropertyMap(this,Map);
83 }
84
85 void PropertyContainer::getPropertyList(std::vector<Property*> &List) const
86 {
87 dynamicProps.getPropertyList(List);
88 getPropertyData().getPropertyList(this,List);
89 }
90
91 void PropertyContainer::visitProperties(const std::function<void(Property *)>& visitor) const
92 {
93 dynamicProps.visitProperties(visitor);
94 getPropertyData().visitProperties(this, visitor);
95 }
96
97 void PropertyContainer::getPropertyNamedList(std::vector<std::pair<const char*, Property*> > &List) const
98 {
99 dynamicProps.getPropertyNamedList(List);
100 getPropertyData().getPropertyNamedList(this,List);
101 }
102
103 void PropertyContainer::setPropertyStatus(unsigned char bit,bool value)
104 {
105 std::vector<Property*> List;
106 getPropertyList(List);
107 for(auto it : List)
108 it->StatusBits.set(bit,value);
109 }
110
111 short PropertyContainer::getPropertyType(const Property* prop) const
112 {
113 return prop?prop->getType():0;
114 }
115
116 short PropertyContainer::getPropertyType(const char *name) const
117 {
118 return getPropertyType(getPropertyByName(name));
119 }
120
121 const char* PropertyContainer::getPropertyGroup(const Property* prop) const
122 {
123 auto group = dynamicProps.getPropertyGroup(prop);
124 if(group)
125 return group;
126 return getPropertyData().getGroup(this,prop);
127 }
128
129 const char* PropertyContainer::getPropertyGroup(const char *name) const
130 {
131 auto group = dynamicProps.getPropertyGroup(name);
132 if(group)
133 return group;
134 return getPropertyData().getGroup(this,name);
135 }
136
137 const char* PropertyContainer::getPropertyDocumentation(const Property* prop) const
138 {
139 auto doc = dynamicProps.getPropertyDocumentation(prop);
140 if(doc)
141 return doc;
142 return getPropertyData().getDocumentation(this,prop);
143 }
144
145 const char* PropertyContainer::getPropertyDocumentation(const char *name) const
146 {
147 auto doc = dynamicProps.getPropertyDocumentation(name);
148 if(doc)
149 return doc;
150 return getPropertyData().getDocumentation(this,name);
151 }
152
153 bool PropertyContainer::isReadOnly(const Property* prop) const
154 {
155 return (getPropertyType(prop) & Prop_ReadOnly) == Prop_ReadOnly;
156 }
157
158 bool PropertyContainer::isReadOnly(const char *name) const
159 {
160 return (getPropertyType(name) & Prop_ReadOnly) == Prop_ReadOnly;
161 }
162
163 bool PropertyContainer::isHidden(const Property* prop) const
164 {
165 return (getPropertyType(prop) & Prop_Hidden) == Prop_Hidden;
166 }
167
168 bool PropertyContainer::isHidden(const char *name) const
169 {
170 return (getPropertyType(name) & Prop_Hidden) == Prop_Hidden;
171 }
172
173 const char* PropertyContainer::getPropertyName(const Property* prop)const
174 {
175 auto res = dynamicProps.getPropertyName(prop);
176 if(!res)
177 res = getPropertyData().getName(this,prop);
178 return res;
179 }
180
181 const PropertyData * PropertyContainer::getPropertyDataPtr(){return &propertyData;}
182 const PropertyData & PropertyContainer::getPropertyData() const{return propertyData;}
183
184
185 void PropertyContainer::handleChangedPropertyName(Base::XMLReader &reader, const char * TypeName, const char *PropName)
186 {
187 (void)reader;
188 (void)TypeName;
189 (void)PropName;
190 }
191
192 void PropertyContainer::handleChangedPropertyType(XMLReader &reader, const char *TypeName, Property *prop)
193 {
194 (void)reader;
195 (void)TypeName;
196 (void)prop;
197 }
198
199 PropertyData PropertyContainer::propertyData;
200
201 void PropertyContainer::beforeSave() const
202 {
203 std::map<std::string, Property*> Map;
204 getPropertyMap(Map);
205 for (auto& entry : Map) {
206 auto prop = entry.second;
207 if (!prop->testStatus(Property::PropDynamic)
208 && (prop->testStatus(Property::Transient)
209 || ((getPropertyType(prop) & Prop_Transient) != 0))) {
210 // Nothing
211 }
212 else {
213 prop->beforeSave();
214 }
215 }
216 }
217
218 void PropertyContainer::Save (Base::Writer &writer) const
219 {
220 std::map<std::string,Property*> Map;
221 getPropertyMap(Map);
222
223 std::vector<Property*> transients;
224 for(auto it=Map.begin();it!=Map.end();) {
225 auto prop = it->second;
226 if(prop->testStatus(Property::PropNoPersist)) {
227 it = Map.erase(it);
228 continue;
229 }
230 if(!prop->testStatus(Property::PropDynamic)
231 && (prop->testStatus(Property::Transient) ||
232 getPropertyType(prop) & Prop_Transient))
233 {
234 transients.push_back(prop);
235 it = Map.erase(it);
236 } else {
237 ++it;
238 }
239 }
240
241 writer.incInd(); // indentation for 'Properties Count'
242 writer.Stream() << writer.ind() << "<Properties Count=\"" << Map.size()
243 << "\" TransientCount=\"" << transients.size() << "\">" << endl;
244
245 // First store transient properties to persist their status value. We use
246 // a new element named "_Property" so that the save file can be opened by
247 // older versions of FC.
248 writer.incInd();
249 for(auto prop : transients) {
250 writer.Stream() << writer.ind() << "<_Property name=\"" << prop->getName()
251 << "\" type=\"" << prop->getTypeId().getName()
252 << "\" status=\"" << prop->getStatus() << "\"/>" << std::endl;
253 }
254 writer.decInd();
255
256 // Now store normal properties
257 for (const auto& it : Map)
258 {
259 writer.incInd(); // indentation for 'Property name'
260 writer.Stream() << writer.ind() << "<Property name=\"" << it.first << "\" type=\""
261 << it.second->getTypeId().getName();
262
263 dynamicProps.save(it.second,writer);
264
265 auto status = it.second->getStatus();
266 if(status)
267 writer.Stream() << "\" status=\"" << status;
268 writer.Stream() << "\">";
269
270 if(it.second->testStatus(Property::Transient)
271 || it.second->getType() & Prop_Transient)
272 {
273 writer.decInd();
274 writer.Stream() << "</Property>" << std::endl;
275 continue;
276 }
277
278 writer.Stream() << std::endl;
279
280 writer.incInd(); // indentation for the actual property
281
282 try {
283 // We must make sure to handle all exceptions accordingly so that
284 // the project file doesn't get invalidated. In the error case this
285 // means to proceed instead of aborting the write operation.
286 it.second->Save(writer);
287 }
288 catch (const Base::Exception &e) {
289 Base::Console().error("%s\n", e.what());
290 }
291 catch (const std::exception &e) {
292 Base::Console().error("%s\n", e.what());
293 }
294 catch (const char* e) {
295 Base::Console().error("%s\n", e);
296 }
297 #ifndef FC_DEBUG
298 catch (...) {
299 Base::Console().error("PropertyContainer::Save: Unknown C++ exception thrown. Try to continue...\n");
300 }
301 #endif
302 writer.decInd(); // indentation for the actual property
303 writer.Stream() << writer.ind() << "</Property>" << endl;
304 writer.decInd(); // indentation for 'Property name'
305 }
306 writer.Stream() << writer.ind() << "</Properties>" << endl;
307 writer.decInd(); // indentation for 'Properties Count'
308 }
309
310 void PropertyContainer::Restore(Base::XMLReader &reader)
311 {
312 reader.clearPartialRestoreProperty();
313 reader.readElement("Properties");
314 int Cnt = reader.getAttribute<long>("Count");
315
316 int transientCount = 0;
317 if(reader.hasAttribute("TransientCount"))
318 transientCount = reader.getAttribute<unsigned long>("TransientCount");
319
320 for (int i=0;i<transientCount; ++i) {
321 reader.readElement("_Property");
322 Property* prop = getPropertyByName(reader.getAttribute<const char*>("name"));
323 if(prop)
324 FC_TRACE("restore transient '" << prop->getName() << "'");
325 if(prop && reader.hasAttribute("status"))
326 prop->setStatusValue(reader.getAttribute<unsigned long>("status"));
327 }
328
329 for (int i=0 ;i<Cnt ;i++) {
330 reader.readElement("Property");
331 std::string PropName = reader.getAttribute<const char*>("name");
332 std::string TypeName = reader.getAttribute<const char*>("type");
333 // NOTE: We must also check the type of the current property because a
334 // subclass of PropertyContainer might change the type of a property but
335 // not its name. In this case we would force to read-in a wrong property
336 // type and the behaviour would be undefined.
337 try {
338 auto prop = getPropertyByName(PropName.c_str());
339 if (!prop || prop->getContainer() != this) {
340 prop = dynamicProps.restore(*this,PropName.c_str(),TypeName.c_str(),reader);
341 }
342
343 decltype(Property::StatusBits) status;
344 if(reader.hasAttribute("status")) {
345 status = decltype(status)(reader.getAttribute<unsigned long>("status"));
346 if(prop)
347 prop->setStatusValue(status.to_ulong());
348 }
349 // name and type match
350 if (prop && strcmp(prop->getTypeId().getName(), TypeName.c_str()) == 0) {
351 if (!prop->testStatus(Property::Transient)
352 && !status.test(Property::Transient)
353 && !status.test(Property::PropTransient)
354 && !prop->testStatus(Property::PropTransient))
355 {
356 FC_TRACE("restore property '" << prop->getName() << "'");
357 prop->Restore(reader);
358 }else
359 FC_TRACE("skip transient '" << prop->getName() << "'");
360 }
361 // name matches but not the type
362 else if (prop) {
363 handleChangedPropertyType(reader, TypeName.c_str(), prop);
364 }
365 // name doesn't match, the sub-class then has to know
366 // if the property has been renamed or removed
367 else {
368 handleChangedPropertyName(reader, TypeName.c_str(), PropName.c_str());
369 }
370
371 if (reader.testStatus(Base::XMLReader::ReaderStatus::PartialRestoreInProperty)) {
372 Base::Console().error("Property %s of type %s was subject to a partial restore.\n",PropName.c_str(),TypeName.c_str());
373 reader.clearPartialRestoreProperty();
374 }
375 }
376 catch (const Base::XMLParseException&) {
377 throw; // re-throw
378 }
379 catch (const Base::RestoreError &) {
380 reader.setPartialRestore(true);
381 reader.clearPartialRestoreProperty();
382 Base::Console().error("Property %s of type %s was subject to a partial restore.\n",PropName.c_str(),TypeName.c_str());
383 }
384 catch (const Base::Exception &e) {
385 Base::Console().error("%s\n", e.what());
386 }
387 catch (const std::exception &e) {
388 Base::Console().error("%s\n", e.what());
389 }
390 catch (const char* e) {
391 Base::Console().error("%s\n", e);
392 }
393 #ifndef FC_DEBUG
394 catch (...) {
395 Base::Console().error("PropertyContainer::Restore: Unknown C++ exception thrown\n");
396 }
397 #endif
398 reader.readEndElement("Property");
399 }
400 reader.readEndElement("Properties");
401 }
402
403 void PropertyContainer::onPropertyStatusChanged(const Property &prop, unsigned long oldStatus)
404 {
405 (void)prop;
406 (void)oldStatus;
407 }
408
409 void PropertyData::addProperty(OffsetBase offsetBase,const char* PropName, Property *Prop, const char* PropertyGroup , PropertyType Type, const char* PropertyDocu)
410 {
411 #ifdef FC_DEBUG
412 if(!parentMerged)
413 #endif
414 {
415 short offset = offsetBase.getOffsetTo(Prop);
416 if(offset < 0)
417 throw Base::RuntimeError("Invalid static property");
418 auto &index = propertyData.get<1>();
419 auto it = index.find(PropName);
420 if(it == index.end()) {
421 if(parentMerged)
422 throw Base::RuntimeError("Cannot add static property");
423 index.emplace(PropName, PropertyGroup, PropertyDocu, offset, Type);
424 } else{
425 #ifdef FC_DEBUG
426 if(it->Offset != offset) {
427 FC_ERR("Duplicate property '" << PropName << "'");
428 }
429 #endif
430 }
431 }
432
433 Prop->syncType(Type);
434 Prop->myName = PropName;
435 }
436
437 void PropertyData::merge(PropertyData *other) const {
438 if(!other)
439 other = const_cast<PropertyData*>(parentPropertyData);
440 if(other == parentPropertyData) {
441 if(parentMerged)
442 return;
443 parentMerged = true;
444 }
445 if(other) {
446 other->merge();
447 auto &index = propertyData.get<0>();
448 for(const auto &spec : other->propertyData.get<0>())
449 index.push_back(spec);
450 }
451 }
452
453 void PropertyData::split(PropertyData *other) {
454 if(other == parentPropertyData) {
455 if(!parentMerged)
456 return;
457 parentMerged = false;
458 }
459 if(other) {
460 auto &index = propertyData.get<2>();
461 for(const auto &spec : other->propertyData.get<0>())
462 index.erase(spec.Offset);
463 }
464 }
465
466 const PropertyData::PropertySpec *PropertyData::findProperty(OffsetBase offsetBase,const char* PropName) const
467 {
468 (void)offsetBase;
469 merge();
470 auto &index = propertyData.get<1>();
471 auto it = index.find(PropName);
472 if(it != index.end())
473 return &(*it);
474 return nullptr;
475 }
476
477 const PropertyData::PropertySpec *PropertyData::findProperty(OffsetBase offsetBase,const Property* prop) const
478 {
479 merge();
480 int diff = offsetBase.getOffsetTo(prop);
481 if(diff<0)
482 return nullptr;
483
484 auto &index = propertyData.get<2>();
485 auto it = index.find(diff);
486 if(it!=index.end())
487 return &(*it);
488
489 return nullptr;
490 }
491
492 const char* PropertyData::getName(OffsetBase offsetBase,const Property* prop) const
493 {
494 const PropertyData::PropertySpec* Spec = findProperty(offsetBase,prop);
495
496 if(Spec)
497 return Spec->Name;
498 else
499 return nullptr;
500 }
501
502 short PropertyData::getType(OffsetBase offsetBase,const Property* prop) const
503 {
504 const PropertyData::PropertySpec* Spec = findProperty(offsetBase,prop);
505
506 if(Spec)
507 return Spec->Type;
508 else
509 return 0;
510 }
511
512 short PropertyData::getType(OffsetBase offsetBase,const char* name) const
513 {
514 const PropertyData::PropertySpec* Spec = findProperty(offsetBase,name);
515
516 if(Spec)
517 return Spec->Type;
518 else
519 return 0;
520 }
521
522 const char* PropertyData::getGroup(OffsetBase offsetBase,const Property* prop) const
523 {
524 const PropertyData::PropertySpec* Spec = findProperty(offsetBase,prop);
525
526 if(Spec)
527 return Spec->Group;
528 else
529 return nullptr;
530 }
531
532 const char* PropertyData::getGroup(OffsetBase offsetBase,const char* name) const
533 {
534 const PropertyData::PropertySpec* Spec = findProperty(offsetBase,name);
535
536 if(Spec)
537 return Spec->Group;
538 else
539 return nullptr;
540 }
541
542 const char* PropertyData::getDocumentation(OffsetBase offsetBase,const Property* prop) const
543 {
544 const PropertyData::PropertySpec* Spec = findProperty(offsetBase,prop);
545
546 if(Spec)
547 return Spec->Docu;
548 else
549 return nullptr;
550 }
551
552 const char* PropertyData::getDocumentation(OffsetBase offsetBase,const char* name) const
553 {
554 const PropertyData::PropertySpec* Spec = findProperty(offsetBase,name);
555
556 if(Spec)
557 return Spec->Docu;
558 else
559 return nullptr;
560 }
561
562 Property *PropertyData::getPropertyByName(OffsetBase offsetBase,const char* name) const
563 {
564 const PropertyData::PropertySpec* Spec = findProperty(offsetBase,name);
565
566 if(Spec)
567 return reinterpret_cast<Property *>(Spec->Offset + offsetBase.getOffset());
568 else
569 return nullptr;
570 }
571
572 void PropertyData::getPropertyMap(OffsetBase offsetBase,std::map<std::string,Property*> &Map) const
573 {
574 merge();
575 for(auto &spec : propertyData.get<0>())
576 Map[spec.Name] = reinterpret_cast<Property *>(spec.Offset + offsetBase.getOffset());
577 }
578
579 void PropertyData::getPropertyList(OffsetBase offsetBase,std::vector<Property*> &List) const
580 {
581 merge();
582 size_t base = List.size();
583 List.reserve(base+propertyData.size());
584 for (auto &spec : propertyData.get<0>())
585 List.push_back(reinterpret_cast<Property *>(spec.Offset + offsetBase.getOffset()));
586 }
587
588 void PropertyData::getPropertyNamedList(OffsetBase offsetBase,
589 std::vector<std::pair<const char*,Property*> > &List) const
590 {
591 merge();
592 size_t base = List.size();
593 List.reserve(base+propertyData.size());
594 for (auto &spec : propertyData.get<0>()) {
595 auto prop = reinterpret_cast<Property *>(spec.Offset + offsetBase.getOffset());
596 List.emplace_back(prop->getName(),prop);
597 }
598 }
599
600 void PropertyData::visitProperties(OffsetBase offsetBase,
601 const std::function<void(Property*)>& visitor) const
602 {
603 merge();
604 char* offset = offsetBase.getOffset();
605 for (const auto& spec : propertyData.get<0>()) {
606 visitor(reinterpret_cast<Property*>(spec.Offset + offset));
607 };
608 }
609