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

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

Issue 2293073002: Tracing V2: Proto fields reflection. (Closed)
Patch Set: Created 4 years, 3 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
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_UTILS_H_ 5 #ifndef COMPONENTS_TRACING_CORE_PROTO_UTILS_H_
6 #define COMPONENTS_TRACING_CORE_PROTO_UTILS_H_ 6 #define COMPONENTS_TRACING_CORE_PROTO_UTILS_H_
7 7
8 #include <inttypes.h> 8 #include <inttypes.h>
9 9
10 #include <type_traits> 10 #include <type_traits>
11 11
12 #include "base/logging.h" 12 #include "base/logging.h"
13 13
14 namespace tracing { 14 namespace tracing {
15 namespace v2 { 15 namespace v2 {
16 namespace proto { 16 namespace proto {
17 17
18 // See https://developers.google.com/protocol-buffers/docs/encoding wire types. 18 // See https://developers.google.com/protocol-buffers/docs/encoding wire types.
19 19
20 enum : uint32_t { 20 enum : uint32_t {
21 kFieldTypeVarInt = 0, 21 kFieldTypeVarInt = 0,
22 kFieldTypeFixed64 = 1, 22 kFieldTypeFixed64 = 1,
23 kFieldTypeLengthDelimited = 2, 23 kFieldTypeLengthDelimited = 2,
24 kFieldTypeFixed32 = 5, 24 kFieldTypeFixed32 = 5,
25 }; 25 };
26 26
27 enum ProtoType : uint32_t {
28 kProtoTypeInvalid = 0,
29 kProtoTypeBool = 1,
30 kProtoTypeInt32 = 2,
31 kProtoTypeInt64 = 3,
32 kProtoTypeUint32 = 4,
33 kProtoTypeUint64 = 5,
34 kProtoTypeSint32 = 6,
35 kProtoTypeSint64 = 7,
36 kProtoTypeFixed32 = 8,
37 kProtoTypeFixed64 = 9,
38 kProtoTypeSfixed32 = 10,
39 kProtoTypeSfixed64 = 11,
40 kProtoTypeFloat = 12,
41 kProtoTypeDouble = 13,
42 kProtoTypeEnum = 14,
43 kProtoTypeString = 15,
44 kProtoTypeBytes = 16,
45 kProtoTypeMessage = 17,
46 };
47
48 struct FieldReflection {
Primiano Tucci (use gerrit) 2016/08/31 09:04:07 Can this have the same shape of https://developer
kraynov 2016/08/31 17:43:33 Done.
49 FieldReflection(const std::string& name,
50 ProtoType type,
51 uint32_t number,
52 bool repeated) : name(name),
53 type(type),
54 number(number),
55 repeated(repeated) {
56 }
57
58 static FieldReflection CreateInvalid() {
59 return FieldReflection("<INVALID>", kProtoTypeInvalid, 0, false);
60 }
61 bool IsValid() const {
62 return type != kProtoTypeInvalid && number > 0 && name != "<INVALID>";
63 }
64
65 const std::string name;
Primiano Tucci (use gerrit) 2016/08/31 09:04:07 const char* const
kraynov 2016/08/31 17:43:33 Done.
66 const ProtoType type;
67 const uint32_t number;
68 const bool repeated;
69 };
Primiano Tucci (use gerrit) 2016/08/31 09:04:07 DISALLOW_COPY_AND_ASSIGN
kraynov 2016/08/31 17:43:33 Done.
70
27 // Maximum message size supported: 256 MiB (4 x 7-bit due to varint encoding). 71 // Maximum message size supported: 256 MiB (4 x 7-bit due to varint encoding).
28 constexpr size_t kMessageLengthFieldSize = 4; 72 constexpr size_t kMessageLengthFieldSize = 4;
29 constexpr size_t kMaxMessageLength = (1u << (kMessageLengthFieldSize * 7)) - 1; 73 constexpr size_t kMaxMessageLength = (1u << (kMessageLengthFieldSize * 7)) - 1;
30 74
31 // Field tag is encoded as 32-bit varint (5 bytes at most). 75 // Field tag is encoded as 32-bit varint (5 bytes at most).
32 // Largest value of simple (not length-delimited) field is 64-bit varint 76 // Largest value of simple (not length-delimited) field is 64-bit varint
33 // (10 bytes at most). 15 bytes buffer is enough to store a simple field. 77 // (10 bytes at most). 15 bytes buffer is enough to store a simple field.
34 constexpr size_t kMaxTagEncodedSize = 5; 78 constexpr size_t kMaxTagEncodedSize = 5;
35 constexpr size_t kMaxSimpleFieldEncodedSize = kMaxTagEncodedSize + 10; 79 constexpr size_t kMaxSimpleFieldEncodedSize = kMaxTagEncodedSize + 10;
36 80
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 void StaticAssertSingleBytePreamble() { 136 void StaticAssertSingleBytePreamble() {
93 static_assert(field_id < 16, 137 static_assert(field_id < 16,
94 "Proto field id too big to fit in a single byte preamble"); 138 "Proto field id too big to fit in a single byte preamble");
95 }; 139 };
96 140
97 } // namespace proto 141 } // namespace proto
98 } // namespace v2 142 } // namespace v2
99 } // namespace tracing 143 } // namespace tracing
100 144
101 #endif // COMPONENTS_TRACING_CORE_PROTO_UTILS_H_ 145 #endif // COMPONENTS_TRACING_CORE_PROTO_UTILS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698