changeset 403:99e31898910e

IObservable.cpp
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 11 Nov 2018 12:13:31 +0100
parents 72355b637945
children ba4ace20454e
files Applications/Samples/SingleFrameApplication.h Applications/Samples/SingleFrameEditorApplication.h Framework/Messages/IObservable.cpp Framework/Messages/IObservable.h Framework/Messages/MessageBroker.h Framework/Messages/Promise.h Framework/Widgets/PanMouseTracker.cpp Framework/Widgets/SliceViewerWidget.cpp Framework/Widgets/TestWorldSceneWidget.cpp Framework/Widgets/WorldSceneWidget.cpp Framework/Widgets/WorldSceneWidget.h Framework/Widgets/ZoomMouseTracker.cpp Resources/CMake/OrthancStoneConfiguration.cmake
diffstat 13 files changed, 127 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/Applications/Samples/SingleFrameApplication.h	Sat Nov 10 12:34:45 2018 +0100
+++ b/Applications/Samples/SingleFrameApplication.h	Sun Nov 11 12:13:31 2018 +0100
@@ -27,6 +27,7 @@
 #include "../../Framework/Widgets/SliceViewerWidget.h"
 
 #include <Core/Logging.h>
+#include <Core/OrthancException.h>
 
 #include <boost/math/constants/constants.hpp>
 
--- a/Applications/Samples/SingleFrameEditorApplication.h	Sat Nov 10 12:34:45 2018 +0100
+++ b/Applications/Samples/SingleFrameEditorApplication.h	Sun Nov 11 12:13:31 2018 +0100
@@ -34,6 +34,7 @@
 #include <Core/Images/PamWriter.h>
 #include <Core/Images/PngWriter.h>
 #include <Core/Logging.h>
+#include <Core/OrthancException.h>
 #include <Core/Toolbox.h>
 #include <Plugins/Samples/Common/DicomDatasetReader.h>
 #include <Plugins/Samples/Common/FullOrthancDataset.h>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Framework/Messages/IObservable.cpp	Sun Nov 11 12:13:31 2018 +0100
