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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 } | 73 } |
74 int size() const { | 74 int size() const { |
75 return accessor_->Size(data_); | 75 return accessor_->Size(data_); |
76 } | 76 } |
77 T Get(int index) const { | 77 T Get(int index) const { |
78 return accessor_->template Get<T>(data_, index); | 78 return accessor_->template Get<T>(data_, index); |
79 } | 79 } |
80 | 80 |
81 typedef IteratorType iterator; | 81 typedef IteratorType iterator; |
82 typedef IteratorType const_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 |
83 iterator begin() const { | 89 iterator begin() const { |
84 return iterator(data_, accessor_, true); | 90 return iterator(data_, accessor_, true); |
85 } | 91 } |
86 iterator end() const { | 92 iterator end() const { |
87 return iterator(data_, accessor_, false); | 93 return iterator(data_, accessor_, false); |
88 } | 94 } |
89 | 95 |
90 private: | 96 private: |
91 friend class Reflection; | 97 friend class Reflection; |
92 RepeatedFieldRef( | 98 RepeatedFieldRef( |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 return *static_cast<const T*>(accessor_->Get(data_, index, scratch_space)); | 201 return *static_cast<const T*>(accessor_->Get(data_, index, scratch_space)); |
196 } | 202 } |
197 // Create a new message of the same type as the messages stored in this | 203 // Create a new message of the same type as the messages stored in this |
198 // repeated field. Caller takes ownership of the returned object. | 204 // repeated field. Caller takes ownership of the returned object. |
199 T* NewMessage() const { | 205 T* NewMessage() const { |
200 return static_cast<T*>(default_instance_->New()); | 206 return static_cast<T*>(default_instance_->New()); |
201 } | 207 } |
202 | 208 |
203 typedef IteratorType iterator; | 209 typedef IteratorType iterator; |
204 typedef IteratorType const_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 |
205 iterator begin() const { | 217 iterator begin() const { |
206 return iterator(data_, accessor_, true, NewMessage()); | 218 return iterator(data_, accessor_, true, NewMessage()); |
207 } | 219 } |
208 iterator end() const { | 220 iterator end() const { |
209 return iterator(data_, accessor_, false, NewMessage()); | 221 // The end iterator must not be dereferenced, no need for scratch space. |
| 222 return iterator(data_, accessor_, false, NULL); |
210 } | 223 } |
211 | 224 |
212 private: | 225 private: |
213 friend class Reflection; | 226 friend class Reflection; |
214 RepeatedFieldRef( | 227 RepeatedFieldRef( |
215 const Message& message, | 228 const Message& message, |
216 const FieldDescriptor* field) { | 229 const FieldDescriptor* field) { |
217 const Reflection* reflection = message.GetReflection(); | 230 const Reflection* reflection = message.GetReflection(); |
218 data_ = reflection->RepeatedFieldData( | 231 data_ = reflection->RepeatedFieldData( |
219 const_cast<Message*>(&message), field, | 232 const_cast<Message*>(&message), field, |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 const Field* data, int index, Value* scratch_space) const = 0; | 362 const Field* data, int index, Value* scratch_space) const = 0; |
350 | 363 |
351 virtual void Clear(Field* data) const = 0; | 364 virtual void Clear(Field* data) const = 0; |
352 virtual void Set(Field* data, int index, const Value* value) const = 0; | 365 virtual void Set(Field* data, int index, const Value* value) const = 0; |
353 virtual void Add(Field* data, const Value* value) const = 0; | 366 virtual void Add(Field* data, const Value* value) const = 0; |
354 virtual void RemoveLast(Field* data) const = 0; | 367 virtual void RemoveLast(Field* data) const = 0; |
355 virtual void SwapElements(Field* data, int index1, int index2) const = 0; | 368 virtual void SwapElements(Field* data, int index1, int index2) const = 0; |
356 virtual void Swap(Field* data, const RepeatedFieldAccessor* other_mutator, | 369 virtual void Swap(Field* data, const RepeatedFieldAccessor* other_mutator, |
357 Field* other_data) const = 0; | 370 Field* other_data) const = 0; |
358 | 371 |
359 // Create an iterator that points at the begining of the repeated field. | 372 // Create an iterator that points at the beginning of the repeated field. |
360 virtual Iterator* BeginIterator(const Field* data) const = 0; | 373 virtual Iterator* BeginIterator(const Field* data) const = 0; |
361 // Create an iterator that points at the end of the repeated field. | 374 // Create an iterator that points at the end of the repeated field. |
362 virtual Iterator* EndIterator(const Field* data) const = 0; | 375 virtual Iterator* EndIterator(const Field* data) const = 0; |
363 // Make a copy of an iterator and return the new copy. | 376 // Make a copy of an iterator and return the new copy. |
364 virtual Iterator* CopyIterator(const Field* data, | 377 virtual Iterator* CopyIterator(const Field* data, |
365 const Iterator* iterator) const = 0; | 378 const Iterator* iterator) const = 0; |
366 // Move an iterator to point to the next element. | 379 // Move an iterator to point to the next element. |
367 virtual Iterator* AdvanceIterator(const Field* data, | 380 virtual Iterator* AdvanceIterator(const Field* data, |
368 Iterator* iterator) const = 0; | 381 Iterator* iterator) const = 0; |
369 // Compare whether two iterators point to the same element. | 382 // Compare whether two iterators point to the same element. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 template<typename T> | 434 template<typename T> |
422 class RepeatedFieldRefIterator | 435 class RepeatedFieldRefIterator |
423 : public std::iterator<std::forward_iterator_tag, T> { | 436 : public std::iterator<std::forward_iterator_tag, T> { |
424 typedef typename RefTypeTraits<T>::AccessorValueType AccessorValueType; | 437 typedef typename RefTypeTraits<T>::AccessorValueType AccessorValueType; |
425 typedef typename RefTypeTraits<T>::IteratorValueType IteratorValueType; | 438 typedef typename RefTypeTraits<T>::IteratorValueType IteratorValueType; |
426 typedef typename RefTypeTraits<T>::IteratorPointerType IteratorPointerType; | 439 typedef typename RefTypeTraits<T>::IteratorPointerType IteratorPointerType; |
427 | 440 |
428 public: | 441 public: |
429 // Constructor for non-message fields. | 442 // Constructor for non-message fields. |
430 RepeatedFieldRefIterator(const void* data, | 443 RepeatedFieldRefIterator(const void* data, |
431 const RepeatedFieldAccessor* accessor, | 444 const RepeatedFieldAccessor* accessor, bool begin) |
432 bool begin) | 445 : data_(data), |
433 : data_(data), accessor_(accessor), | 446 accessor_(accessor), |
434 iterator_(begin ? accessor->BeginIterator(data) : | 447 iterator_(begin ? accessor->BeginIterator(data) |
435 accessor->EndIterator(data)), | 448 : accessor->EndIterator(data)), |
436 scratch_space_(new AccessorValueType) { | 449 // The end iterator must not be dereferenced, no need for scratch space. |
437 } | 450 scratch_space_(begin ? new AccessorValueType : NULL) {} |
438 // Constructor for message fields. | 451 // Constructor for message fields. |
439 RepeatedFieldRefIterator(const void* data, | 452 RepeatedFieldRefIterator(const void* data, |
440 const RepeatedFieldAccessor* accessor, | 453 const RepeatedFieldAccessor* accessor, |
441 bool begin, | 454 bool begin, |
442 AccessorValueType* scratch_space) | 455 AccessorValueType* scratch_space) |
443 : data_(data), accessor_(accessor), | 456 : data_(data), accessor_(accessor), |
444 iterator_(begin ? accessor->BeginIterator(data) : | 457 iterator_(begin ? accessor->BeginIterator(data) : |
445 accessor->EndIterator(data)), | 458 accessor->EndIterator(data)), |
446 scratch_space_(scratch_space) { | 459 scratch_space_(scratch_space) { |
447 } | 460 } |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
591 FieldDescriptor::CPPTYPE_MESSAGE; | 604 FieldDescriptor::CPPTYPE_MESSAGE; |
592 static const Descriptor* GetMessageFieldDescriptor() { | 605 static const Descriptor* GetMessageFieldDescriptor() { |
593 return MessageDescriptorGetter<T>::get(); | 606 return MessageDescriptorGetter<T>::get(); |
594 } | 607 } |
595 }; | 608 }; |
596 } // namespace internal | 609 } // namespace internal |
597 } // namespace protobuf | 610 } // namespace protobuf |
598 } // namespace google | 611 } // namespace google |
599 | 612 |
600 #endif // GOOGLE_PROTOBUF_REFLECTION_H__ | 613 #endif // GOOGLE_PROTOBUF_REFLECTION_H__ |
OLD | NEW |