| 3636 | } |
| 3637 | |
| 3638 | std::vector<DocumentObject*> |
| 3639 | Document::copyObject(const std::vector<DocumentObject*>& objs, bool recursive, bool returnAll) |
| 3640 | { |
| 3641 | std::vector<DocumentObject*> deps; |
| 3642 | if (!recursive) { |
| 3643 | deps = objs; |
| 3644 | } |
| 3645 | else { |
| 3646 | deps = getDependencyList(objs, DepNoXLinked | DepSort); |
| 3647 | } |
| 3648 | |
| 3649 | if (!testStatus(TempDoc) && !isSaved() && PropertyXLink::hasXLink(deps)) { |
| 3650 | throw Base::RuntimeError( |
| 3651 | "Document must be saved at least once before link to external objects"); |
| 3652 | } |
| 3653 | |
| 3654 | MergeDocuments md(this); |
| 3655 | // if not copying recursively then suppress possible warnings |
| 3656 | md.setVerbose(recursive); |
| 3657 | |
| 3658 | unsigned int memsize = 1000; // ~ for the meta-information |
| 3659 | for (auto it : deps) { |
| 3660 | memsize += it->getMemSize(); |
| 3661 | } |
| 3662 | |
| 3663 | // if less than ~10 MB |
| 3664 | bool use_buffer = (memsize < 0xA00000); |
| 3665 | std::string res; |
| 3666 | try { |
| 3667 | res.reserve(memsize); |
| 3668 | } |
| 3669 | catch (const std::bad_alloc&) { |
| 3670 | use_buffer = false; |
| 3671 | } |
| 3672 | |
| 3673 | std::vector<DocumentObject*> imported; |
| 3674 | if (use_buffer) { |
| 3675 | Base::StringOStreambuf obuf(res); |
| 3676 | std::ostream ostr(&obuf); |
| 3677 | exportObjects(deps, ostr); |
| 3678 | |
| 3679 | Base::StringIStreambuf ibuf(res); |
| 3680 | std::istream istr(nullptr); |
| 3681 | istr.rdbuf(&ibuf); |
| 3682 | imported = md.importObjects(istr); |
| 3683 | } |
| 3684 | else { |
| 3685 | static Base::FileInfo fi(Application::getTempFileName()); |
| 3686 | Base::ofstream ostr(fi, std::ios::out | std::ios::binary); |
| 3687 | exportObjects(deps, ostr); |
| 3688 | ostr.close(); |
| 3689 | |
| 3690 | Base::ifstream istr(fi, std::ios::in | std::ios::binary); |
| 3691 | imported = md.importObjects(istr); |
| 3692 | } |
| 3693 | |
| 3694 | if (returnAll || imported.size() != deps.size()) { |
| 3695 | return imported; |
no test coverage detected