type_info.cc
Go to the documentation of this file.
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 
32 
33 #include <map>
34 #include <set>
35 
43 
44 namespace google {
45 namespace protobuf {
46 namespace util {
47 namespace converter {
48 
49 namespace {
50 // A TypeInfo that looks up information provided by a TypeResolver.
51 class TypeInfoForTypeResolver : public TypeInfo {
52  public:
53  explicit TypeInfoForTypeResolver(TypeResolver* type_resolver)
55 
56  virtual ~TypeInfoForTypeResolver() {
57  DeleteCachedTypes(&cached_types_);
58  DeleteCachedTypes(&cached_enums_);
59  }
60 
61  util::StatusOr<const google::protobuf::Type*> ResolveTypeUrl(
62  StringPiece type_url) const override {
63  std::map<StringPiece, StatusOrType>::iterator it =
64  cached_types_.find(type_url);
65  if (it != cached_types_.end()) {
66  return it->second;
67  }
68  // Stores the string value so it can be referenced using StringPiece in the
69  // cached_types_ map.
70  const std::string& string_type_url =
71  *string_storage_.insert(std::string(type_url)).first;
72  std::unique_ptr<google::protobuf::Type> type(new google::protobuf::Type());
73  util::Status status =
74  type_resolver_->ResolveMessageType(string_type_url, type.get());
75  StatusOrType result =
76  status.ok() ? StatusOrType(type.release()) : StatusOrType(status);
77  cached_types_[string_type_url] = result;
78  return result;
79  }
80 
81  const google::protobuf::Type* GetTypeByTypeUrl(
82  StringPiece type_url) const override {
83  StatusOrType result = ResolveTypeUrl(type_url);
84  return result.ok() ? result.ValueOrDie() : NULL;
85  }
86 
87  const google::protobuf::Enum* GetEnumByTypeUrl(
88  StringPiece type_url) const override {
89  std::map<StringPiece, StatusOrEnum>::iterator it =
90  cached_enums_.find(type_url);
91  if (it != cached_enums_.end()) {
92  return it->second.ok() ? it->second.ValueOrDie() : NULL;
93  }
94  // Stores the string value so it can be referenced using StringPiece in the
95  // cached_enums_ map.
96  const std::string& string_type_url =
97  *string_storage_.insert(std::string(type_url)).first;
98  std::unique_ptr<google::protobuf::Enum> enum_type(
100  util::Status status =
101  type_resolver_->ResolveEnumType(string_type_url, enum_type.get());
102  StatusOrEnum result =
103  status.ok() ? StatusOrEnum(enum_type.release()) : StatusOrEnum(status);
104  cached_enums_[string_type_url] = result;
105  return result.ok() ? result.ValueOrDie() : NULL;
106  }
107 
108  const google::protobuf::Field* FindField(
110  StringPiece camel_case_name) const override {
111  std::map<const google::protobuf::Type*, CamelCaseNameTable>::const_iterator
112  it = indexed_types_.find(type);
113  const CamelCaseNameTable& camel_case_name_table =
114  (it == indexed_types_.end())
115  ? PopulateNameLookupTable(type, &indexed_types_[type])
116  : it->second;
117  StringPiece name = FindWithDefault(
118  camel_case_name_table, camel_case_name, StringPiece());
119  if (name.empty()) {
120  // Didn't find a mapping. Use whatever provided.
121  name = camel_case_name;
122  }
124  }
125 
126  private:
127  typedef util::StatusOr<const google::protobuf::Type*> StatusOrType;
128  typedef util::StatusOr<const google::protobuf::Enum*> StatusOrEnum;
129  typedef std::map<StringPiece, StringPiece> CamelCaseNameTable;
130 
131  template <typename T>
132  static void DeleteCachedTypes(std::map<StringPiece, T>* cached_types) {
133  for (typename std::map<StringPiece, T>::iterator it =
134  cached_types->begin();
135  it != cached_types->end(); ++it) {
136  if (it->second.ok()) {
137  delete it->second.ValueOrDie();
138  }
139  }
140  }
141 
142  const CamelCaseNameTable& PopulateNameLookupTable(
144  CamelCaseNameTable* camel_case_name_table) const {
145  for (int i = 0; i < type->fields_size(); ++i) {
146  const google::protobuf::Field& field = type->fields(i);
147  StringPiece name = field.name();
148  StringPiece camel_case_name = field.json_name();
149  const StringPiece* existing = InsertOrReturnExisting(
150  camel_case_name_table, camel_case_name, name);
151  if (existing && *existing != name) {
152  GOOGLE_LOG(WARNING) << "Field '" << name << "' and '" << *existing
153  << "' map to the same camel case name '" << camel_case_name
154  << "'.";
155  }
156  }
157  return *camel_case_name_table;
158  }
159 
160  TypeResolver* type_resolver_;
161 
162  // Stores string values that will be referenced by StringPieces in
163  // cached_types_, cached_enums_.
164  mutable std::set<std::string> string_storage_;
165 
166  mutable std::map<StringPiece, StatusOrType> cached_types_;
167  mutable std::map<StringPiece, StatusOrEnum> cached_enums_;
168 
169  mutable std::map<const google::protobuf::Type*, CamelCaseNameTable>
171 };
172 } // namespace
173 
175  return new TypeInfoForTypeResolver(type_resolver);
176 }
177 
178 } // namespace converter
179 } // namespace util
180 } // namespace protobuf
181 } // namespace google
google::protobuf::FindWithDefault
const Collection::value_type::second_type & FindWithDefault(const Collection &collection, const typename Collection::value_type::first_type &key, const typename Collection::value_type::second_type &value)
Definition: map_util.h:123
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
map_util.h
google::protobuf::Enum
NULL
NULL
Definition: test_security_zap.cpp:405
cached_types_
std::map< StringPiece, StatusOrType > cached_types_
Definition: type_info.cc:166
statusor.h
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
enum_type
zend_class_entry * enum_type
Definition: php/ext/google/protobuf/message.c:1904
indexed_types_
std::map< const google::protobuf::Type *, CamelCaseNameTable > indexed_types_
Definition: type_info.cc:170
utility.h
stringpiece.h
type_resolver_
TypeResolver * type_resolver_
Definition: type_info.cc:160
google::protobuf::util::TypeResolver
Definition: type_resolver.h:52
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: logging.h:146
google::protobuf::util::converter::TypeInfo
Definition: type_info.h:49
google::protobuf::WARNING
static const LogLevel WARNING
Definition: protobuf/src/google/protobuf/testing/googletest.h:71
type_info.h
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
aditof::Status
Status
Status of any operation that the TOF sdk performs.
Definition: status_definitions.h:48
i
int i
Definition: gmock-matchers_test.cc:764
Field
struct Field Field
Definition: php/ext/google/protobuf/protobuf.h:638
type
GLenum type
Definition: glcorearb.h:2695
common.h
cached_enums_
std::map< StringPiece, StatusOrEnum > cached_enums_
Definition: type_info.cc:167
type_resolver
TypeResolver * type_resolver
Definition: conformance_cpp.cc:97
google::protobuf::util::converter::FindFieldInTypeOrNull
const google::protobuf::Field * FindFieldInTypeOrNull(const google::protobuf::Type *type, StringPiece field_name)
Definition: utility.cc:152
type.pb.h
Type
struct Type Type
Definition: php/ext/google/protobuf/protobuf.h:664
google::protobuf::util::converter::TypeInfo::NewTypeInfo
static TypeInfo * NewTypeInfo(TypeResolver *type_resolver)
Definition: type_info.cc:174
type_url
string * type_url
Definition: conformance_cpp.cc:98
google::protobuf::InsertOrReturnExisting
Collection::value_type::second_type * InsertOrReturnExisting(Collection *const collection, const typename Collection::value_type &vt)
Definition: map_util.h:624
it
MapIter it
Definition: php/ext/google/protobuf/map.c:205
status.h
google
Definition: data_proto2_to_proto3_util.h:11
string_storage_
std::set< std::string > string_storage_
Definition: type_info.cc:164


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:00