| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2016 Stefan Tröger <[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 "Extension.h" |
| 33 |
#include "ExtensionContainer.h" |
| 34 |
|
| 35 |
|
| 36 |
using namespace App; |
| 37 |
|
| 38 |
TYPESYSTEM_SOURCE(App::ExtensionContainer, App::PropertyContainer) |
| 39 |
|
| 40 |
ExtensionContainer::ExtensionContainer() = default; |
| 41 |
|
| 42 |
ExtensionContainer::~ExtensionContainer() |
| 43 |
{ |
| 44 |
|
| 45 |
// we need to delete all dynamically added extensions |
| 46 |
for (const auto& entry : _extensions) { |
| 47 |
if (entry.second->isPythonExtension()) { |
| 48 |
delete entry.second; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
void ExtensionContainer::registerExtension(Base::Type extension, Extension* ext) |
| 54 |
{ |
| 55 |
if (ext->getExtendedContainer() != this) { |
| 56 |
throw Base::ValueError( |
| 57 |
"ExtensionContainer::registerExtension: Extension has not this as base object"); |
| 58 |
} |
| 59 |
|
| 60 |
// no duplicate extensions (including base classes) |
| 61 |
if (hasExtension(extension)) { |
| 62 |
for (const auto& entry : _extensions) { |
| 63 |
if (entry.first == extension || entry.first.isDerivedFrom(extension)) { |
| 64 |
_extensions.erase(entry.first); |
| 65 |
break; |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
_extensions[extension] = ext; |
| 71 |
} |
| 72 |
|
| 73 |
bool ExtensionContainer::hasExtension(Base::Type type, bool derived) const |
| 74 |
{ |
| 75 |
|
| 76 |
// check for the exact type |
| 77 |
bool found = _extensions.find(type) != _extensions.end(); |
| 78 |
if (!found && derived) { |
| 79 |
// and for types derived from it, as they can be cast to the extension |
| 80 |
for (const auto& entry : _extensions) { |
| 81 |
if (entry.first.isDerivedFrom(type)) { |
| 82 |
return true; |
| 83 |
} |
| 84 |
} |
| 85 |
return false; |
| 86 |
} |
| 87 |
return found; |
| 88 |
} |
| 89 |
|
| 90 |
bool ExtensionContainer::hasExtension(const std::string& name) const |
| 91 |
{ |
| 92 |
|
| 93 |
// and for types derived from it, as they can be cast to the extension |
| 94 |
for (const auto& entry : _extensions) { |
| 95 |
if (entry.second->name() == name) { |
| 96 |
return true; |
| 97 |
} |
| 98 |
} |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
Extension* ExtensionContainer::getExtension(Base::Type t, bool derived, bool no_except) const |
| 104 |
{ |
| 105 |
|
| 106 |
auto result = _extensions.find(t); |
| 107 |
if ((result == _extensions.end()) && derived) { |
| 108 |
// we need to check for derived types |
| 109 |
for (const auto& entry : _extensions) { |
| 110 |
if (entry.first.isDerivedFrom(t)) { |
| 111 |
return entry.second; |
| 112 |
} |
| 113 |
} |
| 114 |
if (no_except) { |
| 115 |
return nullptr; |
| 116 |
} |
| 117 |
// if we arrive here we don't have anything matching |
| 118 |
throw Base::TypeError( |
| 119 |
"ExtensionContainer::getExtension: No extension of given type available"); |
| 120 |
} |
| 121 |
else if (result != _extensions.end()) { |
| 122 |
return result->second; |
| 123 |
} |
| 124 |
else { |
| 125 |
if (no_except) { |
| 126 |
return nullptr; |
| 127 |
} |
| 128 |
// if we arrive here we don't have anything matching |
| 129 |
throw Base::TypeError( |
| 130 |
"ExtensionContainer::getExtension: No extension of given type available"); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
bool ExtensionContainer::hasExtensions() const |
| 135 |
{ |
| 136 |
|
| 137 |
return !_extensions.empty(); |
| 138 |
} |
| 139 |
|
| 140 |
Extension* ExtensionContainer::getExtension(const std::string& name) const |
| 141 |
{ |
| 142 |
|
| 143 |
// and for types derived from it, as they can be cast to the extension |
| 144 |
for (const auto& entry : _extensions) { |
| 145 |
if (entry.second->name() == name) { |
| 146 |
return entry.second; |
| 147 |
} |
| 148 |
} |
| 149 |
return nullptr; |
| 150 |
} |
| 151 |
|
| 152 |
std::vector<Extension*> ExtensionContainer::getExtensionsDerivedFrom(Base::Type type) const |
| 153 |
{ |
| 154 |
|
| 155 |
std::vector<Extension*> vec; |
| 156 |
// and for types derived from it, as they can be cast to the extension |
| 157 |
for (const auto& entry : _extensions) { |
| 158 |
if (entry.first.isDerivedFrom(type)) { |
| 159 |
vec.push_back(entry.second); |
| 160 |
} |
| 161 |
} |
| 162 |
return vec; |
| 163 |
} |
| 164 |
|
| 165 |
void ExtensionContainer::getPropertyList(std::vector<Property*>& List) const |
| 166 |
{ |
| 167 |
App::PropertyContainer::getPropertyList(List); |
| 168 |
for (const auto& entry : _extensions) { |
| 169 |
entry.second->extensionGetPropertyList(List); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
void ExtensionContainer::getPropertyMap(std::map<std::string, Property*>& Map) const |
| 174 |
{ |
| 175 |
App::PropertyContainer::getPropertyMap(Map); |
| 176 |
for (const auto& entry : _extensions) { |
| 177 |
entry.second->extensionGetPropertyMap(Map); |
| 178 |
} |
| 179 |
} |
| 180 |
void ExtensionContainer::visitProperties(const std::function<void(Property*)>& visitor) const |
| 181 |
{ |
| 182 |
App::PropertyContainer::visitProperties(visitor); |
| 183 |
for (const auto &entry : _extensions) { |
| 184 |
entry.second->extensionVisitProperties(visitor); |
| 185 |
}; |
| 186 |
} |
| 187 |
|
| 188 |
Property* ExtensionContainer::getPropertyByName(const char* name) const |
| 189 |
{ |
| 190 |
auto prop = App::PropertyContainer::getPropertyByName(name); |
| 191 |
if (prop) { |
| 192 |
return prop; |
| 193 |
} |
| 194 |
|
| 195 |
for (const auto& entry : _extensions) { |
| 196 |
auto prop = entry.second->extensionGetPropertyByName(name); |
| 197 |
if (prop) { |
| 198 |
return prop; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return nullptr; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
short int ExtensionContainer::getPropertyType(const Property* prop) const |
| 207 |
{ |
| 208 |
short int res = App::PropertyContainer::getPropertyType(prop); |
| 209 |
if (res != 0) { |
| 210 |
return res; |
| 211 |
} |
| 212 |
|
| 213 |
for (const auto& entry : _extensions) { |
| 214 |
res = entry.second->extensionGetPropertyType(prop); |
| 215 |
if (res != 0) { |
| 216 |
return res; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
short int ExtensionContainer::getPropertyType(const char* name) const |
| 224 |
{ |
| 225 |
|
| 226 |
short int res = App::PropertyContainer::getPropertyType(name); |
| 227 |
if (res != 0) { |
| 228 |
return res; |
| 229 |
} |
| 230 |
|
| 231 |
for (const auto& entry : _extensions) { |
| 232 |
res = entry.second->extensionGetPropertyType(name); |
| 233 |
if (res != 0) { |
| 234 |
return res; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
return 0; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
const char* ExtensionContainer::getPropertyName(const Property* prop) const |
| 243 |
{ |
| 244 |
|
| 245 |
const char* res = App::PropertyContainer::getPropertyName(prop); |
| 246 |
if (res) { |
| 247 |
return res; |
| 248 |
} |
| 249 |
|
| 250 |
for (const auto& entry : _extensions) { |
| 251 |
res = entry.second->extensionGetPropertyName(prop); |
| 252 |
if (res) { |
| 253 |
return res; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
return nullptr; |
| 258 |
} |
| 259 |
|
| 260 |
const char* ExtensionContainer::getPropertyGroup(const Property* prop) const |
| 261 |
{ |
| 262 |
|
| 263 |
const char* res = App::PropertyContainer::getPropertyGroup(prop); |
| 264 |
if (res) { |
| 265 |
return res; |
| 266 |
} |
| 267 |
|
| 268 |
for (const auto& entry : _extensions) { |
| 269 |
res = entry.second->extensionGetPropertyGroup(prop); |
| 270 |
if (res) { |
| 271 |
return res; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
return nullptr; |
| 276 |
} |
| 277 |
|
| 278 |
const char* ExtensionContainer::getPropertyGroup(const char* name) const |
| 279 |
{ |
| 280 |
|
| 281 |
const char* res = App::PropertyContainer::getPropertyGroup(name); |
| 282 |
if (res) { |
| 283 |
return res; |
| 284 |
} |
| 285 |
|
| 286 |
for (const auto& entry : _extensions) { |
| 287 |
res = entry.second->extensionGetPropertyGroup(name); |
| 288 |
if (res) { |
| 289 |
return res; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
return nullptr; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
const char* ExtensionContainer::getPropertyDocumentation(const Property* prop) const |
| 298 |
{ |
| 299 |
|
| 300 |
const char* res = App::PropertyContainer::getPropertyDocumentation(prop); |
| 301 |
if (res) { |
| 302 |
return res; |
| 303 |
} |
| 304 |
|
| 305 |
for (const auto& entry : _extensions) { |
| 306 |
res = entry.second->extensionGetPropertyDocumentation(prop); |
| 307 |
if (res) { |
| 308 |
return res; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
return nullptr; |
| 313 |
} |
| 314 |
|
| 315 |
const char* ExtensionContainer::getPropertyDocumentation(const char* name) const |
| 316 |
{ |
| 317 |
|
| 318 |
const char* res = App::PropertyContainer::getPropertyDocumentation(name); |
| 319 |
if (res) { |
| 320 |
return res; |
| 321 |
} |
| 322 |
|
| 323 |
for (const auto& entry : _extensions) { |
| 324 |
res = entry.second->extensionGetPropertyDocumentation(name); |
| 325 |
if (res) { |
| 326 |
return res; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
return nullptr; |
| 331 |
} |
| 332 |
|
| 333 |
void ExtensionContainer::onChanged(const Property* prop) |
| 334 |
{ |
| 335 |
|
| 336 |
// inform all extensions about changed property. This includes all properties from the |
| 337 |
// extended object (this) as well as all extension properties |
| 338 |
for (const auto& entry : _extensions) { |
| 339 |
entry.second->extensionOnChanged(prop); |
| 340 |
} |
| 341 |
|
| 342 |
App::PropertyContainer::onChanged(prop); |
| 343 |
} |
| 344 |
|
| 345 |
void ExtensionContainer::Save(Base::Writer& writer) const |
| 346 |
{ |
| 347 |
|
| 348 |
// Note: save extensions must be called first to ensure that the extension element is always the |
| 349 |
// very first inside the object element. This is needed since extension element works |
| 350 |
// together with an object attribute, and if another element would be read first the |
| 351 |
// object attributes would be cleared. |
| 352 |
saveExtensions(writer); |
| 353 |
App::PropertyContainer::Save(writer); |
| 354 |
} |
| 355 |
|
| 356 |
void ExtensionContainer::Restore(Base::XMLReader& reader) |
| 357 |
{ |
| 358 |
|
| 359 |
// restore dynamic extensions. |
| 360 |
// Note 1: The extension element must be read first, before all other object elements. That is |
| 361 |
// needed as the element works together with an object element attribute, which would be |
| 362 |
// cleared if another attribute is read first |
| 363 |
// Note 2: This must happen before the py object of this container is used, as only in the |
| 364 |
// pyobject constructor the extension methods are added to the container. |
| 365 |
restoreExtensions(reader); |
| 366 |
App::PropertyContainer::Restore(reader); |
| 367 |
} |
| 368 |
|
| 369 |
void ExtensionContainer::saveExtensions(Base::Writer& writer) const |
| 370 |
{ |
| 371 |
|
| 372 |
// we don't save anything if there are no dynamic extensions |
| 373 |
if (!hasExtensions()) { |
| 374 |
return; |
| 375 |
} |
| 376 |
|
| 377 |
// save dynamic extensions |
| 378 |
writer.incInd(); // indentation for 'Extensions' |
| 379 |
writer.Stream() << writer.ind() << "<Extensions Count=\"" << _extensions.size() << "\">" |
| 380 |
<< std::endl; |
| 381 |
for (const auto& entry : _extensions) { |
| 382 |
|
| 383 |
auto ext = entry.second; |
| 384 |
writer.incInd(); // indentation for 'Extension name' |
| 385 |
writer.Stream() << writer.ind() << "<Extension" |
| 386 |
<< " type=\"" << ext->getExtensionTypeId().getName() << "\"" |
| 387 |
<< " name=\"" << ext->name() << "\">" << std::endl; |
| 388 |
writer.incInd(); // indentation for the actual Extension |
| 389 |
try { |
| 390 |
// We must make sure to handle all exceptions accordingly so that |
| 391 |
// the project file doesn't get invalidated. In the error case this |
| 392 |
// means to proceed instead of aborting the write operation. |
| 393 |
ext->extensionSave(writer); |
| 394 |
} |
| 395 |
catch (const Base::Exception& e) { |
| 396 |
Base::Console().error("%s\n", e.what()); |
| 397 |
} |
| 398 |
catch (const std::exception& e) { |
| 399 |
Base::Console().error("%s\n", e.what()); |
| 400 |
} |
| 401 |
catch (const char* e) { |
| 402 |
Base::Console().error("%s\n", e); |
| 403 |
} |
| 404 |
#ifndef FC_DEBUG |
| 405 |
catch (...) { |
| 406 |
Base::Console().error( |
| 407 |
"ExtensionContainer::Save: Unknown C++ exception thrown. Try to continue...\n"); |
| 408 |
} |
| 409 |
#endif |
| 410 |
writer.decInd(); // indentation for the actual extension |
| 411 |
writer.Stream() << writer.ind() << "</Extension>" << std::endl; |
| 412 |
writer.decInd(); // indentation for 'Extension name' |
| 413 |
} |
| 414 |
writer.Stream() << writer.ind() << "</Extensions>" << std::endl; |
| 415 |
writer.decInd(); |
| 416 |
} |
| 417 |
|
| 418 |
void ExtensionContainer::restoreExtensions(Base::XMLReader& reader) |
| 419 |
{ |
| 420 |
|
| 421 |
// Dynamic extensions are optional (also because they are introduced late into the document |
| 422 |
// format) and hence it is possible that the element does not exist. As we cannot check for the |
| 423 |
// existence of an element a object attribute is set if extensions are available. Here we check |
| 424 |
// that attribute, and only if it exists the extensions element will be available. |
| 425 |
if (!reader.hasAttribute("Extensions")) { |
| 426 |
return; |
| 427 |
} |
| 428 |
|
| 429 |
reader.readElement("Extensions"); |
| 430 |
int Cnt = reader.getAttribute<long>("Count"); |
| 431 |
|
| 432 |
for (int i = 0; i < Cnt; i++) { |
| 433 |
reader.readElement("Extension"); |
| 434 |
const char* Type = reader.getAttribute<const char*>("type"); |
| 435 |
const char* Name = reader.getAttribute<const char*>("name"); |
| 436 |
try { |
| 437 |
App::Extension* ext = getExtension(Name); |
| 438 |
if (!ext) { |
| 439 |
// get the extension type asked for |
| 440 |
Base::Type extension = Base::Type::fromName(Type); |
| 441 |
if (extension.isBad() |
| 442 |
|| !extension.isDerivedFrom(App::Extension::getExtensionClassTypeId())) { |
| 443 |
std::stringstream str; |
| 444 |
str << "No extension found of type '" << Type << "'" << std::ends; |
| 445 |
throw Base::TypeError(str.str()); |
| 446 |
} |
| 447 |
|
| 448 |
// register the extension |
| 449 |
ext = static_cast<App::Extension*>(extension.createInstance()); |
| 450 |
// check if this really is a python extension! |
| 451 |
if (!ext->isPythonExtension()) { |
| 452 |
delete ext; |
| 453 |
std::stringstream str; |
| 454 |
str << "Extension is not a python addable version: '" << Type << "'"; |
| 455 |
throw Base::TypeError(str.str()); |
| 456 |
} |
| 457 |
|
| 458 |
ext->initExtension(this); |
| 459 |
} |
| 460 |
if (ext && strcmp(ext->getExtensionTypeId().getName(), Type) == 0) { |
| 461 |
ext->extensionRestore(reader); |
| 462 |
} |
| 463 |
} |
| 464 |
catch (const Base::XMLParseException&) { |
| 465 |
throw; // re-throw |
| 466 |
} |
| 467 |
catch (const Base::Exception& e) { |
| 468 |
Base::Console().error("%s\n", e.what()); |
| 469 |
} |
| 470 |
catch (const std::exception& e) { |
| 471 |
Base::Console().error("%s\n", e.what()); |
| 472 |
} |
| 473 |
catch (const char* e) { |
| 474 |
Base::Console().error("%s\n", e); |
| 475 |
} |
| 476 |
#ifndef FC_DEBUG |
| 477 |
catch (...) { |
| 478 |
Base::Console().error("ExtensionContainer::Restore: Unknown C++ exception thrown\n"); |
| 479 |
} |
| 480 |
#endif |
| 481 |
|
| 482 |
reader.readEndElement("Extension"); |
| 483 |
} |
| 484 |
reader.readEndElement("Extensions"); |
| 485 |
} |
| 486 |
|
| 487 |
void ExtensionContainer::handleChangedPropertyName(Base::XMLReader& reader, |
| 488 |
const char* TypeName, |
| 489 |
const char* PropName) |
| 490 |
{ |
| 491 |
// inform all extensions about changed property name. This includes all properties from the |
| 492 |
// extended object (this) as well as all extension properties |
| 493 |
for (const auto& entry : _extensions) { |
| 494 |
bool handled = entry.second->extensionHandleChangedPropertyName(reader, TypeName, PropName); |
| 495 |
|
| 496 |
if (handled) { |
| 497 |
return; // one property change needs only be handled once |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
PropertyContainer::handleChangedPropertyName(reader, TypeName, PropName); |
| 502 |
} |
| 503 |
|
| 504 |
void ExtensionContainer::handleChangedPropertyType(Base::XMLReader& reader, |
| 505 |
const char* TypeName, |
| 506 |
Property* prop) |
| 507 |
{ |
| 508 |
// inform all extensions about changed property type. This includes all properties from the |
| 509 |
// extended object (this) as well as all extension properties |
| 510 |
for (const auto& entry : _extensions) { |
| 511 |
bool handled = entry.second->extensionHandleChangedPropertyType(reader, TypeName, prop); |
| 512 |
|
| 513 |
if (handled) { |
| 514 |
return; // one property change needs only be handled once |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
PropertyContainer::handleChangedPropertyType(reader, TypeName, prop); |
| 519 |
} |