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 |
(...skipping 27 matching lines...) Expand all Loading... | |
38 template <class ReturnT, class FunctorT> | 38 template <class ReturnT, class FunctorT> |
39 class FunctorMessageHandler : public MessageHandler { | 39 class FunctorMessageHandler : public MessageHandler { |
40 public: | 40 public: |
41 explicit FunctorMessageHandler(const FunctorT& functor) | 41 explicit FunctorMessageHandler(const FunctorT& functor) |
42 : functor_(functor) {} | 42 : functor_(functor) {} |
43 virtual void OnMessage(Message* msg) { | 43 virtual void OnMessage(Message* msg) { |
44 result_ = functor_(); | 44 result_ = functor_(); |
45 } | 45 } |
46 const ReturnT& result() const { return result_; } | 46 const ReturnT& result() const { return result_; } |
47 | 47 |
48 // Returns moved result. Should not call result() or ConsumeResult() again | |
49 // after this. | |
pthatcher1
2017/02/10 18:18:01
What happens if you do?
Taylor Brandstetter
2017/02/10 19:29:42
Undefined behavior.
| |
50 ReturnT ConsumeResult() { return std::move(result_); } | |
tommi
2017/02/10 19:38:55
MoveResult() ?
Taylor Brandstetter
2017/02/11 01:03:30
Done.
| |
51 | |
48 private: | 52 private: |
49 FunctorT functor_; | 53 FunctorT functor_; |
50 ReturnT result_; | 54 ReturnT result_; |
51 }; | 55 }; |
52 | 56 |
53 // Specialization for std::unique_ptr<ReturnT>. | |
54 template <class ReturnT, class FunctorT> | |
55 class FunctorMessageHandler<class std::unique_ptr<ReturnT>, FunctorT> | |
56 : public MessageHandler { | |
57 public: | |
58 explicit FunctorMessageHandler(const FunctorT& functor) : functor_(functor) {} | |
59 virtual void OnMessage(Message* msg) { result_ = std::move(functor_()); } | |
60 std::unique_ptr<ReturnT> result() { return std::move(result_); } | |
61 | |
62 private: | |
63 FunctorT functor_; | |
64 std::unique_ptr<ReturnT> result_; | |
65 }; | |
66 | |
67 // Specialization for ReturnT of void. | 57 // Specialization for ReturnT of void. |
68 template <class FunctorT> | 58 template <class FunctorT> |
69 class FunctorMessageHandler<void, FunctorT> : public MessageHandler { | 59 class FunctorMessageHandler<void, FunctorT> : public MessageHandler { |
70 public: | 60 public: |
71 explicit FunctorMessageHandler(const FunctorT& functor) | 61 explicit FunctorMessageHandler(const FunctorT& functor) |
72 : functor_(functor) {} | 62 : functor_(functor) {} |
73 virtual void OnMessage(Message* msg) { | 63 virtual void OnMessage(Message* msg) { |
74 functor_(); | 64 functor_(); |
75 } | 65 } |
76 void result() const {} | 66 void result() const {} |
67 void ConsumeResult() {} | |
77 | 68 |
78 private: | 69 private: |
79 FunctorT functor_; | 70 FunctorT functor_; |
80 }; | 71 }; |
81 | 72 |
82 } // namespace rtc | 73 } // namespace rtc |
83 | 74 |
84 #endif // WEBRTC_BASE_MESSAGEHANDLER_H_ | 75 #endif // WEBRTC_BASE_MESSAGEHANDLER_H_ |
OLD | NEW |