tizen 2.3.1 release
[external/protobuf.git] / src / google / protobuf / compiler / cpp / cpp_field.cc
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 // 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/cpp/cpp_field.h>
36 #include <memory>
37
38 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
39 #include <google/protobuf/compiler/cpp/cpp_primitive_field.h>
40 #include <google/protobuf/compiler/cpp/cpp_string_field.h>
41 #include <google/protobuf/compiler/cpp/cpp_enum_field.h>
42 #include <google/protobuf/compiler/cpp/cpp_message_field.h>
43 #include <google/protobuf/descriptor.pb.h>
44 #include <google/protobuf/wire_format.h>
45 #include <google/protobuf/io/printer.h>
46 #include <google/protobuf/stubs/common.h>
47 #include <google/protobuf/stubs/strutil.h>
48
49 namespace google {
50 namespace protobuf {
51 namespace compiler {
52 namespace cpp {
53
54 using internal::WireFormat;
55
56 void SetCommonFieldVariables(const FieldDescriptor* descriptor,
57                              map<string, string>* variables,
58                              const Options& options) {
59   (*variables)["name"] = FieldName(descriptor);
60   (*variables)["index"] = SimpleItoa(descriptor->index());
61   (*variables)["number"] = SimpleItoa(descriptor->number());
62   (*variables)["classname"] = ClassName(FieldScope(descriptor), false);
63   (*variables)["declared_type"] = DeclaredTypeMethodName(descriptor->type());
64
65   (*variables)["tag_size"] = SimpleItoa(
66     WireFormat::TagSize(descriptor->number(), descriptor->type()));
67   (*variables)["deprecation"] = descriptor->options().deprecated()
68       ? " PROTOBUF_DEPRECATED" : "";
69
70   (*variables)["cppget"] = "Get";
71 }
72
73 void SetCommonOneofFieldVariables(const FieldDescriptor* descriptor,
74                                   map<string, string>* variables) {
75   (*variables)["oneof_prefix"] = descriptor->containing_oneof()->name() + "_.";
76   (*variables)["oneof_name"] = descriptor->containing_oneof()->name();
77 }
78
79 FieldGenerator::~FieldGenerator() {}
80
81 void FieldGenerator::
82 GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const {
83   // Reaching here indicates a bug. Cases are:
84   //   - This FieldGenerator should support packing, but this method should be
85   //     overridden.
86   //   - This FieldGenerator doesn't support packing, and this method should
87   //     never have been called.
88   GOOGLE_LOG(FATAL) << "GenerateMergeFromCodedStreamWithPacking() "
89              << "called on field generator that does not support packing.";
90
91 }
92
93 FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor,
94                                      const Options& options)
95     : descriptor_(descriptor),
96       field_generators_(
97           new scoped_ptr<FieldGenerator>[descriptor->field_count()]) {
98   // Construct all the FieldGenerators.
99   for (int i = 0; i < descriptor->field_count(); i++) {
100     field_generators_[i].reset(MakeGenerator(descriptor->field(i), options));
101   }
102 }
103
104 FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field,
105                                                  const Options& options) {
106   if (field->is_repeated()) {
107     switch (field->cpp_type()) {
108       case FieldDescriptor::CPPTYPE_MESSAGE:
109         return new RepeatedMessageFieldGenerator(field, options);
110       case FieldDescriptor::CPPTYPE_STRING:
111         switch (field->options().ctype()) {
112           default:  // RepeatedStringFieldGenerator handles unknown ctypes.
113           case FieldOptions::STRING:
114             return new RepeatedStringFieldGenerator(field, options);
115         }
116       case FieldDescriptor::CPPTYPE_ENUM:
117         return new RepeatedEnumFieldGenerator(field, options);
118       default:
119         return new RepeatedPrimitiveFieldGenerator(field, options);
120     }
121   } else if (field->containing_oneof()) {
122     switch (field->cpp_type()) {
123       case FieldDescriptor::CPPTYPE_MESSAGE:
124         return new MessageOneofFieldGenerator(field, options);
125       case FieldDescriptor::CPPTYPE_STRING:
126         switch (field->options().ctype()) {
127           default:  // StringOneofFieldGenerator handles unknown ctypes.
128           case FieldOptions::STRING:
129             return new StringOneofFieldGenerator(field, options);
130         }
131       case FieldDescriptor::CPPTYPE_ENUM:
132         return new EnumOneofFieldGenerator(field, options);
133       default:
134         return new PrimitiveOneofFieldGenerator(field, options);
135     }
136   } else {
137     switch (field->cpp_type()) {
138       case FieldDescriptor::CPPTYPE_MESSAGE:
139         return new MessageFieldGenerator(field, options);
140       case FieldDescriptor::CPPTYPE_STRING:
141         switch (field->options().ctype()) {
142           default:  // StringFieldGenerator handles unknown ctypes.
143           case FieldOptions::STRING:
144             return new StringFieldGenerator(field, options);
145         }
146       case FieldDescriptor::CPPTYPE_ENUM:
147         return new EnumFieldGenerator(field, options);
148       default:
149         return new PrimitiveFieldGenerator(field, options);
150     }
151   }
152 }
153
154 FieldGeneratorMap::~FieldGeneratorMap() {}
155
156 const FieldGenerator& FieldGeneratorMap::get(
157     const FieldDescriptor* field) const {
158   GOOGLE_CHECK_EQ(field->containing_type(), descriptor_);
159   return *field_generators_[field->index()];
160 }
161
162
163 }  // namespace cpp
164 }  // namespace compiler
165 }  // namespace protobuf
166 }  // namespace google