diff options
author | Yi Jin <jinyithu@google.com> | 2017-10-17 18:29:33 -0700 |
---|---|---|
committer | Yi Jin <jinyithu@google.com> | 2017-10-31 16:54:38 -0700 |
commit | 04625ad4886a478bf74bbfc13937c10fa63eb272 (patch) | |
tree | ba39988adb170b2e6e744ab304de7f8846a4d444 /tools/streaming_proto/cpp | |
parent | eb7d335641ec1b9c91c1609a94cb1cbdba1d3987 (diff) |
Refactor incident_helper to use protoutil and cppstream plugin.
1. Split the parsers to its own file to prevent all the parsers in one
gaint file.
2. Completely get rid of protobuf-cpp-full in incident_helper, use
ProtoOutputStream and cppstream instead, the incident_helper binary is
reduced from ~500K to ~113K.
3. Write data to protobuf even its values are zero/default, the reason
is for example we have a repeated int32 orders = 1; and people
explicitly append 0 so the total repeated field has 10 values, if zero
is not written to serialized data, this repeated field will only have 9
values which is not what we want at first place. This also aligns with
the default protobuf serialization behavior in incident_helper_test.
4. Use Android.bp for protoutil lib since it is not able to depend on
libs compiled by .mk file, it works the other way.
5. Add a new custom message option for streaming_proto, if specified,
the cppstream will create extra metadata to get field ids by field name.
A Table class is created in incident_helper to use it.
Bug: 67860303
Test: unit tested as well as on device test
Change-Id: I8e136fd15f343a4a623d20910ec64b622b478a3e
Diffstat (limited to 'tools/streaming_proto/cpp')
-rw-r--r-- | tools/streaming_proto/cpp/main.cpp | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/tools/streaming_proto/cpp/main.cpp b/tools/streaming_proto/cpp/main.cpp index d4e1b7aede92..dc96d5c54c16 100644 --- a/tools/streaming_proto/cpp/main.cpp +++ b/tools/streaming_proto/cpp/main.cpp @@ -1,6 +1,8 @@ #include "Errors.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" @@ -160,6 +162,12 @@ write_field(stringstream& text, const FieldDescriptorProto& field, const string& text << endl; } +static inline bool +should_generate_fields_mapping(const DescriptorProto& message) +{ + return message.options().GetExtension(stream).enable_fields_mapping(); +} + static void write_message(stringstream& text, const DescriptorProto& message, const string& indent) { @@ -167,8 +175,7 @@ write_message(stringstream& text, const DescriptorProto& message, const string& const string indented = indent + INDENT; text << indent << "// message " << message.name() << endl; - text << indent << "class " << message.name() << " {" << endl; - text << indent << "public:" << endl; + text << indent << "namespace " << message.name() << " {" << endl; // Enums N = message.enum_type_size(); @@ -188,12 +195,27 @@ write_message(stringstream& text, const DescriptorProto& message, const string& write_field(text, message.field(i), indented); } - text << indent << "};" << endl; + if (should_generate_fields_mapping(message)) { + N = message.field_size(); + text << indented << "const int _FIELD_COUNT = " << N << ";" << endl; + text << indented << "const char* _FIELD_NAMES[" << N << "] = {" << endl; + for (int i=0; i<N; i++) { + text << indented << INDENT << "\"" << message.field(i).name() << "\"," << endl; + } + text << indented << "};" << endl; + text << indented << "const uint64_t _FIELD_IDS[" << N << "] = {" << endl; + for (int i=0; i<N; i++) { + text << indented << INDENT << make_constant_name(message.field(i).name()) << "," << endl; + } + text << indented << "};" << endl << endl; + } + + text << indent << "} //" << message.name() << endl; text << endl; } static void -write_cpp_file(CodeGeneratorResponse* response, const FileDescriptorProto& file_descriptor) +write_header_file(CodeGeneratorResponse* response, const FileDescriptorProto& file_descriptor) { stringstream text; @@ -255,7 +277,7 @@ int main(int argc, char const *argv[]) for (int i=0; i<N; i++) { const FileDescriptorProto& file_descriptor = request.proto_file(i); if (should_generate_for_file(request, file_descriptor.name())) { - write_cpp_file(&response, file_descriptor); + write_header_file(&response, file_descriptor); } } @@ -270,4 +292,4 @@ int main(int argc, char const *argv[]) /* code */ return 0; -}
\ No newline at end of file +} |