comparison Resources/Orthanc/Plugins/Samples/Common/OrthancPluginCppWrapper.h @ 59:7a3853d51c45

Move "Framework/Orthanc/" as "Resources/Orthanc/"
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Nov 2016 16:34:34 +0100
parents Framework/Orthanc/Plugins/Samples/Common/OrthancPluginCppWrapper.h@06847108819c
children d529d9ce3c7e
comparison
equal deleted inserted replaced
58:35468714a38e 59:7a3853d51c45
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 !defined(HAS_ORTHANC_EXCEPTION)
41 # error The macro HAS_ORTHANC_EXCEPTION must be defined
42 #endif
43
44
45 #if HAS_ORTHANC_EXCEPTION == 1
46 # include "../../../Core/OrthancException.h"
47 # define ORTHANC_PLUGINS_THROW_EXCEPTION(code) throw ::Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(code))
48 #else
49 # define ORTHANC_PLUGINS_THROW_EXCEPTION(code) throw ::OrthancPlugins::PluginException(code)
50 #endif
51
52
53
54 namespace OrthancPlugins
55 {
56 typedef void (*RestCallback) (OrthancPluginRestOutput* output,
57 const char* url,
58 const OrthancPluginHttpRequest* request);
59
60 const char* GetErrorDescription(OrthancPluginContext* context,
61 OrthancPluginErrorCode code);
62
63 #if HAS_ORTHANC_EXCEPTION == 0
64 class PluginException
65 {
66 private:
67 OrthancPluginErrorCode code_;
68
69 public:
70 PluginException(OrthancPluginErrorCode code) : code_(code)
71 {
72 }
73
74 OrthancPluginErrorCode GetErrorCode() const
75 {
76 return code_;
77 }
78
79 const char* What(OrthancPluginContext* context) const
80 {
81 return ::OrthancPlugins::GetErrorDescription(context, code_);
82 }
83
84 static void Check(OrthancPluginErrorCode code);
85 };
86 #endif
87
88
89 class MemoryBuffer : public boost::noncopyable
90 {
91 private:
92 OrthancPluginContext* context_;
93 OrthancPluginMemoryBuffer buffer_;
94
95 void Check(OrthancPluginErrorCode code);
96
97 public:
98 MemoryBuffer(OrthancPluginContext* context);
99
100 ~MemoryBuffer()
101 {
102 Clear();
103 }
104
105 OrthancPluginMemoryBuffer* operator*()
106 {
107 return &buffer_;
108 }
109
110 // This transfers ownership
111 void Assign(OrthancPluginMemoryBuffer& other);
112
113 const char* GetData() const
114 {
115 if (buffer_.size > 0)
116 {
117 return reinterpret_cast<const char*>(buffer_.data);
118 }
119 else
120 {
121 return NULL;
122 }
123 }
124
125 size_t GetSize() const
126 {
127 return buffer_.size;
128 }
129
130 void Clear();
131
132 void ToString(std::string& target) const;
133
134 void ToJson(Json::Value& target) const;
135
136 bool RestApiGet(const std::string& uri,
137 bool applyPlugins);
138
139 bool RestApiPost(const std::string& uri,
140 const char* body,
141 size_t bodySize,
142 bool applyPlugins);
143
144 bool RestApiPut(const std::string& uri,
145 const char* body,
146 size_t bodySize,
147 bool applyPlugins);
148
149 bool RestApiPost(const std::string& uri,
150 const Json::Value& body,
151 bool applyPlugins);
152
153 bool RestApiPut(const std::string& uri,
154 const Json::Value& body,
155 bool applyPlugins);
156
157 bool RestApiPost(const std::string& uri,
158 const std::string& body,
159 bool applyPlugins)
160 {
161 return RestApiPost(uri, body.empty() ? NULL : body.c_str(), body.size(), applyPlugins);
162 }
163
164 bool RestApiPut(const std::string& uri,
165 const std::string& body,
166 bool applyPlugins)
167 {
168 return RestApiPut(uri, body.empty() ? NULL : body.c_str(), body.size(), applyPlugins);
169 }
170
171 void CreateDicom(const Json::Value& tags,
172 OrthancPluginCreateDicomFlags flags);
173
174 void ReadFile(const std::string& path);
175 };
176
177
178 class OrthancString : public boost::noncopyable
179 {
180 private:
181 OrthancPluginContext* context_;
182 char* str_;
183
184 public:
185 OrthancString(OrthancPluginContext* context,
186 char* str);
187
188 ~OrthancString()
189 {
190 Clear();
191 }
192
193 void Clear();
194
195 const char* GetContent() const
196 {
197 return str_;
198 }
199
200 void ToString(std::string& target) const;
201
202 void ToJson(Json::Value& target) const;
203 };
204
205
206 class OrthancConfiguration : public boost::noncopyable
207 {
208 private:
209 OrthancPluginContext* context_;
210 Json::Value configuration_; // Necessarily a Json::objectValue
211 std::string path_;
212
213 std::string GetPath(const std::string& key) const;
214
215 public:
216 OrthancConfiguration() : context_(NULL)
217 {
218 }
219
220 OrthancConfiguration(OrthancPluginContext* context);
221
222 OrthancPluginContext* GetContext() const;
223
224 const Json::Value& GetJson() const
225 {
226 return configuration_;
227 }
228
229 bool IsSection(const std::string& key) const;
230
231 void GetSection(OrthancConfiguration& target,
232 const std::string& key) const;
233
234 bool LookupStringValue(std::string& target,
235 const std::string& key) const;
236
237 bool LookupIntegerValue(int& target,
238 const std::string& key) const;
239
240 bool LookupUnsignedIntegerValue(unsigned int& target,
241 const std::string& key) const;
242
243 bool LookupBooleanValue(bool& target,
244 const std::string& key) const;
245
246 bool LookupFloatValue(float& target,
247 const std::string& key) const;
248
249 std::string GetStringValue(const std::string& key,
250 const std::string& defaultValue) const;
251
252 int GetIntegerValue(const std::string& key,
253 int defaultValue) const;
254
255 unsigned int GetUnsignedIntegerValue(const std::string& key,
256 unsigned int defaultValue) const;
257
258 bool GetBooleanValue(const std::string& key,
259 bool defaultValue) const;
260
261 float GetFloatValue(const std::string& key,
262 float defaultValue) const;
263 };
264
265 class OrthancImage
266 {
267 private:
268 OrthancPluginContext* context_;
269 OrthancPluginImage* image_;
270
271 void Clear();
272
273 void CheckImageAvailable();
274
275 public:
276 OrthancImage(OrthancPluginContext* context);
277
278 OrthancImage(OrthancPluginContext* context,
279 OrthancPluginImage* image);
280
281 OrthancImage(OrthancPluginContext* context,
282 OrthancPluginPixelFormat format,
283 uint32_t width,
284 uint32_t height);
285
286 ~OrthancImage()
287 {
288 Clear();
289 }
290
291 void UncompressPngImage(const void* data,
292 size_t size);
293
294 void UncompressJpegImage(const void* data,
295 size_t size);
296
297 void DecodeDicomImage(const void* data,
298 size_t size,
299 unsigned int frame);
300
301 OrthancPluginPixelFormat GetPixelFormat();
302
303 unsigned int GetWidth();
304
305 unsigned int GetHeight();
306
307 unsigned int GetPitch();
308
309 const void* GetBuffer();
310
311 void CompressPngImage(MemoryBuffer& target);
312
313 void CompressJpegImage(MemoryBuffer& target,
314 uint8_t quality);
315
316 void AnswerPngImage(OrthancPluginRestOutput* output);
317
318 void AnswerJpegImage(OrthancPluginRestOutput* output,
319 uint8_t quality);
320 };
321
322
323 bool RestApiGet(Json::Value& result,
324 OrthancPluginContext* context,
325 const std::string& uri,
326 bool applyPlugins);
327
328 bool RestApiPost(Json::Value& result,
329 OrthancPluginContext* context,
330 const std::string& uri,
331 const char* body,
332 size_t bodySize,
333 bool applyPlugins);
334
335 bool RestApiPost(Json::Value& result,
336 OrthancPluginContext* context,
337 const std::string& uri,
338 const Json::Value& body,
339 bool applyPlugins);
340
341 inline bool RestApiPost(Json::Value& result,
342 OrthancPluginContext* context,
343 const std::string& uri,
344 const std::string& body,
345 bool applyPlugins)
346 {
347 return RestApiPost(result, context, uri, body.empty() ? NULL : body.c_str(),
348 body.size(), applyPlugins);
349 }
350
351 bool RestApiPut(Json::Value& result,
352 OrthancPluginContext* context,
353 const std::string& uri,
354 const char* body,
355 size_t bodySize,
356 bool applyPlugins);
357
358 bool RestApiPut(Json::Value& result,
359 OrthancPluginContext* context,
360 const std::string& uri,
361 const Json::Value& body,
362 bool applyPlugins);
363
364 inline bool RestApiPut(Json::Value& result,
365 OrthancPluginContext* context,
366 const std::string& uri,
367 const std::string& body,
368 bool applyPlugins)
369 {
370 return RestApiPut(result, context, uri, body.empty() ? NULL : body.c_str(),
371 body.size(), applyPlugins);
372 }
373
374 bool RestApiDelete(OrthancPluginContext* context,
375 const std::string& uri,
376 bool applyPlugins);
377
378 bool RestApiDelete(OrthancPluginContext* context,
379 const std::string& uri,
380 bool applyPlugins);
381
382 inline void LogError(OrthancPluginContext* context,
383 const std::string& message)
384 {
385 if (context != NULL)
386 {
387 OrthancPluginLogError(context, message.c_str());
388 }
389 }
390
391 inline void LogWarning(OrthancPluginContext* context,
392 const std::string& message)
393 {
394 if (context != NULL)
395 {
396 OrthancPluginLogWarning(context, message.c_str());
397 }
398 }
399
400 inline void LogInfo(OrthancPluginContext* context,
401 const std::string& message)
402 {
403 if (context != NULL)
404 {
405 OrthancPluginLogInfo(context, message.c_str());
406 }
407 }
408
409 bool CheckMinimalOrthancVersion(OrthancPluginContext* context,
410 unsigned int major,
411 unsigned int minor,
412 unsigned int revision);
413
414
415 namespace Internals
416 {
417 template <RestCallback Callback>
418 OrthancPluginErrorCode Protect(OrthancPluginRestOutput* output,
419 const char* url,
420 const OrthancPluginHttpRequest* request)
421 {
422 try
423 {
424 Callback(output, url, request);
425 return OrthancPluginErrorCode_Success;
426 }
427 #if HAS_ORTHANC_EXCEPTION == 1
428 catch (Orthanc::OrthancException& e)
429 {
430 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
431 }
432 #else
433 catch (OrthancPlugins::PluginException& e)
434 {
435 return e.GetErrorCode();
436 }
437 #endif
438 catch (boost::bad_lexical_cast&)
439 {
440 return OrthancPluginErrorCode_BadFileFormat;
441 }
442 catch (...)
443 {
444 return OrthancPluginErrorCode_Plugin;
445 }
446 }
447 }
448
449
450 template <RestCallback Callback>
451 void RegisterRestCallback(OrthancPluginContext* context,
452 const std::string& uri,
453 bool isThreadSafe)
454 {
455 if (isThreadSafe)
456 {
457 OrthancPluginRegisterRestCallbackNoLock(context, uri.c_str(), Internals::Protect<Callback>);
458 }
459 else
460 {
461 OrthancPluginRegisterRestCallback(context, uri.c_str(), Internals::Protect<Callback>);
462 }
463 }
464 }