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 <memory> |
14 #include <utility> | 15 #include <utility> |
15 | 16 |
16 #include "webrtc/base/constructormagic.h" | 17 #include "webrtc/base/constructormagic.h" |
17 #include "webrtc/base/scoped_ptr.h" | 18 #include "webrtc/base/scoped_ptr.h" |
18 | 19 |
19 namespace rtc { | 20 namespace rtc { |
20 | 21 |
21 struct Message; | 22 struct Message; |
22 | 23 |
23 // Messages get dispatched to a MessageHandler | 24 // Messages get dispatched to a MessageHandler |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 public: | 58 public: |
58 explicit FunctorMessageHandler(const FunctorT& functor) : functor_(functor) {} | 59 explicit FunctorMessageHandler(const FunctorT& functor) : functor_(functor) {} |
59 virtual void OnMessage(Message* msg) { result_ = std::move(functor_()); } | 60 virtual void OnMessage(Message* msg) { result_ = std::move(functor_()); } |
60 rtc::scoped_ptr<ReturnT> result() { return std::move(result_); } | 61 rtc::scoped_ptr<ReturnT> result() { return std::move(result_); } |
61 | 62 |
62 private: | 63 private: |
63 FunctorT functor_; | 64 FunctorT functor_; |
64 rtc::scoped_ptr<ReturnT> result_; | 65 rtc::scoped_ptr<ReturnT> result_; |
65 }; | 66 }; |
66 | 67 |
| 68 // Specialization for std::unique_ptr<ReturnT>. |
| 69 template <class ReturnT, class FunctorT> |
| 70 class FunctorMessageHandler<class std::unique_ptr<ReturnT>, FunctorT> |
| 71 : public MessageHandler { |
| 72 public: |
| 73 explicit FunctorMessageHandler(const FunctorT& functor) : functor_(functor) {} |
| 74 virtual void OnMessage(Message* msg) { result_ = std::move(functor_()); } |
| 75 std::unique_ptr<ReturnT> result() { return std::move(result_); } |
| 76 |
| 77 private: |
| 78 FunctorT functor_; |
| 79 std::unique_ptr<ReturnT> result_; |
| 80 }; |
| 81 |
67 // Specialization for ReturnT of void. | 82 // Specialization for ReturnT of void. |
68 template <class FunctorT> | 83 template <class FunctorT> |
69 class FunctorMessageHandler<void, FunctorT> : public MessageHandler { | 84 class FunctorMessageHandler<void, FunctorT> : public MessageHandler { |
70 public: | 85 public: |
71 explicit FunctorMessageHandler(const FunctorT& functor) | 86 explicit FunctorMessageHandler(const FunctorT& functor) |
72 : functor_(functor) {} | 87 : functor_(functor) {} |
73 virtual void OnMessage(Message* msg) { | 88 virtual void OnMessage(Message* msg) { |
74 functor_(); | 89 functor_(); |
75 } | 90 } |
76 void result() const {} | 91 void result() const {} |
77 | 92 |
78 private: | 93 private: |
79 FunctorT functor_; | 94 FunctorT functor_; |
80 }; | 95 }; |
81 | 96 |
82 } // namespace rtc | 97 } // namespace rtc |
83 | 98 |
84 #endif // WEBRTC_BASE_MESSAGEHANDLER_H_ | 99 #endif // WEBRTC_BASE_MESSAGEHANDLER_H_ |
OLD | NEW |