~ubuntu-branches/debian/squeeze/protobuf/squeeze

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Julien Cristau
  • Date: 2009-06-02 16:19:00 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090602161900-vm176i3ryt35yk91
Tags: 2.0.3-2.2
* Non-maintainer upload.
* Fix FTBFS from -2.1: don't fail when we can't clean up the java build,
  such as when openjdk isn't installed.
* Disable parallel builds, because libtool is made of fail (if binary-arch
  and build-indep run concurrently, we relink a library while it's being
  used; that doesn't work so well).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// Protocol Buffers - Google's data interchange format
2
 
// Copyright 2008 Google Inc.
 
2
// Copyright 2008 Google Inc.  All rights reserved.
3
3
// http://code.google.com/p/protobuf/
4
4
//
5
 
// Licensed under the Apache License, Version 2.0 (the "License");
6
 
// you may not use this file except in compliance with the License.
7
 
// You may obtain a copy of the License at
8
 
//
9
 
//      http://www.apache.org/licenses/LICENSE-2.0
10
 
//
11
 
// Unless required by applicable law or agreed to in writing, software
12
 
// distributed under the License is distributed on an "AS IS" BASIS,
13
 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
// See the License for the specific language governing permissions and
15
 
// limitations under the License.
 
5
// Redistribution and use in source and binary forms, with or without
 
6
// modification, are permitted provided that the following conditions are
 
7
// met:
 
8
//
 
9
//     * Redistributions of source code must retain the above copyright
 
10
// notice, this list of conditions and the following disclaimer.
 
11
//     * Redistributions in binary form must reproduce the above
 
12
// copyright notice, this list of conditions and the following disclaimer
 
13
// in the documentation and/or other materials provided with the
 
14
// distribution.
 
15
//     * Neither the name of Google Inc. nor the names of its
 
16
// contributors may be used to endorse or promote products derived from
 
17
// this software without specific prior written permission.
 
18
//
 
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
20
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
21
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
22
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
23
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
24
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
25
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
26
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
27
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
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.
16
30
 
17
31
// Author: kenton@google.com (Kenton Varda)
18
32
//  Based on original Protocol Buffers design by
55
69
class LIBPROTOBUF_EXPORT GenericRepeatedField {
56
70
 public:
57
71
  inline GenericRepeatedField() {}
 
72
#if defined(__DECCXX) && defined(__osf__)
 
73
  // HP C++ on Tru64 has trouble when this is not defined inline.
 
74
  virtual ~GenericRepeatedField() {}
 
75
#else
58
76
  virtual ~GenericRepeatedField();
 
77
#endif
59
78
 
60
79
 private:
61
80
  // We only want GeneratedMessageReflection to see and use these, so we
68
87
  virtual void* GenericAdd() = 0;
69
88
  virtual void GenericClear() = 0;
70
89
  virtual int GenericSize() const = 0;
 
90
  virtual int GenericSpaceUsedExcludingSelf() const = 0;
71
91
 
72
92
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GenericRepeatedField);
73
93
};
74
94
 
 
95
// We need this (from generated_message_reflection.cc).
 
96
int StringSpaceUsedExcludingSelf(const string& str);
 
97
 
75
98
}  // namespace internal
76
99
 
77
100
// RepeatedField is used to represent repeated fields of a primitive type (in
121
144
  iterator end();
122
145
  const_iterator end() const;
123
146
 
 
147
  // Returns the number of bytes used by the repeated field, excluding
 
148
  // sizeof(*this)
 
149
  int SpaceUsedExcludingSelf() const;
 
150
 
124
151
 private:  // See GenericRepeatedField for why this is private.
125
152
  // implements GenericRepeatedField ---------------------------------
126
153
  const void* GenericGet(int index) const;
128
155
  void* GenericAdd();
129
156
  void GenericClear();
130
157
  int GenericSize() const;
 
158
  int GenericSpaceUsedExcludingSelf() const;
131
159
 
132
160
 private:
133
161
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedField);
195
223
  iterator end();
196
224
  const_iterator end() const;
197
225
 
 
226
  // Returns (an estimate of) the number of bytes used by the repeated field,
 
227
  // excluding sizeof(*this).
 
228
  int SpaceUsedExcludingSelf() const;
 
229
 
198
230
  // Advanced memory management --------------------------------------
199
231
  // When hardcore memory management becomes necessary -- as it often
200
232
  // does here at Google -- the following methods may be useful.
235
267
  void* GenericAdd();
236
268
  void GenericClear();
237
269
  int GenericSize() const;
 
