comparison Plugins/Samples/Common/OrthancPluginCppWrapper.h @ 2958:bb7a66efbeb1

OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 04 Dec 2018 16:31:29 +0100
parents 859e880ac9a8
children 7364415851ac
comparison
equal deleted inserted replaced
2957:ccf61f6e22ef 2958:bb7a66efbeb1
77 { 77 {
78 typedef void (*RestCallback) (OrthancPluginRestOutput* output, 78 typedef void (*RestCallback) (OrthancPluginRestOutput* output,
79 const char* url, 79 const char* url,
80 const OrthancPluginHttpRequest* request); 80 const OrthancPluginHttpRequest* request);
81 81
82
83 void SetGlobalContext(OrthancPluginContext* context);
84
85 bool HasGlobalContext();
86
87 OrthancPluginContext* GetGlobalContext();
88
89
82 class OrthancImage; 90 class OrthancImage;
83 91
84 92
85 class MemoryBuffer : public boost::noncopyable 93 class MemoryBuffer : public boost::noncopyable
86 { 94 {
87 private: 95 private:
88 OrthancPluginContext* context_;
89 OrthancPluginMemoryBuffer buffer_; 96 OrthancPluginMemoryBuffer buffer_;
90 97
91 void Check(OrthancPluginErrorCode code); 98 void Check(OrthancPluginErrorCode code);
92 99
93 bool CheckHttp(OrthancPluginErrorCode code); 100 bool CheckHttp(OrthancPluginErrorCode code);
94 101
95 public: 102 public:
96 MemoryBuffer(OrthancPluginContext* context); 103 MemoryBuffer();
97 104
98 ~MemoryBuffer() 105 ~MemoryBuffer()
99 { 106 {
100 Clear(); 107 Clear();
101 } 108 }
203 210
204 211
205 class OrthancString : public boost::noncopyable 212 class OrthancString : public boost::noncopyable
206 { 213 {
207 private: 214 private:
208 OrthancPluginContext* context_; 215 char* str_;
209 char* str_;
210 216
211 void Clear(); 217 void Clear();
212 218
213 public: 219 public:
214 OrthancString(OrthancPluginContext* context) : 220 OrthancString() :
215 context_(context),
216 str_(NULL) 221 str_(NULL)
217 { 222 {
218 } 223 }
219 224
220 ~OrthancString() 225 ~OrthancString()
238 243
239 244
240 class OrthancConfiguration : public boost::noncopyable 245 class OrthancConfiguration : public boost::noncopyable
241 { 246 {
242 private: 247 private:
243 OrthancPluginContext* context_; 248 Json::Value configuration_; // Necessarily a Json::objectValue
244 Json::Value configuration_; // Necessarily a Json::objectValue 249 std::string path_;
245 std::string path_;
246 250
247 std::string GetPath(const std::string& key) const; 251 std::string GetPath(const std::string& key) const;
248 252
249 public: 253 public:
250 OrthancConfiguration() : context_(NULL) 254 OrthancConfiguration();
251 {
252 }
253
254 OrthancConfiguration(OrthancPluginContext* context);
255
256 OrthancPluginContext* GetContext() const;
257 255
258 const Json::Value& GetJson() const 256 const Json::Value& GetJson() const
259 { 257 {
260 return configuration_; 258 return configuration_;
261 } 259 }
308 }; 306 };
309 307
310 class OrthancImage : public boost::noncopyable 308 class OrthancImage : public boost::noncopyable
311 { 309 {
312 private: 310 private:
313 OrthancPluginContext* context_;
314 OrthancPluginImage* image_; 311 OrthancPluginImage* image_;
315 312
316 void Clear(); 313 void Clear();
317 314
318 void CheckImageAvailable(); 315 void CheckImageAvailable();
319 316
320 public: 317 public:
321 OrthancImage(OrthancPluginContext* context); 318 OrthancImage();
322 319
323 OrthancImage(OrthancPluginContext* context, 320 OrthancImage(OrthancPluginImage* image);
324 OrthancPluginImage* image); 321
325 322 OrthancImage(OrthancPluginPixelFormat format,
326 OrthancImage(OrthancPluginContext* context,
327 OrthancPluginPixelFormat format,
328 uint32_t width, 323 uint32_t width,
329 uint32_t height); 324 uint32_t height);
330 325
331 OrthancImage(OrthancPluginContext* context, 326 OrthancImage(OrthancPluginPixelFormat format,
332 OrthancPluginPixelFormat format,
333 uint32_t width, 327 uint32_t width,
334 uint32_t height, 328 uint32_t height,
335 uint32_t pitch, 329 uint32_t pitch,
336 void* buffer 330 void* buffer
337 ); 331 );
380 374
381 #if HAS_ORTHANC_PLUGIN_FIND_MATCHER == 1 375 #if HAS_ORTHANC_PLUGIN_FIND_MATCHER == 1
382 class FindMatcher : public boost::noncopyable 376 class FindMatcher : public boost::noncopyable
383 { 377 {
384 private: 378 private:
385 OrthancPluginContext* context_;
386 OrthancPluginFindMatcher* matcher_; 379 OrthancPluginFindMatcher* matcher_;
387 const OrthancPluginWorklistQuery* worklist_; 380 const OrthancPluginWorklistQuery* worklist_;
388 381
389 void SetupDicom(OrthancPluginContext* context, 382 void SetupDicom(const void* query,
390 const void* query,
391 uint32_t size); 383 uint32_t size);
392 384
393 public: 385 public:
394 FindMatcher(OrthancPluginContext* context, 386 FindMatcher(const OrthancPluginWorklistQuery* worklist);
395 const OrthancPluginWorklistQuery* worklist); 387
396 388 FindMatcher(const void* query,
397 FindMatcher(OrthancPluginContext* context,
398 const void* query,
399 uint32_t size) 389 uint32_t size)
400 { 390 {
401 SetupDicom(context, query, size); 391 SetupDicom(query, size);
402 } 392 }
403 393
404 FindMatcher(OrthancPluginContext* context, 394 FindMatcher(const MemoryBuffer& dicom)
405 const MemoryBuffer& dicom) 395 {
406 { 396 SetupDicom(dicom.GetData(), dicom.GetSize());
407 SetupDicom(context, dicom.GetData(), dicom.GetSize());
408 } 397 }
409 398
410 ~FindMatcher(); 399 ~FindMatcher();
411 400
412 bool IsMatch(const void* dicom, 401 bool IsMatch(const void* dicom,
419 }; 408 };
420 #endif 409 #endif
421 410
422 411
423 bool RestApiGet(Json::Value& result, 412 bool RestApiGet(Json::Value& result,
424 OrthancPluginContext* context,
425 const std::string& uri, 413 const std::string& uri,
426 bool applyPlugins); 414 bool applyPlugins);
427 415
428 bool RestApiPost(Json::Value& result, 416 bool RestApiPost(Json::Value& result,
429 OrthancPluginContext* context,
430 const std::string& uri, 417 const std::string& uri,
431 const char* body, 418 const char* body,
432 size_t bodySize, 419 size_t bodySize,
433 bool applyPlugins); 420 bool applyPlugins);
434 421
435 bool RestApiPost(Json::Value& result, 422 bool RestApiPost(Json::Value& result,
436 OrthancPluginContext* context,
437 const std::string& uri, 423 const std::string& uri,
438 const Json::Value& body, 424 const Json::Value& body,
439 bool applyPlugins); 425 bool applyPlugins);
440 426
441 inline bool RestApiPost(Json::Value& result, 427 inline bool RestApiPost(Json::Value& result,
442 OrthancPluginContext* context,
443 const std::string& uri, 428 const std::string& uri,
444 const std::string& body, 429 const std::string& body,
445 bool applyPlugins) 430 bool applyPlugins)
446 { 431 {
447 return RestApiPost(result, context, uri, body.empty() ? NULL : body.c_str(), 432 return RestApiPost(result, uri, body.empty() ? NULL : body.c_str(),
448 body.size(), applyPlugins); 433 body.size(), applyPlugins);
449 } 434 }
450 435
451 bool RestApiPut(Json::Value& result, 436 bool RestApiPut(Json::Value& result,
452 OrthancPluginContext* context,
453 const std::string& uri, 437 const std::string& uri,
454 const char* body, 438 const char* body,
455 size_t bodySize, 439 size_t bodySize,
456 bool applyPlugins); 440 bool applyPlugins);
457 441
458 bool RestApiPut(Json::Value& result, 442 bool RestApiPut(Json::Value& result,
459 OrthancPluginContext* context,
460 const std::string& uri, 443 const std::string& uri,
461 const Json::Value& body, 444 const Json::Value& body,
462 bool applyPlugins); 445 bool applyPlugins);
463 446
464 inline bool RestApiPut(Json::Value& result, 447 inline bool RestApiPut(Json::Value& result,
465 OrthancPluginContext* context,
466 const std::string& uri, 448 const std::string& uri,
467 const std::string& body, 449 const std::string& body,
468 bool applyPlugins) 450 bool applyPlugins)
469 { 451 {
470 return RestApiPut(result, context, uri, body.empty() ? NULL : body.c_str(), 452 return RestApiPut(result, uri, body.empty() ? NULL : body.c_str(),
471 body.size(), applyPlugins); 453 body.size(), applyPlugins);
472 } 454 }
473 455
474 bool RestApiDelete(OrthancPluginContext* context, 456 bool RestApiDelete(const std::string& uri,
475 const std::string& uri,
476 bool applyPlugins); 457 bool applyPlugins);
477 458
478 bool HttpDelete(OrthancPluginContext* context, 459 bool HttpDelete(const std::string& url,
479 const std::string& url,
480 const std::string& username, 460 const std::string& username,
481 const std::string& password); 461 const std::string& password);
482 462
483 inline void LogError(OrthancPluginContext* context, 463 void LogError(const std::string& message);
484 const std::string& message) 464
485 { 465 void LogWarning(const std::string& message);
486 if (context != NULL) 466
487 { 467 void LogInfo(const std::string& message);
488 OrthancPluginLogError(context, message.c_str()); 468
489 } 469 void ReportMinimalOrthancVersion(unsigned int major,
490 }
491
492 inline void LogWarning(OrthancPluginContext* context,
493 const std::string& message)
494 {
495 if (context != NULL)
496 {
497 OrthancPluginLogWarning(context, message.c_str());
498 }
499 }
500
501 inline void LogInfo(OrthancPluginContext* context,
502 const std::string& message)
503 {
504 if (context != NULL)
505 {
506 OrthancPluginLogInfo(context, message.c_str());
507 }
508 }
509
510 void ReportMinimalOrthancVersion(OrthancPluginContext* context,
511 unsigned int major,
512 unsigned int minor, 470 unsigned int minor,
513 unsigned int revision); 471 unsigned int revision);
514 472
515 bool CheckMinimalOrthancVersion(OrthancPluginContext* context, 473 bool CheckMinimalOrthancVersion(unsigned int major,
516 unsigned int major,
517 unsigned int minor, 474 unsigned int minor,
518 unsigned int revision); 475 unsigned int revision);
519 476
520 477
521 namespace Internals 478 namespace Internals
530 Callback(output, url, request); 487 Callback(output, url, request);
531 return OrthancPluginErrorCode_Success; 488 return OrthancPluginErrorCode_Success;
532 } 489 }
533 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) 490 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e)
534 { 491 {
492 #if HAS_ORTHANC_EXCEPTION == 1
493 if (HasGlobalContext() &&
494 e.HasDetails())
495 {
496 OrthancPluginSetHttpErrorDetails(GetGlobalContext(), output, e.GetDetails());
497 }
498 #endif
499
535 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); 500 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
536 } 501 }
537 catch (boost::bad_lexical_cast&) 502 catch (boost::bad_lexical_cast&)
538 { 503 {
539 return OrthancPluginErrorCode_BadFileFormat; 504 return OrthancPluginErrorCode_BadFileFormat;
545 } 510 }
546 } 511 }
547 512
548 513
549 template <RestCallback Callback> 514 template <RestCallback Callback>
550 void RegisterRestCallback(OrthancPluginContext* context, 515 void RegisterRestCallback(const std::string& uri,
551 const std::string& uri,
552 bool isThreadSafe) 516 bool isThreadSafe)
553 { 517 {
554 if (isThreadSafe) 518 if (isThreadSafe)
555 { 519 {
556 OrthancPluginRegisterRestCallbackNoLock(context, uri.c_str(), Internals::Protect<Callback>); 520 OrthancPluginRegisterRestCallbackNoLock
521 (GetGlobalContext(), uri.c_str(), Internals::Protect<Callback>);
557 } 522 }
558 else 523 else
559 { 524 {
560 OrthancPluginRegisterRestCallback(context, uri.c_str(), Internals::Protect<Callback>); 525 OrthancPluginRegisterRestCallback
526 (GetGlobalContext(), uri.c_str(), Internals::Protect<Callback>);
561 } 527 }
562 } 528 }
563 529
564 530
565 #if HAS_ORTHANC_PLUGIN_PEERS == 1 531 #if HAS_ORTHANC_PLUGIN_PEERS == 1
566 class OrthancPeers : public boost::noncopyable 532 class OrthancPeers : public boost::noncopyable
567 { 533 {
568 private: 534 private:
569 typedef std::map<std::string, uint32_t> Index; 535 typedef std::map<std::string, uint32_t> Index;
570 536
571 OrthancPluginContext *context_;
572 OrthancPluginPeers *peers_; 537 OrthancPluginPeers *peers_;
573 Index index_; 538 Index index_;
574 uint32_t timeout_; 539 uint32_t timeout_;
575 540
576 size_t GetPeerIndex(const std::string& name) const; 541 size_t GetPeerIndex(const std::string& name) const;
577 542
578 public: 543 public:
579 OrthancPeers(OrthancPluginContext* context); 544 OrthancPeers();
580 545
581 ~OrthancPeers(); 546 ~OrthancPeers();
582 547
583 uint32_t GetTimeout() const 548 uint32_t GetTimeout() const
584 { 549 {
713 678
714 virtual void Stop(OrthancPluginJobStopReason reason) = 0; 679 virtual void Stop(OrthancPluginJobStopReason reason) = 0;
715 680
716 virtual void Reset() = 0; 681 virtual void Reset() = 0;
717 682
718 static OrthancPluginJob* Create(OrthancPluginContext* context, 683 static OrthancPluginJob* Create(OrthancJob* job /* takes ownership */);
719 OrthancJob* job /* takes ownership */); 684
720 685 static std::string Submit(OrthancJob* job /* takes ownership */,
721 static std::string Submit(OrthancPluginContext* context,
722 OrthancJob* job /* takes ownership */,
723 int priority); 686 int priority);
724 }; 687 };
725 #endif 688 #endif
726 } 689 }