1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #include "google/protobuf/compiler/csharp/csharp_enum_field.h"
9
10 #include <sstream>
11
12 #include "google/protobuf/compiler/code_generator.h"
13 #include "google/protobuf/descriptor.h"
14 #include "google/protobuf/compiler/csharp/csharp_doc_comment.h"
15 #include "google/protobuf/compiler/csharp/csharp_helpers.h"
16 #include "google/protobuf/compiler/csharp/csharp_options.h"
17 #include "google/protobuf/descriptor.pb.h"
18 #include "google/protobuf/io/printer.h"
19 #include "google/protobuf/io/zero_copy_stream.h"
20
21 namespace google {
22 namespace protobuf {
23 namespace compiler {
24 namespace csharp {
25
EnumFieldGenerator(const FieldDescriptor * descriptor,int presenceIndex,const Options * options)26 EnumFieldGenerator::EnumFieldGenerator(const FieldDescriptor* descriptor,
27 int presenceIndex, const Options *options)
28 : PrimitiveFieldGenerator(descriptor, presenceIndex, options) {
29 }
30
~EnumFieldGenerator()31 EnumFieldGenerator::~EnumFieldGenerator() {
32 }
33
GenerateParsingCode(io::Printer * printer)34 void EnumFieldGenerator::GenerateParsingCode(io::Printer* printer) {
35 printer->Print(variables_,
36 "$property_name$ = ($type_name$) input.ReadEnum();\n");
37 }
38
GenerateSerializationCode(io::Printer * printer)39 void EnumFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
40 printer->Print(variables_,
41 "if ($has_property_check$) {\n"
42 " output.WriteRawTag($tag_bytes$);\n"
43 " output.WriteEnum((int) $property_name$);\n"
44 "}\n");
45 }
46
GenerateSerializedSizeCode(io::Printer * printer)47 void EnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
48 printer->Print(
49 variables_,
50 "if ($has_property_check$) {\n"
51 " size += $tag_size$ + pb::CodedOutputStream.ComputeEnumSize((int) $property_name$);\n"
52 "}\n");
53 }
54
GenerateCodecCode(io::Printer * printer)55 void EnumFieldGenerator::GenerateCodecCode(io::Printer* printer) {
56 printer->Print(
57 variables_,
58 "pb::FieldCodec.ForEnum($tag$, x => (int) x, x => ($type_name$) x, $default_value$)");
59 }
60
GenerateExtensionCode(io::Printer * printer)61 void EnumFieldGenerator::GenerateExtensionCode(io::Printer* printer) {
62 WritePropertyDocComment(printer, options(), descriptor_);
63 AddDeprecatedFlag(printer);
64 printer->Print(
65 variables_,
66 "$access_level$ static readonly pb::Extension<$extended_type$, $type_name$> $property_name$ =\n"
67 " new pb::Extension<$extended_type$, $type_name$>($number$, ");
68 GenerateCodecCode(printer);
69 printer->Print(");\n");
70 }
71
EnumOneofFieldGenerator(const FieldDescriptor * descriptor,int presenceIndex,const Options * options)72 EnumOneofFieldGenerator::EnumOneofFieldGenerator(
73 const FieldDescriptor* descriptor, int presenceIndex, const Options *options)
74 : PrimitiveOneofFieldGenerator(descriptor, presenceIndex, options) {
75 }
76
~EnumOneofFieldGenerator()77 EnumOneofFieldGenerator::~EnumOneofFieldGenerator() {
78 }
79
GenerateMergingCode(io::Printer * printer)80 void EnumOneofFieldGenerator::GenerateMergingCode(io::Printer* printer) {
81 printer->Print(variables_, "$property_name$ = other.$property_name$;\n");
82 }
83
GenerateParsingCode(io::Printer * printer)84 void EnumOneofFieldGenerator::GenerateParsingCode(io::Printer* printer) {
85 // TODO: What about if we read the default value?
86 printer->Print(
87 variables_,
88 "$oneof_name$_ = input.ReadEnum();\n"
89 "$oneof_name$Case_ = $oneof_property_name$OneofCase.$oneof_case_name$;\n");
90 }
91
GenerateSerializationCode(io::Printer * printer)92 void EnumOneofFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
93 printer->Print(
94 variables_,
95 "if ($has_property_check$) {\n"
96 " output.WriteRawTag($tag_bytes$);\n"
97 " output.WriteEnum((int) $property_name$);\n"
98 "}\n");
99 }
100
GenerateSerializedSizeCode(io::Printer * printer)101 void EnumOneofFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
102 printer->Print(
103 variables_,
104 "if ($has_property_check$) {\n"
105 " size += $tag_size$ + pb::CodedOutputStream.ComputeEnumSize((int) $property_name$);\n"
106 "}\n");
107 }
108
109 } // namespace csharp
110 } // namespace compiler
111 } // namespace protobuf
112 } // namespace google
113