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:
committed by
Yorik van Havre
parent
7bee0fbde6
commit
89dbab9b0e
@@ -518,7 +518,7 @@ void LinkBaseExtension::syncCopyOnChange()
|
||||
// to match the possible new copy later.
|
||||
objs = copyOnChangeGroup->ElementList.getValues();
|
||||
for (auto obj : objs) {
|
||||
if (!obj->getNameInDocument())
|
||||
if (!obj->isAttachedToDocument())
|
||||
continue;
|
||||
auto prop = Base::freecad_dynamic_cast<PropertyUUID>(
|
||||
obj->getPropertyByName("_SourceUUID"));
|
||||
@@ -872,7 +872,7 @@ App::DocumentObject *LinkBaseExtension::makeCopyOnChange() {
|
||||
|
||||
if (auto prop = getLinkCopyOnChangeGroupProperty()) {
|
||||
if (auto obj = prop->getValue()) {
|
||||
if (obj->getNameInDocument() && obj->getDocument())
|
||||
if (obj->isAttachedToDocument() && obj->getDocument())
|
||||
obj->getDocument()->removeObject(obj->getNameInDocument());
|
||||
}
|
||||
auto group = new LinkGroup;
|
||||
@@ -1088,7 +1088,7 @@ int LinkBaseExtension::getElementIndex(const char *subname, const char **psubnam
|
||||
// pattern, which is the owner object name + "_i" + index
|
||||
const char *name = subname[0]=='$'?subname+1:subname;
|
||||
auto owner = getContainer();
|
||||
if(owner && owner->getNameInDocument()) {
|
||||
if(owner && owner->isAttachedToDocument()) {
|
||||
std::string ownerName(owner->getNameInDocument());
|
||||
ownerName += '_';
|
||||
if(boost::algorithm::starts_with(name,ownerName.c_str())) {
|
||||
@@ -1108,7 +1108,7 @@ int LinkBaseExtension::getElementIndex(const char *subname, const char **psubnam
|
||||
// Then check for the actual linked object's name or label, and
|
||||
// redirect that reference to the first array element
|
||||
auto linked = getTrueLinkedObject(false);
|
||||
if(!linked || !linked->getNameInDocument())
|
||||
if(!linked || !linked->isAttachedToDocument())
|
||||
return -1;
|
||||
if(subname[0]=='$') {
|
||||
CharRange sub(subname+1, dot);
|
||||
@@ -1223,7 +1223,7 @@ Base::Matrix4D LinkBaseExtension::getTransform(bool transform) const {
|
||||
bool LinkBaseExtension::extensionGetSubObjects(std::vector<std::string> &ret, int reason) const {
|
||||
if(!getLinkedObjectProperty() && getElementListProperty()) {
|
||||
for(auto obj : getElementListProperty()->getValues()) {
|
||||
if(obj && obj->getNameInDocument()) {
|
||||
if(obj && obj->isAttachedToDocument()) {
|
||||
std::string name(obj->getNameInDocument());
|
||||
name+='.';
|
||||
ret.push_back(name);
|
||||
@@ -1303,7 +1303,7 @@ bool LinkBaseExtension::extensionGetSubObject(DocumentObject *&ret, const char *
|
||||
if(idx>=0) {
|
||||
const auto &elements = _getElementListValue();
|
||||
if(!elements.empty()) {
|
||||
if(idx>=(int)elements.size() || !elements[idx] || !elements[idx]->getNameInDocument())
|
||||
if(idx>=(int)elements.size() || !elements[idx] || !elements[idx]->isAttachedToDocument())
|
||||
return true;
|
||||
ret = elements[idx]->getSubObject(subname,pyObj,mat,true,depth+1);
|
||||
// do not resolve the link if this element is the last referenced object
|
||||
@@ -1415,7 +1415,7 @@ void LinkBaseExtension::onExtendedUnsetupObject() {
|
||||
return;
|
||||
detachElements();
|
||||
if (auto obj = getLinkCopyOnChangeGroupValue()) {
|
||||
if(obj->getNameInDocument() && !obj->isRemoving())
|
||||
if(obj->isAttachedToDocument() && !obj->isRemoving())
|
||||
obj->getDocument()->removeObject(obj->getNameInDocument());
|
||||
}
|
||||
}
|
||||
@@ -1440,7 +1440,7 @@ DocumentObject *LinkBaseExtension::getTrueLinkedObject(
|
||||
}
|
||||
if(ret && recurse)
|
||||
ret = ret->getLinkedObject(recurse,mat,transform,depth+1);
|
||||
if(ret && !ret->getNameInDocument())
|
||||
if(ret && !ret->isAttachedToDocument())
|
||||
return nullptr;
|
||||
return ret;
|
||||
}
|
||||
@@ -1514,7 +1514,7 @@ void LinkBaseExtension::updateGroup() {
|
||||
groupSet.insert(group->getExtendedObject());
|
||||
}else{
|
||||
for(auto o : getElementListProperty()->getValues()) {
|
||||
if(!o || !o->getNameInDocument())
|
||||
if(!o || !o->isAttachedToDocument())
|
||||
continue;
|
||||
auto ext = o->getExtensionByType<GroupExtension>(true,false);
|
||||
if(ext) {
|
||||
@@ -1636,7 +1636,7 @@ void LinkBaseExtension::update(App::DocumentObject *parent, const Property *prop
|
||||
getScaleListProperty()->setValue(scales);
|
||||
|
||||
for(auto obj : objs) {
|
||||
if(obj && obj->getNameInDocument())
|
||||
if(obj && obj->isAttachedToDocument())
|
||||
obj->getDocument()->removeObject(obj->getNameInDocument());
|
||||
}
|
||||
}
|
||||
@@ -1734,7 +1734,7 @@ void LinkBaseExtension::update(App::DocumentObject *parent, const Property *prop
|
||||
}
|
||||
getElementListProperty()->setValue(objs);
|
||||
for(auto obj : tmpObjs) {
|
||||
if(obj && obj->getNameInDocument())
|
||||
if(obj && obj->isAttachedToDocument())
|
||||
obj->getDocument()->removeObject(obj->getNameInDocument());
|
||||
}
|
||||
}
|
||||
@@ -1859,7 +1859,7 @@ void LinkBaseExtension::cacheChildLabel(int enable) const {
|
||||
|
||||
int idx = 0;
|
||||
for(auto child : _getElementListValue()) {
|
||||
if(child && child->getNameInDocument())
|
||||
if(child && child->isAttachedToDocument())
|
||||
myLabelCache[child->Label.getStrValue()] = idx;
|
||||
++idx;
|
||||
}
|
||||
@@ -2021,7 +2021,7 @@ void LinkBaseExtension::setLink(int index, DocumentObject *obj,
|
||||
objs.push_back(elements[i]);
|
||||
}
|
||||
getElementListProperty()->setValue(objs);
|
||||
}else if(!obj->getNameInDocument())
|
||||
}else if(!obj->isAttachedToDocument())
|
||||
LINK_THROW(Base::ValueError,"Invalid object");
|
||||
else{
|
||||
if(index>(int)elements.size())
|
||||
@@ -2075,7 +2075,7 @@ void LinkBaseExtension::setLink(int index, DocumentObject *obj,
|
||||
|
||||
auto xlink = freecad_dynamic_cast<PropertyXLink>(linkProp);
|
||||
if(obj) {
|
||||
if(!obj->getNameInDocument())
|
||||
if(!obj->isAttachedToDocument())
|
||||
LINK_THROW(Base::ValueError,"Invalid document object");
|
||||
if(!xlink) {
|
||||
if(parent && obj->getDocument()!=parent->getDocument())
|
||||
@@ -2113,7 +2113,7 @@ void LinkBaseExtension::detachElements()
|
||||
}
|
||||
|
||||
void LinkBaseExtension::detachElement(DocumentObject *obj) {
|
||||
if(!obj || !obj->getNameInDocument() || obj->isRemoving())
|
||||
if(!obj || !obj->isAttachedToDocument() || obj->isRemoving())
|
||||
return;
|
||||
auto ext = obj->getExtensionByType<LinkBaseExtension>(true);
|
||||
auto owner = getContainer();
|
||||
|
||||
Reference in New Issue
Block a user