| 3370 | } |
| 3371 | |
| 3372 | std::vector<DocumentObject*> |
| 3373 | Document::addObjects(const char* sType, const std::vector<std::string>& objectNames, bool isNew) |
| 3374 | { |
| 3375 | Base::Type type = |
| 3376 | Base::Type::getTypeIfDerivedFrom(sType, DocumentObject::getClassTypeId(), true); |
| 3377 | if (type.isBad()) { |
| 3378 | std::stringstream str; |
| 3379 | str << "'" << sType << "' is not a document object type"; |
| 3380 | throw Base::TypeError(str.str()); |
| 3381 | } |
| 3382 | |
| 3383 | std::vector<DocumentObject*> objects; |
| 3384 | objects.resize(objectNames.size()); |
| 3385 | std::generate(objects.begin(), objects.end(), [&] { |
| 3386 | return static_cast<DocumentObject*>(type.createInstance()); |
| 3387 | }); |
| 3388 | // the type instance could be a null pointer, it is enough to check the first element |
| 3389 | if (!objects.empty() && !objects[0]) { |
| 3390 | objects.clear(); |
| 3391 | return objects; |
| 3392 | } |
| 3393 | |
| 3394 | for (auto it = objects.begin(); it != objects.end(); ++it) { |
| 3395 | size_t index = std::distance(objects.begin(), it); |
| 3396 | DocumentObject* pcObject = *it; |
| 3397 | pcObject->setDocument(this); |
| 3398 | |
| 3399 | // Add the object but only activate the last one |
| 3400 | bool isLast = index == (objects.size() - 1); |
| 3401 | _addObject(pcObject, |
| 3402 | objectNames[index].c_str(), |
| 3403 | AddObjectOption::SetNewStatus |
| 3404 | | (isNew ? AddObjectOption::DoSetup : AddObjectOption::None) |
| 3405 | | (isLast ? AddObjectOption::ActivateObject : AddObjectOption::None)); |
| 3406 | } |
| 3407 | |
| 3408 | return objects; |
| 3409 | } |
| 3410 | |
| 3411 | void Document::addObject(DocumentObject* obj, const char* name) |
| 3412 | { |
no test coverage detected