javanano_field.cc revision e74fe623e115237968a3de1143d7cdb4df710858
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#include <google/protobuf/compiler/javanano/javanano_field.h> 36#include <google/protobuf/compiler/javanano/javanano_helpers.h> 37#include <google/protobuf/compiler/javanano/javanano_primitive_field.h> 38#include <google/protobuf/compiler/javanano/javanano_enum_field.h> 39#include <google/protobuf/compiler/javanano/javanano_message_field.h> 40#include <google/protobuf/stubs/common.h> 41 42namespace google { 43namespace protobuf { 44namespace compiler { 45namespace javanano { 46 47FieldGenerator::~FieldGenerator() {} 48 49FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor, const Params ¶ms) 50 : descriptor_(descriptor), 51 field_generators_( 52 new scoped_ptr<FieldGenerator>[descriptor->field_count()]), 53 extension_generators_( 54 new scoped_ptr<FieldGenerator>[descriptor->extension_count()]) { 55 56 int next_has_bit_index = 0; 57 // Construct all the FieldGenerators. 58 for (int i = 0; i < descriptor->field_count(); i++) { 59 field_generators_[i].reset( 60 MakeGenerator(descriptor->field(i), params, &next_has_bit_index)); 61 } 62 for (int i = 0; i < descriptor->extension_count(); i++) { 63 extension_generators_[i].reset( 64 MakeGenerator(descriptor->extension(i), params, &next_has_bit_index)); 65 } 66 total_bits_ = next_has_bit_index; 67} 68 69FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field, 70 const Params ¶ms, int* next_has_bit_index) { 71 if (field->is_repeated()) { 72 switch (GetJavaType(field)) { 73 case JAVATYPE_MESSAGE: 74 return new RepeatedMessageFieldGenerator(field, params); 75 case JAVATYPE_ENUM: 76 return new RepeatedEnumFieldGenerator(field, params); 77 default: 78 return new RepeatedPrimitiveFieldGenerator(field, params); 79 } 80 } else if (params.optional_field_accessors() && field->is_optional()) { 81 // We need a has-bit for each primitive/enum field because their default 82 // values could be same as explicitly set values. But we don't need it 83 // for a message field because they have no defaults and Nano uses 'null' 84 // for unset messages, which cannot be set explicitly. 85 switch (GetJavaType(field)) { 86 case JAVATYPE_MESSAGE: 87 return new AccessorMessageFieldGenerator(field, params); 88 case JAVATYPE_ENUM: 89 return new AccessorEnumFieldGenerator( 90 field, params, (*next_has_bit_index)++); 91 default: 92 return new AccessorPrimitiveFieldGenerator( 93 field, params, (*next_has_bit_index)++); 94 } 95 } else { 96 switch (GetJavaType(field)) { 97 case JAVATYPE_MESSAGE: 98 return new MessageFieldGenerator(field, params); 99 case JAVATYPE_ENUM: 100 return new EnumFieldGenerator(field, params); 101 default: 102 return new PrimitiveFieldGenerator(field, params); 103 } 104 } 105} 106 107FieldGeneratorMap::~FieldGeneratorMap() {} 108 109const FieldGenerator& FieldGeneratorMap::get( 110 const FieldDescriptor* field) const { 111 GOOGLE_CHECK_EQ(field->containing_type(), descriptor_); 112 return *field_generators_[field->index()]; 113} 114 115const FieldGenerator& FieldGeneratorMap::get_extension(int index) const { 116 return *extension_generators_[index]; 117} 118 119} // namespace javanano 120} // namespace compiler 121} // namespace protobuf 122} // namespace google 123