Avoids using getNameInDocument() to test if DocumentObject is attached to a Document.

This patch substitutes by isAttachedToDocument() (almost) everywhere where
getNameInDocument() is used for this purpose.

The very few places not touched by this patch demand a (just a little) less trivial change.
When we change the returning type of getNameInDocument() to std::string,
those places will be easily found, because they shall generate a compiler error
(converting std::string to bool).

Rationale:
The fact that getNameInDocument() return nullptr to indicate
that the object is not attached to a document is responsible for lots of bugs
where the developer does not check for "nullptr".

The idea is to eliminate all those uses of getNameInDocument() and, in the near future,
make getNameInDocument() return always a valid std::string.
This commit is contained in:
André Caldas
2023-10-21 21:42:35 -03:00
committed by Yorik van Havre
parent 7bee0fbde6
commit 89dbab9b0e
49 changed files with 229 additions and 233 deletions

View File

@@ -274,7 +274,7 @@ const char* DocumentObject::getStatusString() const
}
std::string DocumentObject::getFullName() const {
if(!getDocument() || !pcNameInDocument)
if(!getDocument() || !isAttachedToDocument())
return "?";
std::string name(getDocument()->getName());
name += '#';
@@ -305,13 +305,13 @@ const char *DocumentObject::getNameInDocument() const
}
int DocumentObject::isExporting() const {
if(!getDocument() || !getNameInDocument())
if(!getDocument() || !isAttachedToDocument())
return 0;
return getDocument()->isExporting(this);
}
std::string DocumentObject::getExportName(bool forced) const {
if(!pcNameInDocument)
if(!isAttachedToDocument())
return {};
if(!forced && !isExporting())
@@ -441,7 +441,7 @@ void DocumentObject::getInListEx(std::set<App::DocumentObject*> &inSet,
// outLists first here.
for(auto doc : GetApplication().getDocuments()) {
for(auto obj : doc->getObjects()) {
if(!obj || !obj->getNameInDocument() || obj==this)
if(!obj || !obj->isAttachedToDocument() || obj==this)
continue;
const auto &outList = obj->getOutList();
outLists[obj].insert(outList.begin(),outList.end());
@@ -481,7 +481,7 @@ void DocumentObject::getInListEx(std::set<App::DocumentObject*> &inSet,
auto obj = pendings.top();
pendings.pop();
for(auto o : obj->getInList()) {
if(o && o->getNameInDocument() && inSet.insert(o).second) {
if(o && o->isAttachedToDocument() && inSet.insert(o).second) {
pendings.push(o);
if(inList)
inList->push_back(o);
@@ -839,7 +839,7 @@ std::vector<DocumentObject*> DocumentObject::getSubObjectList(const char *subnam
char c = sub[pos+1];
sub[pos+1] = 0;
auto sobj = getSubObject(sub.c_str());
if(!sobj || !sobj->getNameInDocument())
if(!sobj || !sobj->isAttachedToDocument())
break;
res.push_back(sobj);
sub[pos+1] = c;
@@ -859,14 +859,14 @@ std::vector<std::string> DocumentObject::getSubObjects(int reason) const {
std::vector<std::pair<App::DocumentObject *,std::string>> DocumentObject::getParents(int depth) const {
std::vector<std::pair<App::DocumentObject *, std::string>> ret;
if (!getNameInDocument() || !GetApplication().checkLinkDepth(depth, MessageOption::Throw)) {
if (!isAttachedToDocument() || !GetApplication().checkLinkDepth(depth, MessageOption::Throw)) {
return ret;
}
std::string name(getNameInDocument());
name += ".";
for (auto parent : getInList()) {
if (!parent || !parent->getNameInDocument()) {
if (!parent || !parent->isAttachedToDocument()) {
continue;
}
@@ -927,7 +927,7 @@ DocumentObject *DocumentObject::getLinkedObject(
void DocumentObject::Save (Base::Writer &writer) const
{
if (this->getNameInDocument())
if (this->isAttachedToDocument())
writer.ObjectName = this->getNameInDocument();
App::ExtensionContainer::Save(writer);
}
@@ -1164,7 +1164,7 @@ DocumentObject *DocumentObject::resolve(const char *subname,
DocumentObject *DocumentObject::resolveRelativeLink(std::string &subname,
DocumentObject *&link, std::string &linkSub) const
{
if(!link || !link->getNameInDocument() || !getNameInDocument())
if(!link || !link->isAttachedToDocument() || !isAttachedToDocument())
return nullptr;
auto ret = const_cast<DocumentObject*>(this);
if(link != ret) {
@@ -1268,6 +1268,6 @@ bool DocumentObject::redirectSubName(std::ostringstream &, DocumentObject *, Doc
void DocumentObject::onPropertyStatusChanged(const Property &prop, unsigned long oldStatus) {
(void)oldStatus;
if(!Document::isAnyRestoring() && getNameInDocument() && getDocument())
if(!Document::isAnyRestoring() && isAttachedToDocument() && getDocument())
getDocument()->signalChangePropertyEditor(*getDocument(),prop);
}