You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  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. #ifndef RTC_BASE_MESSAGE_HANDLER_H_
  11. #define RTC_BASE_MESSAGE_HANDLER_H_
  12. #include <utility>
  13. #include "rtc_base/constructor_magic.h"
  14. #include "rtc_base/system/rtc_export.h"
  15. namespace rtc {
  16. struct Message;
  17. // Messages get dispatched to a MessageHandler
  18. class RTC_EXPORT MessageHandler {
  19. public:
  20. virtual ~MessageHandler();
  21. virtual void OnMessage(Message* msg) = 0;
  22. protected:
  23. MessageHandler() {}
  24. private:
  25. RTC_DISALLOW_COPY_AND_ASSIGN(MessageHandler);
  26. };
  27. // Helper class to facilitate executing a functor on a thread.
  28. template <class ReturnT, class FunctorT>
  29. class FunctorMessageHandler : public MessageHandler {
  30. public:
  31. explicit FunctorMessageHandler(FunctorT&& functor)
  32. : functor_(std::forward<FunctorT>(functor)) {}
  33. virtual void OnMessage(Message* msg) { result_ = functor_(); }
  34. const ReturnT& result() const { return result_; }
  35. // Returns moved result. Should not call result() or MoveResult() again
  36. // after this.
  37. ReturnT MoveResult() { return std::move(result_); }
  38. private:
  39. FunctorT functor_;
  40. ReturnT result_;
  41. };
  42. // Specialization for ReturnT of void.
  43. template <class FunctorT>
  44. class FunctorMessageHandler<void, FunctorT> : public MessageHandler {
  45. public:
  46. explicit FunctorMessageHandler(FunctorT&& functor)
  47. : functor_(std::forward<FunctorT>(functor)) {}
  48. virtual void OnMessage(Message* msg) { functor_(); }
  49. void result() const {}
  50. void MoveResult() {}
  51. private:
  52. FunctorT functor_;
  53. };
  54. } // namespace rtc
  55. #endif // RTC_BASE_MESSAGE_HANDLER_H_