OsmAnd
|
00001 // Protocol Buffers - Google's data interchange format 00002 // Copyright 2008 Google Inc. All rights reserved. 00003 // http://code.google.com/p/protobuf/ 00004 // 00005 // Redistribution and use in source and binary forms, with or without 00006 // modification, are permitted provided that the following conditions are 00007 // met: 00008 // 00009 // * Redistributions of source code must retain the above copyright 00010 // notice, this list of conditions and the following disclaimer. 00011 // * Redistributions in binary form must reproduce the above 00012 // copyright notice, this list of conditions and the following disclaimer 00013 // in the documentation and/or other materials provided with the 00014 // distribution. 00015 // * Neither the name of Google Inc. nor the names of its 00016 // contributors may be used to endorse or promote products derived from 00017 // this software without specific prior written permission. 00018 // 00019 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00020 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00022 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00023 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00025 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00026 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00027 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00028 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00029 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 00031 // from google3/util/gtl/stl_util-inl.h 00032 00033 #ifndef GOOGLE_PROTOBUF_STUBS_STL_UTIL_INL_H__ 00034 #define GOOGLE_PROTOBUF_STUBS_STL_UTIL_INL_H__ 00035 00036 #include <google/protobuf/stubs/common.h> 00037 00038 namespace google { 00039 namespace protobuf { 00040 00041 // STLDeleteContainerPointers() 00042 // For a range within a container of pointers, calls delete 00043 // (non-array version) on these pointers. 00044 // NOTE: for these three functions, we could just implement a DeleteObject 00045 // functor and then call for_each() on the range and functor, but this 00046 // requires us to pull in all of algorithm.h, which seems expensive. 00047 // For hash_[multi]set, it is important that this deletes behind the iterator 00048 // because the hash_set may call the hash function on the iterator when it is 00049 // advanced, which could result in the hash function trying to deference a 00050 // stale pointer. 00051 template <class ForwardIterator> 00052 void STLDeleteContainerPointers(ForwardIterator begin, 00053 ForwardIterator end) { 00054 while (begin != end) { 00055 ForwardIterator temp = begin; 00056 ++begin; 00057 delete *temp; 00058 } 00059 } 00060 00061 // Inside Google, this function implements a horrible, disgusting hack in which 00062 // we reach into the string's private implementation and resize it without 00063 // initializing the new bytes. In some cases doing this can significantly 00064 // improve performance. However, since it's totally non-portable it has no 00065 // place in open source code. Feel free to fill this function in with your 00066 // own disgusting hack if you want the perf boost. 00067 inline void STLStringResizeUninitialized(string* s, size_t new_size) { 00068 s->resize(new_size); 00069 } 00070 00071 // Return a mutable char* pointing to a string's internal buffer, 00072 // which may not be null-terminated. Writing through this pointer will 00073 // modify the string. 00074 // 00075 // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the 00076 // next call to a string method that invalidates iterators. 00077 // 00078 // As of 2006-04, there is no standard-blessed way of getting a 00079 // mutable reference to a string's internal buffer. However, issue 530 00080 // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#530) 00081 // proposes this as the method. According to Matt Austern, this should 00082 // already work on all current implementations. 00083 inline char* string_as_array(string* str) { 00084 // DO NOT USE const_cast<char*>(str->data())! See the unittest for why. 00085 return str->empty() ? NULL : &*str->begin(); 00086 } 00087 00088 // STLDeleteElements() deletes all the elements in an STL container and clears 00089 // the container. This function is suitable for use with a vector, set, 00090 // hash_set, or any other STL container which defines sensible begin(), end(), 00091 // and clear() methods. 00092 // 00093 // If container is NULL, this function is a no-op. 00094 // 00095 // As an alternative to calling STLDeleteElements() directly, consider 00096 // ElementDeleter (defined below), which ensures that your container's elements 00097 // are deleted when the ElementDeleter goes out of scope. 00098 template <class T> 00099 void STLDeleteElements(T *container) { 00100 if (!container) return; 00101 STLDeleteContainerPointers(container->begin(), container->end()); 00102 container->clear(); 00103 } 00104 00105 // Given an STL container consisting of (key, value) pairs, STLDeleteValues 00106 // deletes all the "value" components and clears the container. Does nothing 00107 // in the case it's given a NULL pointer. 00108 00109 template <class T> 00110 void STLDeleteValues(T *v) { 00111 if (!v) return; 00112 for (typename T::iterator i = v->begin(); i != v->end(); ++i) { 00113 delete i->second; 00114 } 00115 v->clear(); 00116 } 00117 00118 } // namespace protobuf 00119 } // namespace google 00120 00121 #endif // GOOGLE_PROTOBUF_STUBS_STL_UTIL_INL_H__