Merge pull request #19019 from hyarion/refactor/countObjectsOfType
Refactor countObjectsOfType in selection and document
This commit is contained in:
@@ -4397,14 +4397,15 @@ Document::findObjects(const Base::Type& typeId, const char* objname, const char*
|
||||
|
||||
int Document::countObjectsOfType(const Base::Type& typeId) const
|
||||
{
|
||||
int ct = 0;
|
||||
for (const auto& it : d->objectMap) {
|
||||
if (it.second->getTypeId().isDerivedFrom(typeId)) {
|
||||
ct++;
|
||||
}
|
||||
}
|
||||
return std::count_if(d->objectMap.begin(), d->objectMap.end(), [&](const auto& it) {
|
||||
return it.second->getTypeId().isDerivedFrom(typeId);
|
||||
});
|
||||
}
|
||||
|
||||
return ct;
|
||||
int Document::countObjectsOfType(const char* typeName) const
|
||||
{
|
||||
Base::Type type = Base::Type::fromName(typeName);
|
||||
return type.isBad() ? 0 : countObjectsOfType(type);
|
||||
}
|
||||
|
||||
PyObject* Document::getPyObject()
|
||||
|
||||
@@ -338,7 +338,9 @@ public:
|
||||
/// Returns an array with the correct types already.
|
||||
template<typename T>
|
||||
inline std::vector<T*> getObjectsOfType() const;
|
||||
int countObjectsOfType(const Base::Type& typeId) const;
|
||||
template<typename T>
|
||||
inline int countObjectsOfType() const;
|
||||
int countObjectsOfType(const char* typeName) const;
|
||||
/// get the number of objects in the document
|
||||
int countObjects() const;
|
||||
//@}
|
||||
@@ -590,6 +592,7 @@ protected:
|
||||
std::vector<App::DocumentObject*> readObjects(Base::XMLReader& reader);
|
||||
void writeObjects(const std::vector<App::DocumentObject*>&, Base::Writer& writer) const;
|
||||
bool saveToFile(const char* filename) const;
|
||||
int countObjectsOfType(const Base::Type& typeId) const;
|
||||
|
||||
void onBeforeChange(const Property* prop) override;
|
||||
void onChanged(const Property* prop) override;
|
||||
@@ -651,6 +654,14 @@ inline std::vector<T*> Document::getObjectsOfType() const
|
||||
return type;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline int Document::countObjectsOfType() const
|
||||
{
|
||||
static_assert(std::is_base_of<App::DocumentObject, T>::value,
|
||||
"T must be derived from App::DocumentObject");
|
||||
return this->countObjectsOfType(T::getClassTypeId());
|
||||
}
|
||||
|
||||
|
||||
} // namespace App
|
||||
|
||||
|
||||
Reference in New Issue
Block a user