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 10 matching lines...) Expand all Loading... |
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 // This header defines the RepeatedFieldRef class template used to access | 31 // This header file is protobuf internal. Users should not include this |
32 // repeated fields with protobuf reflection API. | 32 // file directly. |
33 #ifndef GOOGLE_PROTOBUF_REFLECTION_H__ | 33 #ifndef GOOGLE_PROTOBUF_REPEATED_FIELD_REFLECTION_H__ |
34 #define GOOGLE_PROTOBUF_REFLECTION_H__ | 34 #define GOOGLE_PROTOBUF_REPEATED_FIELD_REFLECTION_H__ |
35 | 35 |
36 #include <memory> | 36 #include <memory> |
37 #ifndef _SHARED_PTR_H | 37 #ifndef _SHARED_PTR_H |
38 #include <google/protobuf/stubs/shared_ptr.h> | 38 #include <google/protobuf/stubs/shared_ptr.h> |
39 #endif | 39 #endif |
40 | 40 |
41 #include <google/protobuf/message.h> | 41 #include <google/protobuf/generated_enum_reflection.h> |
42 #include <google/protobuf/generated_enum_util.h> | |
43 | 42 |
44 namespace google { | 43 namespace google { |
45 namespace protobuf { | 44 namespace protobuf { |
46 namespace internal { | 45 namespace internal { |
47 template<typename T, typename Enable = void> | |
48 struct RefTypeTraits; | |
49 } // namespace internal | |
50 | |
51 template<typename T> | |
52 RepeatedFieldRef<T> Reflection::GetRepeatedFieldRef( | |
53 const Message& message, const FieldDescriptor* field) const { | |
54 return RepeatedFieldRef<T>(message, field); | |
55 } | |
56 | |
57 template<typename T> | |
58 MutableRepeatedFieldRef<T> Reflection::GetMutableRepeatedFieldRef( | |
59 Message* message, const FieldDescriptor* field) const { | |
60 return MutableRepeatedFieldRef<T>(message, field); | |
61 } | |
62 | |
63 // RepeatedFieldRef definition for non-message types. | |
64 template<typename T> | |
65 class RepeatedFieldRef< | |
66 T, typename internal::enable_if<!internal::is_base_of<Message, T>::value>::t
ype> { | |
67 typedef typename internal::RefTypeTraits<T>::iterator IteratorType; | |
68 typedef typename internal::RefTypeTraits<T>::AccessorType AccessorType; | |
69 | |
70 public: | |
71 bool empty() const { | |
72 return accessor_->IsEmpty(data_); | |
73 } | |
74 int size() const { | |
75 return accessor_->Size(data_); | |
76 } | |
77 T Get(int index) const { | |
78 return accessor_->template Get<T>(data_, index); | |
79 } | |
80 | |
81 typedef IteratorType iterator; | |
82 typedef IteratorType const_iterator; | |
83 typedef T value_type; | |
84 typedef T& reference; | |
85 typedef const T& const_reference; | |
86 typedef int size_type; | |
87 typedef ptrdiff_t difference_type; | |
88 | |
89 iterator begin() const { | |
90 return iterator(data_, accessor_, true); | |
91 } | |
92 iterator end() const { | |
93 return iterator(data_, accessor_, false); | |
94 } | |
95 | |
96 private: | |
97 friend class Reflection; | |
98 RepeatedFieldRef( | |
99 const Message& message, | |
100 const FieldDescriptor* field) { | |
101 const Reflection* reflection = message.GetReflection(); | |
102 data_ = reflection->RepeatedFieldData( | |
103 const_cast<Message*>(&message), field, | |
104 internal::RefTypeTraits<T>::cpp_type, NULL); | |
105 accessor_ = reflection->RepeatedFieldAccessor(field); | |
106 } | |
107 | |
108 const void* data_; | |
109 const AccessorType* accessor_; | |
110 }; | |
111 | |
112 // MutableRepeatedFieldRef definition for non-message types. | |
113 template<typename T> | |
114 class MutableRepeatedFieldRef< | |
115 T, typename internal::enable_if<!internal::is_base_of<Message, T>::value>::t
ype> { | |
116 typedef typename internal::RefTypeTraits<T>::AccessorType AccessorType; | |
117 | |
118 public: | |
119 bool empty() const { | |
120 return accessor_->IsEmpty(data_); | |
121 } | |
122 int size() const { | |
123 return accessor_->Size(data_); | |
124 } | |
125 T Get(int index) const { | |
126 return accessor_->template Get<T>(data_, index); | |
127 } | |
128 | |
129 void Set(int index, const T& value) const { | |
130 accessor_->template Set<T>(data_, index, value); | |
131 } | |
132 void Add(const T& value) const { | |
133 accessor_->template Add<T>(data_, value); | |
134 } | |
135 void RemoveLast() const { | |
136 accessor_->RemoveLast(data_); | |
137 } | |
138 void SwapElements(int index1, int index2) const { | |
139 accessor_->SwapElements(data_, index1, index2); | |
140 } | |
141 void Clear() const { | |
142 accessor_->Clear(data_); | |
143 } | |
144 | |
145 void Swap(const MutableRepeatedFieldRef& other) const { | |
146 accessor_->Swap(data_, other.accessor_, other.data_); | |
147 } | |
148 | |
149 template<typename Container> | |
150 void MergeFrom(const Container& container) const { | |
151 typedef typename Container::const_iterator Iterator; | |
152 for (Iterator it = container.begin(); it != container.end(); ++it) { | |
153 Add(*it); | |
154 } | |
155 } | |
156 template<typename Container> | |
157 void CopyFrom(const Container& container) const { | |
158 Clear(); | |
159 MergeFrom(container); | |
160 } | |
161 | |
162 private: | |
163 friend class Reflection; | |
164 MutableRepeatedFieldRef( | |
165 Message* message, | |
166 const FieldDescriptor* field) { | |
167 const Reflection* reflection = message->GetReflection(); | |
168 data_ = reflection->RepeatedFieldData( | |
169 message, field, internal::RefTypeTraits<T>::cpp_type, NULL); | |
170 accessor_ = reflection->RepeatedFieldAccessor(field); | |
171 } | |
172 | |
173 void* data_; | |
174 const AccessorType* accessor_; | |
175 }; | |
176 | |
177 // RepeatedFieldRef definition for message types. | |
178 template<typename T> | |
179 class RepeatedFieldRef< | |
180 T, typename internal::enable_if<internal::is_base_of<Message, T>::value>::ty
pe> { | |
181 typedef typename internal::RefTypeTraits<T>::iterator IteratorType; | |
182 typedef typename internal::RefTypeTraits<T>::AccessorType AccessorType; | |
183 | |
184 public: | |
185 bool empty() const { | |
186 return accessor_->IsEmpty(data_); | |
187 } | |
188 int size() const { | |
189 return accessor_->Size(data_); | |
190 } | |
191 // This method returns a reference to the underlying message object if it | |
192 // exists. If a message object doesn't exist (e.g., data stored in serialized | |
193 // form), scratch_space will be filled with the data and a reference to it | |
194 // will be returned. | |
195 // | |
196 // Example: | |
197 // RepeatedFieldRef<Message> h = ... | |
198 // unique_ptr<Message> scratch_space(h.NewMessage()); | |
199 // const Message& item = h.Get(index, scratch_space.get()); | |
200 const T& Get(int index, T* scratch_space) const { | |
201 return *static_cast<const T*>(accessor_->Get(data_, index, scratch_space)); | |
202 } | |
203 // Create a new message of the same type as the messages stored in this | |
204 // repeated field. Caller takes ownership of the returned object. | |
205 T* NewMessage() const { | |
206 return static_cast<T*>(default_instance_->New()); | |
207 } | |
208 | |
209 typedef IteratorType iterator; | |
210 typedef IteratorType const_iterator; | |
211 typedef T value_type; | |
212 typedef T& reference; | |
213 typedef const T& const_reference; | |
214 typedef int size_type; | |
215 typedef ptrdiff_t difference_type; | |
216 | |
217 iterator begin() const { | |
218 return iterator(data_, accessor_, true, NewMessage()); | |
219 } | |
220 iterator end() const { | |
221 // The end iterator must not be dereferenced, no need for scratch space. | |
222 return iterator(data_, accessor_, false, NULL); | |
223 } | |
224 | |
225 private: | |
226 friend class Reflection; | |
227 RepeatedFieldRef( | |
228 const Message& message, | |
229 const FieldDescriptor* field) { | |
230 const Reflection* reflection = message.GetReflection(); | |
231 data_ = reflection->RepeatedFieldData( | |
232 const_cast<Message*>(&message), field, | |
233 internal::RefTypeTraits<T>::cpp_type, | |
234 internal::RefTypeTraits<T>::GetMessageFieldDescriptor()); | |
235 accessor_ = reflection->RepeatedFieldAccessor(field); | |
236 default_instance_ = | |
237 reflection->GetMessageFactory()->GetPrototype(field->message_type()); | |
238 } | |
239 | |
240 const void* data_; | |
241 const AccessorType* accessor_; | |
242 const Message* default_instance_; | |
243 }; | |
244 | |
245 // MutableRepeatedFieldRef definition for message types. | |
246 template<typename T> | |
247 class MutableRepeatedFieldRef< | |
248 T, typename internal::enable_if<internal::is_base_of<Message, T>::value>::ty
pe> { | |
249 typedef typename internal::RefTypeTraits<T>::AccessorType AccessorType; | |
250 | |
251 public: | |
252 bool empty() const { | |
253 return accessor_->IsEmpty(data_); | |
254 } | |
255 int size() const { | |
256 return accessor_->Size(data_); | |
257 } | |
258 // See comments for RepeatedFieldRef<Message>::Get() | |
259 const T& Get(int index, T* scratch_space) const { | |
260 return *static_cast<const T*>(accessor_->Get(data_, index, scratch_space)); | |
261 } | |
262 // Create a new message of the same type as the messages stored in this | |
263 // repeated field. Caller takes ownership of the returned object. | |
264 T* NewMessage() const { | |
265 return static_cast<T*>(default_instance_->New()); | |
266 } | |
267 | |
268 void Set(int index, const T& value) const { | |
269 accessor_->Set(data_, index, &value); | |
270 } | |
271 void Add(const T& value) const { | |
272 accessor_->Add(data_, &value); | |
273 } | |
274 void RemoveLast() const { | |
275 accessor_->RemoveLast(data_); | |
276 } | |
277 void SwapElements(int index1, int index2) const { | |
278 accessor_->SwapElements(data_, index1, index2); | |
279 } | |
280 void Clear() const { | |
281 accessor_->Clear(data_); | |
282 } | |
283 | |
284 void Swap(const MutableRepeatedFieldRef& other) const { | |
285 accessor_->Swap(data_, other.accessor_, other.data_); | |
286 } | |
287 | |
288 template<typename Container> | |
289 void MergeFrom(const Container& container) const { | |
290 typedef typename Container::const_iterator Iterator; | |
291 for (Iterator it = container.begin(); it != container.end(); ++it) { | |
292 Add(*it); | |
293 } | |
294 } | |
295 template<typename Container> | |
296 void CopyFrom(const Container& container) const { | |
297 Clear(); | |
298 MergeFrom(container); | |
299 } | |
300 | |
301 private: | |
302 friend class Reflection; | |
303 MutableRepeatedFieldRef( | |
304 Message* message, | |
305 const FieldDescriptor* field) { | |
306 const Reflection* reflection = message->GetReflection(); | |
307 data_ = reflection->RepeatedFieldData( | |
308 message, field, internal::RefTypeTraits<T>::cpp_type, | |
309 internal::RefTypeTraits<T>::GetMessageFieldDescriptor()); | |
310 accessor_ = reflection->RepeatedFieldAccessor(field); | |
311 default_instance_ = | |
312 reflection->GetMessageFactory()->GetPrototype(field->message_type()); | |
313 } | |
314 | |
315 void* data_; | |
316 const AccessorType* accessor_; | |
317 const Message* default_instance_; | |
318 }; | |
319 | |
320 namespace internal { | |
321 // Interfaces used to implement reflection RepeatedFieldRef API. | 46 // Interfaces used to implement reflection RepeatedFieldRef API. |
322 // Reflection::GetRepeatedAccessor() should return a pointer to an singleton | 47 // Reflection::GetRepeatedAccessor() should return a pointer to an singleton |
323 // object that implements the below interface. | 48 // object that implements the below interface. |
324 // | 49 // |
325 // This interface passes/returns values using void pointers. The actual type | 50 // This interface passes/returns values using void pointers. The actual type |
326 // of the value depends on the field's cpp_type. Following is a mapping from | 51 // of the value depends on the field's cpp_type. Following is a mapping from |
327 // cpp_type to the type that should be used in this interface: | 52 // cpp_type to the type that should be used in this interface: |
328 // | 53 // |
329 // field->cpp_type() T Actual type of void* | 54 // field->cpp_type() T Actual type of void* |
330 // CPPTYPE_INT32 int32 int32 | 55 // CPPTYPE_INT32 int32 int32 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 const Field* data, int index, Value* scratch_space) const = 0; | 87 const Field* data, int index, Value* scratch_space) const = 0; |
363 | 88 |
364 virtual void Clear(Field* data) const = 0; | 89 virtual void Clear(Field* data) const = 0; |
365 virtual void Set(Field* data, int index, const Value* value) const = 0; | 90 virtual void Set(Field* data, int index, const Value* value) const = 0; |
366 virtual void Add(Field* data, const Value* value) const = 0; | 91 virtual void Add(Field* data, const Value* value) const = 0; |
367 virtual void RemoveLast(Field* data) const = 0; | 92 virtual void RemoveLast(Field* data) const = 0; |
368 virtual void SwapElements(Field* data, int index1, int index2) const = 0; | 93 virtual void SwapElements(Field* data, int index1, int index2) const = 0; |
369 virtual void Swap(Field* data, const RepeatedFieldAccessor* other_mutator, | 94 virtual void Swap(Field* data, const RepeatedFieldAccessor* other_mutator, |
370 Field* other_data) const = 0; | 95 Field* other_data) const = 0; |
371 | 96 |
372 // Create an iterator that points at the beginning of the repeated field. | 97 // Create an iterator that points at the begining of the repeated field. |
373 virtual Iterator* BeginIterator(const Field* data) const = 0; | 98 virtual Iterator* BeginIterator(const Field* data) const = 0; |
374 // Create an iterator that points at the end of the repeated field. | 99 // Create an iterator that points at the end of the repeated field. |
375 virtual Iterator* EndIterator(const Field* data) const = 0; | 100 virtual Iterator* EndIterator(const Field* data) const = 0; |
376 // Make a copy of an iterator and return the new copy. | 101 // Make a copy of an iterator and return the new copy. |
377 virtual Iterator* CopyIterator(const Field* data, | 102 virtual Iterator* CopyIterator(const Field* data, |
378 const Iterator* iterator) const = 0; | 103 const Iterator* iterator) const = 0; |
379 // Move an iterator to point to the next element. | 104 // Move an iterator to point to the next element. |
380 virtual Iterator* AdvanceIterator(const Field* data, | 105 virtual Iterator* AdvanceIterator(const Field* data, |
381 Iterator* iterator) const = 0; | 106 Iterator* iterator) const = 0; |
382 // Compare whether two iterators point to the same element. | 107 // Compare whether two iterators point to the same element. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 template<typename T> | 159 template<typename T> |
435 class RepeatedFieldRefIterator | 160 class RepeatedFieldRefIterator |
436 : public std::iterator<std::forward_iterator_tag, T> { | 161 : public std::iterator<std::forward_iterator_tag, T> { |
437 typedef typename RefTypeTraits<T>::AccessorValueType AccessorValueType; | 162 typedef typename RefTypeTraits<T>::AccessorValueType AccessorValueType; |
438 typedef typename RefTypeTraits<T>::IteratorValueType IteratorValueType; | 163 typedef typename RefTypeTraits<T>::IteratorValueType IteratorValueType; |
439 typedef typename RefTypeTraits<T>::IteratorPointerType IteratorPointerType; | 164 typedef typename RefTypeTraits<T>::IteratorPointerType IteratorPointerType; |
440 | 165 |
441 public: | 166 public: |
442 // Constructor for non-message fields. | 167 // Constructor for non-message fields. |
443 RepeatedFieldRefIterator(const void* data, | 168 RepeatedFieldRefIterator(const void* data, |
444 const RepeatedFieldAccessor* accessor, bool begin) | 169 const RepeatedFieldAccessor* accessor, |
445 : data_(data), | 170 bool begin) |
446 accessor_(accessor), | 171 : data_(data), accessor_(accessor), |
447 iterator_(begin ? accessor->BeginIterator(data) | 172 iterator_(begin ? accessor->BeginIterator(data) : |
448 : accessor->EndIterator(data)), | 173 accessor->EndIterator(data)), |
449 // The end iterator must not be dereferenced, no need for scratch space. | 174 scratch_space_(new AccessorValueType) { |
450 scratch_space_(begin ? new AccessorValueType : NULL) {} | 175 } |
451 // Constructor for message fields. | 176 // Constructor for message fields. |
452 RepeatedFieldRefIterator(const void* data, | 177 RepeatedFieldRefIterator(const void* data, |
453 const RepeatedFieldAccessor* accessor, | 178 const RepeatedFieldAccessor* accessor, |
454 bool begin, | 179 bool begin, |
455 AccessorValueType* scratch_space) | 180 AccessorValueType* scratch_space) |
456 : data_(data), accessor_(accessor), | 181 : data_(data), accessor_(accessor), |
457 iterator_(begin ? accessor->BeginIterator(data) : | 182 iterator_(begin ? accessor->BeginIterator(data) : |
458 accessor->EndIterator(data)), | 183 accessor->EndIterator(data)), |
459 scratch_space_(scratch_space) { | 184 scratch_space_(scratch_space) { |
460 } | 185 } |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 typedef int32* IteratorPointerType; | 284 typedef int32* IteratorPointerType; |
560 static const FieldDescriptor::CppType cpp_type = | 285 static const FieldDescriptor::CppType cpp_type = |
561 FieldDescriptor::CPPTYPE_ENUM; | 286 FieldDescriptor::CPPTYPE_ENUM; |
562 static const Descriptor* GetMessageFieldDescriptor() { | 287 static const Descriptor* GetMessageFieldDescriptor() { |
563 return NULL; | 288 return NULL; |
564 } | 289 } |
565 }; | 290 }; |
566 | 291 |
567 template<typename T> | 292 template<typename T> |
568 struct RefTypeTraits< | 293 struct RefTypeTraits< |
569 T, typename internal::enable_if< ::google::protobuf::internal::is_same<strin
g, T>::value>::type> { | 294 T, typename internal::enable_if<internal::is_same<string, T>::value>::type>
{ |
570 typedef RepeatedFieldRefIterator<T> iterator; | 295 typedef RepeatedFieldRefIterator<T> iterator; |
571 typedef RepeatedFieldAccessor AccessorType; | 296 typedef RepeatedFieldAccessor AccessorType; |
572 typedef string AccessorValueType; | 297 typedef string AccessorValueType; |
573 typedef string IteratorValueType; | 298 typedef string IteratorValueType; |
574 typedef string* IteratorPointerType; | 299 typedef string* IteratorPointerType; |
575 static const FieldDescriptor::CppType cpp_type = | 300 static const FieldDescriptor::CppType cpp_type = |
576 FieldDescriptor::CPPTYPE_STRING; | 301 FieldDescriptor::CPPTYPE_STRING; |
577 static const Descriptor* GetMessageFieldDescriptor() { | 302 static const Descriptor* GetMessageFieldDescriptor() { |
578 return NULL; | 303 return NULL; |
579 } | 304 } |
(...skipping 22 matching lines...) Expand all Loading... |
602 typedef const T* IteratorPointerType; | 327 typedef const T* IteratorPointerType; |
603 static const FieldDescriptor::CppType cpp_type = | 328 static const FieldDescriptor::CppType cpp_type = |
604 FieldDescriptor::CPPTYPE_MESSAGE; | 329 FieldDescriptor::CPPTYPE_MESSAGE; |
605 static const Descriptor* GetMessageFieldDescriptor() { | 330 static const Descriptor* GetMessageFieldDescriptor() { |
606 return MessageDescriptorGetter<T>::get(); | 331 return MessageDescriptorGetter<T>::get(); |
607 } | 332 } |
608 }; | 333 }; |
609 } // namespace internal | 334 } // namespace internal |
610 } // namespace protobuf | 335 } // namespace protobuf |
611 } // namespace google | 336 } // namespace google |
612 | 337 #endif // GOOGLE_PROTOBUF_REPEATED_FIELD_REFLECTION_H__ |
613 #endif // GOOGLE_PROTOBUF_REFLECTION_H__ | |
OLD | NEW |