Mercurial > hg > orthanc-python
changeset 68:0b3ef425db31
refactoring using ICallbackRegistration::Apply()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 11 Jun 2021 14:34:08 +0200 |
parents | 9d0460d0f15b |
children | 07fdf388c12e |
files | Sources/IncomingHttpRequestFilter.cpp Sources/OnChangeCallback.cpp Sources/OnStoredInstanceCallback.cpp Sources/PythonFunction.cpp Sources/PythonModule.cpp Sources/PythonObject.cpp Sources/RestCallbacks.cpp |
diffstat | 7 files changed, 50 insertions(+), 114 deletions(-) [+] |
line wrap: on
line diff
--- a/Sources/IncomingHttpRequestFilter.cpp Fri Jun 11 14:13:36 2021 +0200 +++ b/Sources/IncomingHttpRequestFilter.cpp Fri Jun 11 14:34:08 2021 +0200 @@ -19,10 +19,10 @@ #include "IncomingHttpRequestFilter.h" -#include "PythonString.h" +#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" #include "Autogenerated/sdk.h" - -#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" +#include "ICallbackRegistration.h" +#include "PythonString.h" static PyObject* incomingHttpRequestFilter_ = NULL; @@ -117,42 +117,24 @@ PyObject* RegisterIncomingHttpRequestFilter(PyObject* module, PyObject* args) { // The GIL is locked at this point (no need to create "PythonLock") - - // https://docs.python.org/3/extending/extending.html#calling-python-functions-from-c - PyObject* callback = NULL; - if (!PyArg_ParseTuple(args, "O", &callback) || - callback == NULL) - { - PyErr_SetString(PyExc_ValueError, "Expected a callback function"); - return NULL; - } - - if (incomingHttpRequestFilter_ != NULL) + class Registration : public ICallbackRegistration { - PyErr_SetString(PyExc_RuntimeError, "Can only register one Python incoming-http-request filter"); - return NULL; - } - - OrthancPlugins::LogInfo("Registering a Python incoming-http-request filter"); + public: + virtual void Register() ORTHANC_OVERRIDE + { + OrthancPluginRegisterIncomingHttpRequestFilter2( + OrthancPlugins::GetGlobalContext(), IncomingHttpRequestFilter); + } + }; - OrthancPluginRegisterIncomingHttpRequestFilter2( - OrthancPlugins::GetGlobalContext(), IncomingHttpRequestFilter); - - incomingHttpRequestFilter_ = callback; - Py_XINCREF(incomingHttpRequestFilter_); - - Py_INCREF(Py_None); - return Py_None; + Registration registration; + return ICallbackRegistration::Apply( + registration, args, incomingHttpRequestFilter_, "Python incoming-http-request filter"); } void FinalizeIncomingHttpRequestFilter() { - PythonLock lock; - - if (incomingHttpRequestFilter_ != NULL) - { - Py_XDECREF(incomingHttpRequestFilter_); - } + ICallbackRegistration::Unregister(incomingHttpRequestFilter_); }
--- a/Sources/OnChangeCallback.cpp Fri Jun 11 14:13:36 2021 +0200 +++ b/Sources/OnChangeCallback.cpp Fri Jun 11 14:34:08 2021 +0200 @@ -19,10 +19,10 @@ #include "OnChangeCallback.h" +#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" +#include "ICallbackRegistration.h" #include "PythonString.h" -#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" - #include <Compatibility.h> // For std::unique_ptr<> #include <boost/thread.hpp> @@ -208,50 +208,27 @@ PyObject* RegisterOnChangeCallback(PyObject* module, PyObject* args) { // The GIL is locked at this point (no need to create "PythonLock") - - // https://docs.python.org/3/extending/extending.html#calling-python-functions-from-c - PyObject* callback = NULL; - if (!PyArg_ParseTuple(args, "O", &callback) || - callback == NULL) - { - PyErr_SetString(PyExc_ValueError, "Expected a callback function"); - return NULL; - } - - if (changesCallback_ != NULL) + class Registration : public ICallbackRegistration { - PyErr_SetString(PyExc_RuntimeError, "Can only register one Python on-changes callback"); - return NULL; - } - - OrthancPlugins::LogInfo("Registering a Python on-changes callback"); - - OrthancPluginRegisterOnChangeCallback(OrthancPlugins::GetGlobalContext(), OnChangeCallback); + public: + virtual void Register() ORTHANC_OVERRIDE + { + OrthancPluginRegisterOnChangeCallback(OrthancPlugins::GetGlobalContext(), OnChangeCallback); - stopping_ = false; - changesThread_ = boost::thread(ChangesWorker); + stopping_ = false; + changesThread_ = boost::thread(ChangesWorker); + } + }; - changesCallback_ = callback; - Py_XINCREF(changesCallback_); - - Py_INCREF(Py_None); - return Py_None; + Registration registration; + return ICallbackRegistration::Apply( + registration, args, changesCallback_, "Python on-changes callback"); } - - void FinalizeOnChangeCallback() { StopThread(); - - { - PythonLock lock; - - if (changesCallback_ != NULL) - { - Py_XDECREF(changesCallback_); - } - } + ICallbackRegistration::Unregister(changesCallback_); }
--- a/Sources/OnStoredInstanceCallback.cpp Fri Jun 11 14:13:36 2021 +0200 +++ b/Sources/OnStoredInstanceCallback.cpp Fri Jun 11 14:34:08 2021 +0200 @@ -19,10 +19,10 @@ #include "OnStoredInstanceCallback.h" -#include "PythonString.h" +#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" #include "Autogenerated/sdk.h" - -#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" +#include "ICallbackRegistration.h" +#include "PythonString.h" static PyObject* storedInstanceCallback_ = NULL; @@ -78,42 +78,24 @@ PyObject* RegisterOnStoredInstanceCallback(PyObject* module, PyObject* args) { // The GIL is locked at this point (no need to create "PythonLock") - - // https://docs.python.org/3/extending/extending.html#calling-python-functions-from-c - PyObject* callback = NULL; - if (!PyArg_ParseTuple(args, "O", &callback) || - callback == NULL) - { - PyErr_SetString(PyExc_ValueError, "Expected a callback function"); - return NULL; - } - - if (storedInstanceCallback_ != NULL) + class Registration : public ICallbackRegistration { - PyErr_SetString(PyExc_RuntimeError, "Can only register one Python on-stored-instance callback"); - return NULL; - } - - OrthancPlugins::LogInfo("Registering a Python on-stored-instance callback"); + public: + virtual void Register() ORTHANC_OVERRIDE + { + OrthancPluginRegisterOnStoredInstanceCallback( + OrthancPlugins::GetGlobalContext(), OnStoredInstanceCallback); + } + }; - OrthancPluginRegisterOnStoredInstanceCallback( - OrthancPlugins::GetGlobalContext(), OnStoredInstanceCallback); - - storedInstanceCallback_ = callback; - Py_XINCREF(storedInstanceCallback_); - - Py_INCREF(Py_None); - return Py_None; + Registration registration; + return ICallbackRegistration::Apply( + registration, args, storedInstanceCallback_, "Python on-stored-instance callback"); } void FinalizeOnStoredInstanceCallback() { - PythonLock lock; - - if (storedInstanceCallback_ != NULL) - { - Py_XDECREF(storedInstanceCallback_); - } + ICallbackRegistration::Unregister(storedInstanceCallback_); }
--- a/Sources/PythonFunction.cpp Fri Jun 11 14:13:36 2021 +0200 +++ b/Sources/PythonFunction.cpp Fri Jun 11 14:34:08 2021 +0200 @@ -19,10 +19,9 @@ #include "PythonFunction.h" +#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" #include "PythonModule.h" -#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" - PythonObject* PythonFunction::CallUnchecked(PyObject* args) {
--- a/Sources/PythonModule.cpp Fri Jun 11 14:13:36 2021 +0200 +++ b/Sources/PythonModule.cpp Fri Jun 11 14:34:08 2021 +0200 @@ -19,10 +19,9 @@ #include "PythonModule.h" +#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" #include "PythonString.h" -#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" - #include <boost/thread/mutex.hpp> #include <set>
--- a/Sources/PythonObject.cpp Fri Jun 11 14:13:36 2021 +0200 +++ b/Sources/PythonObject.cpp Fri Jun 11 14:34:08 2021 +0200 @@ -19,11 +19,9 @@ #include "PythonObject.h" - +#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" #include "PythonLock.h" -#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" - PythonObject::PythonObject(PythonLock& lock, PyObject *object,
--- a/Sources/RestCallbacks.cpp Fri Jun 11 14:13:36 2021 +0200 +++ b/Sources/RestCallbacks.cpp Fri Jun 11 14:34:08 2021 +0200 @@ -19,10 +19,9 @@ #include "RestCallbacks.h" -#include "PythonString.h" +#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" #include "Autogenerated/sdk.h" - -#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" +#include "PythonString.h" #include <boost/regex.hpp>