| 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 // https://developers.google.com/protocol-buffers/ |
| 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. |
| 11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
| (...skipping 15 matching lines...) Expand all Loading... |
| 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 // Author: kenton@google.com (Kenton Varda) | 31 // Author: kenton@google.com (Kenton Varda) |
| 32 // Based on original Protocol Buffers design by | 32 // Based on original Protocol Buffers design by |
| 33 // Sanjay Ghemawat, Jeff Dean, and others. | 33 // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | 34 |
| 35 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__ | 35 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__ |
| 36 #define GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__ | 36 #define GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__ |
| 37 | 37 |
| 38 #include <map> | 38 #include <map> |
| 39 #include <memory> |
| 40 #ifndef _SHARED_PTR_H |
| 41 #include <google/protobuf/stubs/shared_ptr.h> |
| 42 #endif |
| 39 #include <string> | 43 #include <string> |
| 40 | 44 |
| 41 #include <google/protobuf/stubs/common.h> | |
| 42 #include <google/protobuf/descriptor.h> | 45 #include <google/protobuf/descriptor.h> |
| 43 #include <google/protobuf/compiler/cpp/cpp_options.h> | 46 #include <google/protobuf/compiler/cpp/cpp_options.h> |
| 44 | 47 |
| 45 namespace google { | 48 namespace google { |
| 46 namespace protobuf { | 49 namespace protobuf { |
| 47 namespace io { | 50 namespace io { |
| 48 class Printer; // printer.h | 51 class Printer; // printer.h |
| 49 } | 52 } |
| 50 } | 53 } |
| 51 | 54 |
| 52 namespace protobuf { | 55 namespace protobuf { |
| 53 namespace compiler { | 56 namespace compiler { |
| 54 namespace cpp { | 57 namespace cpp { |
| 55 | 58 |
| 56 // Helper function: set variables in the map that are the same for all | 59 // Helper function: set variables in the map that are the same for all |
| 57 // field code generators. | 60 // field code generators. |
| 58 // ['name', 'index', 'number', 'classname', 'declared_type', 'tag_size', | 61 // ['name', 'index', 'number', 'classname', 'declared_type', 'tag_size', |
| 59 // 'deprecation']. | 62 // 'deprecation']. |
| 60 void SetCommonFieldVariables(const FieldDescriptor* descriptor, | 63 void SetCommonFieldVariables(const FieldDescriptor* descriptor, |
| 61 map<string, string>* variables, | 64 map<string, string>* variables, |
| 62 const Options& options); | 65 const Options& options); |
| 63 | 66 |
| 67 void SetCommonOneofFieldVariables(const FieldDescriptor* descriptor, |
| 68 map<string, string>* variables); |
| 69 |
| 64 class FieldGenerator { | 70 class FieldGenerator { |
| 65 public: | 71 public: |
| 66 FieldGenerator() {} | 72 FieldGenerator() {} |
| 67 virtual ~FieldGenerator(); | 73 virtual ~FieldGenerator(); |
| 68 | 74 |
| 69 // Generate lines of code declaring members fields of the message class | 75 // Generate lines of code declaring members fields of the message class |
| 70 // needed to represent this field. These are placed inside the message | 76 // needed to represent this field. These are placed inside the message |
| 71 // class. | 77 // class. |
| 72 virtual void GeneratePrivateMembers(io::Printer* printer) const = 0; | 78 virtual void GeneratePrivateMembers(io::Printer* printer) const = 0; |
| 73 | 79 |
| 80 // Generate static default variable for this field. These are placed inside |
| 81 // the message class. Most field types don't need this, so the default |
| 82 // implementation is empty. |
| 83 virtual void GenerateStaticMembers(io::Printer* /*printer*/) const {} |
| 84 |
| 85 // Generate prototypes for accessors that will manipulate imported |
| 86 // messages inline. These are for .proto.h headers. |
| 87 // |
| 88 // In .proto.h mode, the headers of imports are not #included. However, |
| 89 // functions that manipulate the imported message types need access to |
| 90 // the class definition of the imported message, meaning that the headers |
| 91 // must be #included. To get around this, functions that manipulate |
| 92 // imported message objects are defined as dependent functions in a base |
| 93 // template class. By making them dependent template functions, the |
| 94 // function templates will not be instantiated until they are called, so |
| 95 // we can defer to those translation units to #include the necessary |
| 96 // generated headers. |
| 97 // |
| 98 // See: |
| 99 // http://en.cppreference.com/w/cpp/language/class_template#Implicit_instantia
tion |
| 100 // |
| 101 // Most field types don't need this, so the default implementation is empty. |
| 102 virtual void GenerateDependentAccessorDeclarations( |
| 103 io::Printer* printer) const {} |
| 104 |
| 74 // Generate prototypes for all of the accessor functions related to this | 105 // Generate prototypes for all of the accessor functions related to this |
| 75 // field. These are placed inside the class definition. | 106 // field. These are placed inside the class definition. |
| 76 virtual void GenerateAccessorDeclarations(io::Printer* printer) const = 0; | 107 virtual void GenerateAccessorDeclarations(io::Printer* printer) const = 0; |
| 77 | 108 |
| 109 // Generate inline definitions of depenent accessor functions for this field. |
| 110 // These are placed inside the header after all class definitions. |
| 111 virtual void GenerateDependentInlineAccessorDefinitions( |
| 112 io::Printer* printer) const {} |
| 113 |
| 78 // Generate inline definitions of accessor functions for this field. | 114 // Generate inline definitions of accessor functions for this field. |
| 79 // These are placed inside the header after all class definitions. | 115 // These are placed inside the header after all class definitions. |
| 116 // In non-.proto.h mode, this generates dependent accessor functions as well. |
| 80 virtual void GenerateInlineAccessorDefinitions( | 117 virtual void GenerateInlineAccessorDefinitions( |
| 81 io::Printer* printer) const = 0; | 118 io::Printer* printer, bool is_inline) const = 0; |
| 82 | 119 |
| 83 // Generate definitions of accessors that aren't inlined. These are | 120 // Generate definitions of accessors that aren't inlined. These are |
| 84 // placed somewhere in the .cc file. | 121 // placed somewhere in the .cc file. |
| 85 // Most field types don't need this, so the default implementation is empty. | 122 // Most field types don't need this, so the default implementation is empty. |
| 86 virtual void GenerateNonInlineAccessorDefinitions( | 123 virtual void GenerateNonInlineAccessorDefinitions( |
| 87 io::Printer* printer) const {} | 124 io::Printer* /*printer*/) const {} |
| 88 | 125 |
| 89 // Generate lines of code (statements, not declarations) which clear the | 126 // Generate lines of code (statements, not declarations) which clear the |
| 90 // field. This is used to define the clear_$name$() method as well as | 127 // field. This is used to define the clear_$name$() method as well as |
| 91 // the Clear() method for the whole message. | 128 // the Clear() method for the whole message. |
| 92 virtual void GenerateClearingCode(io::Printer* printer) const = 0; | 129 virtual void GenerateClearingCode(io::Printer* printer) const = 0; |
| 93 | 130 |
| 94 // Generate lines of code (statements, not declarations) which merges the | 131 // Generate lines of code (statements, not declarations) which merges the |
| 95 // contents of the field from the current message to the target message, | 132 // contents of the field from the current message to the target message, |
| 96 // which is stored in the generated code variable "from". | 133 // which is stored in the generated code variable "from". |
| 97 // This is used to fill in the MergeFrom method for the whole message. | 134 // This is used to fill in the MergeFrom method for the whole message. |
| 98 // Details of this usage can be found in message.cc under the | 135 // Details of this usage can be found in message.cc under the |
| 99 // GenerateMergeFrom method. | 136 // GenerateMergeFrom method. |
| 100 virtual void GenerateMergingCode(io::Printer* printer) const = 0; | 137 virtual void GenerateMergingCode(io::Printer* printer) const = 0; |
| 101 | 138 |
| 102 // Generate lines of code (statements, not declarations) which swaps | 139 // Generate lines of code (statements, not declarations) which swaps |
| 103 // this field and the corresponding field of another message, which | 140 // this field and the corresponding field of another message, which |
| 104 // is stored in the generated code variable "other". This is used to | 141 // is stored in the generated code variable "other". This is used to |
| 105 // define the Swap method. Details of usage can be found in | 142 // define the Swap method. Details of usage can be found in |
| 106 // message.cc under the GenerateSwap method. | 143 // message.cc under the GenerateSwap method. |
| 107 virtual void GenerateSwappingCode(io::Printer* printer) const = 0; | 144 virtual void GenerateSwappingCode(io::Printer* printer) const = 0; |
| 108 | 145 |
| 109 // Generate initialization code for private members declared by | 146 // Generate initialization code for private members declared by |
| 110 // GeneratePrivateMembers(). These go into the message class's SharedCtor() | 147 // GeneratePrivateMembers(). These go into the message class's SharedCtor() |
| 111 // method, invoked by each of the generated constructors. | 148 // method, invoked by each of the generated constructors. |
| 112 virtual void GenerateConstructorCode(io::Printer* printer) const = 0; | 149 virtual void GenerateConstructorCode(io::Printer* printer) const = 0; |
| 113 | 150 |
| 114 // Generate any code that needs to go in the class's SharedDtor() method, | 151 // Generate any code that needs to go in the class's SharedDtor() method, |
| 115 // invoked by the destructor. | 152 // invoked by the destructor. |
| 116 // Most field types don't need this, so the default implementation is empty. | 153 // Most field types don't need this, so the default implementation is empty. |
| 117 virtual void GenerateDestructorCode(io::Printer* printer) const {} | 154 virtual void GenerateDestructorCode(io::Printer* /*printer*/) const {} |
| 155 |
| 156 // Generate a manual destructor invocation for use when the message is on an |
| 157 // arena. The code that this method generates will be executed inside a |
| 158 // shared-for-the-whole-message-class method registered with OwnDestructor(). |
| 159 // The method should return |true| if it generated any code that requires a |
| 160 // call; this allows the message generator to eliminate the OwnDestructor() |
| 161 // registration if no fields require it. |
| 162 virtual bool GenerateArenaDestructorCode(io::Printer* printer) const { |
| 163 return false; |
| 164 } |
| 118 | 165 |
| 119 // Generate code that allocates the fields's default instance. | 166 // Generate code that allocates the fields's default instance. |
| 120 virtual void GenerateDefaultInstanceAllocator(io::Printer* printer) const {} | 167 virtual void GenerateDefaultInstanceAllocator(io::Printer* /*printer*/) |
| 168 const {} |
| 121 | 169 |
| 122 // Generate code that should be run when ShutdownProtobufLibrary() is called, | 170 // Generate code that should be run when ShutdownProtobufLibrary() is called, |
| 123 // to delete all dynamically-allocated objects. | 171 // to delete all dynamically-allocated objects. |
| 124 virtual void GenerateShutdownCode(io::Printer* printer) const {} | 172 virtual void GenerateShutdownCode(io::Printer* /*printer*/) const {} |
| 125 | 173 |
| 126 // Generate lines to decode this field, which will be placed inside the | 174 // Generate lines to decode this field, which will be placed inside the |
| 127 // message's MergeFromCodedStream() method. | 175 // message's MergeFromCodedStream() method. |
| 128 virtual void GenerateMergeFromCodedStream(io::Printer* printer) const = 0; | 176 virtual void GenerateMergeFromCodedStream(io::Printer* printer) const = 0; |
| 129 | 177 |
| 130 // Generate lines to decode this field from a packed value, which will be | 178 // Generate lines to decode this field from a packed value, which will be |
| 131 // placed inside the message's MergeFromCodedStream() method. | 179 // placed inside the message's MergeFromCodedStream() method. |
| 132 virtual void GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) | 180 virtual void GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) |
| 133 const; | 181 const; |
| 134 | 182 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 153 // Convenience class which constructs FieldGenerators for a Descriptor. | 201 // Convenience class which constructs FieldGenerators for a Descriptor. |
| 154 class FieldGeneratorMap { | 202 class FieldGeneratorMap { |
| 155 public: | 203 public: |
| 156 explicit FieldGeneratorMap(const Descriptor* descriptor, const Options& option
s); | 204 explicit FieldGeneratorMap(const Descriptor* descriptor, const Options& option
s); |
| 157 ~FieldGeneratorMap(); | 205 ~FieldGeneratorMap(); |
| 158 | 206 |
| 159 const FieldGenerator& get(const FieldDescriptor* field) const; | 207 const FieldGenerator& get(const FieldDescriptor* field) const; |
| 160 | 208 |
| 161 private: | 209 private: |
| 162 const Descriptor* descriptor_; | 210 const Descriptor* descriptor_; |
| 163 scoped_array<scoped_ptr<FieldGenerator> > field_generators_; | 211 google::protobuf::scoped_array<google::protobuf::scoped_ptr<FieldGenerator> >
field_generators_; |
| 164 | 212 |
| 165 static FieldGenerator* MakeGenerator(const FieldDescriptor* field, | 213 static FieldGenerator* MakeGenerator(const FieldDescriptor* field, |
| 166 const Options& options); | 214 const Options& options); |
| 167 | 215 |
| 168 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap); | 216 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap); |
| 169 }; | 217 }; |
| 170 | 218 |
| 171 | 219 |
| 172 } // namespace cpp | 220 } // namespace cpp |
| 173 } // namespace compiler | 221 } // namespace compiler |
| 174 } // namespace protobuf | 222 } // namespace protobuf |
| 175 | 223 |
| 176 } // namespace google | 224 } // namespace google |
| 177 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__ | 225 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__ |
| OLD | NEW |