objectivec_map_field.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2015 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 #include <map>
32 #include <string>
33 
37 
38 namespace google {
39 namespace protobuf {
40 namespace compiler {
41 namespace objectivec {
42 
43 // MapFieldGenerator uses RepeatedFieldGenerator as the parent because it
44 // provides a bunch of things (no has* methods, comments for contained type,
45 // etc.).
46 
47 namespace {
48 
49 const char* MapEntryTypeName(const FieldDescriptor* descriptor, bool isKey) {
51  switch (type) {
53  return "Int32";
55  return "UInt32";
57  return "Int64";
59  return "UInt64";
61  return "Float";
63  return "Double";
65  return "Bool";
67  return (isKey ? "String" : "Object");
69  return "Object";
71  return "Enum";
73  return "Object";
74  }
75 
76  // Some compilers report reaching end of function even though all cases of
77  // the enum are handed in the switch.
78  GOOGLE_LOG(FATAL) << "Can't get here.";
79  return NULL;
80 }
81 
82 } // namespace
83 
85  const Options& options)
87  const FieldDescriptor* key_descriptor =
88  descriptor->message_type()->FindFieldByName("key");
89  const FieldDescriptor* value_descriptor =
90  descriptor->message_type()->FindFieldByName("value");
91  value_field_generator_.reset(FieldGenerator::Make(value_descriptor, options));
92 
93  // Pull over some variables_ from the value.
94  variables_["field_type"] = value_field_generator_->variable("field_type");
95  variables_["default"] = value_field_generator_->variable("default");
96  variables_["default_name"] = value_field_generator_->variable("default_name");
97 
98  // Build custom field flags.
99  std::vector<string> field_flags;
100  field_flags.push_back("GPBFieldMapKey" + GetCapitalizedType(key_descriptor));
101  // Pull over the current text format custom name values that was calculated.
102  if (variables_["fieldflags"].find("GPBFieldTextFormatNameCustom") !=
103  string::npos) {
104  field_flags.push_back("GPBFieldTextFormatNameCustom");
105  }
106  // Pull over some info from the value's flags.
107  const string& value_field_flags =
108  value_field_generator_->variable("fieldflags");
109  if (value_field_flags.find("GPBFieldHasDefaultValue") != string::npos) {
110  field_flags.push_back("GPBFieldHasDefaultValue");
111  }
112  if (value_field_flags.find("GPBFieldHasEnumDescriptor") != string::npos) {
113  field_flags.push_back("GPBFieldHasEnumDescriptor");
114  }
115  variables_["fieldflags"] = BuildFlagsString(FLAGTYPE_FIELD, field_flags);
116 
117  ObjectiveCType value_objc_type = GetObjectiveCType(value_descriptor);
118  const bool value_is_object_type =
119  ((value_objc_type == OBJECTIVECTYPE_STRING) ||
120  (value_objc_type == OBJECTIVECTYPE_DATA) ||
121  (value_objc_type == OBJECTIVECTYPE_MESSAGE));
122  if ((GetObjectiveCType(key_descriptor) == OBJECTIVECTYPE_STRING) &&
123  value_is_object_type) {
124  variables_["array_storage_type"] = "NSMutableDictionary";
125  variables_["array_property_type"] =
126  "NSMutableDictionary<NSString*, " +
127  value_field_generator_->variable("storage_type") + "*>";
128  } else {
129  string class_name("GPB");
130  class_name += MapEntryTypeName(key_descriptor, true);
131  class_name += MapEntryTypeName(value_descriptor, false);
132  class_name += "Dictionary";
133  variables_["array_storage_type"] = class_name;
134  if (value_is_object_type) {
135  variables_["array_property_type"] =
136  class_name + "<" +
137  value_field_generator_->variable("storage_type") + "*>";
138  }
139  }
140 
141  variables_["dataTypeSpecific_name"] =
142  value_field_generator_->variable("dataTypeSpecific_name");
143  variables_["dataTypeSpecific_value"] =
144  value_field_generator_->variable("dataTypeSpecific_value");
145 }
146 
148 
151  // Use the array_comment support in RepeatedFieldGenerator to output what the
152  // values in the map are.
153  const FieldDescriptor* value_descriptor =
155  if (GetObjectiveCType(value_descriptor) == OBJECTIVECTYPE_ENUM) {
156  variables_["array_comment"] =
157  "// |" + variables_["name"] + "| values are |" + value_field_generator_->variable("storage_type") + "|\n";
158  }
159 }
160 
162  std::set<string>* fwd_decls) const {
164  const FieldDescriptor* value_descriptor =
166  if (GetObjectiveCType(value_descriptor) == OBJECTIVECTYPE_MESSAGE) {
167  const string& value_storage_type =
168  value_field_generator_->variable("storage_type");
169  fwd_decls->insert("@class " + value_storage_type);
170  }
171 }
172 
173 
174 } // namespace objectivec
175 } // namespace compiler
176 } // namespace protobuf
177 } // namespace google
google::protobuf::compiler::objectivec::MapFieldGenerator::DetermineForwardDeclarations
virtual void DetermineForwardDeclarations(std::set< string > *fwd_decls) const
Definition: objectivec_map_field.cc:161
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
google::protobuf::compiler::objectivec::OBJECTIVECTYPE_INT32
@ OBJECTIVECTYPE_INT32
Definition: objectivec_helpers.h:138
google::protobuf::compiler::objectivec::OBJECTIVECTYPE_UINT32
@ OBJECTIVECTYPE_UINT32
Definition: objectivec_helpers.h:139
NULL
NULL
Definition: test_security_zap.cpp:405
google::protobuf::compiler::objectivec::OBJECTIVECTYPE_STRING
@ OBJECTIVECTYPE_STRING
Definition: objectivec_helpers.h:145
google::protobuf::compiler::objectivec::MapFieldGenerator::MapFieldGenerator
MapFieldGenerator(const MapFieldGenerator &)=delete
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
FATAL
const int FATAL
Definition: log_severity.h:60
google::protobuf::compiler::objectivec::Options
Definition: objectivec_helpers.h:50
objectivec_helpers.h
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::compiler::objectivec::RepeatedFieldGenerator::FinishInitialization
virtual void FinishInitialization(void)
Definition: objectivec_field.cc:358
FieldDescriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:129
google::protobuf::compiler::objectivec::BuildFlagsString
string BuildFlagsString(const FlagType flag_type, const std::vector< string > &strings)
Definition: objectivec_helpers.cc:910
printer.h
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: logging.h:146
google::protobuf::compiler::objectivec::RepeatedFieldGenerator
Definition: objectivec_field.h:140
google::protobuf::compiler::objectivec::FieldGenerator::Make
static FieldGenerator * Make(const FieldDescriptor *field, const Options &options)
Definition: objectivec_field.cc:113
google::protobuf::compiler::objectivec::GetCapitalizedType
string GetCapitalizedType(const FieldDescriptor *field)
Definition: objectivec_helpers.cc:619
google::protobuf::compiler::objectivec::OBJECTIVECTYPE_INT64
@ OBJECTIVECTYPE_INT64
Definition: objectivec_helpers.h:140
google::protobuf::compiler::objectivec::OBJECTIVECTYPE_ENUM
@ OBJECTIVECTYPE_ENUM
Definition: objectivec_helpers.h:147
google::protobuf::compiler::objectivec::OBJECTIVECTYPE_FLOAT
@ OBJECTIVECTYPE_FLOAT
Definition: objectivec_helpers.h:142
google::protobuf::compiler::objectivec::OBJECTIVECTYPE_DATA
@ OBJECTIVECTYPE_DATA
Definition: objectivec_helpers.h:146
google::protobuf::compiler::objectivec::MapFieldGenerator::value_field_generator_
std::unique_ptr< FieldGenerator > value_field_generator_
Definition: objectivec_map_field.h:60
type
GLenum type
Definition: glcorearb.h:2695
google::protobuf::compiler::objectivec::MapFieldGenerator::FinishInitialization
virtual void FinishInitialization(void)
Definition: objectivec_map_field.cc:149
google::protobuf::compiler::objectivec::ObjectiveCType
ObjectiveCType
Definition: objectivec_helpers.h:137
objectivec_map_field.h
google::protobuf::compiler::objectivec::FLAGTYPE_FIELD
@ FLAGTYPE_FIELD
Definition: objectivec_helpers.h:154
google::protobuf::compiler::objectivec::OBJECTIVECTYPE_MESSAGE
@ OBJECTIVECTYPE_MESSAGE
Definition: objectivec_helpers.h:148
google::protobuf::Descriptor::FindFieldByName
const FieldDescriptor * FindFieldByName(const std::string &name) const
Definition: src/google/protobuf/descriptor.cc:1615
google::protobuf::compiler::objectivec::FieldGenerator::DetermineForwardDeclarations
virtual void DetermineForwardDeclarations(std::set< string > *fwd_decls) const
Definition: objectivec_field.cc:179
google::protobuf::compiler::objectivec::FieldGenerator::variables_
std::map< string, string > variables_
Definition: objectivec_field.h:101
google::protobuf::FieldDescriptor::message_type
const Descriptor * message_type() const
Definition: src/google/protobuf/descriptor.cc:7228
google::protobuf::compiler::objectivec::FieldGenerator::descriptor_
const FieldDescriptor * descriptor_
Definition: objectivec_field.h:100
google::protobuf::compiler::objectivec::OBJECTIVECTYPE_DOUBLE
@ OBJECTIVECTYPE_DOUBLE
Definition: objectivec_helpers.h:143
google::protobuf::compiler::objectivec::OBJECTIVECTYPE_UINT64
@ OBJECTIVECTYPE_UINT64
Definition: objectivec_helpers.h:141
google::protobuf::compiler::objectivec::GetObjectiveCType
ObjectiveCType GetObjectiveCType(FieldDescriptor::Type field_type)
Definition: objectivec_helpers.cc:665
google::protobuf::compiler::objectivec::OBJECTIVECTYPE_BOOLEAN
@ OBJECTIVECTYPE_BOOLEAN
Definition: objectivec_helpers.h:144
google::protobuf::compiler::objectivec::MapFieldGenerator::~MapFieldGenerator
virtual ~MapFieldGenerator()
Definition: objectivec_map_field.cc:147
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57