comparison Framework/Orthanc/Plugins/Samples/Common/OrthancPluginCppWrapper.h @ 1:dc730d11b101

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