Mercurial > hg > orthanc-python
diff Sources/OnStoredInstanceCallback.cpp @ 68:0b3ef425db31
refactoring using ICallbackRegistration::Apply()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 11 Jun 2021 14:34:08 +0200 |
parents | 32de70a1e4c7 |
children | 627b8a19fb9f |
line wrap: on
line diff
--- 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_); }