thread_message.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2020 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_THREAD_MESSAGE_H_
  11. #define RTC_BASE_THREAD_MESSAGE_H_
  12. #include <list>
  13. #include <memory>
  14. #include <utility>
  15. #include "api/scoped_refptr.h"
  16. #include "rtc_base/location.h"
  17. #include "rtc_base/message_handler.h"
  18. namespace rtc {
  19. // Derive from this for specialized data
  20. // App manages lifetime, except when messages are purged
  21. class MessageData {
  22. public:
  23. MessageData() {}
  24. virtual ~MessageData() {}
  25. };
  26. template <class T>
  27. class TypedMessageData : public MessageData {
  28. public:
  29. explicit TypedMessageData(const T& data) : data_(data) {}
  30. const T& data() const { return data_; }
  31. T& data() { return data_; }
  32. private:
  33. T data_;
  34. };
  35. // Like TypedMessageData, but for pointers that require a delete.
  36. template <class T>
  37. class ScopedMessageData : public MessageData {
  38. public:
  39. explicit ScopedMessageData(std::unique_ptr<T> data)
  40. : data_(std::move(data)) {}
  41. // Deprecated.
  42. // TODO(deadbeef): Remove this once downstream applications stop using it.
  43. explicit ScopedMessageData(T* data) : data_(data) {}
  44. // Deprecated.
  45. // TODO(deadbeef): Returning a reference to a unique ptr? Why. Get rid of
  46. // this once downstream applications stop using it, then rename inner_data to
  47. // just data.
  48. const std::unique_ptr<T>& data() const { return data_; }
  49. std::unique_ptr<T>& data() { return data_; }
  50. const T& inner_data() const { return *data_; }
  51. T& inner_data() { return *data_; }
  52. private:
  53. std::unique_ptr<T> data_;
  54. };
  55. // Like ScopedMessageData, but for reference counted pointers.
  56. template <class T>
  57. class ScopedRefMessageData : public MessageData {
  58. public:
  59. explicit ScopedRefMessageData(T* data) : data_(data) {}
  60. const scoped_refptr<T>& data() const { return data_; }
  61. scoped_refptr<T>& data() { return data_; }
  62. private:
  63. scoped_refptr<T> data_;
  64. };
  65. template <class T>
  66. inline MessageData* WrapMessageData(const T& data) {
  67. return new TypedMessageData<T>(data);
  68. }
  69. template <class T>
  70. inline const T& UseMessageData(MessageData* data) {
  71. return static_cast<TypedMessageData<T>*>(data)->data();
  72. }
  73. template <class T>
  74. class DisposeData : public MessageData {
  75. public:
  76. explicit DisposeData(T* data) : data_(data) {}
  77. virtual ~DisposeData() { delete data_; }
  78. private:
  79. T* data_;
  80. };
  81. const uint32_t MQID_ANY = static_cast<uint32_t>(-1);
  82. const uint32_t MQID_DISPOSE = static_cast<uint32_t>(-2);
  83. // No destructor
  84. struct Message {
  85. Message() : phandler(nullptr), message_id(0), pdata(nullptr) {}
  86. inline bool Match(MessageHandler* handler, uint32_t id) const {
  87. return (handler == nullptr || handler == phandler) &&
  88. (id == MQID_ANY || id == message_id);
  89. }
  90. Location posted_from;
  91. MessageHandler* phandler;
  92. uint32_t message_id;
  93. MessageData* pdata;
  94. };
  95. typedef std::list<Message> MessageList;
  96. } // namespace rtc
  97. #endif // RTC_BASE_THREAD_MESSAGE_H_