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/javamicro/javamicro_field.h>
36 #include <google/protobuf/compiler/javamicro/javamicro_helpers.h>
37 #include <google/protobuf/compiler/javamicro/javamicro_primitive_field.h>
38 #include <google/protobuf/compiler/javamicro/javamicro_enum_field.h>
39 #include <google/protobuf/compiler/javamicro/javamicro_message_field.h>
40 #include <google/protobuf/stubs/common.h>
41
42 namespace google {
43 namespace protobuf {
44 namespace compiler {
45 namespace javamicro {
46
~FieldGenerator()47 FieldGenerator::~FieldGenerator() {}
48
FieldGeneratorMap(const Descriptor * descriptor,const Params & params)49 FieldGeneratorMap::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 // Construct all the FieldGenerators.
57 for (int i = 0; i < descriptor->field_count(); i++) {
58 field_generators_[i].reset(MakeGenerator(descriptor->field(i), params));
59 }
60 for (int i = 0; i < descriptor->extension_count(); i++) {
61 extension_generators_[i].reset(MakeGenerator(descriptor->extension(i), params));
62 }
63 }
64
MakeGenerator(const FieldDescriptor * field,const Params & params)65 FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field, const Params ¶ms) {
66 if (field->is_repeated()) {
67 switch (GetJavaType(field)) {
68 case JAVATYPE_MESSAGE:
69 return new RepeatedMessageFieldGenerator(field, params);
70 case JAVATYPE_ENUM:
71 return new RepeatedEnumFieldGenerator(field, params);
72 default:
73 return new RepeatedPrimitiveFieldGenerator(field, params);
74 }
75 } else {
76 switch (GetJavaType(field)) {
77 case JAVATYPE_MESSAGE:
78 return new MessageFieldGenerator(field, params);
79 case JAVATYPE_ENUM:
80 return new EnumFieldGenerator(field, params);
81 default:
82 return new PrimitiveFieldGenerator(field, params);
83 }
84 }
85 }
86
~FieldGeneratorMap()87 FieldGeneratorMap::~FieldGeneratorMap() {}
88
get(const FieldDescriptor * field) const89 const FieldGenerator& FieldGeneratorMap::get(
90 const FieldDescriptor* field) const {
91 GOOGLE_CHECK_EQ(field->containing_type(), descriptor_);
92 return *field_generators_[field->index()];
93 }
94
get_extension(int index) const95 const FieldGenerator& FieldGeneratorMap::get_extension(int index) const {
96 return *extension_generators_[index];
97 }
98
99 } // namespace javamicro
100 } // namespace compiler
101 } // namespace protobuf
102 } // namespace google
103