# HG changeset patch # User Sebastien Jodogne # Date 1541934811 -3600 # Node ID 99e31898910e4358f85dba4786445be744d30434 # Parent 72355b6379457e3171f48d81c7151be443fbb999 IObservable.cpp diff -r 72355b637945 -r 99e31898910e Applications/Samples/SingleFrameApplication.h --- 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 +#include #include diff -r 72355b637945 -r 99e31898910e Applications/Samples/SingleFrameEditorApplication.h --- 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 #include #include +#include #include #include #include diff -r 72355b637945 -r 99e31898910e Framework/Messages/IObservable.cpp --- /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 . + **/ + + +#include "IObservable.h" + +#include + +#include + +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::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(*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::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); + } +} diff -r 72355b637945 -r 99e31898910e Framework/Messages/IObservable.h --- 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::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(*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::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); }; } diff -r 72355b637945 -r 99e31898910e Framework/Messages/MessageBroker.h --- 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 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 activeObservers_; // the list of observers that are currently alive (that have not been deleted) + private: + std::set 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(); } }; - } diff -r 72355b637945 -r 99e31898910e Framework/Messages/Promise.h --- 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; } - }; - - } diff -r 72355b637945 -r 99e31898910e Framework/Widgets/PanMouseTracker.cpp --- 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 +#include namespace OrthancStone { diff -r 72355b637945 -r 99e31898910e Framework/Widgets/SliceViewerWidget.cpp --- 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 +#include #include diff -r 72355b637945 -r 99e31898910e Framework/Widgets/TestWorldSceneWidget.cpp --- 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 + #include #include diff -r 72355b637945 -r 99e31898910e Framework/Widgets/WorldSceneWidget.cpp --- 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 +#include #include #include diff -r 72355b637945 -r 99e31898910e Framework/Widgets/WorldSceneWidget.h --- 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 diff -r 72355b637945 -r 99e31898910e Framework/Widgets/ZoomMouseTracker.cpp --- 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 +#include namespace OrthancStone { diff -r 72355b637945 -r 99e31898910e Resources/CMake/OrthancStoneConfiguration.cmake --- 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