From e10b23ca70079993ada128750ab10df557aecdf7 Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 17 Nov 2022 17:38:31 +0100 Subject: [PATCH] App: replace boolean with enum --- src/App/Application.cpp | 22 +++++++++++++++------- src/App/Application.h | 11 +++++++++-- src/App/ApplicationPy.cpp | 2 +- src/App/Document.cpp | 2 +- src/App/Link.cpp | 2 +- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/App/Application.cpp b/src/App/Application.cpp index 30dc9de779..47a82cba73 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -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 Application::getLinksTo( diff --git a/src/App/Application.h b/src/App/Application.h index 5542b5961e..2607a64f0a 100644 --- a/src/App/Application.h +++ b/src/App/Application.h @@ -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 * diff --git a/src/App/ApplicationPy.cpp b/src/App/ApplicationPy.cpp index 105fa53580..63d449b5af 100644 --- a/src/App/ApplicationPy.cpp +++ b/src/App/ApplicationPy.cpp @@ -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; } diff --git a/src/App/Document.cpp b/src/App/Document.cpp index 634b13b222..2c03c50354 100644 --- a/src/App/Document.cpp +++ b/src/App/Document.cpp @@ -3021,7 +3021,7 @@ void Document::getLinksTo(std::set &links, std::vector current(1,obj); for(int depth=0;!current.empty();++depth) { - if(!GetApplication().checkLinkDepth(depth,true)) + if(!GetApplication().checkLinkDepth(depth, MessageOption::Error)) break; std::vector next; for(const App::DocumentObject *o : current) { diff --git a/src/App/Link.cpp b/src/App/Link.cpp index a180b2be38..6229399dd1 100644 --- a/src/App/Link.cpp +++ b/src/App/Link.cpp @@ -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();