blob: 90563e4083993cf1333f9190304d078822f54675 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
kwiberg0eb15ed2015-12-17 03:04:15 -080014#include <utility>
15
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016#include "webrtc/base/constructormagic.h"
Jelena Marusic5d6e58e2015-07-13 11:16:39 +020017#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
19namespace rtc {
20
21struct Message;
22
23// Messages get dispatched to a MessageHandler
24
25class MessageHandler {
26 public:
27 virtual ~MessageHandler();
28 virtual void OnMessage(Message* msg) = 0;
29
30 protected:
31 MessageHandler() {}
32
33 private:
henrikg3c089d72015-09-16 05:37:44 -070034 RTC_DISALLOW_COPY_AND_ASSIGN(MessageHandler);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035};
36
37// Helper class to facilitate executing a functor on a thread.
38template <class ReturnT, class FunctorT>
39class FunctorMessageHandler : public MessageHandler {
40 public:
41 explicit FunctorMessageHandler(const FunctorT& functor)
42 : functor_(functor) {}
43 virtual void OnMessage(Message* msg) {
44 result_ = functor_();
45 }
46 const ReturnT& result() const { return result_; }
47
48 private:
49 FunctorT functor_;
50 ReturnT result_;
51};
52
kwibergba5ea442016-04-25 18:08:40 -070053// Specialization for std::unique_ptr<ReturnT>.
Jelena Marusic5d6e58e2015-07-13 11:16:39 +020054template <class ReturnT, class FunctorT>
kwibergba5ea442016-04-25 18:08:40 -070055class FunctorMessageHandler<class std::unique_ptr<ReturnT>, FunctorT>
Jelena Marusic5d6e58e2015-07-13 11:16:39 +020056 : public MessageHandler {
57 public:
58 explicit FunctorMessageHandler(const FunctorT& functor) : functor_(functor) {}
kwiberg0eb15ed2015-12-17 03:04:15 -080059 virtual void OnMessage(Message* msg) { result_ = std::move(functor_()); }
kwibergba5ea442016-04-25 18:08:40 -070060 std::unique_ptr<ReturnT> result() { return std::move(result_); }
Jelena Marusic5d6e58e2015-07-13 11:16:39 +020061
62 private:
63 FunctorT functor_;
kwibergba5ea442016-04-25 18:08:40 -070064 std::unique_ptr<ReturnT> result_;
Jelena Marusic5d6e58e2015-07-13 11:16:39 +020065};
66
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067// Specialization for ReturnT of void.
68template <class FunctorT>
69class FunctorMessageHandler<void, FunctorT> : public MessageHandler {
70 public:
71 explicit FunctorMessageHandler(const FunctorT& functor)
72 : functor_(functor) {}
73 virtual void OnMessage(Message* msg) {
74 functor_();
75 }
76 void result() const {}
77
78 private:
79 FunctorT functor_;
80};
81
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082} // namespace rtc
83
84#endif // WEBRTC_BASE_MESSAGEHANDLER_H_