cpp_message.h revision d0332953cda33fb4f8e24ebff9c49159b69c43d6
1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.  All rights reserved.
3// http://code.google.com/p/protobuf/
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_MESSAGE_H__
36#define GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__
37
38#include <string>
39#include <google/protobuf/stubs/common.h>
40#include <google/protobuf/compiler/cpp/cpp_field.h>
41
42namespace google {
43namespace protobuf {
44  namespace io {
45    class Printer;             // printer.h
46  }
47}
48
49namespace protobuf {
50namespace compiler {
51namespace cpp {
52
53class EnumGenerator;           // enum.h
54class ExtensionGenerator;      // extension.h
55
56class MessageGenerator {
57 public:
58  // See generator.cc for the meaning of dllexport_decl.
59  explicit MessageGenerator(const Descriptor* descriptor,
60                            const string& dllexport_decl);
61  ~MessageGenerator();
62
63  // Header stuff.
64
65  // Generate foward declarations for this class and all its nested types.
66  void GenerateForwardDeclaration(io::Printer* printer);
67
68  // Generate definitions of all nested enums (must come before class
69  // definitions because those classes use the enums definitions).
70  void GenerateEnumDefinitions(io::Printer* printer);
71
72  // Generate specializations of GetEnumDescriptor<MyEnum>().
73  // Precondition: in ::google::protobuf namespace.
74  void GenerateGetEnumDescriptorSpecializations(io::Printer* printer);
75
76  // Generate definitions for this class and all its nested types.
77  void GenerateClassDefinition(io::Printer* printer);
78
79  // Generate definitions of inline methods (placed at the end of the header
80  // file).
81  void GenerateInlineMethods(io::Printer* printer);
82
83  // Source file stuff.
84
85  // Generate code which declares all the global descriptor pointers which
86  // will be initialized by the methods below.
87  void GenerateDescriptorDeclarations(io::Printer* printer);
88
89  // Generate code that initializes the global variable storing the message's
90  // descriptor.
91  void GenerateDescriptorInitializer(io::Printer* printer, int index);
92
93  // Generate code that calls MessageFactory::InternalRegisterGeneratedMessage()
94  // for all types.
95  void GenerateTypeRegistrations(io::Printer* printer);
96
97  // Generates code that allocates the message's default instance.
98  void GenerateDefaultInstanceAllocator(io::Printer* printer);
99
100  // Generates code that initializes the message's default instance.  This
101  // is separate from allocating because all default instances must be
102  // allocated before any can be initialized.
103  void GenerateDefaultInstanceInitializer(io::Printer* printer);
104
105  // Generates code that should be run when ShutdownProtobufLibrary() is called,
106  // to delete all dynamically-allocated objects.
107  void GenerateShutdownCode(io::Printer* printer);
108
109  // Generate all non-inline methods for this class.
110  void GenerateClassMethods(io::Printer* printer);
111
112 private:
113  // Generate declarations and definitions of accessors for fields.
114  void GenerateFieldAccessorDeclarations(io::Printer* printer);
115  void GenerateFieldAccessorDefinitions(io::Printer* printer);
116
117  // Generate the field offsets array.
118  void GenerateOffsets(io::Printer* printer);
119
120  // Generate constructors and destructor.
121  void GenerateStructors(io::Printer* printer);
122
123  // The compiler typically generates multiple copies of each constructor and
124  // destructor: http://gcc.gnu.org/bugs.html#nonbugs_cxx
125  // Placing common code in a separate method reduces the generated code size.
126  //
127  // Generate the shared constructor code.
128  void GenerateSharedConstructorCode(io::Printer* printer);
129  // Generate the shared destructor code.
130  void GenerateSharedDestructorCode(io::Printer* printer);
131
132  // Generate standard Message methods.
133  void GenerateClear(io::Printer* printer);
134  void GenerateMergeFromCodedStream(io::Printer* printer);
135  void GenerateSerializeWithCachedSizes(io::Printer* printer);
136  void GenerateSerializeWithCachedSizesToArray(io::Printer* printer);
137  void GenerateSerializeWithCachedSizesBody(io::Printer* printer,
138                                            bool to_array);
139  void GenerateByteSize(io::Printer* printer);
140  void GenerateMergeFrom(io::Printer* printer);
141  void GenerateCopyFrom(io::Printer* printer);
142  void GenerateSwap(io::Printer* printer);
143  void GenerateIsInitialized(io::Printer* printer);
144
145  // Helpers for GenerateSerializeWithCachedSizes().
146  void GenerateSerializeOneField(io::Printer* printer,
147                                 const FieldDescriptor* field,
148                                 bool unbounded);
149  void GenerateSerializeOneExtensionRange(
150      io::Printer* printer, const Descriptor::ExtensionRange* range,
151      bool unbounded);
152
153
154  const Descriptor* descriptor_;
155  string classname_;
156  string dllexport_decl_;
157  FieldGeneratorMap field_generators_;
158  scoped_array<scoped_ptr<MessageGenerator> > nested_generators_;
159  scoped_array<scoped_ptr<EnumGenerator> > enum_generators_;
160  scoped_array<scoped_ptr<ExtensionGenerator> > extension_generators_;
161
162  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator);
163};
164
165}  // namespace cpp
166}  // namespace compiler
167}  // namespace protobuf
168
169}  // namespace google
170#endif  // GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__
171