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

@@ -611,7 +611,7 @@ bool ExportOCAF2::canFallback(std::vector<App::DocumentObject*> objs)
{
for (size_t i = 0; i < objs.size(); ++i) {
auto obj = objs[i];
if (!obj || !obj->getNameInDocument()) {
if (!obj || !obj->isAttachedToDocument()) {
continue;
}
if (obj->getExtensionByType<App::LinkBaseExtension>(true)) {

View File

@@ -448,7 +448,7 @@ TopoShape Feature::getTopoShape(const App::DocumentObject *obj, const char *subn
bool needSubElement, Base::Matrix4D *pmat, App::DocumentObject **powner,
bool resolveLink, bool transform, bool noElementMap)
{
if(!obj || !obj->getNameInDocument())
if(!obj || !obj->isAttachedToDocument())
return {};
std::vector<App::DocumentObject*> linkStack;

View File

@@ -1003,7 +1003,7 @@ void TaskAttacher::visibilityAutomation(bool opening_not_closing)
return;
if (!ViewProvider->getObject())
return;
if (!ViewProvider->getObject()->getNameInDocument())
if (!ViewProvider->getObject()->isAttachedToDocument())
return;
auto editDoc = Gui::Application::Instance->editDocument();

View File

@@ -396,11 +396,11 @@ App::DocumentObject* SubShapeBinder::getSubObject(const char* subname, PyObject*
std::string name(subname, dot - subname);
for (auto& l : Support.getSubListValues()) {
auto obj = l.getValue();
if (!obj || !obj->getNameInDocument())
if (!obj || !obj->isAttachedToDocument())
continue;
for (auto& sub : l.getSubValues()) {
auto sobj = obj->getSubObject(sub.c_str());
if (!sobj || !sobj->getNameInDocument())
if (!sobj || !sobj->isAttachedToDocument())
continue;
if (subname[0] == '$') {
if (sobj->Label.getStrValue() != name.c_str() + 1)
@@ -512,7 +512,7 @@ void SubShapeBinder::update(SubShapeBinder::UpdateOption options) {
std::unordered_map<const App::DocumentObject*, Base::Matrix4D> mats;
for (auto& l : Support.getSubListValues()) {
auto obj = l.getValue();
if (!obj || !obj->getNameInDocument())
if (!obj || !obj->isAttachedToDocument())
continue;
auto res = mats.emplace(obj, Base::Matrix4D());
if (parent && res.second) {
@@ -909,7 +909,7 @@ void SubShapeBinder::setLinks(std::map<App::DocumentObject*, std::vector<std::st
inSet.insert(this);
for (auto& v : values) {
if (!v.first || !v.first->getNameInDocument())
if (!v.first || !v.first->isAttachedToDocument())
FC_THROWM(Base::ValueError, "Invalid document object");
if (inSet.find(v.first) != inSet.end())
FC_THROWM(Base::ValueError, "Cyclic reference to " << v.first->getFullName());

View File

@@ -337,7 +337,7 @@ void TaskDlgBooleanParameters::clicked(int)
bool TaskDlgBooleanParameters::accept()
{
auto obj = BooleanView->getObject();
if(!obj || !obj->getNameInDocument())
if(!obj || !obj->isAttachedToDocument())
return false;
BooleanView->Visibility.setValue(true);

View File

@@ -73,7 +73,7 @@ App::DocumentObject* getParent(App::DocumentObject* obj, std::string& subname)
}
bool setEdit(App::DocumentObject *obj, PartDesign::Body *body) {
if (!obj || !obj->getNameInDocument()) {
if (!obj || !obj->isAttachedToDocument()) {
FC_ERR("invalid object");
return false;
}

View File

@@ -324,7 +324,7 @@ bool ViewProviderSubShapeBinder::setEdit(int ModNum) {
Gui::Selection().clearSelection();
for (auto& link : self->Support.getSubListValues()) {
auto obj = link.getValue();
if (!obj || !obj->getNameInDocument())
if (!obj || !obj->isAttachedToDocument())
continue;
const auto& subs = link.getSubValues();
if (!subs.empty())

View File

@@ -43,7 +43,7 @@ PyObject* FeaturePathCompoundPy::addObject(PyObject *args)
return nullptr;
DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) {
if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->isAttachedToDocument()) {
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot add an invalid object");
return nullptr;
}
@@ -88,7 +88,7 @@ PyObject* FeaturePathCompoundPy::removeObject(PyObject *args)
return nullptr;
DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) {
if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->isAttachedToDocument()) {
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot remove an invalid object");
return nullptr;
}

View File

@@ -1668,7 +1668,7 @@ void PropertySheet::recomputeDependencies(CellAddress key)
void PropertySheet::hasSetValue()
{
if (updateCount == 0 || !owner || !owner->getNameInDocument() || owner->isRestoring()
if (updateCount == 0 || !owner || !owner->isAttachedToDocument() || owner->isRestoring()
|| this != &owner->cells || testFlag(LinkDetached)) {
PropertyExpressionContainer::hasSetValue();
return;

View File

@@ -279,8 +279,7 @@ int DrawPage::removeView(App::DocumentObject* docObj)
return -1;
}
const char* name = docObj->getNameInDocument();
if (!name) {
if (!docObj->isAttachedToDocument()) {
return -1;
}
const std::vector<App::DocumentObject*> currViews = Views.getValues();
@@ -292,7 +291,7 @@ int DrawPage::removeView(App::DocumentObject* docObj)
continue;
}
std::string viewName = name;
std::string viewName = docObj->getNameInDocument();
if (viewName.compare((*it)->getNameInDocument()) != 0) {
newViews.push_back((*it));
}

View File

@@ -939,7 +939,7 @@ void QGSPage::fixOrphans(bool force)
// if we ever have collections of collections, we'll need to revisit this
TechDraw::DrawPage* thisPage = m_vpPage->getDrawPage();
if (!thisPage->getNameInDocument())
if (!thisPage->isAttachedToDocument())
return;
std::vector<App::DocumentObject*> pChildren = thisPage->getAllViews();

View File

@@ -85,11 +85,10 @@ void ViewProviderDrawingView::attach(App::DocumentObject *pcFeat)
//NOLINTEND
auto feature = getViewObject();
if (feature) {
const char* temp = feature->getNameInDocument();
if (temp) {
if (feature->isAttachedToDocument()) {
// it could happen that feature is not completely in the document yet and getNameInDocument returns
// nullptr, so we only update m_myName if we got a valid string.
m_myName = temp;
m_myName = feature->getNameInDocument();
}
connectGuiRepaint = feature->signalGuiPaint.connect(bnd);
connectProgressMessage = feature->signalProgressMessage.connect(bndProgressMessage);

View File

@@ -120,11 +120,10 @@ void ViewProviderPage::attach(App::DocumentObject* pcFeat)
TechDraw::DrawPage* feature = dynamic_cast<TechDraw::DrawPage*>(pcFeat);
if (feature) {
connectGuiRepaint = feature->signalGuiPaint.connect(bnd);
const char* temp = feature->getNameInDocument();
if (temp) {
if (feature->isAttachedToDocument()) {
// it could happen that feature is not completely in the document yet and getNameInDocument returns
// nullptr, so we only update m_myName if we got a valid string.
m_pageName = temp;
m_pageName = feature->getNameInDocument();
}
m_graphicsScene->setObjectName(QString::fromLocal8Bit(m_pageName.c_str()));
}