~ubuntu-branches/debian/stretch/protobuf/stretch

« back to all changes in this revision

Viewing changes to src/google/protobuf/repeated_field.h

  • Committer: Package Import Robot
  • Author(s): Robert S. Edmonds
  • Date: 2014-09-11 22:50:10 UTC
  • mfrom: (10.1.9 experimental)
  • Revision ID: package-import@ubuntu.com-20140911225010-wt4yo9dpc1fzuq5g
Tags: 2.6.0-3
Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
#ifndef GOOGLE_PROTOBUF_REPEATED_FIELD_H__
47
47
#define GOOGLE_PROTOBUF_REPEATED_FIELD_H__
48
48
 
 
49
#ifdef _MSC_VER
 
50
// This is required for min/max on VS2013 only.
49
51
#include <algorithm>
 
52
#endif
 
53
 
50
54
#include <string>
51
55
#include <iterator>
52
56
#include <google/protobuf/stubs/common.h>
72
76
 
73
77
// A utility function for logging that doesn't need any template types.
74
78
void LogIndexOutOfBounds(int index, int size);
 
79
 
 
80
template <typename Iter>
 
81
inline int CalculateReserve(Iter begin, Iter end, std::forward_iterator_tag) {
 
82
  return std::distance(begin, end);
 
83
}
 
84
 
 
85
template <typename Iter>
 
86
inline int CalculateReserve(Iter begin, Iter end, std::input_iterator_tag) {
 
87
  return -1;
 
88
}
 
89
 
 
90
template <typename Iter>
 
91
inline int CalculateReserve(Iter begin, Iter end) {
 
92
  typedef typename std::iterator_traits<Iter>::iterator_category Category;
 
93
  return CalculateReserve(begin, end, Category());
 
94
}
75
95
}  // namespace internal
76
96
 
77
97
 
90
110
 
91
111
  RepeatedField& operator=(const RepeatedField& other);
92
112
 
 
113
  bool empty() const;
93
114
  int size() const;
94
115
 
95
116
  const Element& Get(int index) const;
121
142
  Element* AddAlreadyReserved();
122
143
  int Capacity() const;
123
144
 
 
145
  // Like STL resize.  Uses value to fill appended elements.
 
146
  // Like Truncate() if new_size <= size(), otherwise this is
 
147
  // O(new_size - size()).
 
148
  void Resize(int new_size, const Element& value);
 
149
 
124
150
  // Gets the underlying array.  This pointer is possibly invalidated by
125
151
  // any add or remove operation.
126
152
  Element* mutable_data();
245
271
  template <typename TypeHandler>
246
272
  void Destroy();
247
273
 
 
274
  bool empty() const;
248
275
  int size() const;
249
276
 
250
277
  template <typename TypeHandler>
309
336
  typename TypeHandler::Type* ReleaseCleared();
310
337
 
311
338
 private:
312
 
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedPtrFieldBase);
313
 
 
314
339
  static const int kInitialSize = 0;
315
340
 
316
341
  void** elements_;
326
351
  static inline const typename TypeHandler::Type* cast(const void* element) {
327
352
    return reinterpret_cast<const typename TypeHandler::Type*>(element);
328
353
  }
 
354
 
 
355
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedPtrFieldBase);
329
356
};
330
357
 
331
358
template <typename GenericType>
383
410
  static void Clear(string* value) { value->clear(); }
384
411
  static void Merge(const string& from, string* to) { *to = from; }
385
412
  static const Type& default_instance() {
386
 
    return ::google::protobuf::internal::kEmptyString;
 
413
    return ::google::protobuf::internal::GetEmptyString();
387
414
  }
388
415
};
389
416
 
410
437
 
411
438
  RepeatedPtrField& operator=(const RepeatedPtrField& other);
412
439
 
 
440
  bool empty() const;
413
441
  int size() const;
414
442
 
415
443
  const Element& Get(int index) const;
568
596
  : elements_(NULL),
569
597
    current_size_(0),