270
  int GenericSpaceUsedExcludingSelf() const;
238
271
 
239
272
 private:
 
273
  // Returns (an estimate of) the number of bytes used by an individual
 
274
  // element.
 
275
  int ElementSpaceUsed(Element* element) const;
 
276
 
240
277
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedPtrField);
241
278
 
242
279
  static const int kInitialSize = 4;
379
416
  return elements_ + current_size_;
380
417
}
381
418
 
 
419
template <typename Element>
 
420
inline int RepeatedField<Element>::SpaceUsedExcludingSelf() const {
 
421
  return (elements_ != initial_space_) ? total_size_ * sizeof(elements_[0]) : 0;
 
422
}
382
423
 
383
424
template <typename Element>
384
425
const void* RepeatedField<Element>::GenericGet(int index) const {
408
449
}
409
450
 
410
451
template <typename Element>
 
452
int RepeatedField<Element>::GenericSpaceUsedExcludingSelf() const {
 
453
  return SpaceUsedExcludingSelf();
 
454
}
 
455
 
 
456
template <typename Element>
411
457
inline void RepeatedField<Element>::Reserve(int new_size) {
412
458
  if (total_size_ >= new_size) return;
413
459
 
502
548
  current_size_ = 0;
503
549
}
504
550
 
 
551
#if defined(__DECCXX) && defined(__osf__)
 
552
// HP C++ on Tru64 has trouble when this is not defined inline.
 
553
template <>
 
554
inline void RepeatedPtrField<string>::Clear() {
 
555
  for (int i = 0; i < current_size_; i++) {
 
556
    elements_[i]->clear();
 
557
  }
 
558
  current_size_ = 0;
 
559
}
 
560
#else
505
561
// Specialization defined in repeated_field.cc.
506
562
template <>
507
563
void LIBPROTOBUF_EXPORT RepeatedPtrField<string>::Clear();
 
564
#endif
508
565
 
509
566
template <typename Element>
510
567
void RepeatedPtrField<Element>::MergeFrom(const RepeatedPtrField& other) {
565
622
  }
566
623
}
567
624
 
 
625
template <typename Element>
 
626
inline int RepeatedPtrField<Element>::SpaceUsedExcludingSelf() const {
 
627
  int allocated_bytes =
 
628
      (elements_ != initial_space_) ? total_size_ * sizeof(elements_[0]) : 0;
 
629
  for (int i = 0; i < allocated_size_; ++i) {
 
630
    allocated_bytes += ElementSpaceUsed(elements_[i]);
 
631
  }
 
632
  return allocated_bytes;
 
633
}
 
634
 
 
635
template <typename Element>
 
636
inline int RepeatedPtrField<Element>::ElementSpaceUsed(Element* e) const {
 
637
  return e->SpaceUsed();
 
638
}
 
639
 
 
640
template <>
 
641
inline int RepeatedPtrField<string>::ElementSpaceUsed(string* s) const {
 
642
  return sizeof(*s) + internal::StringSpaceUsedExcludingSelf(*s);
 
643
}
 
644
 
568
645
 
569
646
template <typename Element>
570
647
inline void RepeatedPtrField<Element>::AddAllocated(Element* value) {
635
712
  return size();
636
713
}
637
714
 
 
715
template <typename Element>
 
716
int RepeatedPtrField<Element>::GenericSpaceUsedExcludingSelf() const {
 
717
  return SpaceUsedExcludingSelf();
 
718
}
 
719
 
638
720
 
639
721
template <typename Element>
640
722
inline void RepeatedPtrField<Element>::Reserve(int new_size) {
684
766
              typename internal::remove_pointer<It>::type>::type> {
685
767
 public:
686
768
  typedef RepeatedPtrIterator<It> iterator;
687
 
  typedef typename iterator::reference reference;
688
 
  typedef typename iterator::pointer pointer;
689
 
  typedef typename iterator::difference_type difference_type;
 
769
  typedef std::iterator<
 
770
          std::random_access_iterator_tag,
 
771
          typename internal::remove_pointer<
 
772
              typename internal::remove_pointer<It>::type>::type> superclass;
 
773
 
 
774
  // Let the compiler know that these are type names, so we don't have to
 
775
  // write "typename" in front of them everywhere.
 
776
  typedef typename superclass::reference reference;
 
777
  typedef typename superclass::pointer pointer;
 
778
  typedef typename superclass::difference_type difference_type;
690
779
 
691
780
  RepeatedPtrIterator() : it_(NULL) {}
692
781
  explicit RepeatedPtrIterator(const It& it) : it_(it) {}