Mercurial > hg > orthanc-python
comparison 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 |
comparison
equal
deleted
inserted
replaced
67:9d0460d0f15b | 68:0b3ef425db31 |
---|---|
17 **/ | 17 **/ |
18 | 18 |
19 | 19 |
20 #include "OnStoredInstanceCallback.h" | 20 #include "OnStoredInstanceCallback.h" |
21 | 21 |
22 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" | |
23 #include "Autogenerated/sdk.h" | |
24 #include "ICallbackRegistration.h" | |
22 #include "PythonString.h" | 25 #include "PythonString.h" |
23 #include "Autogenerated/sdk.h" | |
24 | |
25 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" | |
26 | 26 |
27 | 27 |
28 static PyObject* storedInstanceCallback_ = NULL; | 28 static PyObject* storedInstanceCallback_ = NULL; |
29 | 29 |
30 | 30 |
76 | 76 |
77 | 77 |
78 PyObject* RegisterOnStoredInstanceCallback(PyObject* module, PyObject* args) | 78 PyObject* RegisterOnStoredInstanceCallback(PyObject* module, PyObject* args) |
79 { | 79 { |
80 // The GIL is locked at this point (no need to create "PythonLock") | 80 // The GIL is locked at this point (no need to create "PythonLock") |
81 | |
82 // https://docs.python.org/3/extending/extending.html#calling-python-functions-from-c | |
83 PyObject* callback = NULL; | |
84 | 81 |
85 if (!PyArg_ParseTuple(args, "O", &callback) || | 82 class Registration : public ICallbackRegistration |
86 callback == NULL) | |
87 { | 83 { |
88 PyErr_SetString(PyExc_ValueError, "Expected a callback function"); | 84 public: |
89 return NULL; | 85 virtual void Register() ORTHANC_OVERRIDE |
90 } | 86 { |
87 OrthancPluginRegisterOnStoredInstanceCallback( | |
88 OrthancPlugins::GetGlobalContext(), OnStoredInstanceCallback); | |
89 } | |
90 }; | |
91 | 91 |
92 if (storedInstanceCallback_ != NULL) | 92 Registration registration; |
93 { | 93 return ICallbackRegistration::Apply( |
94 PyErr_SetString(PyExc_RuntimeError, "Can only register one Python on-stored-instance callback"); | 94 registration, args, storedInstanceCallback_, "Python on-stored-instance callback"); |
95 return NULL; | |
96 } | |
97 | |
98 OrthancPlugins::LogInfo("Registering a Python on-stored-instance callback"); | |
99 | |
100 OrthancPluginRegisterOnStoredInstanceCallback( | |
101 OrthancPlugins::GetGlobalContext(), OnStoredInstanceCallback); | |
102 | |
103 storedInstanceCallback_ = callback; | |
104 Py_XINCREF(storedInstanceCallback_); | |
105 | |
106 Py_INCREF(Py_None); | |
107 return Py_None; | |
108 } | 95 } |
109 | 96 |
110 | 97 |
111 void FinalizeOnStoredInstanceCallback() | 98 void FinalizeOnStoredInstanceCallback() |
112 { | 99 { |
113 PythonLock lock; | 100 ICallbackRegistration::Unregister(storedInstanceCallback_); |
114 | |
115 if (storedInstanceCallback_ != NULL) | |
116 { | |
117 Py_XDECREF(storedInstanceCallback_); | |
118 } | |
119 } | 101 } |