570
598
    total_size_(kInitialSize) {
571
 
  for (; begin != end; ++begin) {
572
 
    Add(*begin);
 
599
  int reserve = internal::CalculateReserve(begin, end);
 
600
  if (reserve != -1) {
 
601
    Reserve(reserve);
 
602
    for (; begin != end; ++begin) {
 
603
      AddAlreadyReserved(*begin);
 
604
    }
 
605
  } else {
 
606
    for (; begin != end; ++begin) {
 
607
      Add(*begin);
 
608
    }
573
609
  }
574
610
}
575
611
 
587
623
}
588
624
 
589
625
template <typename Element>
 
626
inline bool RepeatedField<Element>::empty() const {
 
627
  return current_size_ == 0;
 
628
}
 
629
 
 
630
template <typename Element>
590
631
inline int RepeatedField<Element>::size() const {
591
632
  return current_size_;
592
633
}
608
649
  return &elements_[current_size_++];
609
650
}
610
651
 
 
652
template<typename Element>
 
653
inline void RepeatedField<Element>::Resize(int new_size, const Element& value) {
 
654
  GOOGLE_DCHECK_GE(new_size, 0);
 
655
  if (new_size > size()) {
 
656
    Reserve(new_size);
 
657
    std::fill(&elements_[current_size_], &elements_[new_size], value);
 
658
  }
 
659
  current_size_ = new_size;
 
660
}
 
661
 
611
662
template <typename Element>
612
663
inline const Element& RepeatedField<Element>::Get(int index) const {
 
664
  GOOGLE_DCHECK_GE(index, 0);
613
665
  GOOGLE_DCHECK_LT(index, size());
614
666
  return elements_[index];
615
667
}
616
668
 
617
669
template <typename Element>
618
670
inline Element* RepeatedField<Element>::Mutable(int index) {
 
671
  GOOGLE_DCHECK_GE(index, 0);
619
672
  GOOGLE_DCHECK_LT(index, size());
620
673
  return elements_ + index;
621
674
}
622
675
 
623
676
template <typename Element>
624
677
inline void RepeatedField<Element>::Set(int index, const Element& value) {
 
678
  GOOGLE_DCHECK_GE(index, 0);
625
679
  GOOGLE_DCHECK_LT(index, size());
626
680
  elements_[index] = value;
627
681
}
672
726
 
673
727
template <typename Element>
674
728
inline void RepeatedField<Element>::MergeFrom(const RepeatedField& other) {
 
729
  GOOGLE_CHECK_NE(&other, this);
675
730
  if (other.current_size_ != 0) {
676
731
    Reserve(current_size_ + other.current_size_);
677
732
    CopyArray(elements_ + current_size_, other.elements_, other.current_size_);
681
736
 
682
737
template <typename Element>
683
738
inline void RepeatedField<Element>::CopyFrom(const RepeatedField& other) {
 
739
  if (&other == this) return;
684
740
  Clear();
685
741
  MergeFrom(other);
686
742
}
714
770
 
715
771
template <typename Element>
716
772
void RepeatedField<Element>::SwapElements(int index1, int index2) {
717
 
  std::swap(elements_[index1], elements_[index2]);
 
773
  using std::swap;  // enable ADL with fallback
 
774
  swap(elements_[index1], elements_[index2]);
718
775
}
719
776
 
720
777
template <typename Element>
814
871
  delete [] elements_;
815
872
}
816
873
 
 
874
inline bool RepeatedPtrFieldBase::empty() const {
 
875
  return current_size_ == 0;
 
876
}
 
877
 
817
878
inline int RepeatedPtrFieldBase::size() const {
818
879
  return current_size_;
819
880
}
821
882
template <typename TypeHandler>
822
883
inline const typename TypeHandler::Type&
823
884
RepeatedPtrFieldBase::Get(int index) const {
 
885
  GOOGLE_DCHECK_GE(index, 0);
824
886
  GOOGLE_DCHECK_LT(index, size());
825
887
  return *cast<TypeHandler>(elements_[index]);
826
888
}
829
891
template <typename TypeHandler>
830
892
inline typename TypeHandler::Type*
831
893
RepeatedPtrFieldBase::Mutable(int index) {
 
894
  GOOGLE_DCHECK_GE(index, 0);
832
895
  GOOGLE_DCHECK_LT(index, size());
833
896
  return cast<TypeHandler>(elements_[index]);
834
897
}
839
902
    return cast<TypeHandler>(elements_[current_size_++]);
840
903
  }
