1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
35 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
36 #define GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
41 #include <google/protobuf/stubs/common.h>
42 #include <google/protobuf/descriptor.h>
43 #include <google/protobuf/compiler/cpp/cpp_options.h>
48 class Printer; // printer.h
56 // Helper function: set variables in the map that are the same for all
57 // field code generators.
58 // ['name', 'index', 'number', 'classname', 'declared_type', 'tag_size',
60 void SetCommonFieldVariables(const FieldDescriptor* descriptor,
61 map<string, string>* variables,
62 const Options& options);
64 class FieldGenerator {
67 virtual ~FieldGenerator();
69 // Generate lines of code declaring members fields of the message class
70 // needed to represent this field. These are placed inside the message
72 virtual void GeneratePrivateMembers(io::Printer* printer) const = 0;
74 // Generate prototypes for all of the accessor functions related to this
75 // field. These are placed inside the class definition.
76 virtual void GenerateAccessorDeclarations(io::Printer* printer) const = 0;
78 // Generate inline definitions of accessor functions for this field.
79 // These are placed inside the header after all class definitions.
80 virtual void GenerateInlineAccessorDefinitions(
81 io::Printer* printer) const = 0;
83 // Generate definitions of accessors that aren't inlined. These are
84 // placed somewhere in the .cc file.
85 // Most field types don't need this, so the default implementation is empty.
86 virtual void GenerateNonInlineAccessorDefinitions(
87 io::Printer* printer) const {}
89 // Generate lines of code (statements, not declarations) which clear the
90 // field. This is used to define the clear_$name$() method as well as
91 // the Clear() method for the whole message.
92 virtual void GenerateClearingCode(io::Printer* printer) const = 0;
94 // Generate lines of code (statements, not declarations) which merges the
95 // contents of the field from the current message to the target message,
96 // which is stored in the generated code variable "from".
97 // 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
99 // GenerateMergeFrom method.
100 virtual void GenerateMergingCode(io::Printer* printer) const = 0;
102 // Generate lines of code (statements, not declarations) which swaps
103 // this field and the corresponding field of another message, which
104 // is stored in the generated code variable "other". This is used to
105 // define the Swap method. Details of usage can be found in
106 // message.cc under the GenerateSwap method.
107 virtual void GenerateSwappingCode(io::Printer* printer) const = 0;
109 // Generate initialization code for private members declared by
110 // GeneratePrivateMembers(). These go into the message class's SharedCtor()
111 // method, invoked by each of the generated constructors.
112 virtual void GenerateConstructorCode(io::Printer* printer) const = 0;
114 // Generate any code that needs to go in the class's SharedDtor() method,
115 // invoked by the destructor.
116 // Most field types don't need this, so the default implementation is empty.
117 virtual void GenerateDestructorCode(io::Printer* printer) const {}
119 // Generate code that allocates the fields's default instance.
120 virtual void GenerateDefaultInstanceAllocator(io::Printer* printer) const {}
122 // Generate code that should be run when ShutdownProtobufLibrary() is called,
123 // to delete all dynamically-allocated objects.
124 virtual void GenerateShutdownCode(io::Printer* printer) const {}
126 // Generate lines to decode this field, which will be placed inside the
127 // message's MergeFromCodedStream() method.
128 virtual void GenerateMergeFromCodedStream(io::Printer* printer) const = 0;
130 // Generate lines to decode this field from a packed value, which will be
131 // placed inside the message's MergeFromCodedStream() method.
132 virtual void GenerateMergeFromCodedStreamWithPacking(io::Printer* printer)
135 // Generate lines to serialize this field, which are placed within the
136 // message's SerializeWithCachedSizes() method.
137 virtual void GenerateSerializeWithCachedSizes(io::Printer* printer) const = 0;
139 // Generate lines to serialize this field directly to the array "target",
140 // which are placed within the message's SerializeWithCachedSizesToArray()
141 // method. This must also advance "target" past the written bytes.
142 virtual void GenerateSerializeWithCachedSizesToArray(
143 io::Printer* printer) const = 0;
145 // Generate lines to compute the serialized size of this field, which
146 // are placed in the message's ByteSize() method.
147 virtual void GenerateByteSize(io::Printer* printer) const = 0;
150 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator);
153 // Convenience class which constructs FieldGenerators for a Descriptor.
154 class FieldGeneratorMap {
156 explicit FieldGeneratorMap(const Descriptor* descriptor, const Options& options);
157 ~FieldGeneratorMap();
159 const FieldGenerator& get(const FieldDescriptor* field) const;
162 const Descriptor* descriptor_;
163 scoped_array<scoped_ptr<FieldGenerator> > field_generators_;
165 static FieldGenerator* MakeGenerator(const FieldDescriptor* field,
166 const Options& options);
168 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap);
173 } // namespace compiler
174 } // namespace protobuf
176 } // namespace google
177 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__