OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_BASE_MESSAGEHANDLER_H_ | 11 #ifndef WEBRTC_BASE_MESSAGEHANDLER_H_ |
12 #define WEBRTC_BASE_MESSAGEHANDLER_H_ | 12 #define WEBRTC_BASE_MESSAGEHANDLER_H_ |
13 | 13 |
14 #include "webrtc/base/constructormagic.h" | 14 #include "webrtc/base/constructormagic.h" |
| 15 #include "webrtc/base/scoped_ptr.h" |
15 | 16 |
16 namespace rtc { | 17 namespace rtc { |
17 | 18 |
18 struct Message; | 19 struct Message; |
19 | 20 |
20 // Messages get dispatched to a MessageHandler | 21 // Messages get dispatched to a MessageHandler |
21 | 22 |
22 class MessageHandler { | 23 class MessageHandler { |
23 public: | 24 public: |
24 virtual ~MessageHandler(); | 25 virtual ~MessageHandler(); |
(...skipping 15 matching lines...) Expand all Loading... |
40 virtual void OnMessage(Message* msg) { | 41 virtual void OnMessage(Message* msg) { |
41 result_ = functor_(); | 42 result_ = functor_(); |
42 } | 43 } |
43 const ReturnT& result() const { return result_; } | 44 const ReturnT& result() const { return result_; } |
44 | 45 |
45 private: | 46 private: |
46 FunctorT functor_; | 47 FunctorT functor_; |
47 ReturnT result_; | 48 ReturnT result_; |
48 }; | 49 }; |
49 | 50 |
| 51 // Specialization for rtc::scoped_ptr<ReturnT>. |
| 52 template <class ReturnT, class FunctorT> |
| 53 class FunctorMessageHandler<class rtc::scoped_ptr<ReturnT>, FunctorT> |
| 54 : public MessageHandler { |
| 55 public: |
| 56 explicit FunctorMessageHandler(const FunctorT& functor) : functor_(functor) {} |
| 57 virtual void OnMessage(Message* msg) { result_ = functor_().Pass(); } |
| 58 rtc::scoped_ptr<ReturnT> result() { return result_.Pass(); } |
| 59 |
| 60 private: |
| 61 FunctorT functor_; |
| 62 rtc::scoped_ptr<ReturnT> result_; |
| 63 }; |
| 64 |
50 // Specialization for ReturnT of void. | 65 // Specialization for ReturnT of void. |
51 template <class FunctorT> | 66 template <class FunctorT> |
52 class FunctorMessageHandler<void, FunctorT> : public MessageHandler { | 67 class FunctorMessageHandler<void, FunctorT> : public MessageHandler { |
53 public: | 68 public: |
54 explicit FunctorMessageHandler(const FunctorT& functor) | 69 explicit FunctorMessageHandler(const FunctorT& functor) |
55 : functor_(functor) {} | 70 : functor_(functor) {} |
56 virtual void OnMessage(Message* msg) { | 71 virtual void OnMessage(Message* msg) { |
57 functor_(); | 72 functor_(); |
58 } | 73 } |
59 void result() const {} | 74 void result() const {} |
60 | 75 |
61 private: | 76 private: |
62 FunctorT functor_; | 77 FunctorT functor_; |
63 }; | 78 }; |
64 | 79 |
65 | |
66 } // namespace rtc | 80 } // namespace rtc |
67 | 81 |
68 #endif // WEBRTC_BASE_MESSAGEHANDLER_H_ | 82 #endif // WEBRTC_BASE_MESSAGEHANDLER_H_ |
OLD | NEW |