841
904
  if (allocated_size_ == total_size_) Reserve(total_size_ + 1);
 
905
  typename TypeHandler::Type* result = TypeHandler::New();
842
906
  ++allocated_size_;
843
 
  typename TypeHandler::Type* result = TypeHandler::New();
844
907
  elements_[current_size_++] = result;
845
908
  return result;
846
909
}
861
924
 
862
925
template <typename TypeHandler>
863
926
inline void RepeatedPtrFieldBase::MergeFrom(const RepeatedPtrFieldBase& other) {
 
927
  GOOGLE_CHECK_NE(&other, this);
864
928
  Reserve(current_size_ + other.current_size_);
865
929
  for (int i = 0; i < other.current_size_; i++) {
866
930
    TypeHandler::Merge(other.template Get<TypeHandler>(i), Add<TypeHandler>());
869
933
 
870
934
template <typename TypeHandler>
871
935
inline void RepeatedPtrFieldBase::CopyFrom(const RepeatedPtrFieldBase& other) {
 
936
  if (&other == this) return;
872
937
  RepeatedPtrFieldBase::Clear<TypeHandler>();
873
938
  RepeatedPtrFieldBase::MergeFrom<TypeHandler>(other);
874
939
}
901
966
}
902
967
 
903
968
inline void RepeatedPtrFieldBase::SwapElements(int index1, int index2) {
904
 
  std::swap(elements_[index1], elements_[index2]);
 
969
  using std::swap;  // enable ADL with fallback
 
970
  swap(elements_[index1], elements_[index2]);
905
971
}
906
972
 
907
973
template <typename TypeHandler>
1001
1067
 
1002
1068
template <typename Element>
1003
1069
inline RepeatedPtrField<Element>::RepeatedPtrField(
1004
 
    const RepeatedPtrField& other) {
 
1070
    const RepeatedPtrField& other)
 
1071
    : RepeatedPtrFieldBase() {
1005
1072
  CopyFrom(other);
1006
1073
}
1007
1074
 
1009
1076
template <typename Iter>
1010
1077
inline RepeatedPtrField<Element>::RepeatedPtrField(
1011
1078
    Iter begin, const Iter& end) {
 
1079
  int reserve = internal::CalculateReserve(begin, end);
 
1080
  if (reserve != -1) {
 
1081
    Reserve(reserve);
 
1082
  }
1012
1083
  for (; begin != end; ++begin) {
1013
1084
    *Add() = *begin;
1014
1085
  }
1028
1099
}
1029
1100
 
1030
1101
template <typename Element>
 
1102
inline bool RepeatedPtrField<Element>::empty() const {
 
1103
  return RepeatedPtrFieldBase::empty();
 
1104
}
 
1105
 
 
1106
template <typename Element>
1031
1107
inline int RepeatedPtrField<Element>::size() const {
1032
1108
  return RepeatedPtrFieldBase::size();
1033
1109
}
1182
1258
  typedef std::iterator<
1183
1259
          std::random_access_iterator_tag, Element> superclass;
1184
1260
 
 
1261
  // Shadow the value_type in std::iterator<> because const_iterator::value_type
 
1262
  // needs to be T, not const T.
 
1263
  typedef typename remove_const<Element>::type value_type;
 
1264
 
1185
1265
  // Let the compiler know that these are type names, so we don't have to
1186
1266
  // write "typename" in front of them everywhere.
1187
1267
  typedef typename superclass::reference reference;
1273
1353
  typedef std::iterator<
1274
1354
          std::random_access_iterator_tag, Element*> superclass;
1275
1355
 
 
1356
  // Shadow the value_type in std::iterator<> because const_iterator::value_type
 
1357
  // needs to be T, not const T.
 
1358
  typedef typename remove_const<Element*>::type value_type;
 
1359
 
1276
1360
  // Let the compiler know that these are type names, so we don't have to
1277
1361
  // write "typename" in front of them everywhere.
1278
1362
  typedef typename superclass::reference reference;