Mercurial > hg > orthanc-python
annotate CodeAnalysis/Class.mustache @ 168:f5f3cc819f7a OrthancPython-4.0
closing OrthancPython-4.0
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 30 May 2024 22:28:41 +0200 |
parents | eb6ac5a801d1 |
children | 65ec5597ec70 5643e97d9367 |
rev | line source |
---|---|
0 | 1 /** |
2 * Python plugin for Orthanc | |
101 | 3 * Copyright (C) 2020-2022 Osimis S.A., Belgium |
4 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium | |
0 | 5 * |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU Affero General Public License | |
8 * as published by the Free Software Foundation, either version 3 of | |
9 * the License, or (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * Affero General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Affero General Public License | |
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 **/ | |
19 | |
20 | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
21 // Forward declaration of the autogenerated methods |
0 | 22 {{#methods}} |
23 static PyObject *sdk_{{class_name}}_{{c_function}}( | |
24 sdk_{{class_name}}_Object* self, PyObject *args); | |
25 {{/methods}} | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
26 // End of forward declarations |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
27 |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
28 |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
29 // Forward declaration of the custom methods |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
30 {{#custom_methods}} |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
31 extern PyObject *{{implementation}}( |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
32 sdk_{{class_name}}_Object* self, PyObject *args); |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
33 {{/custom_methods}} |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
34 // End of forward declarations |
0 | 35 |
36 | |
37 static PyMethodDef sdk_{{class_name}}_Methods[] = { | |
38 {{#methods}} | |
39 { "{{short_name}}", | |
40 (PyCFunction) sdk_{{class_name}}_{{c_function}}, METH_VARARGS, | |
41 "Generated from C function {{c_function}}()" }, | |
42 {{/methods}} | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
43 {{#custom_methods}} |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
44 { "{{method_name}}", |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
45 (PyCFunction) {{implementation}}, METH_VARARGS, |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
46 "Generated from C function {{sdk_function}}()" }, |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
47 {{/custom_methods}} |
0 | 48 { NULL } /* Sentinel */ |
49 }; | |
50 | |
51 | |
52 static int sdk_{{class_name}}_Constructor( | |
53 sdk_{{class_name}}_Object *self, PyObject *args, PyObject *kwds) | |
54 { | |
28
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
55 PythonLock::LogCall("Creating Python object of class {{class_name}}"); |
0 | 56 |
57 self->object_ = NULL; | |
58 self->borrowed_ = false; | |
59 | |
60 long long object = 0; | |
61 unsigned char borrowed = false; | |
62 | |
63 if (PyArg_ParseTuple(args, "Lb", &object, &borrowed)) | |
64 { | |
65 self->object_ = reinterpret_cast<{{class_name}}*>(static_cast<intptr_t>(object)); | |
66 self->borrowed_ = borrowed; | |
67 return 0; | |
68 } | |
69 else | |
70 { | |
71 PyErr_SetString(PyExc_ValueError, "Expected a pair (pointer, borrowed) in the constructor"); | |
72 return -1; | |
73 } | |
74 } | |
75 | |
76 | |
77 /** | |
78 * Static global structure => the fields that are beyond the last | |
79 * initialized field are set to zero. | |
80 * https://stackoverflow.com/a/11152199/881731 | |
81 **/ | |
82 static PyTypeObject sdk_{{class_name}}_Type = { | |
83 PyVarObject_HEAD_INIT(NULL, 0) | |
84 "orthanc.{{short_name}}", /* tp_name */ | |
85 sizeof(sdk_{{class_name}}_Object), /* tp_basicsize */ | |
86 }; | |
87 | |
88 | |
89 {{#destructor}} | |
90 static void sdk_{{class_name}}_Destructor(PyObject *self) | |
91 { | |
28
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
92 PythonLock::LogCall("Destroying Python object of class {{class_name}}"); |
0 | 93 |
94 sdk_{{class_name}}_Object& tmp = *((sdk_{{class_name}}_Object*) self); | |
95 | |
96 if (tmp.object_ != NULL && | |
97 !tmp.borrowed_) | |
98 { | |
99 {{destructor}}(OrthancPlugins::GetGlobalContext(), tmp.object_); | |
100 tmp.object_ = NULL; | |
101 } | |
102 | |
103 Py_TYPE(self)->tp_free((PyObject *)self); | |
104 } | |
105 {{/destructor}} | |
106 | |
107 | |
108 // Actual implementation of the methods | |
109 {{#methods}} | |
110 static PyObject *sdk_{{class_name}}_{{c_function}}( | |
111 sdk_{{class_name}}_Object* self, PyObject *args) | |
112 { | |
28
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
113 PythonLock::LogCall("Calling method {{c_function}}() on object of class {{class_name}}"); |
0 | 114 |
115 if (self->object_ == NULL) | |
116 { | |
117 PyErr_SetString(PyExc_ValueError, "Invalid object"); | |
118 return NULL; | |
119 } | |
120 | |
121 {{> function_body}} | |
122 } | |
123 | |
124 {{/methods}} | |
125 | |
126 | |
127 static void Register{{class_name}}Class(PyObject* module) | |
128 { | |
129 sdk_{{class_name}}_Type.tp_new = PyType_GenericNew; | |
130 sdk_{{class_name}}_Type.tp_flags = Py_TPFLAGS_DEFAULT; | |
131 sdk_{{class_name}}_Type.tp_doc = "Generated from Orthanc C class: {{class_name}}"; | |
132 sdk_{{class_name}}_Type.tp_methods = sdk_{{class_name}}_Methods; | |
133 sdk_{{class_name}}_Type.tp_init = (initproc) sdk_{{class_name}}_Constructor; | |
134 | |
135 {{#destructor}} | |
136 /** | |
137 * "tp_dealloc is called when the reference count of the object goes | |
138 * down to zero. This is where you destroy the object and its | |
139 * members. It should then free the memory occupied by the object by | |
140 * calling tp_free." | |
141 * https://stackoverflow.com/a/24863227/881731 | |
142 **/ | |
143 sdk_{{class_name}}_Type.tp_dealloc = sdk_{{class_name}}_Destructor; | |
144 {{/destructor}} | |
145 | |
146 if (PyType_Ready(&sdk_{{class_name}}_Type) < 0) | |
147 { | |
148 OrthancPlugins::LogError("Cannot register Python class: {{class_name}}"); | |
149 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
150 } | |
151 | |
152 Py_INCREF(&sdk_{{class_name}}_Type); | |
153 if (PyModule_AddObject(module, "{{short_name}}", (PyObject *)&sdk_{{class_name}}_Type) < 0) | |
154 { | |
155 OrthancPlugins::LogError("Cannot register Python class: {{class_name}}"); | |
156 Py_DECREF(&sdk_{{class_name}}_Type); | |
157 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
158 } | |
159 } | |
160 | |
161 | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
162 PyTypeObject* Get{{class_name}}Type() |
0 | 163 { |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
164 return &sdk_{{class_name}}_Type; |
0 | 165 } |