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#ifndef GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__ 32#define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__ 33 34#include <map> 35#include <string> 36#include <google/protobuf/compiler/objectivec/objectivec_helpers.h> 37#include <google/protobuf/stubs/common.h> 38#include <google/protobuf/descriptor.h> 39 40namespace google { 41namespace protobuf { 42 43namespace io { 44class Printer; // printer.h 45} // namespace io 46 47namespace compiler { 48namespace objectivec { 49 50class FieldGenerator { 51 public: 52 static FieldGenerator* Make(const FieldDescriptor* field, 53 const Options& options); 54 55 virtual ~FieldGenerator(); 56 57 // Exposed for subclasses to fill in. 58 virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const = 0; 59 virtual void GeneratePropertyDeclaration(io::Printer* printer) const = 0; 60 virtual void GeneratePropertyImplementation(io::Printer* printer) const = 0; 61 62 // Called by GenerateFieldDescription, exposed for classes that need custom 63 // generation. 64 65 // Exposed for subclasses to extend, base does nothing. 66 virtual void GenerateCFunctionDeclarations(io::Printer* printer) const; 67 virtual void GenerateCFunctionImplementations(io::Printer* printer) const; 68 69 // Exposed for subclasses, should always call it on the parent class also. 70 virtual void DetermineForwardDeclarations(set<string>* fwd_decls) const; 71 72 // Used during generation, not intended to be extended by subclasses. 73 void GenerateFieldDescription( 74 io::Printer* printer, bool include_default) const; 75 void GenerateFieldNumberConstant(io::Printer* printer) const; 76 77 // Exposed to get and set the has bits information. 78 virtual bool RuntimeUsesHasBit(void) const = 0; 79 void SetRuntimeHasBit(int has_index); 80 void SetNoHasBit(void); 81 virtual int ExtraRuntimeHasBitsNeeded(void) const; 82 virtual void SetExtraRuntimeHasBitsBase(int index_base); 83 void SetOneofIndexBase(int index_base); 84 85 string variable(const char* key) const { 86 return variables_.find(key)->second; 87 } 88 89 bool needs_textformat_name_support() const { 90 const string& field_flags = variable("fieldflags"); 91 return field_flags.find("GPBFieldTextFormatNameCustom") != string::npos; 92 } 93 string generated_objc_name() const { return variable("name"); } 94 string raw_field_name() const { return variable("raw_field_name"); } 95 96 protected: 97 FieldGenerator(const FieldDescriptor* descriptor, const Options& options); 98 99 virtual void FinishInitialization(void); 100 virtual bool WantsHasProperty(void) const = 0; 101 102 const FieldDescriptor* descriptor_; 103 map<string, string> variables_; 104 105 private: 106 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator); 107}; 108 109class SingleFieldGenerator : public FieldGenerator { 110 public: 111 virtual ~SingleFieldGenerator(); 112 113 virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const; 114 virtual void GeneratePropertyDeclaration(io::Printer* printer) const; 115 116 virtual void GeneratePropertyImplementation(io::Printer* printer) const; 117 118 virtual bool RuntimeUsesHasBit(void) const; 119 120 protected: 121 SingleFieldGenerator(const FieldDescriptor* descriptor, 122 const Options& options); 123 virtual bool WantsHasProperty(void) const; 124 125 private: 126 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SingleFieldGenerator); 127}; 128 129// Subclass with common support for when the field ends up as an ObjC Object. 130class ObjCObjFieldGenerator : public SingleFieldGenerator { 131 public: 132 virtual ~ObjCObjFieldGenerator(); 133 134 virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const; 135 virtual void GeneratePropertyDeclaration(io::Printer* printer) const; 136 137 protected: 138 ObjCObjFieldGenerator(const FieldDescriptor* descriptor, 139 const Options& options); 140 141 private: 142 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ObjCObjFieldGenerator); 143}; 144 145class RepeatedFieldGenerator : public ObjCObjFieldGenerator { 146 public: 147 virtual ~RepeatedFieldGenerator(); 148 149 virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const; 150 virtual void GeneratePropertyDeclaration(io::Printer* printer) const; 151 152 virtual void GeneratePropertyImplementation(io::Printer* printer) const; 153 154 virtual bool RuntimeUsesHasBit(void) const; 155 156 protected: 157 RepeatedFieldGenerator(const FieldDescriptor* descriptor, 158 const Options& options); 159 virtual void FinishInitialization(void); 160 virtual bool WantsHasProperty(void) const; 161 162 private: 163 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedFieldGenerator); 164}; 165 166// Convenience class which constructs FieldGenerators for a Descriptor. 167class FieldGeneratorMap { 168 public: 169 FieldGeneratorMap(const Descriptor* descriptor, const Options& options); 170 ~FieldGeneratorMap(); 171 172 const FieldGenerator& get(const FieldDescriptor* field) const; 173 const FieldGenerator& get_extension(int index) const; 174 175 // Assigns the has bits and returns the number of bits needed. 176 int CalculateHasBits(void); 177 178 void SetOneofIndexBase(int index_base); 179 180 // Check if any field of this message has a non zero default. 181 bool DoesAnyFieldHaveNonZeroDefault(void) const; 182 183 private: 184 const Descriptor* descriptor_; 185 scoped_array<scoped_ptr<FieldGenerator> > field_generators_; 186 scoped_array<scoped_ptr<FieldGenerator> > extension_generators_; 187 188 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap); 189}; 190} // namespace objectivec 191} // namespace compiler 192} // namespace protobuf 193} // namespace google 194#endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__ 195