Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_HANDLE_H_ | 5 #ifndef COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_HANDLE_H_ |
| 6 #define COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_HANDLE_H_ | 6 #define COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_HANDLE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <string> | |
| 11 #include <unordered_set> | |
| 12 | |
| 13 #include "base/logging.h" | |
| 8 #include "base/macros.h" | 14 #include "base/macros.h" |
| 9 #include "components/tracing/tracing_export.h" | 15 #include "components/tracing/tracing_export.h" |
| 10 | 16 |
| 11 namespace tracing { | 17 namespace tracing { |
| 12 namespace v2 { | 18 namespace v2 { |
| 13 | 19 |
| 14 class ProtoZeroMessage; | 20 class ProtoZeroMessage; |
| 15 | 21 |
| 16 // ProtoZeroMessageHandle allows to decouple the lifetime of a proto message | 22 // ProtoZeroMessageHandle allows to decouple the lifetime of a proto message |
| 17 // from the underlying storage. It gives the following guarantees: | 23 // from the underlying storage. It gives the following guarantees: |
| 18 // - The underlying message is finalized (if still alive) if the handle goes | 24 // - The underlying message is finalized (if still alive) if the handle goes |
| 19 // out of scope. | 25 // out of scope. |
| 20 // - In Debug / DCHECK_ALWAYS_ON builds, the handle becomes null once the | 26 // - 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 | 27 // 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 | 28 // when adding two repeated messages, the addition of the 2nd one forces |
| 23 // the finalization of the first. | 29 // the finalization of the first. |
| 24 // Think about this as a WeakPtr<ProtoZeroMessage> which calls | 30 // Think about this as a WeakPtr<ProtoZeroMessage> which calls |
| 25 // ProtoZeroMessage::Finalize() when going out of scope. | 31 // ProtoZeroMessage::Finalize() when going out of scope. |
| 26 | 32 |
| 27 class TRACING_EXPORT ProtoZeroMessageHandleBase { | 33 class TRACING_EXPORT ProtoZeroMessageHandleBase { |
| 28 public: | 34 public: |
| 29 ~ProtoZeroMessageHandleBase(); | 35 ~ProtoZeroMessageHandleBase(); |
| 30 | 36 |
| 31 // Move-only type. | 37 // Move-only type. |
| 32 ProtoZeroMessageHandleBase(ProtoZeroMessageHandleBase&& other); | 38 ProtoZeroMessageHandleBase(ProtoZeroMessageHandleBase&& other); |
| 33 ProtoZeroMessageHandleBase& operator=(ProtoZeroMessageHandleBase&& other); | 39 ProtoZeroMessageHandleBase& operator=(ProtoZeroMessageHandleBase&& other); |
| 34 | 40 |
| 41 inline void SealField(uint32_t field_id) { | |
|
Primiano Tucci (use gerrit)
2016/08/24 08:59:45
wait why sealfield is a property of the Handle?
Th
| |
| 42 DCHECK(sealed_fields_.count(field_id) == 0) << | |
| 43 "Field (" + std::to_string(field_id) + ") can't be sealed again."; | |
| 44 sealed_fields_.insert(field_id); | |
| 45 } | |
| 46 | |
| 35 protected: | 47 protected: |
| 36 explicit ProtoZeroMessageHandleBase(ProtoZeroMessage*); | 48 explicit ProtoZeroMessageHandleBase(ProtoZeroMessage*); |
| 37 ProtoZeroMessage& operator*() const { return *message_; } | 49 ProtoZeroMessage& operator*() const { return *message_; } |
| 38 ProtoZeroMessage* operator->() const { return message_; } | 50 ProtoZeroMessage* operator->() const { return message_; } |
| 39 | 51 |
| 40 private: | 52 private: |
| 41 friend class ProtoZeroMessage; | 53 friend class ProtoZeroMessage; |
| 42 | 54 |
| 43 void reset_message() { message_ = nullptr; } | 55 void reset_message() { |
| 56 message_ = nullptr; | |
| 57 sealed_fields_.clear(); | |
| 58 } | |
| 44 void Move(ProtoZeroMessageHandleBase* other); | 59 void Move(ProtoZeroMessageHandleBase* other); |
| 45 | 60 |
| 46 ProtoZeroMessage* message_; | 61 ProtoZeroMessage* message_; |
| 62 std::unordered_set<uint32_t> sealed_fields_; | |
| 47 | 63 |
| 48 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessageHandleBase); | 64 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessageHandleBase); |
| 49 }; | 65 }; |
| 50 | 66 |
| 51 template <typename T> | 67 template <typename T> |
| 52 class ProtoZeroMessageHandle : ProtoZeroMessageHandleBase { | 68 class ProtoZeroMessageHandle : public ProtoZeroMessageHandleBase { |
| 53 public: | 69 public: |
| 54 explicit ProtoZeroMessageHandle(T* message) | 70 explicit ProtoZeroMessageHandle(T* message) |
| 55 : ProtoZeroMessageHandleBase(message) {} | 71 : ProtoZeroMessageHandleBase(message) {} |
| 56 | 72 |
| 57 T& operator*() const { | 73 T& operator*() const { |
| 58 return static_cast<T&>(ProtoZeroMessageHandleBase::operator*()); | 74 return static_cast<T&>(ProtoZeroMessageHandleBase::operator*()); |
| 59 } | 75 } |
| 60 | 76 |
| 61 T* operator->() const { | 77 T* operator->() const { |
| 62 return static_cast<T*>(ProtoZeroMessageHandleBase::operator->()); | 78 return static_cast<T*>(ProtoZeroMessageHandleBase::operator->()); |
| 63 } | 79 } |
| 64 }; | 80 }; |
| 65 | 81 |
| 66 } // namespace v2 | 82 } // namespace v2 |
| 67 } // namespace tracing | 83 } // namespace tracing |
| 68 | 84 |
| 69 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_HANDLE_H_ | 85 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_HANDLE_H_ |
| OLD | NEW |