1fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Protocol Buffers - Google's data interchange format
2fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Copyright 2008 Google Inc.  All rights reserved.
3fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// http://code.google.com/p/protobuf/
4fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
5fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Redistribution and use in source and binary forms, with or without
6fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// modification, are permitted provided that the following conditions are
7fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// met:
8fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
9fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Redistributions of source code must retain the above copyright
10fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// notice, this list of conditions and the following disclaimer.
11fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Redistributions in binary form must reproduce the above
12fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// copyright notice, this list of conditions and the following disclaimer
13fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// in the documentation and/or other materials provided with the
14fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// distribution.
15fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Neither the name of Google Inc. nor the names of its
16fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// contributors may be used to endorse or promote products derived from
17fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// this software without specific prior written permission.
18fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
19fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
31fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Author: kenton@google.com (Kenton Varda)
32fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//  Based on original Protocol Buffers design by
33fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//  Sanjay Ghemawat, Jeff Dean, and others.
34fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
35fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <google/protobuf/compiler/java/java_field.h>
36fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <google/protobuf/compiler/java/java_helpers.h>
37fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <google/protobuf/compiler/java/java_primitive_field.h>
38fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <google/protobuf/compiler/java/java_enum_field.h>
39fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <google/protobuf/compiler/java/java_message_field.h>
40fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <google/protobuf/stubs/common.h>
41fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
42fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace google {
43fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace protobuf {
44fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace compiler {
45fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace java {
46fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
47fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleFieldGenerator::~FieldGenerator() {}
48fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
49d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savillevoid FieldGenerator::GenerateParsingCodeFromPacked(io::Printer* printer) const {
50d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // Reaching here indicates a bug. Cases are:
51d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  //   - This FieldGenerator should support packing, but this method should be
52d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  //     overridden.
53d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  //   - This FieldGenerator doesn't support packing, and this method should
54d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  //     never have been called.
55d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  GOOGLE_LOG(FATAL) << "GenerateParsingCodeFromPacked() "
56d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville             << "called on field generator that does not support packing.";
57d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville}
58d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
59fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleFieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor)
60fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  : descriptor_(descriptor),
61fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    field_generators_(
62fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      new scoped_ptr<FieldGenerator>[descriptor->field_count()]),
63fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    extension_generators_(
64fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      new scoped_ptr<FieldGenerator>[descriptor->extension_count()]) {
65fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
66fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Construct all the FieldGenerators.
67fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  for (int i = 0; i < descriptor->field_count(); i++) {
68fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    field_generators_[i].reset(MakeGenerator(descriptor->field(i)));
69fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
70fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  for (int i = 0; i < descriptor->extension_count(); i++) {
71fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    extension_generators_[i].reset(MakeGenerator(descriptor->extension(i)));
72fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
73fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
74fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
75fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleFieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field) {
76fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  if (field->is_repeated()) {
77fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    switch (GetJavaType(field)) {
78fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      case JAVATYPE_MESSAGE:
79fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        return new RepeatedMessageFieldGenerator(field);
80fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      case JAVATYPE_ENUM:
81fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        return new RepeatedEnumFieldGenerator(field);
82fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      default:
83fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        return new RepeatedPrimitiveFieldGenerator(field);
84fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    }
85fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  } else {
86fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    switch (GetJavaType(field)) {
87fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      case JAVATYPE_MESSAGE:
88fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        return new MessageFieldGenerator(field);
89fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      case JAVATYPE_ENUM:
90fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        return new EnumFieldGenerator(field);
91fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      default:
92fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        return new PrimitiveFieldGenerator(field);
93fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    }
94fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
95fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
96fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
97fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleFieldGeneratorMap::~FieldGeneratorMap() {}
98fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
99fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst FieldGenerator& FieldGeneratorMap::get(
100fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    const FieldDescriptor* field) const {
101fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  GOOGLE_CHECK_EQ(field->containing_type(), descriptor_);
102fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return *field_generators_[field->index()];
103fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
104fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
105fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst FieldGenerator& FieldGeneratorMap::get_extension(int index) const {
106fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return *extension_generators_[index];
107fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
108fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
109fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}  // namespace java
110fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}  // namespace compiler
111fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}  // namespace protobuf
112fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}  // namespace google
113