comparison Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 2177:11420238f337

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