comparison Plugins/Samples/Common/OrthancPluginCppWrapper.h @ 2817:473bf302d629

C++ wrappers around OrthancPluginJob and OrthancPluginPeers
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 12 Sep 2018 15:25:50 +0200
parents 579acc5e5412
children 807169f85ba9
comparison
equal deleted inserted replaced
2816:567d1be0849e 2817:473bf302d629
44 #include <map> 44 #include <map>
45 45
46 46
47 47
48 #if !defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) 48 #if !defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE)
49 #define ORTHANC_PLUGINS_VERSION_IS_ABOVE(major, minor, revision) \ 49 #define ORTHANC_PLUGINS_VERSION_IS_ABOVE(major, minor, revision) \
50 (ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER > major || \ 50 (ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER > major || \
51 (ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER == major && \ 51 (ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER == major && \
52 (ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER > minor || \ 52 (ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER > minor || \
53 (ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER == minor && \ 53 (ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER == minor && \
54 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER >= revision)))) 54 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER >= revision))))
55 #endif 55 #endif
56 56
57 57
58 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 2, 0) 58 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 2, 0)
59 // The "OrthancPluginFindMatcher()" primitive was introduced in Orthanc 1.2.0 59 // The "OrthancPluginFindMatcher()" primitive was introduced in Orthanc 1.2.0
60 # define HAS_ORTHANC_PLUGIN_FIND_MATCHER 1 60 # define HAS_ORTHANC_PLUGIN_FIND_MATCHER 1
61 #else 61 #else
62 # define HAS_ORTHANC_PLUGIN_FIND_MATCHER 0 62 # define HAS_ORTHANC_PLUGIN_FIND_MATCHER 0
63 #endif
64
65
66 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 4, 2)
67 # define HAS_ORTHANC_PLUGIN_PEERS 1
68 # define HAS_ORTHANC_PLUGIN_JOB 1
69 #else
70 # define HAS_ORTHANC_PLUGIN_PEERS 0
71 # define HAS_ORTHANC_PLUGIN_JOB 0
63 #endif 72 #endif
64 73
65 74
66 75
67 namespace OrthancPlugins 76 namespace OrthancPlugins
529 else 538 else
530 { 539 {
531 OrthancPluginRegisterRestCallback(context, uri.c_str(), Internals::Protect<Callback>); 540 OrthancPluginRegisterRestCallback(context, uri.c_str(), Internals::Protect<Callback>);
532 } 541 }
533 } 542 }
543
544
545 #if HAS_ORTHANC_PLUGIN_PEERS == 1
546 class OrthancPeers : public boost::noncopyable
547 {
548 private:
549 typedef std::map<std::string, uint32_t> Index;
550
551 OrthancPluginContext *context_;
552 OrthancPluginPeers *peers_;
553 Index index_;
554 uint32_t timeout_;
555
556 public:
557 OrthancPeers(OrthancPluginContext* context);
558
559 ~OrthancPeers();
560
561 uint32_t GetTimeout() const
562 {
563 return timeout_;
564 }
565
566 void SetTimeout(uint32_t timeout)
567 {
568 timeout_ = timeout;
569 }
570
571 bool LookupName(size_t& target,
572 const std::string& name) const;
573
574 std::string GetPeerName(size_t index) const;
575
576 std::string GetPeerUrl(size_t index) const;
577
578 std::string GetPeerUrl(const std::string& name) const;
579
580 size_t GetPeersCount() const
581 {
582 return index_.size();
583 }
584
585 bool DoGet(MemoryBuffer& target,
586 size_t index,
587 const std::string& uri) const;
588
589 bool DoGet(MemoryBuffer& target,
590 const std::string& name,
591 const std::string& uri) const;
592
593 bool DoGet(Json::Value& target,
594 size_t index,
595 const std::string& uri) const;
596
597 bool DoGet(Json::Value& target,
598 const std::string& name,
599 const std::string& uri) const;
600
601 bool DoPost(MemoryBuffer& target,
602 size_t index,
603 const std::string& uri,
604 const std::string& body) const;
605
606 bool DoPost(MemoryBuffer& target,
607 const std::string& name,
608 const std::string& uri,
609 const std::string& body) const;
610
611 bool DoPost(Json::Value& target,
612 size_t index,
613 const std::string& uri,
614 const std::string& body) const;
615
616 bool DoPost(Json::Value& target,
617 const std::string& name,
618 const std::string& uri,
619 const std::string& body) const;
620
621 bool DoPut(size_t index,
622 const std::string& uri,
623 const std::string& body) const;
624
625 bool DoPut(const std::string& name,
626 const std::string& uri,
627 const std::string& body) const;
628
629 bool DoDelete(size_t index,
630 const std::string& uri) const;
631
632 bool DoDelete(const std::string& name,
633 const std::string& uri) const;
634 };
635 #endif
636
637
638
639 #if HAS_ORTHANC_PLUGIN_JOB == 1
640 class OrthancJob : public boost::noncopyable
641 {
642 private:
643 std::string jobType_;
644 std::string content_;
645 bool hasSerialized_;
646 std::string serialized_;
647 float progress_;
648
649 static void CallbackFinalize(void* job);
650
651 static float CallbackGetProgress(void* job);
652
653 static const char* CallbackGetContent(void* job);
654
655 static const char* CallbackGetSerialized(void* job);
656
657 static OrthancPluginJobStepStatus CallbackStep(void* job);
658
659 static OrthancPluginErrorCode CallbackStop(void* job,
660 OrthancPluginJobStopReason reason);
661
662 static OrthancPluginErrorCode CallbackReset(void* job);
663
664 protected:
665 void ClearContent();
666
667 void UpdateContent(const Json::Value& content);
668
669 void ClearSerialized();
670
671 void UpdateSerialized(const Json::Value& serialized);
672
673 void UpdateProgress(float progress);
674
675 public:
676 OrthancJob(const std::string& jobType);
677
678 virtual ~OrthancJob()
679 {
680 }
681
682 virtual OrthancPluginJobStepStatus Step() = 0;
683
684 virtual void Stop(OrthancPluginJobStopReason reason) = 0;
685
686 virtual void Reset() = 0;
687
688 static OrthancPluginJob* Create(OrthancPluginContext* context,
689 OrthancJob* job /* takes ownership */);
690
691 static std::string Submit(OrthancPluginContext* context,
692 OrthancJob* job /* takes ownership */,
693 int priority);
694 };
695 #endif
534 } 696 }