tizen 2.3.1 release
[external/protobuf.git] / src / google / protobuf / compiler / cpp / cpp_field.h
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 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 // 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
38 #include <map>
39 #include <memory>
40 #include <string>
41
42 #include <google/protobuf/descriptor.h>
43 #include <google/protobuf/compiler/cpp/cpp_options.h>
44
45 namespace google {
46 namespace protobuf {
47   namespace io {
48     class Printer;             // printer.h
49   }
50 }
51
52 namespace protobuf {
53 namespace compiler {
54 namespace cpp {
55
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',
59 // 'deprecation'].
60 void SetCommonFieldVariables(const FieldDescriptor* descriptor,
61                              map<string, string>* variables,
62                              const Options& options);
63
64 void SetCommonOneofFieldVariables(const FieldDescriptor* descriptor,
65                                   map<string, string>* variables);
66
67 class FieldGenerator {
68  public:
69   FieldGenerator() {}
70   virtual ~FieldGenerator();
71
72   // Generate lines of code declaring members fields of the message class
73   // needed to represent this field.  These are placed inside the message
74   // class.
75   virtual void GeneratePrivateMembers(io::Printer* printer) const = 0;
76
77   // Generate static default variable for this field. These are placed inside
78   // the message class. Most field types don't need this, so the default
79   // implementation is empty.
80   virtual void GenerateStaticMembers(io::Printer* printer) const {}
81
82   // Generate prototypes for all of the accessor functions related to this
83   // field.  These are placed inside the class definition.
84   virtual void GenerateAccessorDeclarations(io::Printer* printer) const = 0;
85
86   // Generate inline definitions of accessor functions for this field.
87   // These are placed inside the header after all class definitions.
88   virtual void GenerateInlineAccessorDefinitions(
89     io::Printer* printer) const = 0;
90
91   // Generate definitions of accessors that aren't inlined.  These are
92   // placed somewhere in the .cc file.
93   // Most field types don't need this, so the default implementation is empty.
94   virtual void GenerateNonInlineAccessorDefinitions(
95     io::Printer* printer) const {}
96
97   // Generate lines of code (statements, not declarations) which clear the
98   // field.  This is used to define the clear_$name$() method as well as
99   // the Clear() method for the whole message.
100   virtual void GenerateClearingCode(io::Printer* printer) const = 0;
101
102   // Generate lines of code (statements, not declarations) which merges the
103   // contents of the field from the current message to the target message,
104   // which is stored in the generated code variable "from".
105   // This is used to fill in the MergeFrom method for the whole message.
106   // Details of this usage can be found in message.cc under the
107   // GenerateMergeFrom method.
108   virtual void GenerateMergingCode(io::Printer* printer) const = 0;
109
110   // Generate lines of code (statements, not declarations) which swaps
111   // this field and the corresponding field of another message, which
112   // is stored in the generated code variable "other". This is used to
113   // define the Swap method. Details of usage can be found in
114   // message.cc under the GenerateSwap method.
115   virtual void GenerateSwappingCode(io::Printer* printer) const = 0;
116
117   // Generate initialization code for private members declared by
118   // GeneratePrivateMembers(). These go into the message class's SharedCtor()
119   // method, invoked by each of the generated constructors.
120   virtual void GenerateConstructorCode(io::Printer* printer) const = 0;
121
122   // Generate any code that needs to go in the class's SharedDtor() method,
123   // invoked by the destructor.
124   // Most field types don't need this, so the default implementation is empty.
125   virtual void GenerateDestructorCode(io::Printer* printer) const {}
126
127   // Generate code that allocates the fields's default instance.
128   virtual void GenerateDefaultInstanceAllocator(io::Printer* printer) const {}
129
130   // Generate code that should be run when ShutdownProtobufLibrary() is called,
131   // to delete all dynamically-allocated objects.
132   virtual void GenerateShutdownCode(io::Printer* printer) const {}
133
134   // Generate lines to decode this field, which will be placed inside the
135   // message's MergeFromCodedStream() method.
136   virtual void GenerateMergeFromCodedStream(io::Printer* printer) const = 0;
137
138   // Generate lines to decode this field from a packed value, which will be
139   // placed inside the message's MergeFromCodedStream() method.
140   virtual void GenerateMergeFromCodedStreamWithPacking(io::Printer* printer)
141       const;
142
143   // Generate lines to serialize this field, which are placed within the
144   // message's SerializeWithCachedSizes() method.
145   virtual void GenerateSerializeWithCachedSizes(io::Printer* printer) const = 0;
146
147   // Generate lines to serialize this field directly to the array "target",
148   // which are placed within the message's SerializeWithCachedSizesToArray()
149   // method. This must also advance "target" past the written bytes.
150   virtual void GenerateSerializeWithCachedSizesToArray(
151       io::Printer* printer) const = 0;
152
153   // Generate lines to compute the serialized size of this field, which
154   // are placed in the message's ByteSize() method.
155   virtual void GenerateByteSize(io::Printer* printer) const = 0;
156
157  private:
158   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator);
159 };
160
161 // Convenience class which constructs FieldGenerators for a Descriptor.
162 class FieldGeneratorMap {
163  public:
164   explicit FieldGeneratorMap(const Descriptor* descriptor, const Options& options);
165   ~FieldGeneratorMap();
166
167   const FieldGenerator& get(const FieldDescriptor* field) const;
168
169  private:
170   const Descriptor* descriptor_;
171   scoped_array<scoped_ptr<FieldGenerator> > field_generators_;
172
173   static FieldGenerator* MakeGenerator(const FieldDescriptor* field,
174                                        const Options& options);
175
176   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap);
177 };
178
179
180 }  // namespace cpp
181 }  // namespace compiler
182 }  // namespace protobuf
183
184 }  // namespace google
185 #endif  // GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__