Mercurial > hg > orthanc
comparison Plugins/Samples/Common/OrthancPluginCppWrapper.h @ 2047:438f86ee19fc
toolbox shared by all plugins
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 24 Jun 2016 22:28:25 +0200 |
parents | |
children | e166a902b3c4 |
comparison
equal
deleted
inserted
replaced
2046:b534834a300e | 2047:438f86ee19fc |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #pragma once | |
34 | |
35 #include <orthanc/OrthancCPlugin.h> | |
36 #include <boost/noncopyable.hpp> | |
37 #include <boost/lexical_cast.hpp> | |
38 #include <json/value.h> | |
39 | |
40 #if HAS_ORTHANC_EXCEPTION == 1 | |
41 # include <OrthancException.h> | |
42 #endif | |
43 | |
44 | |
45 namespace OrthancPlugins | |
46 { | |
47 typedef void (*RestCallback) (OrthancPluginRestOutput* output, | |
48 const char* url, | |
49 const OrthancPluginHttpRequest* request); | |
50 | |
51 | |
52 class PluginException | |
53 { | |
54 private: | |
55 OrthancPluginErrorCode code_; | |
56 | |
57 public: | |
58 PluginException(OrthancPluginErrorCode code) : code_(code) | |
59 { | |
60 } | |
61 | |
62 OrthancPluginErrorCode GetErrorCode() const | |
63 { | |
64 return code_; | |
65 } | |
66 | |
67 const char* GetErrorDescription(OrthancPluginContext* context) const; | |
68 }; | |
69 | |
70 | |
71 class MemoryBuffer : public boost::noncopyable | |
72 { | |
73 private: | |
74 OrthancPluginContext* context_; | |
75 OrthancPluginMemoryBuffer buffer_; | |
76 | |
77 public: | |
78 MemoryBuffer(OrthancPluginContext* context); | |
79 | |
80 ~MemoryBuffer() | |
81 { | |
82 Clear(); | |
83 } | |
84 | |
85 OrthancPluginMemoryBuffer* operator*() | |
86 { | |
87 return &buffer_; | |
88 } | |
89 | |
90 // This transfers ownership | |
91 void Assign(OrthancPluginMemoryBuffer& other); | |
92 | |
93 const char* GetData() const | |
94 { | |
95 if (buffer_.size > 0) | |
96 { | |
97 return reinterpret_cast<const char*>(buffer_.data); | |
98 } | |
99 else | |
100 { | |
101 return NULL; | |
102 } | |
103 } | |
104 | |
105 size_t GetSize() const | |
106 { | |
107 return buffer_.size; | |
108 } | |
109 | |
110 void Clear(); | |
111 | |
112 void ToString(std::string& target) const; | |
113 | |
114 void ToJson(Json::Value& target) const; | |
115 | |
116 bool RestApiGet(const std::string& uri, | |
117 bool applyPlugins); | |
118 | |
119 bool RestApiPost(const std::string& uri, | |
120 const char* body, | |
121 size_t bodySize, | |
122 bool applyPlugins); | |
123 | |
124 bool RestApiPut(const std::string& uri, | |
125 const char* body, | |
126 size_t bodySize, | |
127 bool applyPlugins); | |
128 | |
129 bool RestApiPost(const std::string& uri, | |
130 const std::string& body, | |
131 bool applyPlugins) | |
132 { | |
133 return RestApiPost(uri, body.empty() ? NULL : body.c_str(), body.size(), applyPlugins); | |
134 } | |
135 | |
136 bool RestApiPut(const std::string& uri, | |
137 const std::string& body, | |
138 bool applyPlugins) | |
139 { | |
140 return RestApiPut(uri, body.empty() ? NULL : body.c_str(), body.size(), applyPlugins); | |
141 } | |
142 }; | |
143 | |
144 | |
145 class OrthancString : public boost::noncopyable | |
146 { | |
147 private: | |
148 OrthancPluginContext* context_; | |
149 char* str_; | |
150 | |
151 public: | |
152 OrthancString(OrthancPluginContext* context, | |
153 char* str); | |
154 | |
155 ~OrthancString() | |
156 { | |
157 Clear(); | |
158 } | |
159 | |
160 void Clear(); | |
161 | |
162 const char* GetContent() const | |
163 { | |
164 return str_; | |
165 } | |
166 | |
167 void ToString(std::string& target) const; | |
168 | |
169 void ToJson(Json::Value& target) const; | |
170 }; | |
171 | |
172 | |
173 class OrthancConfiguration : public boost::noncopyable | |
174 { | |
175 private: | |
176 OrthancPluginContext* context_; | |
177 Json::Value configuration_; | |
178 std::string path_; | |
179 | |
180 std::string GetPath(const std::string& key) const; | |
181 | |
182 public: | |
183 OrthancConfiguration() : context_(NULL) | |
184 { | |
185 } | |
186 | |
187 OrthancConfiguration(OrthancPluginContext* context); | |
188 | |
189 OrthancPluginContext* GetContext() const; | |
190 | |
191 const Json::Value& GetJson() const | |
192 { | |
193 return configuration_; | |
194 } | |
195 | |
196 void GetSection(OrthancConfiguration& target, | |
197 const std::string& key) const; | |
198 | |
199 bool LookupStringValue(std::string& target, | |
200 const std::string& key) const; | |
201 | |
202 bool LookupIntegerValue(int& target, | |
203 const std::string& key) const; | |
204 | |
205 bool LookupUnsignedIntegerValue(unsigned int& target, | |
206 const std::string& key) const; | |
207 | |
208 bool LookupBooleanValue(bool& target, | |
209 const std::string& key) const; | |
210 | |
211 bool LookupFloatValue(float& target, | |
212 const std::string& key) const; | |
213 | |
214 std::string GetStringValue(const std::string& key, | |
215 const std::string& defaultValue) const; | |
216 | |
217 int GetIntegerValue(const std::string& key, | |
218 int defaultValue) const; | |
219 | |
220 unsigned int GetUnsignedIntegerValue(const std::string& key, | |
221 unsigned int defaultValue) const; | |
222 | |
223 bool GetBooleanValue(const std::string& key, | |
224 bool defaultValue) const; | |
225 | |
226 float GetFloatValue(const std::string& key, | |
227 float defaultValue) const; | |
228 }; | |
229 | |
230 class OrthancImage | |
231 { | |
232 private: | |
233 OrthancPluginContext* context_; | |
234 OrthancPluginImage* image_; | |
235 | |
236 void Clear(); | |
237 | |
238 void CheckImageAvailable(); | |
239 | |
240 public: | |
241 OrthancImage(OrthancPluginContext* context); | |
242 | |
243 OrthancImage(OrthancPluginContext* context, | |
244 OrthancPluginImage* image); | |
245 | |
246 OrthancImage(OrthancPluginContext* context, | |
247 OrthancPluginPixelFormat format, | |
248 uint32_t width, | |
249 uint32_t height); | |
250 | |
251 ~OrthancImage() | |
252 { | |
253 Clear(); | |
254 } | |
255 | |
256 void UncompressPngImage(const void* data, | |
257 size_t size); | |
258 | |
259 void UncompressJpegImage(const void* data, | |
260 size_t size); | |
261 | |
262 void DecodeDicomImage(const void* data, | |
263 size_t size, | |
264 unsigned int frame); | |
265 | |
266 OrthancPluginPixelFormat GetPixelFormat(); | |
267 | |
268 unsigned int GetWidth(); | |
269 | |
270 unsigned int GetHeight(); | |
271 | |
272 unsigned int GetPitch(); | |
273 | |
274 const void* GetBuffer(); | |
275 | |
276 void CompressPngImage(MemoryBuffer& target); | |
277 | |
278 void CompressJpegImage(MemoryBuffer& target, | |
279 uint8_t quality); | |
280 | |
281 void AnswerPngImage(OrthancPluginRestOutput* output); | |
282 | |
283 void AnswerJpegImage(OrthancPluginRestOutput* output, | |
284 uint8_t quality); | |
285 }; | |
286 | |
287 | |
288 bool RestApiGetJson(Json::Value& result, | |
289 OrthancPluginContext* context, | |
290 const std::string& uri, | |
291 bool applyPlugins); | |
292 | |
293 bool RestApiPostJson(Json::Value& result, | |
294 OrthancPluginContext* context, | |
295 const std::string& uri, | |
296 const char* body, | |
297 size_t bodySize, | |
298 bool applyPlugins); | |
299 | |
300 bool RestApiPutJson(Json::Value& result, | |
301 OrthancPluginContext* context, | |
302 const std::string& uri, | |
303 const char* body, | |
304 size_t bodySize, | |
305 bool applyPlugins); | |
306 | |
307 inline bool RestApiPostJson(Json::Value& result, | |
308 OrthancPluginContext* context, | |
309 const std::string& uri, | |
310 const std::string& body, | |
311 bool applyPlugins) | |
312 { | |
313 return RestApiPostJson(result, context, uri, body.empty() ? NULL : body.c_str(), | |
314 body.size(), applyPlugins); | |
315 } | |
316 | |
317 bool RestApiDelete(OrthancPluginContext* context, | |
318 const std::string& uri, | |
319 bool applyPlugins); | |
320 | |
321 inline bool RestApiPutJson(Json::Value& result, | |
322 OrthancPluginContext* context, | |
323 const std::string& uri, | |
324 const std::string& body, | |
325 bool applyPlugins) | |
326 { | |
327 return RestApiPutJson(result, context, uri, body.empty() ? NULL : body.c_str(), | |
328 body.size(), applyPlugins); | |
329 } | |
330 | |
331 bool RestApiDelete(OrthancPluginContext* context, | |
332 const std::string& uri, | |
333 bool applyPlugins); | |
334 | |
335 | |
336 namespace Internals | |
337 { | |
338 template <RestCallback Callback> | |
339 OrthancPluginErrorCode Protect(OrthancPluginRestOutput* output, | |
340 const char* url, | |
341 const OrthancPluginHttpRequest* request) | |
342 { | |
343 try | |
344 { | |
345 Callback(output, url, request); | |
346 return OrthancPluginErrorCode_Success; | |
347 } | |
348 catch (OrthancPlugins::PluginException& e) | |
349 { | |
350 return e.GetErrorCode(); | |
351 } | |
352 #if HAS_ORTHANC_EXCEPTION == 1 | |
353 catch (Orthanc::OrthancException& e) | |
354 { | |
355 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
356 } | |
357 #endif | |
358 catch (boost::bad_lexical_cast& e) | |
359 { | |
360 return OrthancPluginErrorCode_BadFileFormat; | |
361 } | |
362 catch (...) | |
363 { | |
364 return OrthancPluginErrorCode_Plugin; | |
365 } | |
366 } | |
367 } | |
368 | |
369 | |
370 template <RestCallback Callback> | |
371 void RegisterRestCallback(OrthancPluginContext* context, | |
372 const std::string& uri, | |
373 bool isThreadSafe) | |
374 { | |
375 if (isThreadSafe) | |
376 { | |
377 OrthancPluginRegisterRestCallbackNoLock(context, uri.c_str(), Internals::Protect<Callback>); | |
378 } | |
379 else | |
380 { | |
381 OrthancPluginRegisterRestCallback(context, uri.c_str(), Internals::Protect<Callback>); | |
382 } | |
383 } | |
384 } |