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/
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
9
// http://www.apache.org/licenses/LICENSE-2.0
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
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
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.
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.
17
31
// Author: kenton@google.com (Kenton Varda)
18
32
// Based on original Protocol Buffers design by
55
69
class LIBPROTOBUF_EXPORT GenericRepeatedField {
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() {}
58
76
virtual ~GenericRepeatedField();
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;
72
92
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GenericRepeatedField);
95
// We need this (from generated_message_reflection.cc).
96
int StringSpaceUsedExcludingSelf(const string& str);
75
98
} // namespace internal
77
100
// RepeatedField is used to represent repeated fields of a primitive type (in
122
145
const_iterator end() const;
147
// Returns the number of bytes used by the repeated field, excluding
149
int SpaceUsedExcludingSelf() const;
124
151
private: // See GenericRepeatedField for why this is private.
125
152
// implements GenericRepeatedField ---------------------------------
126
153
const void* GenericGet(int index) const;
196
224
const_iterator end() const;
226
// Returns (an estimate of) the number of bytes used by the repeated field,
227
// excluding sizeof(*this).
228
int SpaceUsedExcludingSelf() const;
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.
502
548
current_size_ = 0;
551
#if defined(__DECCXX) && defined(__osf__)
552
// HP C++ on Tru64 has trouble when this is not defined inline.
554
inline void RepeatedPtrField<string>::Clear() {
555
for (int i = 0; i < current_size_; i++) {
556
elements_[i]->clear();
505
561
// Specialization defined in repeated_field.cc.
507
563
void LIBPROTOBUF_EXPORT RepeatedPtrField<string>::Clear();
509
566
template <typename Element>
510
567
void RepeatedPtrField<Element>::MergeFrom(const RepeatedPtrField& other) {
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]);
632
return allocated_bytes;
635
template <typename Element>
636
inline int RepeatedPtrField<Element>::ElementSpaceUsed(Element* e) const {
637
return e->SpaceUsed();
641
inline int RepeatedPtrField<string>::ElementSpaceUsed(string* s) const {
642
return sizeof(*s) + internal::StringSpaceUsedExcludingSelf(*s);
569
646
template <typename Element>
570
647
inline void RepeatedPtrField<Element>::AddAllocated(Element* value) {
684
766
typename internal::remove_pointer<It>::type>::type> {
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;
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;
691
780
RepeatedPtrIterator() : it_(NULL) {}
692
781
explicit RepeatedPtrIterator(const It& it) : it_(it) {}