henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_BASE_MESSAGEHANDLER_H_ |
| 12 | #define WEBRTC_BASE_MESSAGEHANDLER_H_ |
| 13 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 14 | #include <memory> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 15 | #include <utility> |
| 16 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | #include "webrtc/base/constructormagic.h" |
Jelena Marusic | 5d6e58e | 2015-07-13 11:16:39 +0200 | [diff] [blame] | 18 | #include "webrtc/base/scoped_ptr.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | |
| 20 | namespace rtc { |
| 21 | |
| 22 | struct Message; |
| 23 | |
| 24 | // Messages get dispatched to a MessageHandler |
| 25 | |
| 26 | class MessageHandler { |
| 27 | public: |
| 28 | virtual ~MessageHandler(); |
| 29 | virtual void OnMessage(Message* msg) = 0; |
| 30 | |
| 31 | protected: |
| 32 | MessageHandler() {} |
| 33 | |
| 34 | private: |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 35 | RTC_DISALLOW_COPY_AND_ASSIGN(MessageHandler); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | // Helper class to facilitate executing a functor on a thread. |
| 39 | template <class ReturnT, class FunctorT> |
| 40 | class FunctorMessageHandler : public MessageHandler { |
| 41 | public: |
| 42 | explicit FunctorMessageHandler(const FunctorT& functor) |
| 43 | : functor_(functor) {} |
| 44 | virtual void OnMessage(Message* msg) { |
| 45 | result_ = functor_(); |
| 46 | } |
| 47 | const ReturnT& result() const { return result_; } |
| 48 | |
| 49 | private: |
| 50 | FunctorT functor_; |
| 51 | ReturnT result_; |
| 52 | }; |
| 53 | |
kwiberg | ba5ea44 | 2016-04-25 18:08:40 -0700 | [diff] [blame] | 54 | // Specialization for std::unique_ptr<ReturnT>. |
Jelena Marusic | 5d6e58e | 2015-07-13 11:16:39 +0200 | [diff] [blame] | 55 | template <class ReturnT, class FunctorT> |
kwiberg | ba5ea44 | 2016-04-25 18:08:40 -0700 | [diff] [blame] | 56 | class FunctorMessageHandler<class std::unique_ptr<ReturnT>, FunctorT> |
Jelena Marusic | 5d6e58e | 2015-07-13 11:16:39 +0200 | [diff] [blame] | 57 | : public MessageHandler { |
| 58 | public: |
| 59 | explicit FunctorMessageHandler(const FunctorT& functor) : functor_(functor) {} |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 60 | virtual void OnMessage(Message* msg) { result_ = std::move(functor_()); } |
kwiberg | ba5ea44 | 2016-04-25 18:08:40 -0700 | [diff] [blame] | 61 | std::unique_ptr<ReturnT> result() { return std::move(result_); } |
Jelena Marusic | 5d6e58e | 2015-07-13 11:16:39 +0200 | [diff] [blame] | 62 | |
| 63 | private: |
| 64 | FunctorT functor_; |
kwiberg | ba5ea44 | 2016-04-25 18:08:40 -0700 | [diff] [blame] | 65 | std::unique_ptr<ReturnT> result_; |
Jelena Marusic | 5d6e58e | 2015-07-13 11:16:39 +0200 | [diff] [blame] | 66 | }; |
| 67 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 68 | // Specialization for ReturnT of void. |
| 69 | template <class FunctorT> |
| 70 | class FunctorMessageHandler<void, FunctorT> : public MessageHandler { |
| 71 | public: |
| 72 | explicit FunctorMessageHandler(const FunctorT& functor) |
| 73 | : functor_(functor) {} |
| 74 | virtual void OnMessage(Message* msg) { |
| 75 | functor_(); |
| 76 | } |
| 77 | void result() const {} |
| 78 | |
| 79 | private: |
| 80 | FunctorT functor_; |
| 81 | }; |
| 82 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 83 | } // namespace rtc |
| 84 | |
| 85 | #endif // WEBRTC_BASE_MESSAGEHANDLER_H_ |