OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 explicit TypeInfoForTypeResolver(TypeResolver* type_resolver) | 53 explicit TypeInfoForTypeResolver(TypeResolver* type_resolver) |
54 : type_resolver_(type_resolver) {} | 54 : type_resolver_(type_resolver) {} |
55 | 55 |
56 virtual ~TypeInfoForTypeResolver() { | 56 virtual ~TypeInfoForTypeResolver() { |
57 DeleteCachedTypes(&cached_types_); | 57 DeleteCachedTypes(&cached_types_); |
58 DeleteCachedTypes(&cached_enums_); | 58 DeleteCachedTypes(&cached_enums_); |
59 } | 59 } |
60 | 60 |
61 virtual util::StatusOr<const google::protobuf::Type*> ResolveTypeUrl( | 61 virtual util::StatusOr<const google::protobuf::Type*> ResolveTypeUrl( |
62 StringPiece type_url) const { | 62 StringPiece type_url) const { |
63 std::map<StringPiece, StatusOrType>::iterator it = | 63 map<StringPiece, StatusOrType>::iterator it = cached_types_.find(type_url); |
64 cached_types_.find(type_url); | |
65 if (it != cached_types_.end()) { | 64 if (it != cached_types_.end()) { |
66 return it->second; | 65 return it->second; |
67 } | 66 } |
68 // Stores the string value so it can be referenced using StringPiece in the | 67 // Stores the string value so it can be referenced using StringPiece in the |
69 // cached_types_ map. | 68 // cached_types_ map. |
70 const string& string_type_url = | 69 const string& string_type_url = |
71 *string_storage_.insert(type_url.ToString()).first; | 70 *string_storage_.insert(type_url.ToString()).first; |
72 google::protobuf::scoped_ptr<google::protobuf::Type> type(new google::protob
uf::Type()); | 71 google::protobuf::scoped_ptr<google::protobuf::Type> type(new google::protob
uf::Type()); |
73 util::Status status = | 72 util::Status status = |
74 type_resolver_->ResolveMessageType(string_type_url, type.get()); | 73 type_resolver_->ResolveMessageType(string_type_url, type.get()); |
75 StatusOrType result = | 74 StatusOrType result = |
76 status.ok() ? StatusOrType(type.release()) : StatusOrType(status); | 75 status.ok() ? StatusOrType(type.release()) : StatusOrType(status); |
77 cached_types_[string_type_url] = result; | 76 cached_types_[string_type_url] = result; |
78 return result; | 77 return result; |
79 } | 78 } |
80 | 79 |
81 virtual const google::protobuf::Type* GetTypeByTypeUrl( | 80 virtual const google::protobuf::Type* GetTypeByTypeUrl( |
82 StringPiece type_url) const { | 81 StringPiece type_url) const { |
83 StatusOrType result = ResolveTypeUrl(type_url); | 82 StatusOrType result = ResolveTypeUrl(type_url); |
84 return result.ok() ? result.ValueOrDie() : NULL; | 83 return result.ok() ? result.ValueOrDie() : NULL; |
85 } | 84 } |
86 | 85 |
87 virtual const google::protobuf::Enum* GetEnumByTypeUrl( | 86 virtual const google::protobuf::Enum* GetEnumByTypeUrl( |
88 StringPiece type_url) const { | 87 StringPiece type_url) const { |
89 std::map<StringPiece, StatusOrEnum>::iterator it = | 88 map<StringPiece, StatusOrEnum>::iterator it = cached_enums_.find(type_url); |
90 cached_enums_.find(type_url); | |
91 if (it != cached_enums_.end()) { | 89 if (it != cached_enums_.end()) { |
92 return it->second.ok() ? it->second.ValueOrDie() : NULL; | 90 return it->second.ok() ? it->second.ValueOrDie() : NULL; |
93 } | 91 } |
94 // Stores the string value so it can be referenced using StringPiece in the | 92 // Stores the string value so it can be referenced using StringPiece in the |
95 // cached_enums_ map. | 93 // cached_enums_ map. |
96 const string& string_type_url = | 94 const string& string_type_url = |
97 *string_storage_.insert(type_url.ToString()).first; | 95 *string_storage_.insert(type_url.ToString()).first; |
98 google::protobuf::scoped_ptr<google::protobuf::Enum> enum_type( | 96 google::protobuf::scoped_ptr<google::protobuf::Enum> enum_type( |
99 new google::protobuf::Enum()); | 97 new google::protobuf::Enum()); |
100 util::Status status = | 98 util::Status status = |
(...skipping 17 matching lines...) Expand all Loading... |
118 name = camel_case_name; | 116 name = camel_case_name; |
119 } | 117 } |
120 return FindFieldInTypeOrNull(type, name); | 118 return FindFieldInTypeOrNull(type, name); |
121 } | 119 } |
122 | 120 |
123 private: | 121 private: |
124 typedef util::StatusOr<const google::protobuf::Type*> StatusOrType; | 122 typedef util::StatusOr<const google::protobuf::Type*> StatusOrType; |
125 typedef util::StatusOr<const google::protobuf::Enum*> StatusOrEnum; | 123 typedef util::StatusOr<const google::protobuf::Enum*> StatusOrEnum; |
126 | 124 |
127 template <typename T> | 125 template <typename T> |
128 static void DeleteCachedTypes(std::map<StringPiece, T>* cached_types) { | 126 static void DeleteCachedTypes(map<StringPiece, T>* cached_types) { |
129 for (typename std::map<StringPiece, T>::iterator it = cached_types->begin(); | 127 for (typename map<StringPiece, T>::iterator it = cached_types->begin(); |
130 it != cached_types->end(); ++it) { | 128 it != cached_types->end(); ++it) { |
131 if (it->second.ok()) { | 129 if (it->second.ok()) { |
132 delete it->second.ValueOrDie(); | 130 delete it->second.ValueOrDie(); |
133 } | 131 } |
134 } | 132 } |
135 } | 133 } |
136 | 134 |
137 void PopulateNameLookupTable(const google::protobuf::Type* type) const { | 135 void PopulateNameLookupTable(const google::protobuf::Type* type) const { |
138 for (int i = 0; i < type->fields_size(); ++i) { | 136 for (int i = 0; i < type->fields_size(); ++i) { |
139 const google::protobuf::Field& field = type->fields(i); | 137 const google::protobuf::Field& field = type->fields(i); |
140 StringPiece name = field.name(); | 138 StringPiece name = field.name(); |
141 StringPiece camel_case_name = field.json_name(); | 139 StringPiece camel_case_name = field.json_name(); |
142 const StringPiece* existing = InsertOrReturnExisting( | 140 const StringPiece* existing = InsertOrReturnExisting( |
143 &camel_case_name_table_, camel_case_name, name); | 141 &camel_case_name_table_, camel_case_name, name); |
144 if (existing && *existing != name) { | 142 if (existing && *existing != name) { |
145 GOOGLE_LOG(WARNING) << "Field '" << name << "' and '" << *existing | 143 GOOGLE_LOG(WARNING) << "Field '" << name << "' and '" << *existing |
146 << "' map to the same camel case name '" << camel_case_name | 144 << "' map to the same camel case name '" << camel_case_name |
147 << "'."; | 145 << "'."; |
148 } | 146 } |
149 } | 147 } |
150 } | 148 } |
151 | 149 |
152 TypeResolver* type_resolver_; | 150 TypeResolver* type_resolver_; |
153 | 151 |
154 // Stores string values that will be referenced by StringPieces in | 152 // Stores string values that will be referenced by StringPieces in |
155 // cached_types_, cached_enums_ and camel_case_name_table_. | 153 // cached_types_, cached_enums_ and camel_case_name_table_. |
156 mutable std::set<string> string_storage_; | 154 mutable set<string> string_storage_; |
157 | 155 |
158 mutable std::map<StringPiece, StatusOrType> cached_types_; | 156 mutable map<StringPiece, StatusOrType> cached_types_; |
159 mutable std::map<StringPiece, StatusOrEnum> cached_enums_; | 157 mutable map<StringPiece, StatusOrEnum> cached_enums_; |
160 | 158 |
161 mutable std::set<const google::protobuf::Type*> indexed_types_; | 159 mutable set<const google::protobuf::Type*> indexed_types_; |
162 mutable std::map<StringPiece, StringPiece> camel_case_name_table_; | 160 mutable map<StringPiece, StringPiece> camel_case_name_table_; |
163 }; | 161 }; |
164 } // namespace | 162 } // namespace |
165 | 163 |
166 TypeInfo* TypeInfo::NewTypeInfo(TypeResolver* type_resolver) { | 164 TypeInfo* TypeInfo::NewTypeInfo(TypeResolver* type_resolver) { |
167 return new TypeInfoForTypeResolver(type_resolver); | 165 return new TypeInfoForTypeResolver(type_resolver); |
168 } | 166 } |
169 | 167 |
170 } // namespace converter | 168 } // namespace converter |
171 } // namespace util | 169 } // namespace util |
172 } // namespace protobuf | 170 } // namespace protobuf |
173 } // namespace google | 171 } // namespace google |
OLD | NEW |