diff options
author | Yi Jin <jinyithu@google.com> | 2017-10-20 16:17:58 -0700 |
---|---|---|
committer | Yi Jin <jinyithu@google.com> | 2017-11-01 17:49:42 +0000 |
commit | f9ed04b3d938b06329fcd14d4f2b461000d028da (patch) | |
tree | 2a660ca1ce48c4e9deaea14524393a63ba98d1c1 | |
parent | f8a9169949c589755d300530f7b2390e687a9f8b (diff) |
Remove the extra mapping of field types in ProtoOutputStream
It is very unlikely the protobuf changes the value in descriptor.h,
and if defines an extra mapping, there are several places to maintain:
1. java-stream,
2. cpp-stream,
3. ProtoOutputStream.java
4. ProtoOutputStream.cpp
5. Privacy.h (GetFieldId)
6. StatsLog to generate field id (type << 32 + field number)
Therefore use the current value in descriptor.h seems reasonable unless
they change that, very very unlikely, they probably will just add new
types, and deprect the existing ones like Group.
Test: test output of dumpsys proto
Change-Id: I6e150ab427851dd3b5dd55d3b273deeed7a0963c
-rw-r--r-- | api/test-current.txt | 30 | ||||
-rw-r--r-- | core/java/android/util/proto/ProtoOutputStream.java | 82 | ||||
-rw-r--r-- | libs/protoutil/include/android/util/ProtoOutputStream.h | 8 | ||||
-rw-r--r-- | tools/streaming_proto/Android.bp | 5 | ||||
-rw-r--r-- | tools/streaming_proto/cpp/main.cpp | 108 | ||||
-rw-r--r-- | tools/streaming_proto/java/main.cpp | 174 | ||||
-rw-r--r-- | tools/streaming_proto/stream_proto_utils.cpp | 102 | ||||
-rw-r--r-- | tools/streaming_proto/stream_proto_utils.h | 29 | ||||
-rw-r--r-- | tools/streaming_proto/string_utils.h | 3 |
9 files changed, 205 insertions, 336 deletions
diff --git a/api/test-current.txt b/api/test-current.txt index 6f251e965173..c9b77c1c87e5 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -44980,25 +44980,25 @@ package android.util.proto { field public static final long FIELD_COUNT_UNKNOWN = 0L; // 0x0L field public static final int FIELD_ID_MASK = -8; // 0xfffffff8 field public static final int FIELD_ID_SHIFT = 3; // 0x3 - field public static final long FIELD_TYPE_BOOL = 55834574848L; // 0xd00000000L - field public static final long FIELD_TYPE_BYTES = 64424509440L; // 0xf00000000L + field public static final long FIELD_TYPE_BOOL = 34359738368L; // 0x800000000L + field public static final long FIELD_TYPE_BYTES = 51539607552L; // 0xc00000000L field public static final long FIELD_TYPE_DOUBLE = 4294967296L; // 0x100000000L - field public static final long FIELD_TYPE_ENUM = 68719476736L; // 0x1000000000L - field public static final long FIELD_TYPE_FIXED32 = 38654705664L; // 0x900000000L - field public static final long FIELD_TYPE_FIXED64 = 42949672960L; // 0xa00000000L + field public static final long FIELD_TYPE_ENUM = 60129542144L; // 0xe00000000L + field public static final long FIELD_TYPE_FIXED32 = 30064771072L; // 0x700000000L + field public static final long FIELD_TYPE_FIXED64 = 25769803776L; // 0x600000000L field public static final long FIELD_TYPE_FLOAT = 8589934592L; // 0x200000000L - field public static final long FIELD_TYPE_INT32 = 12884901888L; // 0x300000000L - field public static final long FIELD_TYPE_INT64 = 17179869184L; // 0x400000000L + field public static final long FIELD_TYPE_INT32 = 21474836480L; // 0x500000000L + field public static final long FIELD_TYPE_INT64 = 12884901888L; // 0x300000000L field public static final long FIELD_TYPE_MASK = 1095216660480L; // 0xff00000000L - field public static final long FIELD_TYPE_OBJECT = 73014444032L; // 0x1100000000L - field public static final long FIELD_TYPE_SFIXED32 = 47244640256L; // 0xb00000000L - field public static final long FIELD_TYPE_SFIXED64 = 51539607552L; // 0xc00000000L + field public static final long FIELD_TYPE_MESSAGE = 47244640256L; // 0xb00000000L + field public static final long FIELD_TYPE_SFIXED32 = 64424509440L; // 0xf00000000L + field public static final long FIELD_TYPE_SFIXED64 = 68719476736L; // 0x1000000000L field public static final int FIELD_TYPE_SHIFT = 32; // 0x20 - field public static final long FIELD_TYPE_SINT32 = 30064771072L; // 0x700000000L - field public static final long FIELD_TYPE_SINT64 = 34359738368L; // 0x800000000L - field public static final long FIELD_TYPE_STRING = 60129542144L; // 0xe00000000L - field public static final long FIELD_TYPE_UINT32 = 21474836480L; // 0x500000000L - field public static final long FIELD_TYPE_UINT64 = 25769803776L; // 0x600000000L + field public static final long FIELD_TYPE_SINT32 = 73014444032L; // 0x1100000000L + field public static final long FIELD_TYPE_SINT64 = 77309411328L; // 0x1200000000L + field public static final long FIELD_TYPE_STRING = 38654705664L; // 0x900000000L + field public static final long FIELD_TYPE_UINT32 = 55834574848L; // 0xd00000000L + field public static final long FIELD_TYPE_UINT64 = 17179869184L; // 0x400000000L field public static final long FIELD_TYPE_UNKNOWN = 0L; // 0x0L field public static final java.lang.String TAG = "ProtoOutputStream"; field public static final int WIRE_TYPE_END_GROUP = 4; // 0x4 diff --git a/core/java/android/util/proto/ProtoOutputStream.java b/core/java/android/util/proto/ProtoOutputStream.java index 43a978974fe3..a94806a02fbb 100644 --- a/core/java/android/util/proto/ProtoOutputStream.java +++ b/core/java/android/util/proto/ProtoOutputStream.java @@ -127,42 +127,48 @@ public final class ProtoOutputStream { public static final long FIELD_TYPE_UNKNOWN = 0; + /** + * The types are copied from external/protobuf/src/google/protobuf/descriptor.h directly, + * so no extra mapping needs to be maintained in this case. + */ public static final long FIELD_TYPE_DOUBLE = 1L << FIELD_TYPE_SHIFT; public static final long FIELD_TYPE_FLOAT = 2L << FIELD_TYPE_SHIFT; - public static final long FIELD_TYPE_INT32 = 3L << FIELD_TYPE_SHIFT; - public static final long FIELD_TYPE_INT64 = 4L << FIELD_TYPE_SHIFT; - public static final long FIELD_TYPE_UINT32 = 5L << FIELD_TYPE_SHIFT; - public static final long FIELD_TYPE_UINT64 = 6L << FIELD_TYPE_SHIFT; - public static final long FIELD_TYPE_SINT32 = 7L << FIELD_TYPE_SHIFT; - public static final long FIELD_TYPE_SINT64 = 8L << FIELD_TYPE_SHIFT; - public static final long FIELD_TYPE_FIXED32 = 9L << FIELD_TYPE_SHIFT; - public static final long FIELD_TYPE_FIXED64 = 10L << FIELD_TYPE_SHIFT; - public static final long FIELD_TYPE_SFIXED32 = 11L << FIELD_TYPE_SHIFT; - public static final long FIELD_TYPE_SFIXED64 = 12L << FIELD_TYPE_SHIFT; - public static final long FIELD_TYPE_BOOL = 13L << FIELD_TYPE_SHIFT; - public static final long FIELD_TYPE_STRING = 14L << FIELD_TYPE_SHIFT; - public static final long FIELD_TYPE_BYTES = 15L << FIELD_TYPE_SHIFT; - public static final long FIELD_TYPE_ENUM = 16L << FIELD_TYPE_SHIFT; - public static final long FIELD_TYPE_OBJECT = 17L << FIELD_TYPE_SHIFT; + public static final long FIELD_TYPE_INT64 = 3L << FIELD_TYPE_SHIFT; + public static final long FIELD_TYPE_UINT64 = 4L << FIELD_TYPE_SHIFT; + public static final long FIELD_TYPE_INT32 = 5L << FIELD_TYPE_SHIFT; + public static final long FIELD_TYPE_FIXED64 = 6L << FIELD_TYPE_SHIFT; + public static final long FIELD_TYPE_FIXED32 = 7L << FIELD_TYPE_SHIFT; + public static final long FIELD_TYPE_BOOL = 8L << FIELD_TYPE_SHIFT; + public static final long FIELD_TYPE_STRING = 9L << FIELD_TYPE_SHIFT; +// public static final long FIELD_TYPE_GROUP = 10L << FIELD_TYPE_SHIFT; // Deprecated. + public static final long FIELD_TYPE_MESSAGE = 11L << FIELD_TYPE_SHIFT; + public static final long FIELD_TYPE_BYTES = 12L << FIELD_TYPE_SHIFT; + public static final long FIELD_TYPE_UINT32 = 13L << FIELD_TYPE_SHIFT; + public static final long FIELD_TYPE_ENUM = 14L << FIELD_TYPE_SHIFT; + public static final long FIELD_TYPE_SFIXED32 = 15L << FIELD_TYPE_SHIFT; + public static final long FIELD_TYPE_SFIXED64 = 16L << FIELD_TYPE_SHIFT; + public static final long FIELD_TYPE_SINT32 = 17L << FIELD_TYPE_SHIFT; + public static final long FIELD_TYPE_SINT64 = 18L << FIELD_TYPE_SHIFT; private static final String[] FIELD_TYPE_NAMES = new String[] { "Double", "Float", - "Int32", "Int64", - "UInt32", "UInt64", - "SInt32", - "SInt64", - "Fixed32", + "Int32", "Fixed64", - "SFixed32", - "SFixed64", + "Fixed32", "Bool", "String", + "Group", // This field is deprecated but reserved here for indexing. + "Message", "Bytes", + "UInt32", "Enum", - "Object", + "SFixed32", + "SFixed64", + "SInt32", + "SInt64", }; // @@ -867,21 +873,21 @@ public final class ProtoOutputStream { assertNotCompacted(); final int id = (int)fieldId; - switch ((int)((fieldId & (FIELD_TYPE_MASK | FIELD_COUNT_MASK)) >> FIELD_TYPE_SHIFT)) { + switch ((int) ((fieldId & (FIELD_TYPE_MASK | FIELD_COUNT_MASK)) >> FIELD_TYPE_SHIFT)) { // bytes - case (int)((FIELD_TYPE_BYTES | FIELD_COUNT_SINGLE) >> FIELD_TYPE_SHIFT): + case (int) ((FIELD_TYPE_BYTES | FIELD_COUNT_SINGLE) >> FIELD_TYPE_SHIFT): writeBytesImpl(id, val); break; - case (int)((FIELD_TYPE_BYTES | FIELD_COUNT_REPEATED) >> FIELD_TYPE_SHIFT): - case (int)((FIELD_TYPE_BYTES | FIELD_COUNT_PACKED) >> FIELD_TYPE_SHIFT): + case (int) ((FIELD_TYPE_BYTES | FIELD_COUNT_REPEATED) >> FIELD_TYPE_SHIFT): + case (int) ((FIELD_TYPE_BYTES | FIELD_COUNT_PACKED) >> FIELD_TYPE_SHIFT): writeRepeatedBytesImpl(id, val); break; // Object - case (int)((FIELD_TYPE_OBJECT | FIELD_COUNT_SINGLE) >> FIELD_TYPE_SHIFT): + case (int) ((FIELD_TYPE_MESSAGE | FIELD_COUNT_SINGLE) >> FIELD_TYPE_SHIFT): writeObjectImpl(id, val); break; - case (int)((FIELD_TYPE_OBJECT | FIELD_COUNT_REPEATED) >> FIELD_TYPE_SHIFT): - case (int)((FIELD_TYPE_OBJECT | FIELD_COUNT_PACKED) >> FIELD_TYPE_SHIFT): + case (int) ((FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED) >> FIELD_TYPE_SHIFT): + case (int) ((FIELD_TYPE_MESSAGE | FIELD_COUNT_PACKED) >> FIELD_TYPE_SHIFT): writeRepeatedObjectImpl(id, val); break; // nothing else allowed @@ -899,7 +905,7 @@ public final class ProtoOutputStream { assertNotCompacted(); final int id = (int)fieldId; - if ((fieldId & FIELD_TYPE_MASK) == FIELD_TYPE_OBJECT) { + if ((fieldId & FIELD_TYPE_MASK) == FIELD_TYPE_MESSAGE) { final long count = fieldId & FIELD_COUNT_MASK; if (count == FIELD_COUNT_SINGLE) { return startObjectImpl(id, false); @@ -2091,7 +2097,7 @@ public final class ProtoOutputStream { @Deprecated public long startObject(long fieldId) { assertNotCompacted(); - final int id = checkFieldId(fieldId, FIELD_COUNT_SINGLE | FIELD_TYPE_OBJECT); + final int id = checkFieldId(fieldId, FIELD_COUNT_SINGLE | FIELD_TYPE_MESSAGE); return startObjectImpl(id, false); } @@ -2119,7 +2125,7 @@ public final class ProtoOutputStream { @Deprecated public long startRepeatedObject(long fieldId) { assertNotCompacted(); - final int id = checkFieldId(fieldId, FIELD_COUNT_REPEATED | FIELD_TYPE_OBJECT); + final int id = checkFieldId(fieldId, FIELD_COUNT_REPEATED | FIELD_TYPE_MESSAGE); return startObjectImpl(id, true); } @@ -2217,7 +2223,7 @@ public final class ProtoOutputStream { @Deprecated public void writeObject(long fieldId, byte[] value) { assertNotCompacted(); - final int id = checkFieldId(fieldId, FIELD_COUNT_SINGLE | FIELD_TYPE_OBJECT); + final int id = checkFieldId(fieldId, FIELD_COUNT_SINGLE | FIELD_TYPE_MESSAGE); writeObjectImpl(id, value); } @@ -2237,7 +2243,7 @@ public final class ProtoOutputStream { @Deprecated public void writeRepeatedObject(long fieldId, byte[] value) { assertNotCompacted(); - final int id = checkFieldId(fieldId, FIELD_COUNT_REPEATED | FIELD_TYPE_OBJECT); + final int id = checkFieldId(fieldId, FIELD_COUNT_REPEATED | FIELD_TYPE_MESSAGE); writeRepeatedObjectImpl(id, value); } @@ -2296,7 +2302,7 @@ public final class ProtoOutputStream { final String typeString = getFieldTypeString(fieldType); if (typeString != null && countString != null) { final StringBuilder sb = new StringBuilder(); - if (expectedType == FIELD_TYPE_OBJECT) { + if (expectedType == FIELD_TYPE_MESSAGE) { sb.append("start"); } else { sb.append("write"); @@ -2306,7 +2312,7 @@ public final class ProtoOutputStream { sb.append(" called for field "); sb.append((int)fieldId); sb.append(" which should be used with "); - if (fieldType == FIELD_TYPE_OBJECT) { + if (fieldType == FIELD_TYPE_MESSAGE) { sb.append("start"); } else { sb.append("write"); @@ -2321,7 +2327,7 @@ public final class ProtoOutputStream { throw new IllegalArgumentException(sb.toString()); } else { final StringBuilder sb = new StringBuilder(); - if (expectedType == FIELD_TYPE_OBJECT) { + if (expectedType == FIELD_TYPE_MESSAGE) { sb.append("start"); } else { sb.append("write"); diff --git a/libs/protoutil/include/android/util/ProtoOutputStream.h b/libs/protoutil/include/android/util/ProtoOutputStream.h index 2155084db534..ce4184967026 100644 --- a/libs/protoutil/include/android/util/ProtoOutputStream.h +++ b/libs/protoutil/include/android/util/ProtoOutputStream.h @@ -35,6 +35,10 @@ const uint64_t FIELD_TYPE_SHIFT = 32; */ const uint64_t FIELD_TYPE_MASK = 0x0ffULL << FIELD_TYPE_SHIFT; +/** + * The types are copied from external/protobuf/src/google/protobuf/descriptor.h directly, + * so no extra mapping needs to be maintained in this case. + */ const uint64_t FIELD_TYPE_UNKNOWN = 0; const uint64_t FIELD_TYPE_DOUBLE = 1ULL << FIELD_TYPE_SHIFT; // double, exactly eight bytes on the wire. const uint64_t FIELD_TYPE_FLOAT = 2ULL << FIELD_TYPE_SHIFT; // float, exactly four bytes on the wire. @@ -49,7 +53,7 @@ const uint64_t FIELD_TYPE_FIXED64 = 6ULL << FIELD_TYPE_SHIFT; // uint64, exac const uint64_t FIELD_TYPE_FIXED32 = 7ULL << FIELD_TYPE_SHIFT; // uint32, exactly four bytes on the wire. const uint64_t FIELD_TYPE_BOOL = 8ULL << FIELD_TYPE_SHIFT; // bool, varint on the wire. const uint64_t FIELD_TYPE_STRING = 9ULL << FIELD_TYPE_SHIFT; // UTF-8 text. -const uint64_t FIELD_TYPE_GROUP = 10ULL << FIELD_TYPE_SHIFT; // Tag-delimited message. Deprecated. +// const uint64_t FIELD_TYPE_GROUP = 10ULL << FIELD_TYPE_SHIFT; // Tag-delimited message. Deprecated. const uint64_t FIELD_TYPE_MESSAGE = 11ULL << FIELD_TYPE_SHIFT; // Length-delimited message. const uint64_t FIELD_TYPE_BYTES = 12ULL << FIELD_TYPE_SHIFT; // Arbitrary byte array. @@ -69,7 +73,7 @@ const uint64_t FIELD_COUNT_MASK = 0x0fULL << FIELD_COUNT_SHIFT; const uint64_t FIELD_COUNT_UNKNOWN = 0; const uint64_t FIELD_COUNT_SINGLE = 1ULL << FIELD_COUNT_SHIFT; const uint64_t FIELD_COUNT_REPEATED = 2ULL << FIELD_COUNT_SHIFT; -const uint64_t FIELD_COUNT_PACKED = 4ULL << FIELD_COUNT_SHIFT; +const uint64_t FIELD_COUNT_PACKED = 5ULL << FIELD_COUNT_SHIFT; /** * Class to write to a protobuf stream. diff --git a/tools/streaming_proto/Android.bp b/tools/streaming_proto/Android.bp index 96e060d0fc26..dc5c14e32cbd 100644 --- a/tools/streaming_proto/Android.bp +++ b/tools/streaming_proto/Android.bp @@ -21,8 +21,11 @@ cc_defaults { name: "protoc-gen-stream-defaults", srcs: [ "Errors.cpp", + "stream_proto_utils.cpp", "string_utils.cpp", ], + + shared_libs: ["libprotoc"], } cc_library { @@ -52,7 +55,6 @@ cc_binary_host { ], defaults: ["protoc-gen-stream-defaults"], - shared_libs: ["libprotoc"], } cc_binary_host { @@ -62,6 +64,5 @@ cc_binary_host { ], defaults: ["protoc-gen-stream-defaults"], - shared_libs: ["libprotoc"], static_libs: ["streamingflags"], } diff --git a/tools/streaming_proto/cpp/main.cpp b/tools/streaming_proto/cpp/main.cpp index dc96d5c54c16..481698432711 100644 --- a/tools/streaming_proto/cpp/main.cpp +++ b/tools/streaming_proto/cpp/main.cpp @@ -1,108 +1,23 @@ #include "Errors.h" +#include "stream_proto_utils.h" #include "string_utils.h" #include <frameworks/base/tools/streaming_proto/stream.pb.h> -#include "google/protobuf/compiler/plugin.pb.h" -#include "google/protobuf/io/zero_copy_stream_impl.h" -#include "google/protobuf/text_format.h" - #include <iomanip> #include <iostream> #include <sstream> using namespace android::stream_proto; -using namespace google::protobuf; -using namespace google::protobuf::compiler; using namespace google::protobuf::io; using namespace std; -/** - * Position of the field type in a (long long) fieldId. - */ -const uint64_t FIELD_TYPE_SHIFT = 32; - -// -// FieldId flags for whether the field is single, repeated or packed. -// TODO: packed is not supported yet. -// -const uint64_t FIELD_COUNT_SHIFT = 40; -const uint64_t FIELD_COUNT_MASK = 0x0fULL << FIELD_COUNT_SHIFT; -const uint64_t FIELD_COUNT_UNKNOWN = 0; -const uint64_t FIELD_COUNT_SINGLE = 1ULL << FIELD_COUNT_SHIFT; -const uint64_t FIELD_COUNT_REPEATED = 2ULL << FIELD_COUNT_SHIFT; -const uint64_t FIELD_COUNT_PACKED = 4ULL << FIELD_COUNT_SHIFT; - -// Indent -const string INDENT = " "; - -/** - * See if this is the file for this request, and not one of the imported ones. - */ -static bool -should_generate_for_file(const CodeGeneratorRequest& request, const string& file) -{ - const int N = request.file_to_generate_size(); - for (int i=0; i<N; i++) { - if (request.file_to_generate(i) == file) { - return true; - } - } - return false; -} - static string make_filename(const FileDescriptorProto& file_descriptor) { return file_descriptor.name() + ".h"; } -static string -get_proto_type(const FieldDescriptorProto& field) -{ - switch (field.type()) { - case FieldDescriptorProto::TYPE_DOUBLE: - return "double"; - case FieldDescriptorProto::TYPE_FLOAT: - return "float"; - case FieldDescriptorProto::TYPE_INT64: - return "int64"; - case FieldDescriptorProto::TYPE_UINT64: - return "uint64"; - case FieldDescriptorProto::TYPE_INT32: - return "int32"; - case FieldDescriptorProto::TYPE_FIXED64: - return "fixed64"; - case FieldDescriptorProto::TYPE_FIXED32: - return "fixed32"; - case FieldDescriptorProto::TYPE_BOOL: - return "bool"; - case FieldDescriptorProto::TYPE_STRING: - return "string"; - case FieldDescriptorProto::TYPE_GROUP: - return "group<unsupported!>"; - case FieldDescriptorProto::TYPE_MESSAGE: - return field.type_name(); - case FieldDescriptorProto::TYPE_BYTES: - return "bytes"; - case FieldDescriptorProto::TYPE_UINT32: - return "uint32"; - case FieldDescriptorProto::TYPE_ENUM: - return field.type_name(); - case FieldDescriptorProto::TYPE_SFIXED32: - return "sfixed32"; - case FieldDescriptorProto::TYPE_SFIXED64: - return "sfixed64"; - case FieldDescriptorProto::TYPE_SINT32: - return "sint32"; - case FieldDescriptorProto::TYPE_SINT64: - return "sint64"; - default: - // won't happen - return "void"; - } -} - static void write_enum(stringstream& text, const EnumDescriptorProto& enu, const string& indent) { @@ -117,27 +32,6 @@ write_enum(stringstream& text, const EnumDescriptorProto& enu, const string& ind text << endl; } -static uint64_t -get_field_id(const FieldDescriptorProto& field) -{ - // Number - uint64_t result = (uint64_t)field.number(); - - // Type - result |= (uint64_t)field.type() << FIELD_TYPE_SHIFT; - - // Count - if (field.options().packed()) { - result |= FIELD_COUNT_PACKED; - } else if (field.label() == FieldDescriptorProto::LABEL_REPEATED) { - result |= FIELD_COUNT_REPEATED; - } else { - result |= FIELD_COUNT_SINGLE; - } - - return result; -} - static void write_field(stringstream& text, const FieldDescriptorProto& field, const string& indent) { diff --git a/tools/streaming_proto/java/main.cpp b/tools/streaming_proto/java/main.cpp index b7d594bb9465..c9c50a561a04 100644 --- a/tools/streaming_proto/java/main.cpp +++ b/tools/streaming_proto/java/main.cpp @@ -1,11 +1,7 @@ #include "Errors.h" - +#include "stream_proto_utils.h" #include "string_utils.h" -#include "google/protobuf/compiler/plugin.pb.h" -#include "google/protobuf/io/zero_copy_stream_impl.h" -#include "google/protobuf/text_format.h" - #include <stdio.h> #include <iomanip> #include <iostream> @@ -13,51 +9,9 @@ #include <map> using namespace android::stream_proto; -using namespace google::protobuf; -using namespace google::protobuf::compiler; using namespace google::protobuf::io; using namespace std; -const int FIELD_TYPE_SHIFT = 32; -const uint64_t FIELD_TYPE_DOUBLE = 1L << FIELD_TYPE_SHIFT; -const uint64_t FIELD_TYPE_FLOAT = 2L << FIELD_TYPE_SHIFT; -const uint64_t FIELD_TYPE_INT32 = 3L << FIELD_TYPE_SHIFT; -const uint64_t FIELD_TYPE_INT64 = 4L << FIELD_TYPE_SHIFT; -const uint64_t FIELD_TYPE_UINT32 = 5L << FIELD_TYPE_SHIFT; -const uint64_t FIELD_TYPE_UINT64 = 6L << FIELD_TYPE_SHIFT; -const uint64_t FIELD_TYPE_SINT32 = 7L << FIELD_TYPE_SHIFT; -const uint64_t FIELD_TYPE_SINT64 = 8L << FIELD_TYPE_SHIFT; -const uint64_t FIELD_TYPE_FIXED32 = 9L << FIELD_TYPE_SHIFT; -const uint64_t FIELD_TYPE_FIXED64 = 10L << FIELD_TYPE_SHIFT; -const uint64_t FIELD_TYPE_SFIXED32 = 11L << FIELD_TYPE_SHIFT; -const uint64_t FIELD_TYPE_SFIXED64 = 12L << FIELD_TYPE_SHIFT; -const uint64_t FIELD_TYPE_BOOL = 13L << FIELD_TYPE_SHIFT; -const uint64_t FIELD_TYPE_STRING = 14L << FIELD_TYPE_SHIFT; -const uint64_t FIELD_TYPE_BYTES = 15L << FIELD_TYPE_SHIFT; -const uint64_t FIELD_TYPE_ENUM = 16L << FIELD_TYPE_SHIFT; -const uint64_t FIELD_TYPE_OBJECT = 17L << FIELD_TYPE_SHIFT; - -const int FIELD_COUNT_SHIFT = 40; -const uint64_t FIELD_COUNT_SINGLE = 1L << FIELD_COUNT_SHIFT; -const uint64_t FIELD_COUNT_REPEATED = 2L << FIELD_COUNT_SHIFT; -const uint64_t FIELD_COUNT_PACKED = 5L << FIELD_COUNT_SHIFT; - - -/** - * See if this is the file for this request, and not one of the imported ones. - */ -static bool -should_generate_for_file(const CodeGeneratorRequest& request, const string& file) -{ - const int N = request.file_to_generate_size(); - for (int i=0; i<N; i++) { - if (request.file_to_generate(i) == file) { - return true; - } - } - return false; -} - /** * If the descriptor gives us a class name, use that. Otherwise make one up from * the filename of the .proto file. @@ -112,7 +66,7 @@ make_file_name(const FileDescriptorProto& file_descriptor, const string& class_n static string indent_more(const string& indent) { - return indent + " "; + return indent + INDENT; } /** @@ -133,130 +87,6 @@ write_enum(stringstream& text, const EnumDescriptorProto& enu, const string& ind } /** - * Get the string name for a field. - */ -static string -get_proto_type(const FieldDescriptorProto& field) -{ - switch (field.type()) { - case FieldDescriptorProto::TYPE_DOUBLE: - return "double"; - case FieldDescriptorProto::TYPE_FLOAT: - return "float"; - case FieldDescriptorProto::TYPE_INT64: - return "int64"; - case FieldDescriptorProto::TYPE_UINT64: - return "uint64"; - case FieldDescriptorProto::TYPE_INT32: - return "int32"; - case FieldDescriptorProto::TYPE_FIXED64: - return "fixed64"; - case FieldDescriptorProto::TYPE_FIXED32: - return "fixed32"; - case FieldDescriptorProto::TYPE_BOOL: - return "bool"; - case FieldDescriptorProto::TYPE_STRING: - return "string"; - case FieldDescriptorProto::TYPE_GROUP: - return "group<unsupported!>"; - case FieldDescriptorProto::TYPE_MESSAGE: - return field.type_name(); - case FieldDescriptorProto::TYPE_BYTES: - return "bytes"; - case FieldDescriptorProto::TYPE_UINT32: - return "uint32"; - case FieldDescriptorProto::TYPE_ENUM: - return field.type_name(); - case FieldDescriptorProto::TYPE_SFIXED32: - return "sfixed32"; - case FieldDescriptorProto::TYPE_SFIXED64: - return "sfixed64"; - case FieldDescriptorProto::TYPE_SINT32: - return "sint32"; - case FieldDescriptorProto::TYPE_SINT64: - return "sint64"; - default: - // won't happen - return "void"; - } -} - -static uint64_t -get_field_id(const FieldDescriptorProto& field) -{ - // Number - uint64_t result = (uint32_t)field.number(); - - // Type - switch (field.type()) { - case FieldDescriptorProto::TYPE_DOUBLE: - result |= FIELD_TYPE_DOUBLE; - break; - case FieldDescriptorProto::TYPE_FLOAT: - result |= FIELD_TYPE_FLOAT; - break; - case FieldDescriptorProto::TYPE_INT64: - result |= FIELD_TYPE_INT64; - break; - case FieldDescriptorProto::TYPE_UINT64: - result |= FIELD_TYPE_UINT64; - break; - case FieldDescriptorProto::TYPE_INT32: - result |= FIELD_TYPE_INT32; - break; - case FieldDescriptorProto::TYPE_FIXED64: - result |= FIELD_TYPE_FIXED64; - break; - case FieldDescriptorProto::TYPE_FIXED32: - result |= FIELD_TYPE_FIXED32; - break; - case FieldDescriptorProto::TYPE_BOOL: - result |= FIELD_TYPE_BOOL; - break; - case FieldDescriptorProto::TYPE_STRING: - result |= FIELD_TYPE_STRING; - break; - case FieldDescriptorProto::TYPE_MESSAGE: - result |= FIELD_TYPE_OBJECT; - break; - case FieldDescriptorProto::TYPE_BYTES: - result |= FIELD_TYPE_BYTES; - break; - case FieldDescriptorProto::TYPE_UINT32: - result |= FIELD_TYPE_UINT32; - break; - case FieldDescriptorProto::TYPE_ENUM: - result |= FIELD_TYPE_ENUM; - break; - case FieldDescriptorProto::TYPE_SFIXED32: - result |= FIELD_TYPE_SFIXED32; - break; - case FieldDescriptorProto::TYPE_SFIXED64: - result |= FIELD_TYPE_SFIXED64; - break; - case FieldDescriptorProto::TYPE_SINT32: - result |= FIELD_TYPE_SINT32; - break; - case FieldDescriptorProto::TYPE_SINT64: - result |= FIELD_TYPE_SINT64; - break; - default: - ; - } - - // Count - if (field.options().packed()) { - result |= FIELD_COUNT_PACKED; - } else if (field.label() == FieldDescriptorProto::LABEL_REPEATED) { - result |= FIELD_COUNT_REPEATED; - } else { - result |= FIELD_COUNT_SINGLE; - } - - return result; -} - -/** * Write a field. */ static void diff --git a/tools/streaming_proto/stream_proto_utils.cpp b/tools/streaming_proto/stream_proto_utils.cpp new file mode 100644 index 000000000000..e8f86bc0be83 --- /dev/null +++ b/tools/streaming_proto/stream_proto_utils.cpp @@ -0,0 +1,102 @@ +#include "stream_proto_utils.h" + +namespace android { +namespace stream_proto { + +/** + * Position of the field type in a (long long) fieldId. + */ +const uint64_t FIELD_TYPE_SHIFT = 32; + +// +// FieldId flags for whether the field is single, repeated or packed. +// TODO: packed is not supported yet. +// +const uint64_t FIELD_COUNT_SHIFT = 40; +const uint64_t FIELD_COUNT_MASK = 0x0fULL << FIELD_COUNT_SHIFT; +const uint64_t FIELD_COUNT_UNKNOWN = 0; +const uint64_t FIELD_COUNT_SINGLE = 1ULL << FIELD_COUNT_SHIFT; +const uint64_t FIELD_COUNT_REPEATED = 2ULL << FIELD_COUNT_SHIFT; +const uint64_t FIELD_COUNT_PACKED = 5ULL << FIELD_COUNT_SHIFT; + +uint64_t +get_field_id(const FieldDescriptorProto& field) +{ + // Number + uint64_t result = (uint32_t)field.number(); + + // Type + result |= (uint64_t)field.type() << FIELD_TYPE_SHIFT; + + // Count + if (field.options().packed()) { + result |= FIELD_COUNT_PACKED; + } else if (field.label() == FieldDescriptorProto::LABEL_REPEATED) { + result |= FIELD_COUNT_REPEATED; + } else { + result |= FIELD_COUNT_SINGLE; + } + + return result; +} + +string +get_proto_type(const FieldDescriptorProto& field) +{ + switch (field.type()) { + case FieldDescriptorProto::TYPE_DOUBLE: + return "double"; + case FieldDescriptorProto::TYPE_FLOAT: + return "float"; + case FieldDescriptorProto::TYPE_INT64: + return "int64"; + case FieldDescriptorProto::TYPE_UINT64: + return "uint64"; + case FieldDescriptorProto::TYPE_INT32: + return "int32"; + case FieldDescriptorProto::TYPE_FIXED64: + return "fixed64"; + case FieldDescriptorProto::TYPE_FIXED32: + return "fixed32"; + case FieldDescriptorProto::TYPE_BOOL: + return "bool"; + case FieldDescriptorProto::TYPE_STRING: + return "string"; + case FieldDescriptorProto::TYPE_GROUP: + return "group<unsupported!>"; + case FieldDescriptorProto::TYPE_MESSAGE: + return field.type_name(); + case FieldDescriptorProto::TYPE_BYTES: + return "bytes"; + case FieldDescriptorProto::TYPE_UINT32: + return "uint32"; + case FieldDescriptorProto::TYPE_ENUM: + return field.type_name(); + case FieldDescriptorProto::TYPE_SFIXED32: + return "sfixed32"; + case FieldDescriptorProto::TYPE_SFIXED64: + return "sfixed64"; + case FieldDescriptorProto::TYPE_SINT32: + return "sint32"; + case FieldDescriptorProto::TYPE_SINT64: + return "sint64"; + default: + // won't happen + return "void"; + } +} + +bool +should_generate_for_file(const CodeGeneratorRequest& request, const string& file) +{ + const int N = request.file_to_generate_size(); + for (int i=0; i<N; i++) { + if (request.file_to_generate(i) == file) { + return true; + } + } + return false; +} + +} // stream_proto +} // android diff --git a/tools/streaming_proto/stream_proto_utils.h b/tools/streaming_proto/stream_proto_utils.h new file mode 100644 index 000000000000..5297eccba849 --- /dev/null +++ b/tools/streaming_proto/stream_proto_utils.h @@ -0,0 +1,29 @@ +#include <stdint.h> + +#include "google/protobuf/compiler/plugin.pb.h" +#include "google/protobuf/io/zero_copy_stream_impl.h" + +namespace android { +namespace stream_proto { + +using namespace google::protobuf; +using namespace google::protobuf::compiler; +using namespace std; + +/** + * Get encoded field id from a field. + */ +uint64_t get_field_id(const FieldDescriptorProto& field); + +/** + * Get the string name for a field. + */ +string get_proto_type(const FieldDescriptorProto& field); + +/** + * See if this is the file for this request, and not one of the imported ones. + */ +bool should_generate_for_file(const CodeGeneratorRequest& request, const string& file); + +} // stream_proto +} // android diff --git a/tools/streaming_proto/string_utils.h b/tools/streaming_proto/string_utils.h index 03284d16e1be..d6f195f67188 100644 --- a/tools/streaming_proto/string_utils.h +++ b/tools/streaming_proto/string_utils.h @@ -6,6 +6,9 @@ namespace stream_proto { using namespace std; +// Indent +const string INDENT = " "; + /** * Capitalizes the string, removes underscores and makes the next letter * capitalized, and makes the letter following numbers capitalized. |