blob: 123c8509736794a978f3b644f37ca0bfe4db2cbd [file] [log] [blame]
henrike@webrtc.orgf7795df2014-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
14#include "webrtc/base/constructormagic.h"
15
16namespace rtc {
17
18struct Message;
19
20// Messages get dispatched to a MessageHandler
21
22class MessageHandler {
23 public:
24 virtual ~MessageHandler();
25 virtual void OnMessage(Message* msg) = 0;
26
27 protected:
28 MessageHandler() {}
29
30 private:
31 DISALLOW_COPY_AND_ASSIGN(MessageHandler);
32};
33
34// Helper class to facilitate executing a functor on a thread.
35template <class ReturnT, class FunctorT>
36class FunctorMessageHandler : public MessageHandler {
37 public:
38 explicit FunctorMessageHandler(const FunctorT& functor)
39 : functor_(functor) {}
40 virtual void OnMessage(Message* msg) {
41 result_ = functor_();
42 }
43 const ReturnT& result() const { return result_; }
44
45 private:
46 FunctorT functor_;
47 ReturnT result_;
48};
49
50// Specialization for ReturnT of void.
51template <class FunctorT>
52class FunctorMessageHandler<void, FunctorT> : public MessageHandler {
53 public:
54 explicit FunctorMessageHandler(const FunctorT& functor)
55 : functor_(functor) {}
56 virtual void OnMessage(Message* msg) {
57 functor_();
58 }
59 void result() const {}
60
61 private:
62 FunctorT functor_;
63};
64
65
66} // namespace rtc
67
68#endif // WEBRTC_BASE_MESSAGEHANDLER_H_