comparison CodeGeneration/CppNativeSDK.mustache @ 0:3ecef5782f2c

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 18 Oct 2023 17:59:44 +0200
parents
children c8f19e93ff99
comparison
equal deleted inserted replaced
-1:000000000000 0:3ecef5782f2c
1 /**
2 * SPDX-FileCopyrightText: 2023 Sebastien Jodogne, UCLouvain, Belgium
3 * SPDX-License-Identifier: GPL-3.0-or-later
4 */
5
6 /**
7 * Java plugin for Orthanc
8 * Copyright (C) 2023 Sebastien Jodogne, UCLouvain, Belgium
9 *
10 * This program is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, either version 3 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 **/
23
24
25 {{#functions}}
26
27 JNIEXPORT {{return.c_type}} JNI_{{c_function}}(JNIEnv* env, jobject sdkObject{{#class_name}}, jlong self{{/class_name}}{{#args}}, {{c_type}} {{name}}{{/args}})
28 {
29 try
30 {
31 {{#args}}
32 {{#convert_string}}
33 JavaString c_{{name}}(env, {{name}});
34 {{/convert_string}}
35 {{#convert_bytes}}
36 JavaBytes c_{{name}}(env, {{name}});
37 {{/convert_bytes}}
38 {{/args}}
39
40 {{#return.is_void}}
41 {{c_function}}(context_
42 {{#class_name}}, reinterpret_cast<{{class_name}}*>(static_cast<intptr_t>(self)){{/class_name}}
43 {{#args}}, {{c_accessor}}{{/args}});
44 {{/return.is_void}}
45
46 {{#return.is_exception}}
47 OrthancPluginErrorCode code = {{c_function}}(context_
48 {{#class_name}}, reinterpret_cast<{{class_name}}*>(static_cast<intptr_t>(self)){{/class_name}}
49 {{#args}}, {{c_accessor}}{{/args}});
50 if (code != OrthancPluginErrorCode_Success)
51 {
52 JavaEnvironment::ThrowException(env, code);
53 }
54 {{/return.is_exception}}
55
56 {{#return.is_number}}
57 return {{c_function}}(context_
58 {{#class_name}}, reinterpret_cast<{{class_name}}*>(static_cast<intptr_t>(self)){{/class_name}}
59 {{#args}}, {{c_accessor}}{{/args}});
60 {{/return.is_number}}
61
62 {{#return.is_static_string}}
63 const char* s = {{c_function}}(context_
64 {{#class_name}}, reinterpret_cast<{{class_name}}*>(static_cast<intptr_t>(self)){{/class_name}}
65 {{#args}}, {{c_accessor}}{{/args}});
66 if (s == NULL)
67 {
68 JavaEnvironment::ThrowException(env, OrthancPluginErrorCode_Plugin);
69 return NULL;
70 }
71 else
72 {
73 return env->NewStringUTF(s);
74 }
75 {{/return.is_static_string}}
76
77 {{#return.is_dynamic_string}}
78 OrthancString s({{c_function}}(context_
79 {{#class_name}}, reinterpret_cast<{{class_name}}*>(static_cast<intptr_t>(self)){{/class_name}}
80 {{#args}}, {{c_accessor}}{{/args}}));
81 if (s.GetValue() == NULL)
82 {
83 JavaEnvironment::ThrowException(env, OrthancPluginErrorCode_Plugin);
84 return NULL;
85 }
86 else
87 {
88 jstring t = env->NewStringUTF(s.GetValue());
89 if (t == NULL)
90 {
91 JavaEnvironment::ThrowException(env, OrthancPluginErrorCode_NotEnoughMemory);
92 return NULL;
93 }
94 else
95 {
96 return t;
97 }
98 }
99 {{/return.is_dynamic_string}}
100
101 {{#return.is_bytes}}
102 OrthancBytes b;
103 OrthancPluginErrorCode code = {{c_function}}(context_, b.GetMemoryBuffer()
104 {{#class_name}}, reinterpret_cast<{{class_name}}*>(static_cast<intptr_t>(self)){{/class_name}}
105 {{#args}}, {{c_accessor}}{{/args}});
106 if (code == OrthancPluginErrorCode_Success)
107 {
108 jbyteArray answer = env->NewByteArray(b.GetSize());
109 if (answer == NULL)
110 {
111 JavaEnvironment::ThrowException(env, OrthancPluginErrorCode_NotEnoughMemory);
112 return NULL;
113 }
114 else
115 {
116 env->SetByteArrayRegion(answer, 0, b.GetSize(), reinterpret_cast<const jbyte*>(b.GetData()));
117 return answer;
118 }
119 }
120 else
121 {
122 JavaEnvironment::ThrowException(env, code);
123 return NULL;
124 }
125 {{/return.is_bytes}}
126
127 {{#return.is_object}}
128 {{return.class_name}}* answer = {{c_function}}(context_
129 {{#class_name}}, reinterpret_cast<{{class_name}}*>(static_cast<intptr_t>(self)){{/class_name}}
130 {{#args}}, {{c_accessor}}{{/args}});
131 if (answer == NULL)
132 {
133 JavaEnvironment::ThrowException(env, OrthancPluginErrorCode_Plugin);
134 return 0;
135 }
136 else
137 {
138 return reinterpret_cast<intptr_t>(answer);
139 }
140 {{/return.is_object}}
141
142 {{#return.is_enumeration}}
143 return {{c_function}}(context_
144 {{#class_name}}, reinterpret_cast<{{class_name}}*>(static_cast<intptr_t>(self)){{/class_name}}
145 {{#args}}, {{c_accessor}}{{/args}});
146 {{/return.is_enumeration}}
147 }
148 catch (std::runtime_error& e)
149 {
150 JavaEnvironment::ThrowException(env, e.what());
151 {{#return.default_value}}return {{return.default_value}};{{/return.default_value}}
152 }
153 catch (...)
154 {
155 JavaEnvironment::ThrowException(env, OrthancPluginErrorCode_Plugin);
156 {{#return.default_value}}return {{return.default_value}};{{/return.default_value}}
157 }
158 }
159
160 {{/functions}}
161
162 static void JNI_LoadNatives(std::vector<JNINativeMethod>& methods)
163 {
164 methods.clear();
165 {{#functions}}
166
167 {{#java_signature}}
168 methods.push_back((JNINativeMethod) {
169 const_cast<char*>("{{c_function}}"),
170 const_cast<char*>("{{java_signature}}"),
171 (void*) JNI_{{c_function}}
172 });
173 {{/java_signature}}
174 {{/functions}}
175 }