comparison CodeAnalysis/Class.mustache @ 0:7ed502b17b8f

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