| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // http://code.google.com/p/protobuf/ | 3 // http://code.google.com/p/protobuf/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 #include <google/protobuf/stubs/strutil.h> | 45 #include <google/protobuf/stubs/strutil.h> |
| 46 | 46 |
| 47 namespace google { | 47 namespace google { |
| 48 namespace protobuf { | 48 namespace protobuf { |
| 49 namespace compiler { | 49 namespace compiler { |
| 50 namespace cpp { | 50 namespace cpp { |
| 51 | 51 |
| 52 using internal::WireFormat; | 52 using internal::WireFormat; |
| 53 | 53 |
| 54 void SetCommonFieldVariables(const FieldDescriptor* descriptor, | 54 void SetCommonFieldVariables(const FieldDescriptor* descriptor, |
| 55 map<string, string>* variables) { | 55 map<string, string>* variables, |
| 56 const Options& options) { |
| 56 (*variables)["name"] = FieldName(descriptor); | 57 (*variables)["name"] = FieldName(descriptor); |
| 57 (*variables)["index"] = SimpleItoa(descriptor->index()); | 58 (*variables)["index"] = SimpleItoa(descriptor->index()); |
| 58 (*variables)["number"] = SimpleItoa(descriptor->number()); | 59 (*variables)["number"] = SimpleItoa(descriptor->number()); |
| 59 (*variables)["classname"] = ClassName(FieldScope(descriptor), false); | 60 (*variables)["classname"] = ClassName(FieldScope(descriptor), false); |
| 60 (*variables)["declared_type"] = DeclaredTypeMethodName(descriptor->type()); | 61 (*variables)["declared_type"] = DeclaredTypeMethodName(descriptor->type()); |
| 61 | 62 |
| 62 (*variables)["tag_size"] = SimpleItoa( | 63 (*variables)["tag_size"] = SimpleItoa( |
| 63 WireFormat::TagSize(descriptor->number(), descriptor->type())); | 64 WireFormat::TagSize(descriptor->number(), descriptor->type())); |
| 64 (*variables)["deprecation"] = descriptor->options().deprecated() | 65 (*variables)["deprecation"] = descriptor->options().deprecated() |
| 65 ? " PROTOBUF_DEPRECATED" : ""; | 66 ? " PROTOBUF_DEPRECATED" : ""; |
| 66 | 67 |
| 68 (*variables)["cppget"] = "Get"; |
| 67 } | 69 } |
| 68 | 70 |
| 69 FieldGenerator::~FieldGenerator() {} | 71 FieldGenerator::~FieldGenerator() {} |
| 70 | 72 |
| 71 void FieldGenerator:: | 73 void FieldGenerator:: |
| 72 GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const { | 74 GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const { |
| 73 // Reaching here indicates a bug. Cases are: | 75 // Reaching here indicates a bug. Cases are: |
| 74 // - This FieldGenerator should support packing, but this method should be | 76 // - This FieldGenerator should support packing, but this method should be |
| 75 // overridden. | 77 // overridden. |
| 76 // - This FieldGenerator doesn't support packing, and this method should | 78 // - This FieldGenerator doesn't support packing, and this method should |
| 77 // never have been called. | 79 // never have been called. |
| 78 GOOGLE_LOG(FATAL) << "GenerateMergeFromCodedStreamWithPacking() " | 80 GOOGLE_LOG(FATAL) << "GenerateMergeFromCodedStreamWithPacking() " |
| 79 << "called on field generator that does not support packing."; | 81 << "called on field generator that does not support packing."; |
| 80 | 82 |
| 81 } | 83 } |
| 82 | 84 |
| 83 FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor) | 85 FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor, |
| 86 const Options& options) |
| 84 : descriptor_(descriptor), | 87 : descriptor_(descriptor), |
| 85 field_generators_( | 88 field_generators_(new scoped_ptr<FieldGenerator>[descriptor->field_count()])
{ |
| 86 new scoped_ptr<FieldGenerator>[descriptor->field_count()]) { | |
| 87 // Construct all the FieldGenerators. | 89 // Construct all the FieldGenerators. |
| 88 for (int i = 0; i < descriptor->field_count(); i++) { | 90 for (int i = 0; i < descriptor->field_count(); i++) { |
| 89 field_generators_[i].reset(MakeGenerator(descriptor->field(i))); | 91 field_generators_[i].reset(MakeGenerator(descriptor->field(i), options)); |
| 90 } | 92 } |
| 91 } | 93 } |
| 92 | 94 |
| 93 FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field) { | 95 FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field, |
| 96 const Options& options) { |
| 94 if (field->is_repeated()) { | 97 if (field->is_repeated()) { |
| 95 switch (field->cpp_type()) { | 98 switch (field->cpp_type()) { |
| 96 case FieldDescriptor::CPPTYPE_MESSAGE: | 99 case FieldDescriptor::CPPTYPE_MESSAGE: |
| 97 return new RepeatedMessageFieldGenerator(field); | 100 return new RepeatedMessageFieldGenerator(field, options); |
| 98 case FieldDescriptor::CPPTYPE_STRING: | 101 case FieldDescriptor::CPPTYPE_STRING: |
| 99 switch (field->options().ctype()) { | 102 switch (field->options().ctype()) { |
| 100 default: // RepeatedStringFieldGenerator handles unknown ctypes. | 103 default: // RepeatedStringFieldGenerator handles unknown ctypes. |
| 101 case FieldOptions::STRING: | 104 case FieldOptions::STRING: |
| 102 return new RepeatedStringFieldGenerator(field); | 105 return new RepeatedStringFieldGenerator(field, options); |
| 103 } | 106 } |
| 104 case FieldDescriptor::CPPTYPE_ENUM: | 107 case FieldDescriptor::CPPTYPE_ENUM: |
| 105 return new RepeatedEnumFieldGenerator(field); | 108 return new RepeatedEnumFieldGenerator(field, options); |
| 106 default: | 109 default: |
| 107 return new RepeatedPrimitiveFieldGenerator(field); | 110 return new RepeatedPrimitiveFieldGenerator(field, options); |
| 108 } | 111 } |
| 109 } else { | 112 } else { |
| 110 switch (field->cpp_type()) { | 113 switch (field->cpp_type()) { |
| 111 case FieldDescriptor::CPPTYPE_MESSAGE: | 114 case FieldDescriptor::CPPTYPE_MESSAGE: |
| 112 return new MessageFieldGenerator(field); | 115 return new MessageFieldGenerator(field, options); |
| 113 case FieldDescriptor::CPPTYPE_STRING: | 116 case FieldDescriptor::CPPTYPE_STRING: |
| 114 switch (field->options().ctype()) { | 117 switch (field->options().ctype()) { |
| 115 default: // StringFieldGenerator handles unknown ctypes. | 118 default: // StringFieldGenerator handles unknown ctypes. |
| 116 case FieldOptions::STRING: | 119 case FieldOptions::STRING: |
| 117 return new StringFieldGenerator(field); | 120 return new StringFieldGenerator(field, options); |
| 118 } | 121 } |
| 119 case FieldDescriptor::CPPTYPE_ENUM: | 122 case FieldDescriptor::CPPTYPE_ENUM: |
| 120 return new EnumFieldGenerator(field); | 123 return new EnumFieldGenerator(field, options); |
| 121 default: | 124 default: |
| 122 return new PrimitiveFieldGenerator(field); | 125 return new PrimitiveFieldGenerator(field, options); |
| 123 } | 126 } |
| 124 } | 127 } |
| 125 } | 128 } |
| 126 | 129 |
| 127 FieldGeneratorMap::~FieldGeneratorMap() {} | 130 FieldGeneratorMap::~FieldGeneratorMap() {} |
| 128 | 131 |
| 129 const FieldGenerator& FieldGeneratorMap::get( | 132 const FieldGenerator& FieldGeneratorMap::get( |
| 130 const FieldDescriptor* field) const { | 133 const FieldDescriptor* field) const { |
| 131 GOOGLE_CHECK_EQ(field->containing_type(), descriptor_); | 134 GOOGLE_CHECK_EQ(field->containing_type(), descriptor_); |
| 132 return *field_generators_[field->index()]; | 135 return *field_generators_[field->index()]; |
| 133 } | 136 } |
| 134 | 137 |
| 135 | 138 |
| 136 } // namespace cpp | 139 } // namespace cpp |
| 137 } // namespace compiler | 140 } // namespace compiler |
| 138 } // namespace protobuf | 141 } // namespace protobuf |
| 139 } // namespace google | 142 } // namespace google |
| OLD | NEW |