Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 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 | |
| 13 | #include <list> |
| 14 | #include <memory> |
| 15 | #include <utility> |
| 16 | |
| 17 | #include "api/scoped_refptr.h" |
| 18 | #include "rtc_base/location.h" |
| 19 | #include "rtc_base/message_handler.h" |
| 20 | |
| 21 | namespace rtc { |
| 22 | |
| 23 | // Derive from this for specialized data |
| 24 | // App manages lifetime, except when messages are purged |
| 25 | |
| 26 | class MessageData { |
| 27 | public: |
| 28 | MessageData() {} |
| 29 | virtual ~MessageData() {} |
| 30 | }; |
| 31 | |
| 32 | template <class T> |
| 33 | class TypedMessageData : public MessageData { |
| 34 | public: |
| 35 | explicit TypedMessageData(const T& data) : data_(data) {} |
| 36 | const T& data() const { return data_; } |
| 37 | T& data() { return data_; } |
| 38 | |
| 39 | private: |
| 40 | T data_; |
| 41 | }; |
| 42 | |
| 43 | // Like TypedMessageData, but for pointers that require a delete. |
| 44 | template <class T> |
| 45 | class ScopedMessageData : public MessageData { |
| 46 | public: |
| 47 | explicit ScopedMessageData(std::unique_ptr<T> data) |
| 48 | : data_(std::move(data)) {} |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 49 | |
Mirko Bonadei | e8e4d69 | 2021-07-28 09:02:25 +0200 | [diff] [blame] | 50 | const T& data() const { return *data_; } |
| 51 | T& data() { return *data_; } |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 52 | |
Mirko Bonadei | 179b46b | 2021-07-24 21:50:24 +0200 | [diff] [blame] | 53 | T* Release() { return data_.release(); } |
| 54 | |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 55 | private: |
| 56 | std::unique_ptr<T> data_; |
| 57 | }; |
| 58 | |
| 59 | // Like ScopedMessageData, but for reference counted pointers. |
| 60 | template <class T> |
| 61 | class ScopedRefMessageData : public MessageData { |
| 62 | public: |
| 63 | explicit ScopedRefMessageData(T* data) : data_(data) {} |
| 64 | const scoped_refptr<T>& data() const { return data_; } |
| 65 | scoped_refptr<T>& data() { return data_; } |
| 66 | |
| 67 | private: |
| 68 | scoped_refptr<T> data_; |
| 69 | }; |
| 70 | |
| 71 | template <class T> |
| 72 | inline MessageData* WrapMessageData(const T& data) { |
| 73 | return new TypedMessageData<T>(data); |
| 74 | } |
| 75 | |
| 76 | template <class T> |
| 77 | inline const T& UseMessageData(MessageData* data) { |
| 78 | return static_cast<TypedMessageData<T>*>(data)->data(); |
| 79 | } |
| 80 | |
| 81 | template <class T> |
| 82 | class DisposeData : public MessageData { |
| 83 | public: |
| 84 | explicit DisposeData(T* data) : data_(data) {} |
| 85 | virtual ~DisposeData() { delete data_; } |
| 86 | |
| 87 | private: |
| 88 | T* data_; |
| 89 | }; |
| 90 | |
| 91 | const uint32_t MQID_ANY = static_cast<uint32_t>(-1); |
| 92 | const uint32_t MQID_DISPOSE = static_cast<uint32_t>(-2); |
| 93 | |
| 94 | // No destructor |
| 95 | |
| 96 | struct Message { |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 97 | Message() : phandler(nullptr), message_id(0), pdata(nullptr) {} |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 98 | inline bool Match(MessageHandler* handler, uint32_t id) const { |
| 99 | return (handler == nullptr || handler == phandler) && |
| 100 | (id == MQID_ANY || id == message_id); |
| 101 | } |
| 102 | Location posted_from; |
| 103 | MessageHandler* phandler; |
| 104 | uint32_t message_id; |
| 105 | MessageData* pdata; |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | typedef std::list<Message> MessageList; |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 109 | } // namespace rtc |
| 110 | #endif // RTC_BASE_THREAD_MESSAGE_H_ |