Chromium Code Reviews| Index: components/tracing/core/proto_zero_message.h |
| diff --git a/components/tracing/core/proto_zero_message.h b/components/tracing/core/proto_zero_message.h |
| index b5b0a37cce1bc4b82788c62108ee7036702cb6d0..65a5402c29f2f033a77b5a02961fba813106d38a 100644 |
| --- a/components/tracing/core/proto_zero_message.h |
| +++ b/components/tracing/core/proto_zero_message.h |
| @@ -7,6 +7,7 @@ |
| #include <stdint.h> |
| +#include <set> |
| #include <type_traits> |
| #include "base/compiler_specific.h" |
| @@ -22,7 +23,9 @@ |
| namespace tracing { |
| namespace v2 { |
| +#if DCHECK_IS_ON() |
| class ProtoZeroMessageHandleBase; |
| +#endif |
| // Base class extended by the proto C++ stubs generated by the ProtoZero |
| // compiler (see //components/tracing/tools/). This class provides the minimal |
| @@ -62,7 +65,7 @@ class TRACING_EXPORT ProtoZeroMessage { |
| protected: |
| ProtoZeroMessage(); |
| - // Proto types: uint64, uint32, int64, int32, bool, enum. |
| + // Proto types: uint64, uint32, int64, int32, enum (large). |
| template <typename T> |
| void AppendVarInt(uint32_t field_id, T value) { |
| if (nested_message_) |
| @@ -83,6 +86,22 @@ class TRACING_EXPORT ProtoZeroMessage { |
| AppendVarInt(field_id, proto::ZigZagEncode(value)); |
| } |
| + // Proto types: bool, enum (small). |
| + // It could be looking weird not to optimize writing field_id, but it's |
| + // unnecessary because compiler already optimize WriteVarInt for constexpr. |
| + void AppendTinyVarInt(uint32_t field_id, int32_t value) { |
| + DCHECK(0 <= value && value < 0x80); |
| + if (nested_message_) |
| + EndNestedMessage(); |
| + |
| + uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; |
| + uint8_t* pos = buffer; |
| + pos = proto::WriteVarInt(proto::MakeTagVarInt(field_id), pos); |
| + *pos = static_cast<uint8_t>(value); |
|
alph
2016/08/22 22:48:35
nit: *pos++ =
kraynov
2016/08/23 12:43:49
Done.
|
| + pos++; |
| + WriteToStream(buffer, pos); |
| + } |
| + |
| // Proto types: fixed64, sfixed64, fixed32, sfixed32, double, float. |
| template <typename T> |
| void AppendFixed(uint32_t field_id, T value) { |
| @@ -99,7 +118,6 @@ class TRACING_EXPORT ProtoZeroMessage { |
| WriteToStream(buffer, pos); |
| } |
| - // TODO(kraynov): Add AppendTinyInt. |
| void AppendString(uint32_t field_id, const char* str); |
| void AppendBytes(uint32_t field_id, const void* value, size_t size); |
| @@ -122,6 +140,14 @@ class TRACING_EXPORT ProtoZeroMessage { |
| return message; |
| } |
| + // Prevent multiple appends for non-repeated fields. |
| + inline void DCheckSealField(uint32_t field_id) { |
| +#if DCHECK_IS_ON() |
| + DCHECK(field_id > 0); |
| + SealField(field_id); |
| +#endif |
| + } |
| + |
| private: |
| friend class ProtoZeroMessageTest; |
| FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BasicTypesNoNesting); |
| @@ -147,6 +173,10 @@ class TRACING_EXPORT ProtoZeroMessage { |
| size_ += size; |
| } |
| +#if DCHECK_IS_ON() |
| + void SealField(uint32_t field_id); |
| +#endif |
| + |
| // Only POD fields are allowed. This class's dtor is never called. |
| // See the comment on the static_assert in the the corresponding .cc file. |