Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(572)

Side by Side Diff: components/tracing/core/proto_zero_message_handle.h

Issue 2158323005: tracing v2: Introduce handles for safe usage of ProtoZeroMessage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 when adding two repeated messages, the addition of the 2nd one forces
alph 2016/07/20 18:35:00 double when
Primiano Tucci (use gerrit) 2016/07/21 10:50:03 Done.
23 // the finalization of the first.
24 // Think to this as a WeakPtr<ProtoZeroMessage> which calls
oystein (OOO til 10th of July) 2016/07/20 21:19:37 nit: s/to/about/
Primiano Tucci (use gerrit) 2016/07/21 10:50:03 Done.
25 // ProtoZeroMessage::Finalize() when going out of scope.
26
27 // Use the templated version below. This base class is just here to avoid
alph 2016/07/20 18:35:00 no need to go into such explanation as this is the
Primiano Tucci (use gerrit) 2016/07/21 10:50:03 Oh good point. Also the protected ctor removes the
28 // template code bloating.
29 class TRACING_EXPORT ProtoZeroMessageHandleBase {
30 public:
31 explicit ProtoZeroMessageHandleBase(ProtoZeroMessage*);
32 ~ProtoZeroMessageHandleBase();
33
34 // Move-only type.
35 ProtoZeroMessageHandleBase(ProtoZeroMessageHandleBase&& other);
36 ProtoZeroMessageHandleBase& operator=(ProtoZeroMessageHandleBase&& other);
37
38 ProtoZeroMessage& operator*() const { return *message_; }
39 ProtoZeroMessage* operator->() const { return message_; }
40
41 private:
42 friend class ProtoZeroMessage;
43
44 void reset_message() { message_ = nullptr; }
45 void Move(ProtoZeroMessageHandleBase* other);
46
47 ProtoZeroMessage* message_;
48
49 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessageHandleBase);
50 };
51
52 template <typename T>
53 class ProtoZeroMessageHandle : ProtoZeroMessageHandleBase {
54 public:
55 explicit ProtoZeroMessageHandle(ProtoZeroMessage*) = delete;
56 explicit ProtoZeroMessageHandle(T* message)
57 : ProtoZeroMessageHandleBase(message) {}
58
59 T& operator*() const {
60 return static_cast<T&>(ProtoZeroMessageHandleBase::operator*());
61 }
62
63 T* operator->() const {
64 return static_cast<T*>(ProtoZeroMessageHandleBase::operator->());
65 }
66 };
67
68 } // namespace v2
69 } // namespace tracing
70
71 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_HANDLE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698