comparison Framework/Messages/ICallable.h @ 299:3897f9f28cfa am-callable-and-promise

backup work in progress: updated messaging framework with ICallable
author am@osimis.io
date Fri, 14 Sep 2018 16:44:01 +0200
parents
children b70e9be013e4
comparison
equal deleted inserted replaced
298:f58bfb7bbcc9 299:3897f9f28cfa
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #pragma once
23
24 #include "IMessage.h"
25
26 #include <boost/noncopyable.hpp>
27
28 namespace OrthancStone {
29
30 class IObserver;
31
32 // This is referencing an object and member function that can be notified
33 // by an IObservable. The object must derive from IO
34 // The member functions must be of type "void Function(const IMessage& message)" or reference a derived class of IMessage
35 class ICallable : public boost::noncopyable
36 {
37 public:
38 virtual ~ICallable()
39 {
40 }
41
42 virtual void Apply(const IMessage& message) = 0;
43
44 virtual MessageType GetMessageType() const = 0;
45 virtual IObserver* GetObserver() const = 0;
46 };
47
48 template <typename TMessage>
49 class MessageHandler: public ICallable
50 {
51 };
52
53
54 template <typename TObserver,
55 typename TMessage>
56 class Callable : public MessageHandler<TMessage>
57 {
58 private:
59 typedef void (TObserver::* MemberFunction) (const TMessage&);
60
61 TObserver& observer_;
62 MemberFunction function_;
63
64 public:
65 Callable(TObserver& observer,
66 MemberFunction function) :
67 observer_(observer),
68 function_(function)
69 {
70 }
71
72 void ApplyInternal(const TMessage& message)
73 {
74 (observer_.*function_) (message);
75 }
76
77 virtual void Apply(const IMessage& message)
78 {
79 ApplyInternal(dynamic_cast<const TMessage&>(message));
80 }
81
82 virtual MessageType GetMessageType() const
83 {
84 return static_cast<MessageType>(TMessage::Type);
85 }
86
87 virtual IObserver* GetObserver() const
88 {
89 return &observer_;
90 }
91 };
92 }