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