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 map<StringPiece, StatusOrType>::iterator it = cached_types_.find(type_url); | 63 std::map<StringPiece, StatusOrType>::iterator it = |
| 64 cached_types_.find(type_url); |
64 if (it != cached_types_.end()) { | 65 if (it != cached_types_.end()) { |
65 return it->second; | 66 return it->second; |
66 } | 67 } |
67 // Stores the string value so it can be referenced using StringPiece in the | 68 // Stores the string value so it can be referenced using StringPiece in the |
68 // cached_types_ map. | 69 // cached_types_ map. |
69 const string& string_type_url = | 70 const string& string_type_url = |
70 *string_storage_.insert(type_url.ToString()).first; | 71 *string_storage_.insert(type_url.ToString()).first; |
71 google::protobuf::scoped_ptr<google::protobuf::Type> type(new google::protob
uf::Type()); | 72 google::protobuf::scoped_ptr<google::protobuf::Type> type(new google::protob
uf::Type()); |
72 util::Status status = | 73 util::Status status = |
73 type_resolver_->ResolveMessageType(string_type_url, type.get()); | 74 type_resolver_->ResolveMessageType(string_type_url, type.get()); |
74 StatusOrType result = | 75 StatusOrType result = |
75 status.ok() ? StatusOrType(type.release()) : StatusOrType(status); | 76 status.ok() ? StatusOrType(type.release()) : StatusOrType(status); |
76 cached_types_[string_type_url] = result; | 77 cached_types_[string_type_url] = result; |
77 return result; | 78 return result; |
78 } | 79 } |
79 | 80 |
80 virtual const google::protobuf::Type* GetTypeByTypeUrl( | 81 virtual const google::protobuf::Type* GetTypeByTypeUrl( |
81 StringPiece type_url) const { | 82 StringPiece type_url) const { |
82 StatusOrType result = ResolveTypeUrl(type_url); | 83 StatusOrType result = ResolveTypeUrl(type_url); |
83 return result.ok() ? result.ValueOrDie() : NULL; | 84 return result.ok() ? result.ValueOrDie() : NULL; |
84 } | 85 } |
85 | 86 |
86 virtual const google::protobuf::Enum* GetEnumByTypeUrl( | 87 virtual const google::protobuf::Enum* GetEnumByTypeUrl( |
87 StringPiece type_url) const { | 88 StringPiece type_url) const { |
88 map<StringPiece, StatusOrEnum>::iterator it = cached_enums_.find(type_url); | 89 std::map<StringPiece, StatusOrEnum>::iterator it = |
| 90 cached_enums_.find(type_url); |
89 if (it != cached_enums_.end()) { | 91 if (it != cached_enums_.end()) { |
90 return it->second.ok() ? it->second.ValueOrDie() : NULL; | 92 return it->second.ok() ? it->second.ValueOrDie() : NULL; |
91 } | 93 } |
92 // Stores the string value so it can be referenced using StringPiece in the | 94 // Stores the string value so it can be referenced using StringPiece in the |
93 // cached_enums_ map. | 95 // cached_enums_ map. |
94 const string& string_type_url = | 96 const string& string_type_url = |
95 *string_storage_.insert(type_url.ToString()).first; | 97 *string_storage_.insert(type_url.ToString()).first; |
96 google::protobuf::scoped_ptr<google::protobuf::Enum> enum_type( | 98 google::protobuf::scoped_ptr<google::protobuf::Enum> enum_type( |
97 new google::protobuf::Enum()); | 99 new google::protobuf::Enum()); |
98 util::Status status = | 100 util::Status status = |
(...skipping 17 matching lines...) Expand all Loading... |
116 name = camel_case_name; | 118 name = camel_case_name; |
117 } | 119 } |
118 return FindFieldInTypeOrNull(type, name); | 120 return FindFieldInTypeOrNull(type, name); |
119 } | 121 } |
120 | 122 |
121 private: | 123 private: |
122 typedef util::StatusOr<const google::protobuf::Type*> StatusOrType; | 124 typedef util::StatusOr<const google::protobuf::Type*> StatusOrType; |
123 typedef util::StatusOr<const google::protobuf::Enum*> StatusOrEnum; | 125 typedef util::StatusOr<const google::protobuf::Enum*> StatusOrEnum; |
124 | 126 |
125 template <typename T> | 127 template <typename T> |
126 static void DeleteCachedTypes(map<StringPiece, T>* cached_types) { | 128 static void DeleteCachedTypes(std::map<StringPiece, T>* cached_types) { |
127 for (typename map<StringPiece, T>::iterator it = cached_types->begin(); | 129 for (typename std::map<StringPiece, T>::iterator it = cached_types->begin(); |
128 it != cached_types->end(); ++it) { | 130 it != cached_types->end(); ++it) { |
129 if (it->second.ok()) { | 131 if (it->second.ok()) { |
130 delete it->second.ValueOrDie(); | 132 delete it->second.ValueOrDie(); |
131 } | 133 } |
132 } | 134 } |
133 } | 135 } |
134 | 136 |
135 void PopulateNameLookupTable(const google::protobuf::Type* type) const { | 137 void PopulateNameLookupTable(const google::protobuf::Type* type) const { |
136 for (int i = 0; i < type->fields_size(); ++i) { | 138 for (int i = 0; i < type->fields_size(); ++i) { |
137 const google::protobuf::Field& field = type->fields(i); | 139 const google::protobuf::Field& field = type->fields(i); |
138 StringPiece name = field.name(); | 140 StringPiece name = field.name(); |
139 StringPiece camel_case_name = field.json_name(); | 141 StringPiece camel_case_name = field.json_name(); |
140 const StringPiece* existing = InsertOrReturnExisting( | 142 const StringPiece* existing = InsertOrReturnExisting( |
141 &camel_case_name_table_, camel_case_name, name); | 143 &camel_case_name_table_, camel_case_name, name); |
142 if (existing && *existing != name) { | 144 if (existing && *existing != name) { |
143 GOOGLE_LOG(WARNING) << "Field '" << name << "' and '" << *existing | 145 GOOGLE_LOG(WARNING) << "Field '" << name << "' and '" << *existing |
144 << "' map to the same camel case name '" << camel_case_name | 146 << "' map to the same camel case name '" << camel_case_name |
145 << "'."; | 147 << "'."; |
146 } | 148 } |
147 } | 149 } |
148 } | 150 } |
149 | 151 |
150 TypeResolver* type_resolver_; | 152 TypeResolver* type_resolver_; |
151 | 153 |
152 // Stores string values that will be referenced by StringPieces in | 154 // Stores string values that will be referenced by StringPieces in |
153 // cached_types_, cached_enums_ and camel_case_name_table_. | 155 // cached_types_, cached_enums_ and camel_case_name_table_. |
154 mutable set<string> string_storage_; | 156 mutable std::set<string> string_storage_; |
155 | 157 |
156 mutable map<StringPiece, StatusOrType> cached_types_; | 158 mutable std::map<StringPiece, StatusOrType> cached_types_; |
157 mutable map<StringPiece, StatusOrEnum> cached_enums_; | 159 mutable std::map<StringPiece, StatusOrEnum> cached_enums_; |
158 | 160 |
159 mutable set<const google::protobuf::Type*> indexed_types_; | 161 mutable std::set<const google::protobuf::Type*> indexed_types_; |
160 mutable map<StringPiece, StringPiece> camel_case_name_table_; | 162 mutable std::map<StringPiece, StringPiece> camel_case_name_table_; |
161 }; | 163 }; |
162 } // namespace | 164 } // namespace |
163 | 165 |
164 TypeInfo* TypeInfo::NewTypeInfo(TypeResolver* type_resolver) { | 166 TypeInfo* TypeInfo::NewTypeInfo(TypeResolver* type_resolver) { |
165 return new TypeInfoForTypeResolver(type_resolver); | 167 return new TypeInfoForTypeResolver(type_resolver); |
166 } | 168 } |
167 | 169 |
168 } // namespace converter | 170 } // namespace converter |
169 } // namespace util | 171 } // namespace util |
170 } // namespace protobuf | 172 } // namespace protobuf |
171 } // namespace google | 173 } // namespace google |
OLD | NEW |