• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "stream_proto_utils.h"
2 
3 namespace android {
4 namespace stream_proto {
5 
6 /**
7  * Position of the field type in a (long long) fieldId.
8  */
9 const uint64_t FIELD_TYPE_SHIFT = 32;
10 
11 //
12 // FieldId flags for whether the field is single, repeated or packed.
13 // TODO: packed is not supported yet.
14 //
15 const uint64_t FIELD_COUNT_SHIFT = 40;
16 const uint64_t FIELD_COUNT_SINGLE = 1ULL << FIELD_COUNT_SHIFT;
17 const uint64_t FIELD_COUNT_REPEATED = 2ULL << FIELD_COUNT_SHIFT;
18 const uint64_t FIELD_COUNT_PACKED = 5ULL << FIELD_COUNT_SHIFT;
19 
20 uint64_t
get_field_id(const FieldDescriptorProto & field)21 get_field_id(const FieldDescriptorProto& field)
22 {
23     // Number
24     uint64_t result = (uint32_t)field.number();
25 
26     // Type
27     result |= (uint64_t)field.type() << FIELD_TYPE_SHIFT;
28 
29     // Count
30     if (field.options().packed()) {
31         result |= FIELD_COUNT_PACKED;
32     } else if (field.label() == FieldDescriptorProto::LABEL_REPEATED) {
33         result |= FIELD_COUNT_REPEATED;
34     } else {
35         result |= FIELD_COUNT_SINGLE;
36     }
37 
38     return result;
39 }
40 
41 string
get_proto_type(const FieldDescriptorProto & field)42 get_proto_type(const FieldDescriptorProto& field)
43 {
44     switch (field.type()) {
45         case FieldDescriptorProto::TYPE_DOUBLE:
46             return "double";
47         case FieldDescriptorProto::TYPE_FLOAT:
48             return "float";
49         case FieldDescriptorProto::TYPE_INT64:
50             return "int64";
51         case FieldDescriptorProto::TYPE_UINT64:
52             return "uint64";
53         case FieldDescriptorProto::TYPE_INT32:
54             return "int32";
55         case FieldDescriptorProto::TYPE_FIXED64:
56             return "fixed64";
57         case FieldDescriptorProto::TYPE_FIXED32:
58             return "fixed32";
59         case FieldDescriptorProto::TYPE_BOOL:
60             return "bool";
61         case FieldDescriptorProto::TYPE_STRING:
62             return "string";
63         case FieldDescriptorProto::TYPE_GROUP:
64             return "group<unsupported!>";
65         case FieldDescriptorProto::TYPE_MESSAGE:
66             return field.type_name();
67         case FieldDescriptorProto::TYPE_BYTES:
68             return "bytes";
69         case FieldDescriptorProto::TYPE_UINT32:
70             return "uint32";
71         case FieldDescriptorProto::TYPE_ENUM:
72             return field.type_name();
73         case FieldDescriptorProto::TYPE_SFIXED32:
74             return "sfixed32";
75         case FieldDescriptorProto::TYPE_SFIXED64:
76             return "sfixed64";
77         case FieldDescriptorProto::TYPE_SINT32:
78             return "sint32";
79         case FieldDescriptorProto::TYPE_SINT64:
80             return "sint64";
81         default:
82             // won't happen
83             return "void";
84     }
85 }
86 
87 bool
should_generate_for_file(const CodeGeneratorRequest & request,const string & file)88 should_generate_for_file(const CodeGeneratorRequest& request, const string& file)
89 {
90     const int N = request.file_to_generate_size();
91     for (int i=0; i<N; i++) {
92         if (request.file_to_generate(i) == file) {
93             return true;
94         }
95     }
96     return false;
97 }
98 
99 } // stream_proto
100 } // android
101