1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2015 Google Inc. All rights reserved. 3 // https://developers.google.com/protocol-buffers/ 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions are 7 // met: 8 // 9 // * Redistributions of source code must retain the above copyright 10 // notice, this list of conditions and the following disclaimer. 11 // * Redistributions in binary form must reproduce the above 12 // copyright notice, this list of conditions and the following disclaimer 13 // in the documentation and/or other materials provided with the 14 // distribution. 15 // * Neither the name of Google Inc. nor the names of its 16 // contributors may be used to endorse or promote products derived from 17 // this software without specific prior written permission. 18 // 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 #include <sstream> 32 33 #include <google/protobuf/compiler/code_generator.h> 34 #include <google/protobuf/compiler/plugin.h> 35 #include <google/protobuf/descriptor.h> 36 #include <google/protobuf/descriptor.pb.h> 37 #include <google/protobuf/io/printer.h> 38 #include <google/protobuf/io/zero_copy_stream.h> 39 #include <google/protobuf/stubs/strutil.h> 40 41 #include <google/protobuf/compiler/csharp/csharp_doc_comment.h> 42 #include <google/protobuf/compiler/csharp/csharp_helpers.h> 43 #include <google/protobuf/compiler/csharp/csharp_map_field.h> 44 45 namespace google { 46 namespace protobuf { 47 namespace compiler { 48 namespace csharp { 49 50 MapFieldGenerator::MapFieldGenerator(const FieldDescriptor* descriptor, 51 int fieldOrdinal, 52 const Options* options) 53 : FieldGeneratorBase(descriptor, fieldOrdinal, options) { 54 } 55 56 MapFieldGenerator::~MapFieldGenerator() { 57 } 58 59 void MapFieldGenerator::GenerateMembers(io::Printer* printer) { 60 const FieldDescriptor* key_descriptor = 61 descriptor_->message_type()->FindFieldByName("key"); 62 const FieldDescriptor* value_descriptor = 63 descriptor_->message_type()->FindFieldByName("value"); 64 variables_["key_type_name"] = type_name(key_descriptor); 65 variables_["value_type_name"] = type_name(value_descriptor); 66 scoped_ptr<FieldGeneratorBase> key_generator( 67 CreateFieldGenerator(key_descriptor, 1, this->options())); 68 scoped_ptr<FieldGeneratorBase> value_generator( 69 CreateFieldGenerator(value_descriptor, 2, this->options())); 70 71 printer->Print( 72 variables_, 73 "private static readonly pbc::MapField<$key_type_name$, $value_type_name$>.Codec _map_$name$_codec\n" 74 " = new pbc::MapField<$key_type_name$, $value_type_name$>.Codec("); 75 key_generator->GenerateCodecCode(printer); 76 printer->Print(", "); 77 value_generator->GenerateCodecCode(printer); 78 printer->Print( 79 variables_, 80 ", $tag$);\n" 81 "private readonly pbc::MapField<$key_type_name$, $value_type_name$> $name$_ = new pbc::MapField<$key_type_name$, $value_type_name$>();\n"); 82 WritePropertyDocComment(printer, descriptor_); 83 AddDeprecatedFlag(printer); 84 printer->Print( 85 variables_, 86 "$access_level$ pbc::MapField<$key_type_name$, $value_type_name$> $property_name$ {\n" 87 " get { return $name$_; }\n" 88 "}\n"); 89 } 90 91 void MapFieldGenerator::GenerateMergingCode(io::Printer* printer) { 92 printer->Print( 93 variables_, 94 "$name$_.Add(other.$name$_);\n"); 95 } 96 97 void MapFieldGenerator::GenerateParsingCode(io::Printer* printer) { 98 printer->Print( 99 variables_, 100 "$name$_.AddEntriesFrom(input, _map_$name$_codec);\n"); 101 } 102 103 void MapFieldGenerator::GenerateSerializationCode(io::Printer* printer) { 104 printer->Print( 105 variables_, 106 "$name$_.WriteTo(output, _map_$name$_codec);\n"); 107 } 108 109 void MapFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) { 110 printer->Print( 111 variables_, 112 "size += $name$_.CalculateSize(_map_$name$_codec);\n"); 113 } 114 115 void MapFieldGenerator::WriteHash(io::Printer* printer) { 116 printer->Print( 117 variables_, 118 "hash ^= $property_name$.GetHashCode();\n"); 119 } 120 void MapFieldGenerator::WriteEquals(io::Printer* printer) { 121 printer->Print( 122 variables_, 123 "if (!$property_name$.Equals(other.$property_name$)) return false;\n"); 124 } 125 126 void MapFieldGenerator::WriteToString(io::Printer* printer) { 127 // TODO: If we ever actually use ToString, we'll need to impleme this... 128 } 129 130 void MapFieldGenerator::GenerateCloningCode(io::Printer* printer) { 131 printer->Print(variables_, 132 "$name$_ = other.$name$_.Clone();\n"); 133 } 134 135 void MapFieldGenerator::GenerateFreezingCode(io::Printer* printer) { 136 } 137 138 } // namespace csharp 139 } // namespace compiler 140 } // namespace protobuf 141 } // namespace google 142