| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_HANDLE_H_ |
| 6 #define COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_HANDLE_H_ |
| 7 |
| 8 #include "base/macros.h" |
| 9 #include "components/tracing/tracing_export.h" |
| 10 |
| 11 namespace tracing { |
| 12 namespace v2 { |
| 13 |
| 14 class ProtoZeroMessage; |
| 15 |
| 16 // ProtoZeroMessageHandle allows to decouple the lifetime of a proto message |
| 17 // from the underlying storage. It gives the following guarantees: |
| 18 // - The underlying message is finalized (if still alive) if the handle goes |
| 19 // out of scope. |
| 20 // - In Debug / DCHECK_ALWAYS_ON builds, the handle becomes null once the |
| 21 // message is finalized. This is to enforce the append-only API. For instance |
| 22 // when adding two repeated messages, the addition of the 2nd one forces |
| 23 // the finalization of the first. |
| 24 // Think about this as a WeakPtr<ProtoZeroMessage> which calls |
| 25 // ProtoZeroMessage::Finalize() when going out of scope. |
| 26 |
| 27 class TRACING_EXPORT ProtoZeroMessageHandleBase { |
| 28 public: |
| 29 ~ProtoZeroMessageHandleBase(); |
| 30 |
| 31 // Move-only type. |
| 32 ProtoZeroMessageHandleBase(ProtoZeroMessageHandleBase&& other); |
| 33 ProtoZeroMessageHandleBase& operator=(ProtoZeroMessageHandleBase&& other); |
| 34 |
| 35 protected: |
| 36 explicit ProtoZeroMessageHandleBase(ProtoZeroMessage*); |
| 37 ProtoZeroMessage& operator*() const { return *message_; } |
| 38 ProtoZeroMessage* operator->() const { return message_; } |
| 39 |
| 40 private: |
| 41 friend class ProtoZeroMessage; |
| 42 |
| 43 void reset_message() { message_ = nullptr; } |
| 44 void Move(ProtoZeroMessageHandleBase* other); |
| 45 |
| 46 ProtoZeroMessage* message_; |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessageHandleBase); |
| 49 }; |
| 50 |
| 51 template <typename T> |
| 52 class ProtoZeroMessageHandle : ProtoZeroMessageHandleBase { |
| 53 public: |
| 54 explicit ProtoZeroMessageHandle(T* message) |
| 55 : ProtoZeroMessageHandleBase(message) {} |
| 56 |
| 57 T& operator*() const { |
| 58 return static_cast<T&>(ProtoZeroMessageHandleBase::operator*()); |
| 59 } |
| 60 |
| 61 T* operator->() const { |
| 62 return static_cast<T*>(ProtoZeroMessageHandleBase::operator->()); |
| 63 } |
| 64 }; |
| 65 |
| 66 } // namespace v2 |
| 67 } // namespace tracing |
| 68 |
| 69 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_HANDLE_H_ |
| OLD | NEW |