blob: 2d964df79c5781d8798c9502b6691afb71f764c9 [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
jbauch555604a2016-04-26 03:13:22 -070014#include <memory>
kwiberg0eb15ed2015-12-17 03:04:15 -080015#include <utility>
16
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#include "webrtc/base/constructormagic.h"
Jelena Marusic5d6e58e2015-07-13 11:16:39 +020018#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
20namespace rtc {
21
22struct Message;
23
24// Messages get dispatched to a MessageHandler
25
26class MessageHandler {
27 public:
28 virtual ~MessageHandler();
29 virtual void OnMessage(Message* msg) = 0;
30
31 protected:
32 MessageHandler() {}
33
34 private:
henrikg3c089d72015-09-16 05:37:44 -070035 RTC_DISALLOW_COPY_AND_ASSIGN(MessageHandler);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036};
37
38// Helper class to facilitate executing a functor on a thread.
39template <class ReturnT, class FunctorT>
40class 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
kwibergba5ea442016-04-25 18:08:40 -070054// Specialization for std::unique_ptr<ReturnT>.
Jelena Marusic5d6e58e2015-07-13 11:16:39 +020055template <class ReturnT, class FunctorT>
kwibergba5ea442016-04-25 18:08:40 -070056class FunctorMessageHandler<class std::unique_ptr<ReturnT>, FunctorT>
Jelena Marusic5d6e58e2015-07-13 11:16:39 +020057 : public MessageHandler {
58 public:
59 explicit FunctorMessageHandler(const FunctorT& functor) : functor_(functor) {}
kwiberg0eb15ed2015-12-17 03:04:15 -080060 virtual void OnMessage(Message* msg) { result_ = std::move(functor_()); }
kwibergba5ea442016-04-25 18:08:40 -070061 std::unique_ptr<ReturnT> result() { return std::move(result_); }
Jelena Marusic5d6e58e2015-07-13 11:16:39 +020062
63 private:
64 FunctorT functor_;
kwibergba5ea442016-04-25 18:08:40 -070065 std::unique_ptr<ReturnT> result_;
Jelena Marusic5d6e58e2015-07-13 11:16:39 +020066};
67
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068// Specialization for ReturnT of void.
69template <class FunctorT>
70class 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.orgf0488722014-05-13 18:00:26 +000083} // namespace rtc
84
85#endif // WEBRTC_BASE_MESSAGEHANDLER_H_