App: replace boolean with enum

This commit is contained in:
wmayer
2022-11-17 17:38:31 +01:00
parent 07711bb8a8
commit e10b23ca70
5 changed files with 27 additions and 12 deletions

View File

@@ -1150,22 +1150,30 @@ std::string Application::getHelpDir()
#endif
}
int Application::checkLinkDepth(int depth, bool no_throw) {
if(_objCount<0) {
int Application::checkLinkDepth(int depth, MessageOption option)
{
if (_objCount < 0) {
_objCount = 0;
for(auto &v : DocMap)
for (auto &v : DocMap) {
_objCount += v.second->countObjects();
}
}
if(depth > _objCount+2) {
if (depth > _objCount + 2) {
const char *msg = "Link recursion limit reached. "
"Please check for cyclic reference.";
if(no_throw) {
switch (option) {
case MessageOption::Quiet:
return 0;
case MessageOption::Error:
FC_ERR(msg);
return 0;
}else
case MessageOption::Throw:
throw Base::RuntimeError(msg);
}
}
return _objCount+2;
return _objCount + 2;
}
std::set<DocumentObject *> Application::getLinksTo(

View File

@@ -63,6 +63,12 @@ enum GetLinkOption {
GetLinkExternal = 8,
};
enum class MessageOption {
Quiet, /**< Suppress error. */
Error, /**< Print an error message. */
Throw, /**< Throw an execption. */
};
/** The Application
* The root of the whole application
@@ -420,14 +426,15 @@ public:
/** Check for link recursion depth
*
* @param depth: current depth
* @param no_throw: whether to throw exception
* @param option: whether to throw exception, print an error message or quieten any output.
* In the latter case the caller must check the returned value.
*
* @return Return the maximum remaining depth.
*
* The function uses an internal count of all objects in all documents as
* the limit of recursion depth.
*/
int checkLinkDepth(int depth, bool no_throw=true);
int checkLinkDepth(int depth, MessageOption option = MessageOption::Error);
/** Return the links to a given object
*

View File

@@ -832,7 +832,7 @@ PyObject *Application::sCheckLinkDepth(PyObject * /*self*/, PyObject *args)
return nullptr;
PY_TRY {
return Py::new_reference_to(Py::Int(GetApplication().checkLinkDepth(depth,false)));
return Py::new_reference_to(Py::Int(GetApplication().checkLinkDepth(depth, MessageOption::Throw)));
}PY_CATCH;
}

View File

@@ -3021,7 +3021,7 @@ void Document::getLinksTo(std::set<DocumentObject*> &links,
std::vector<const DocumentObject*> current(1,obj);
for(int depth=0;!current.empty();++depth) {
if(!GetApplication().checkLinkDepth(depth,true))
if(!GetApplication().checkLinkDepth(depth, MessageOption::Error))
break;
std::vector<const DocumentObject*> next;
for(const App::DocumentObject *o : current) {

View File

@@ -1043,7 +1043,7 @@ DocumentObject *LinkBaseExtension::getContainer(){
}
DocumentObject *LinkBaseExtension::getLink(int depth) const{
if (!GetApplication().checkLinkDepth(depth,true))
if (!GetApplication().checkLinkDepth(depth, MessageOption::Error))
return nullptr;
if(getLinkedObjectProperty())
return getLinkedObjectValue();