@@ -0,0 +1,96 @@
+/**
+ * Stone of Orthanc
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2018 Osimis S.A., Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "IObservable.h"
+
+#include <Core/OrthancException.h>
+
+#include <cassert>
+
+namespace OrthancStone 
+{
+  IObservable::~IObservable()
+  {
+    // delete all callables (this will also unregister them from the broker)
+    for (Callables::const_iterator it = callables_.begin();
+         it != callables_.end(); ++it)
+    {
+      for (std::set<ICallable*>::const_iterator
+             it2 = it->second.begin(); it2 != it->second.end(); ++it2)
+      {
+        delete *it2;
+      }
+    }
+
+    // unregister the forwarders but don't delete them (they'll be
+    // deleted by the observable they are observing as any other
+    // callable)
+    for (Forwarders::iterator it = forwarders_.begin();
+         it != forwarders_.end(); ++it)
+    {
+      IMessageForwarder* fw = *it;
+      broker_.Unregister(dynamic_cast<IObserver&>(*fw));
+    }
+  }
+  
+
+  void IObservable::RegisterObserverCallback(ICallable* callable)
+  {
+    if (callable == NULL)
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
+    }
+    
+    MessageType messageType = callable->GetMessageType();
+
+    callables_[messageType].insert(callable);
+  }
+
+  
+  void IObservable::EmitMessage(const IMessage& message)
+  {
+    Callables::const_iterator found = callables_.find(message.GetType());
+
+    if (found != callables_.end())
+    {
+      for (std::set<ICallable*>::const_iterator
+             it = found->second.begin(); it != found->second.end(); ++it)
+      {
+        assert(*it != NULL);
+        if (broker_.IsActive(*(*it)->GetObserver()))
+        {
+          (*it)->Apply(message);
+        }
+      }
+    }
+  }
+
+  
+  void IObservable::RegisterForwarder(IMessageForwarder* forwarder)
+  {
+    if (forwarder == NULL)
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
+    }
+    
+    forwarders_.insert(forwarder);
+  }
+}
--- a/Framework/Messages/IObservable.h	Sat Nov 10 12:34:45 2018 +0100
+++ b/Framework/Messages/IObservable.h	Sun Nov 11 12:13:31 2018 +0100
@@ -48,60 +48,19 @@
     {
     }
 
-    virtual ~IObservable()
-    {
-      // delete all callables (this will also unregister them from the broker)
-      for (Callables::const_iterator it = callables_.begin();
-           it != callables_.end(); ++it)
-      {
-        for (std::set<ICallable*>::const_iterator
-               it2 = it->second.begin(); it2 != it->second.end(); ++it2)
-        {
-          delete *it2;
-        }
-      }
-
-      // unregister the forwarders but don't delete them (they'll be deleted by the observable they are observing as any other callable)
-      for (Forwarders::iterator it = forwarders_.begin();
-           it != forwarders_.end(); ++it)
-      {
-        IMessageForwarder* fw = *it;
-        broker_.Unregister(dynamic_cast<IObserver&>(*fw));
-      }
-    }
-
-    void RegisterObserverCallback(ICallable* callable)
-    {
-      MessageType messageType = callable->GetMessageType();
-
-      callables_[messageType].insert(callable);
-    }
-
-    void EmitMessage(const IMessage& message)
-    {
-      Callables::const_iterator found = callables_.find(message.GetType());
-
-      if (found != callables_.end())
-      {
-        for (std::set<ICallable*>::const_iterator
-               it = found->second.begin(); it != found->second.end(); ++it)
-        {
-          if (broker_.IsActive((*it)->GetObserver()))
-          {
-            (*it)->Apply(message);
-          }
-        }
-      }
-    }
-
-    void RegisterForwarder(IMessageForwarder* forwarder)
-    {
-      forwarders_.insert(forwarder);
-    }
+    virtual ~IObservable();
 
     MessageBroker& GetBroker() const
     {
       return broker_;
     }
+
+    // Takes ownsership
+    void RegisterObserverCallback(ICallable* callable);
+
+    void EmitMessage(const IMessage& message);
+
+    // Takes ownsership
+    void RegisterForwarder(IMessageForwarder* forwarder);
   };
 }
--- a/Framework/Messages/MessageBroker.h	Sat Nov 10 12:34:45 2018 +0100
+++ b/Framework/Messages/MessageBroker.h	Sun Nov 11 12:13:31 2018 +0100
@@ -22,12 +22,12 @@
 #pragma once
 
 #include "boost/noncopyable.hpp"
+
 #include <set>
 
 namespace OrthancStone
 {
   class IObserver;
-  class IObservable;
 
   /*
    * This is a central message broker.  It keeps track of all observers and knows
@@ -36,25 +36,23 @@
    */
   class MessageBroker : public boost::noncopyable
   {
-
-    std::set<IObserver*> activeObservers_;  // the list of observers that are currently alive (that have not been deleted)
+  private:
+    std::set<const IObserver*> activeObservers_;  // the list of observers that are currently alive (that have not been deleted)
 
   public:
-
-    void Register(IObserver& observer)
+    void Register(const IObserver& observer)
     {
       activeObservers_.insert(&observer);
     }
 
-    void Unregister(IObserver& observer)
+    void Unregister(const IObserver& observer)
     {
       activeObservers_.erase(&observer);
     }
 
-    bool IsActive(IObserver* observer)
+    bool IsActive(const IObserver& observer)
     {
-      return activeObservers_.find(observer) != activeObservers_.end();
+      return activeObservers_.find(&observer) != activeObservers_.end();
     }
   };
-
 }
