comparison Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 2176:fead5549aaa7

introduction of HAS_ORTHANC_EXCEPTION to avoid PluginException if not necessary
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Nov 2016 12:07:36 +0100
parents aa2915963531
children 11420238f337
comparison
equal deleted inserted replaced
2175:bed8e7ad8bab 2176:fead5549aaa7
34 34
35 #include <json/reader.h> 35 #include <json/reader.h>
36 #include <json/writer.h> 36 #include <json/writer.h>
37 37
38 38
39 #if HAS_ORTHANC_EXCEPTION == 1
40 # define THROW_EXCEPTION(code) throw Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(code))
41 #else
42 # define THROW_EXCEPTION(code) throw PluginException(code)
43 #endif
44
45
46
39 namespace OrthancPlugins 47 namespace OrthancPlugins
40 { 48 {
41 const char* PluginException::GetErrorDescription(OrthancPluginContext* context) const 49 void ThrowException(OrthancPluginErrorCode code)
42 { 50 {
43 const char* description = OrthancPluginGetErrorDescription(context, code_); 51 THROW_EXCEPTION(code);
52 }
53
54
55 const char* GetErrorDescription(OrthancPluginContext* context,
56 OrthancPluginErrorCode code)
57 {
58 const char* description = OrthancPluginGetErrorDescription(context, code);
44 if (description) 59 if (description)
45 { 60 {
46 return description; 61 return description;
47 } 62 }
48 else 63 else
50 return "No description available"; 65 return "No description available";
51 } 66 }
52 } 67 }
53 68
54 69
70 #if HAS_ORTHANC_EXCEPTION == 0
55 void PluginException::Check(OrthancPluginErrorCode code) 71 void PluginException::Check(OrthancPluginErrorCode code)
56 { 72 {
57 if (code != OrthancPluginErrorCode_Success) 73 if (code != OrthancPluginErrorCode_Success)
58 { 74 {
59 throw PluginException(code); 75 THROW_EXCEPTION(code);
60 } 76 }
61 } 77 }
78 #endif
62 79
63 80
64 void MemoryBuffer::Check(OrthancPluginErrorCode code) 81 void MemoryBuffer::Check(OrthancPluginErrorCode code)
65 { 82 {
66 if (code != OrthancPluginErrorCode_Success) 83 if (code != OrthancPluginErrorCode_Success)
67 { 84 {
68 // Prevent using garbage information 85 // Prevent using garbage information
69 buffer_.data = NULL; 86 buffer_.data = NULL;
70 buffer_.size = 0; 87 buffer_.size = 0;
71 throw PluginException(code); 88 THROW_EXCEPTION(code);
72 } 89 }
73 } 90 }
74 91
75 92
76 MemoryBuffer::MemoryBuffer(OrthancPluginContext* context) : 93 MemoryBuffer::MemoryBuffer(OrthancPluginContext* context) :
120 void MemoryBuffer::ToJson(Json::Value& target) const 137 void MemoryBuffer::ToJson(Json::Value& target) const
121 { 138 {
122 if (buffer_.data == NULL || 139 if (buffer_.data == NULL ||
123 buffer_.size == 0) 140 buffer_.size == 0)
124 { 141 {
125 throw PluginException(OrthancPluginErrorCode_InternalError); 142 THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
126 } 143 }
127 144
128 const char* tmp = reinterpret_cast<const char*>(buffer_.data); 145 const char* tmp = reinterpret_cast<const char*>(buffer_.data);
129 146
130 Json::Reader reader; 147 Json::Reader reader;
131 if (!reader.parse(tmp, tmp + buffer_.size, target)) 148 if (!reader.parse(tmp, tmp + buffer_.size, target))
132 { 149 {
133 OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON"); 150 OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON");
134 throw PluginException(OrthancPluginErrorCode_BadFileFormat); 151 THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
135 } 152 }
136 } 153 }
137 154
138 155
139 bool MemoryBuffer::RestApiGet(const std::string& uri, 156 bool MemoryBuffer::RestApiGet(const std::string& uri,
161 { 178 {
162 return false; 179 return false;
163 } 180 }
164 else 181 else
165 { 182 {
166 throw PluginException(error); 183 THROW_EXCEPTION(error);
167 } 184 }
168 } 185 }
169 186
170 187
171 bool MemoryBuffer::RestApiPost(const std::string& uri, 188 bool MemoryBuffer::RestApiPost(const std::string& uri,
195 { 212 {
196 return false; 213 return false;
197 } 214 }
198 else 215 else
199 { 216 {
200 throw PluginException(error); 217 THROW_EXCEPTION(error);
201 } 218 }
202 } 219 }
203 220
204 221
205 bool MemoryBuffer::RestApiPut(const std::string& uri, 222 bool MemoryBuffer::RestApiPut(const std::string& uri,
229 { 246 {
230 return false; 247 return false;
231 } 248 }
232 else 249 else
233 { 250 {
234 throw PluginException(error); 251 THROW_EXCEPTION(error);
235 } 252 }
236 } 253 }
237 254
238 255
239 bool MemoryBuffer::RestApiPost(const std::string& uri, 256 bool MemoryBuffer::RestApiPost(const std::string& uri,
307 void OrthancString::ToJson(Json::Value& target) const 324 void OrthancString::ToJson(Json::Value& target) const
308 { 325 {
309 if (str_ == NULL) 326 if (str_ == NULL)
310 { 327 {
311 OrthancPluginLogError(context_, "Cannot convert an empty memory buffer to JSON"); 328 OrthancPluginLogError(context_, "Cannot convert an empty memory buffer to JSON");
312 throw PluginException(OrthancPluginErrorCode_InternalError); 329 THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
313 } 330 }
314 331
315 Json::Reader reader; 332 Json::Reader reader;
316 if (!reader.parse(str_, target)) 333 if (!reader.parse(str_, target))
317 { 334 {
318 OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON"); 335 OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON");
319 throw PluginException(OrthancPluginErrorCode_BadFileFormat); 336 THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
320 } 337 }
321 } 338 }
322 339
323 340
324 OrthancConfiguration::OrthancConfiguration(OrthancPluginContext* context) : 341 OrthancConfiguration::OrthancConfiguration(OrthancPluginContext* context) :
327 OrthancString str(context, OrthancPluginGetConfiguration(context)); 344 OrthancString str(context, OrthancPluginGetConfiguration(context));
328 345
329 if (str.GetContent() == NULL) 346 if (str.GetContent() == NULL)
330 { 347 {
331 OrthancPluginLogError(context, "Cannot access the Orthanc configuration"); 348 OrthancPluginLogError(context, "Cannot access the Orthanc configuration");
332 throw PluginException(OrthancPluginErrorCode_InternalError); 349 THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
333 } 350 }
334 351
335 str.ToJson(configuration_); 352 str.ToJson(configuration_);
336 353
337 if (configuration_.type() != Json::objectValue) 354 if (configuration_.type() != Json::objectValue)
338 { 355 {
339 OrthancPluginLogError(context, "Unable to read the Orthanc configuration"); 356 OrthancPluginLogError(context, "Unable to read the Orthanc configuration");
340 throw PluginException(OrthancPluginErrorCode_InternalError); 357 THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
341 } 358 }
342 } 359 }
343 360
344 361
345 OrthancPluginContext* OrthancConfiguration::GetContext() const 362 OrthancPluginContext* OrthancConfiguration::GetContext() const
346 { 363 {
347 if (context_ == NULL) 364 if (context_ == NULL)
348 { 365 {
349 throw PluginException(OrthancPluginErrorCode_Plugin); 366 THROW_EXCEPTION(OrthancPluginErrorCode_Plugin);
350 } 367 }
351 else 368 else
352 { 369 {
353 return context_; 370 return context_;
354 } 371 }
397 { 414 {
398 std::string s = "The configuration section \"" + target.path_ + "\" is not an associative array as expected"; 415 std::string s = "The configuration section \"" + target.path_ + "\" is not an associative array as expected";
399 OrthancPluginLogError(context_, s.c_str()); 416 OrthancPluginLogError(context_, s.c_str());
400 } 417 }
401 418
402 throw PluginException(OrthancPluginErrorCode_BadFileFormat); 419 THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
403 } 420 }
404 421
405 target.configuration_ = configuration_[key]; 422 target.configuration_ = configuration_[key];
406 } 423 }
407 } 424 }
423 { 440 {
424 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a string as expected"; 441 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a string as expected";
425 OrthancPluginLogError(context_, s.c_str()); 442 OrthancPluginLogError(context_, s.c_str());
426 } 443 }
427 444
428 throw PluginException(OrthancPluginErrorCode_BadFileFormat); 445 THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
429 } 446 }
430 447
431 target = configuration_[key].asString(); 448 target = configuration_[key].asString();
432 return true; 449 return true;
433 } 450 }
458 { 475 {
459 std::string s = "The configuration option \"" + GetPath(key) + "\" is not an integer as expected"; 476 std::string s = "The configuration option \"" + GetPath(key) + "\" is not an integer as expected";
460 OrthancPluginLogError(context_, s.c_str()); 477 OrthancPluginLogError(context_, s.c_str());
461 } 478 }
462 479
463 throw PluginException(OrthancPluginErrorCode_BadFileFormat); 480 THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
464 } 481 }
465 } 482 }
466 483
467 484
468 bool OrthancConfiguration::LookupUnsignedIntegerValue(unsigned int& target, 485 bool OrthancConfiguration::LookupUnsignedIntegerValue(unsigned int& target,
480 { 497 {
481 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a positive integer as expected"; 498 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a positive integer as expected";
482 OrthancPluginLogError(context_, s.c_str()); 499 OrthancPluginLogError(context_, s.c_str());
483 } 500 }
484 501
485 throw PluginException(OrthancPluginErrorCode_BadFileFormat); 502 THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
486 } 503 }
487 else 504 else
488 { 505 {
489 target = static_cast<unsigned int>(tmp); 506 target = static_cast<unsigned int>(tmp);
490 return true; 507 return true;
508 { 525 {
509 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a Boolean as expected"; 526 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a Boolean as expected";
510 OrthancPluginLogError(context_, s.c_str()); 527 OrthancPluginLogError(context_, s.c_str());
511 } 528 }
512 529
513 throw PluginException(OrthancPluginErrorCode_BadFileFormat); 530 THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
514 } 531 }
515 532
516 target = configuration_[key].asBool(); 533 target = configuration_[key].asBool();
517 return true; 534 return true;
518 } 535 }
547 { 564 {
548 std::string s = "The configuration option \"" + GetPath(key) + "\" is not an integer as expected"; 565 std::string s = "The configuration option \"" + GetPath(key) + "\" is not an integer as expected";
549 OrthancPluginLogError(context_, s.c_str()); 566 OrthancPluginLogError(context_, s.c_str());
550 } 567 }
551 568
552 throw PluginException(OrthancPluginErrorCode_BadFileFormat); 569 THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
553 } 570 }
554 } 571 }
555 572
556 573
557 std::string OrthancConfiguration::GetStringValue(const std::string& key, 574 std::string OrthancConfiguration::GetStringValue(const std::string& key,
642 void OrthancImage::CheckImageAvailable() 659 void OrthancImage::CheckImageAvailable()
643 { 660 {
644 if (image_ == NULL) 661 if (image_ == NULL)
645 { 662 {
646 OrthancPluginLogError(context_, "Trying to access a NULL image"); 663 OrthancPluginLogError(context_, "Trying to access a NULL image");
647 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange); 664 THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
648 } 665 }
649 } 666 }
650 667
651 668
652 OrthancImage::OrthancImage(OrthancPluginContext* context) : 669 OrthancImage::OrthancImage(OrthancPluginContext* context) :
653 context_(context), 670 context_(context),
654 image_(NULL) 671 image_(NULL)
655 { 672 {
656 if (context == NULL) 673 if (context == NULL)
657 { 674 {
658 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange); 675 THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
659 } 676 }
660 } 677 }
661 678
662 679
663 OrthancImage::OrthancImage(OrthancPluginContext* context, 680 OrthancImage::OrthancImage(OrthancPluginContext* context,
665 context_(context), 682 context_(context),
666 image_(image) 683 image_(image)
667 { 684 {
668 if (context == NULL) 685 if (context == NULL)
669 { 686 {
670 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange); 687 THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
671 } 688 }
672 } 689 }
673 690
674 691
675 OrthancImage::OrthancImage(OrthancPluginContext* context, 692 OrthancImage::OrthancImage(OrthancPluginContext* context,
678 uint32_t height) : 695 uint32_t height) :
679 context_(context) 696 context_(context)
680 { 697 {
681 if (context == NULL) 698 if (context == NULL)
682 { 699 {
683 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange); 700 THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
684 } 701 }
685 else 702 else
686 { 703 {
687 image_ = OrthancPluginCreateImage(context, format, width, height); 704 image_ = OrthancPluginCreateImage(context, format, width, height);
688 } 705 }
695 Clear(); 712 Clear();
696 image_ = OrthancPluginUncompressImage(context_, data, size, OrthancPluginImageFormat_Png); 713 image_ = OrthancPluginUncompressImage(context_, data, size, OrthancPluginImageFormat_Png);
697 if (image_ == NULL) 714 if (image_ == NULL)
698 { 715 {
699 OrthancPluginLogError(context_, "Cannot uncompress a PNG image"); 716 OrthancPluginLogError(context_, "Cannot uncompress a PNG image");
700 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange); 717 THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
701 } 718 }
702 } 719 }
703 720
704 721
705 void OrthancImage::UncompressJpegImage(const void* data, 722 void OrthancImage::UncompressJpegImage(const void* data,
708 Clear(); 725 Clear();
709 image_ = OrthancPluginUncompressImage(context_, data, size, OrthancPluginImageFormat_Jpeg); 726 image_ = OrthancPluginUncompressImage(context_, data, size, OrthancPluginImageFormat_Jpeg);
710 if (image_ == NULL) 727 if (image_ == NULL)
711 { 728 {
712 OrthancPluginLogError(context_, "Cannot uncompress a JPEG image"); 729 OrthancPluginLogError(context_, "Cannot uncompress a JPEG image");
713 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange); 730 THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
714 } 731 }
715 } 732 }
716 733
717 734
718 void OrthancImage::DecodeDicomImage(const void* data, 735 void OrthancImage::DecodeDicomImage(const void* data,
722 Clear(); 739 Clear();
723 image_ = OrthancPluginDecodeDicomImage(context_, data, size, frame); 740 image_ = OrthancPluginDecodeDicomImage(context_, data, size, frame);
724 if (image_ == NULL) 741 if (image_ == NULL)
725 { 742 {
726 OrthancPluginLogError(context_, "Cannot uncompress a DICOM image"); 743 OrthancPluginLogError(context_, "Cannot uncompress a DICOM image");
727 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange); 744 THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
728 } 745 }
729 } 746 }
730 747
731 748
732 OrthancPluginPixelFormat OrthancImage::GetPixelFormat() 749 OrthancPluginPixelFormat OrthancImage::GetPixelFormat()
910 { 927 {
911 return false; 928 return false;
912 } 929 }
913 else 930 else
914 { 931 {
915 throw PluginException(error); 932 THROW_EXCEPTION(error);
916 } 933 }
917 } 934 }
918 935
919 936
920 static void ReportIncompatibleVersion(OrthancPluginContext* context, 937 static void ReportIncompatibleVersion(OrthancPluginContext* context,