temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2 | // Copyright 2008 Google Inc. All rights reserved. |
Feng Xiao | e428862 | 2014-10-01 16:26:23 -0700 | [diff] [blame] | 3 | // https://developers.google.com/protocol-buffers/ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 4 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 18 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 30 | |
| 31 | // Author: kenton@google.com (Kenton Varda) |
| 32 | // Based on original Protocol Buffers design by |
| 33 | // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | |
| 35 | #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__ |
| 36 | #define GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__ |
| 37 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 38 | #include <map> |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 39 | #include <memory> |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 40 | #ifndef _SHARED_PTR_H |
| 41 | #include <google/protobuf/stubs/shared_ptr.h> |
| 42 | #endif |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 43 | #include <string> |
| 44 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 45 | #include <google/protobuf/descriptor.h> |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 46 | #include <google/protobuf/compiler/cpp/cpp_options.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 47 | |
| 48 | namespace google { |
| 49 | namespace protobuf { |
| 50 | namespace io { |
| 51 | class Printer; // printer.h |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | namespace protobuf { |
| 56 | namespace compiler { |
| 57 | namespace cpp { |
| 58 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 59 | // Helper function: set variables in the map that are the same for all |
| 60 | // field code generators. |
| 61 | // ['name', 'index', 'number', 'classname', 'declared_type', 'tag_size', |
| 62 | // 'deprecation']. |
| 63 | void SetCommonFieldVariables(const FieldDescriptor* descriptor, |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 64 | map<string, string>* variables, |
| 65 | const Options& options); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 66 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 67 | void SetCommonOneofFieldVariables(const FieldDescriptor* descriptor, |
| 68 | map<string, string>* variables); |
| 69 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 70 | class FieldGenerator { |
| 71 | public: |
| 72 | FieldGenerator() {} |
| 73 | virtual ~FieldGenerator(); |
| 74 | |
| 75 | // Generate lines of code declaring members fields of the message class |
| 76 | // needed to represent this field. These are placed inside the message |
| 77 | // class. |
| 78 | virtual void GeneratePrivateMembers(io::Printer* printer) const = 0; |
| 79 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 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. |
Austin Schuh | ec71d5d | 2014-10-31 19:15:54 -0700 | [diff] [blame] | 83 | virtual void GenerateStaticMembers(io::Printer* /*printer*/) const {} |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 84 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 85 | // Generate prototypes for all of the accessor functions related to this |
| 86 | // field. These are placed inside the class definition. |
| 87 | virtual void GenerateAccessorDeclarations(io::Printer* printer) const = 0; |
| 88 | |
| 89 | // Generate inline definitions of accessor functions for this field. |
| 90 | // These are placed inside the header after all class definitions. |
| 91 | virtual void GenerateInlineAccessorDefinitions( |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 92 | io::Printer* printer, bool is_inline) const = 0; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 93 | |
| 94 | // Generate definitions of accessors that aren't inlined. These are |
| 95 | // placed somewhere in the .cc file. |
| 96 | // Most field types don't need this, so the default implementation is empty. |
| 97 | virtual void GenerateNonInlineAccessorDefinitions( |
Austin Schuh | ec71d5d | 2014-10-31 19:15:54 -0700 | [diff] [blame] | 98 | io::Printer* /*printer*/) const {} |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 99 | |
| 100 | // Generate lines of code (statements, not declarations) which clear the |
| 101 | // field. This is used to define the clear_$name$() method as well as |
| 102 | // the Clear() method for the whole message. |
| 103 | virtual void GenerateClearingCode(io::Printer* printer) const = 0; |
| 104 | |
| 105 | // Generate lines of code (statements, not declarations) which merges the |
| 106 | // contents of the field from the current message to the target message, |
| 107 | // which is stored in the generated code variable "from". |
| 108 | // This is used to fill in the MergeFrom method for the whole message. |
| 109 | // Details of this usage can be found in message.cc under the |
| 110 | // GenerateMergeFrom method. |
| 111 | virtual void GenerateMergingCode(io::Printer* printer) const = 0; |
| 112 | |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 113 | // Generate lines of code (statements, not declarations) which swaps |
| 114 | // this field and the corresponding field of another message, which |
| 115 | // is stored in the generated code variable "other". This is used to |
| 116 | // define the Swap method. Details of usage can be found in |
| 117 | // message.cc under the GenerateSwap method. |
| 118 | virtual void GenerateSwappingCode(io::Printer* printer) const = 0; |
| 119 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 120 | // Generate initialization code for private members declared by |
| 121 | // GeneratePrivateMembers(). These go into the message class's SharedCtor() |
| 122 | // method, invoked by each of the generated constructors. |
| 123 | virtual void GenerateConstructorCode(io::Printer* printer) const = 0; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 124 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 125 | // Generate any code that needs to go in the class's SharedDtor() method, |
| 126 | // invoked by the destructor. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 127 | // Most field types don't need this, so the default implementation is empty. |
Austin Schuh | ec71d5d | 2014-10-31 19:15:54 -0700 | [diff] [blame] | 128 | virtual void GenerateDestructorCode(io::Printer* /*printer*/) const {} |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 129 | |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 130 | // Generate a manual destructor invocation for use when the message is on an |
| 131 | // arena. The code that this method generates will be executed inside a |
| 132 | // shared-for-the-whole-message-class method registered with OwnDestructor(). |
| 133 | // The method should return |true| if it generated any code that requires a |
| 134 | // call; this allows the message generator to eliminate the OwnDestructor() |
| 135 | // registration if no fields require it. |
| 136 | virtual bool GenerateArenaDestructorCode(io::Printer* printer) const { |
| 137 | return false; |
| 138 | } |
| 139 | |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 140 | // Generate code that allocates the fields's default instance. |
Austin Schuh | ec71d5d | 2014-10-31 19:15:54 -0700 | [diff] [blame] | 141 | virtual void GenerateDefaultInstanceAllocator(io::Printer* /*printer*/) |
| 142 | const {} |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 143 | |
| 144 | // Generate code that should be run when ShutdownProtobufLibrary() is called, |
| 145 | // to delete all dynamically-allocated objects. |
Austin Schuh | ec71d5d | 2014-10-31 19:15:54 -0700 | [diff] [blame] | 146 | virtual void GenerateShutdownCode(io::Printer* /*printer*/) const {} |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 147 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 148 | // Generate lines to decode this field, which will be placed inside the |
| 149 | // message's MergeFromCodedStream() method. |
| 150 | virtual void GenerateMergeFromCodedStream(io::Printer* printer) const = 0; |
| 151 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 152 | // Generate lines to decode this field from a packed value, which will be |
| 153 | // placed inside the message's MergeFromCodedStream() method. |
| 154 | virtual void GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) |
| 155 | const; |
| 156 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 157 | // Generate lines to serialize this field, which are placed within the |
| 158 | // message's SerializeWithCachedSizes() method. |
| 159 | virtual void GenerateSerializeWithCachedSizes(io::Printer* printer) const = 0; |
| 160 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 161 | // Generate lines to serialize this field directly to the array "target", |
| 162 | // which are placed within the message's SerializeWithCachedSizesToArray() |
| 163 | // method. This must also advance "target" past the written bytes. |
| 164 | virtual void GenerateSerializeWithCachedSizesToArray( |
| 165 | io::Printer* printer) const = 0; |
| 166 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 167 | // Generate lines to compute the serialized size of this field, which |
| 168 | // are placed in the message's ByteSize() method. |
| 169 | virtual void GenerateByteSize(io::Printer* printer) const = 0; |
| 170 | |
| 171 | private: |
| 172 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator); |
| 173 | }; |
| 174 | |
| 175 | // Convenience class which constructs FieldGenerators for a Descriptor. |
| 176 | class FieldGeneratorMap { |
| 177 | public: |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 178 | explicit FieldGeneratorMap(const Descriptor* descriptor, const Options& options); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 179 | ~FieldGeneratorMap(); |
| 180 | |
| 181 | const FieldGenerator& get(const FieldDescriptor* field) const; |
| 182 | |
| 183 | private: |
| 184 | const Descriptor* descriptor_; |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 185 | google::protobuf::scoped_array<google::protobuf::scoped_ptr<FieldGenerator> > field_generators_; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 186 | |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 187 | static FieldGenerator* MakeGenerator(const FieldDescriptor* field, |
| 188 | const Options& options); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 189 | |
| 190 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap); |
| 191 | }; |
| 192 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 193 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 194 | } // namespace cpp |
| 195 | } // namespace compiler |
| 196 | } // namespace protobuf |
| 197 | |
| 198 | } // namespace google |
| 199 | #endif // GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__ |