--- a/Framework/Messages/Promise.h	Sat Nov 10 12:34:45 2018 +0100
+++ b/Framework/Messages/Promise.h	Sun Nov 11 12:13:31 2018 +0100
@@ -47,7 +47,8 @@
     void Success(const IMessage& message)
     {
       // check the target is still alive in the broker
-      if (broker_.IsActive(successCallable_->GetObserver()))
+      if (successCallable_.get() != NULL &&
+          broker_.IsActive(*successCallable_->GetObserver()))
       {
         successCallable_->Apply(message);
       }
@@ -56,13 +57,14 @@
     void Failure(const IMessage& message)
     {
       // check the target is still alive in the broker
-      if (broker_.IsActive(failureCallable_->GetObserver()))
+      if (failureCallable_.get() != NULL &&
+          broker_.IsActive(*failureCallable_->GetObserver()))
       {
         failureCallable_->Apply(message);
       }
     }
 
-    Promise& Then(ICallable* successCallable)
+    Promise& Then(ICallable* successCallable)   // Takes ownership
     {
       if (successCallable_.get() != NULL)
       {
@@ -72,7 +74,7 @@
       return *this;
     }
 
-    Promise& Else(ICallable* failureCallable)
+    Promise& Else(ICallable* failureCallable)   // Takes ownership
     {
       if (failureCallable_.get() != NULL)
       {
@@ -81,8 +83,5 @@
       failureCallable_.reset(failureCallable);
       return *this;
     }
-
   };
-
-
 }
--- a/Framework/Widgets/PanMouseTracker.cpp	Sat Nov 10 12:34:45 2018 +0100
+++ b/Framework/Widgets/PanMouseTracker.cpp	Sun Nov 11 12:13:31 2018 +0100
@@ -22,6 +22,7 @@
 #include "PanMouseTracker.h"
 
 #include <Core/Logging.h>
+#include <Core/OrthancException.h>
 
 namespace OrthancStone
 {
--- a/Framework/Widgets/SliceViewerWidget.cpp	Sat Nov 10 12:34:45 2018 +0100
+++ b/Framework/Widgets/SliceViewerWidget.cpp	Sun Nov 11 12:13:31 2018 +0100
@@ -26,6 +26,7 @@
 #include "Framework/Layers/FrameRenderer.h"
 
 #include <Core/Logging.h>
+#include <Core/OrthancException.h>
 
 #include <boost/math/constants/constants.hpp>
 
--- a/Framework/Widgets/TestWorldSceneWidget.cpp	Sat Nov 10 12:34:45 2018 +0100
+++ b/Framework/Widgets/TestWorldSceneWidget.cpp	Sun Nov 11 12:13:31 2018 +0100
@@ -21,6 +21,8 @@
 
 #include "TestWorldSceneWidget.h"
 
+#include <Core/OrthancException.h>
+
 #include <math.h>
 #include <stdio.h>
 
--- a/Framework/Widgets/WorldSceneWidget.cpp	Sat Nov 10 12:34:45 2018 +0100
+++ b/Framework/Widgets/WorldSceneWidget.cpp	Sun Nov 11 12:13:31 2018 +0100
@@ -25,6 +25,7 @@
 #include "ZoomMouseTracker.h"
 
 #include <Core/Logging.h>
+#include <Core/OrthancException.h>
 
 #include <math.h>
 #include <memory>
--- a/Framework/Widgets/WorldSceneWidget.h	Sat Nov 10 12:34:45 2018 +0100
+++ b/Framework/Widgets/WorldSceneWidget.h	Sun Nov 11 12:13:31 2018 +0100
@@ -24,7 +24,6 @@
 #include "CairoWidget.h"
 #include "IWorldSceneInteractor.h"
 
-#include "../Toolbox/ObserversRegistry.h"
 #include "../Toolbox/ViewportGeometry.h"
 
 namespace OrthancStone
--- a/Framework/Widgets/ZoomMouseTracker.cpp	Sat Nov 10 12:34:45 2018 +0100
+++ b/Framework/Widgets/ZoomMouseTracker.cpp	Sun Nov 11 12:13:31 2018 +0100
@@ -22,6 +22,7 @@
 #include "ZoomMouseTracker.h"
 
 #include <Core/Logging.h>
+#include <Core/OrthancException.h>
 
 namespace OrthancStone
 {
--- a/Resources/CMake/OrthancStoneConfiguration.cmake	Sat Nov 10 12:34:45 2018 +0100
+++ b/Resources/CMake/OrthancStoneConfiguration.cmake	Sun Nov 11 12:13:31 2018 +0100
@@ -297,6 +297,7 @@
 
   ${ORTHANC_STONE_ROOT}/Framework/Messages/ICallable.h
   ${ORTHANC_STONE_ROOT}/Framework/Messages/IMessage.h
+  ${ORTHANC_STONE_ROOT}/Framework/Messages/IObservable.cpp
   ${ORTHANC_STONE_ROOT}/Framework/Messages/IObservable.h
   ${ORTHANC_STONE_ROOT}/Framework/Messages/IObserver.h
   ${ORTHANC_STONE_ROOT}/Framework/Messages/MessageBroker.h