1fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Protocol Buffers - Google's data interchange format
2fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Copyright 2008 Google Inc.  All rights reserved.
3fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// http://code.google.com/p/protobuf/
4fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
5fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Redistribution and use in source and binary forms, with or without
6fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// modification, are permitted provided that the following conditions are
7fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// met:
8fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
9fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Redistributions of source code must retain the above copyright
10fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// notice, this list of conditions and the following disclaimer.
11fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Redistributions in binary form must reproduce the above
12fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// copyright notice, this list of conditions and the following disclaimer
13fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// in the documentation and/or other materials provided with the
14fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// distribution.
15fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Neither the name of Google Inc. nor the names of its
16fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// contributors may be used to endorse or promote products derived from
17fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// this software without specific prior written permission.
18fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
19fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
31fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// from google3/util/gtl/stl_util-inl.h
32fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
33fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#ifndef GOOGLE_PROTOBUF_STUBS_STL_UTIL_INL_H__
34fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GOOGLE_PROTOBUF_STUBS_STL_UTIL_INL_H__
35fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
36fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <google/protobuf/stubs/common.h>
37fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
38fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace google {
39fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace protobuf {
40fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
41fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// STLDeleteContainerPointers()
42fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//  For a range within a container of pointers, calls delete
43fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//  (non-array version) on these pointers.
44fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// NOTE: for these three functions, we could just implement a DeleteObject
45fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// functor and then call for_each() on the range and functor, but this
46fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// requires us to pull in all of algorithm.h, which seems expensive.
47fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// For hash_[multi]set, it is important that this deletes behind the iterator
48fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// because the hash_set may call the hash function on the iterator when it is
49fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// advanced, which could result in the hash function trying to deference a
50fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// stale pointer.
51fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <class ForwardIterator>
52fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillevoid STLDeleteContainerPointers(ForwardIterator begin,
53fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                                ForwardIterator end) {
54fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  while (begin != end) {
55fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    ForwardIterator temp = begin;
56fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    ++begin;
57fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    delete *temp;
58fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
59fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
60fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
61fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Inside Google, this function implements a horrible, disgusting hack in which
62fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// we reach into the string's private implementation and resize it without
63fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// initializing the new bytes.  In some cases doing this can significantly
64fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// improve performance.  However, since it's totally non-portable it has no
65fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// place in open source code.  Feel free to fill this function in with your
66fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// own disgusting hack if you want the perf boost.
67fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline void STLStringResizeUninitialized(string* s, size_t new_size) {
68fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  s->resize(new_size);
69fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
70fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
71fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Return a mutable char* pointing to a string's internal buffer,
72fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// which may not be null-terminated. Writing through this pointer will
73fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// modify the string.
74fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
75fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// string_as_array(&str)[i] is valid for 0 <= i < str.size() until the
76fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// next call to a string method that invalidates iterators.
77fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
78fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// As of 2006-04, there is no standard-blessed way of getting a
79fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// mutable reference to a string's internal buffer. However, issue 530
80fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#530)
81fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// proposes this as the method. According to Matt Austern, this should
82fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// already work on all current implementations.
83fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline char* string_as_array(string* str) {
84fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // DO NOT USE const_cast<char*>(str->data())! See the unittest for why.
85fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return str->empty() ? NULL : &*str->begin();
86fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
87fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
88fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// STLDeleteElements() deletes all the elements in an STL container and clears
89fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// the container.  This function is suitable for use with a vector, set,
90fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// hash_set, or any other STL container which defines sensible begin(), end(),
91fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// and clear() methods.
92fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
93fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// If container is NULL, this function is a no-op.
94fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
95fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// As an alternative to calling STLDeleteElements() directly, consider
96fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// ElementDeleter (defined below), which ensures that your container's elements
97fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// are deleted when the ElementDeleter goes out of scope.
98fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <class T>
99fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillevoid STLDeleteElements(T *container) {
100fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  if (!container) return;
101fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  STLDeleteContainerPointers(container->begin(), container->end());
102fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  container->clear();
103fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
104fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
105fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Given an STL container consisting of (key, value) pairs, STLDeleteValues
106fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// deletes all the "value" components and clears the container.  Does nothing
107fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// in the case it's given a NULL pointer.
108fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
109fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <class T>
110fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillevoid STLDeleteValues(T *v) {
111fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  if (!v) return;
112fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  for (typename T::iterator i = v->begin(); i != v->end(); ++i) {
113fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    delete i->second;
114fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
115fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  v->clear();
116fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
117fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
118fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}  // namespace protobuf
119fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}  // namespace google
120fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
121fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif  // GOOGLE_PROTOBUF_STUBS_STL_UTIL_INL_H__
122