Mercurial > hg > orthanc-python
annotate CodeAnalysis/Enumeration.mustache @ 158:5adf2e1186ab OrthancPython-4.2
4.2
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Thu, 16 May 2024 12:21:18 +0200 |
parents | 71d305c29cfa |
children | 6fada29b6759 |
rev | line source |
---|---|
0 | 1 /** |
2 * Python plugin for Orthanc | |
155
71d305c29cfa
updated year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
131
diff
changeset
|
3 * Copyright (C) 2020-2024 Osimis S.A., Belgium |
71d305c29cfa
updated year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
131
diff
changeset
|
4 * Copyright (C) 2021-2024 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 | |
21 typedef struct | |
22 { | |
23 PyObject_HEAD | |
24 } sdk_{{name}}_Object; | |
25 | |
26 | |
27 /** | |
28 * Static global structure => the fields that are beyond the last | |
29 * initialized field are set to zero. | |
30 * https://stackoverflow.com/a/11152199/881731 | |
31 **/ | |
32 static PyTypeObject sdk_{{name}}_Type = { | |
33 PyVarObject_HEAD_INIT(NULL, 0) | |
34 "orthanc.{{short_name}}", /* tp_name */ | |
35 sizeof(sdk_{{name}}_Object), /* tp_basicsize */ | |
36 }; | |
37 | |
38 | |
39 void Register{{name}}Enumeration(PyObject* module) | |
40 { | |
41 sdk_{{name}}_Type.tp_new = PyType_GenericNew; | |
42 sdk_{{name}}_Type.tp_flags = Py_TPFLAGS_DEFAULT; | |
43 sdk_{{name}}_Type.tp_doc = "Generated from C enumeration OrthancPlugin{{name}}"; | |
44 | |
45 sdk_{{name}}_Type.tp_dict = PyDict_New(); | |
46 | |
47 if (PyType_Ready(&sdk_{{name}}_Type) < 0) | |
48 { | |
49 OrthancPlugins::LogError("Cannot register Python enumeration: {{name}}"); | |
50 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
51 } | |
52 | |
53 /** | |
54 * Declare constants here (static members = class attributes) | |
55 * https://stackoverflow.com/a/8017906/881731 | |
56 * | |
57 * "Static and class methods can be defined in tp_methods by adding | |
58 * METH_STATIC or METH_CLASS to the ml_flags field of the | |
59 * PyMethodDef structure. This is equivalent to @staticmethod and | |
60 * @classmethod decorators." | |
61 * | |
62 * "Class attributes can be added by setting the tp_dict to a | |
63 * dictionary with these attributes before calling PyType_Ready() | |
64 * (in your module initialization function)." | |
65 **/ | |
66 | |
67 {{#values}} | |
128
5b59ebc267e1
fixed leaks in the initialization of the enums
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
68 { |
5b59ebc267e1
fixed leaks in the initialization of the enums
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
69 PyObject* tmp = PyLong_FromLong({{value}}); |
5b59ebc267e1
fixed leaks in the initialization of the enums
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
70 PyDict_SetItemString(sdk_{{name}}_Type.tp_dict, "{{key}}", tmp); |
5b59ebc267e1
fixed leaks in the initialization of the enums
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
71 Py_DECREF(tmp); |
5b59ebc267e1
fixed leaks in the initialization of the enums
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
72 } |
5b59ebc267e1
fixed leaks in the initialization of the enums
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
73 |
0 | 74 {{/values}} |
75 | |
76 Py_INCREF(&sdk_{{name}}_Type); | |
77 if (PyModule_AddObject(module, "{{short_name}}", (PyObject *)&sdk_{{name}}_Type) < 0) | |
78 { | |
79 OrthancPlugins::LogError("Cannot register Python enumeration: {{name}}"); | |
80 Py_DECREF(&sdk_{{name}}_Type); | |
81 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
82 } | |
83 } |