1 // Copyright 2014 The Android Open Source Project 2 // 3 // This software is licensed under the terms of the GNU General Public 4 // License version 2, as published by the Free Software Foundation, and 5 // may be copied, distributed, and modified under those terms. 6 // 7 // This program is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 // GNU General Public License for more details. 11 12 #ifndef ANDROID_BASE_CONTAINERS_STRING_VECTOR_H 13 #define ANDROID_BASE_CONTAINERS_STRING_VECTOR_H 14 15 #include "android/base/containers/PodVector.h" 16 #include "android/base/String.h" 17 #include "android/base/StringView.h" 18 19 namespace android { 20 namespace base { 21 22 // A StringVector is a vector of strings. This implementation is optimized 23 // to use less memory and be more efficient than std::vector<std::string> 24 // for most operations. 25 class StringVector : public PodVector<String> { 26 public: 27 // Default constructor. The vector will be empty. 28 StringVector() : PodVector<String>() {} 29 30 // Copy-constructor. 31 StringVector(const StringVector& other); 32 33 // Assignment operator 34 StringVector& operator=(const StringVector& other); 35 36 // Destructor. 37 ~StringVector(); 38 39 // Any operations that may change the underlying storage must be 40 // overriden. However, the behaviour / documentation should be 41 // identical to the one from PodVector<String> here. 42 void resize(size_t newSize); 43 void reserve(size_t newSize); 44 45 void remove(size_t index); 46 String* emplace(size_t index); 47 void insert(size_t index, const String& str); 48 void prepend(const String& str); 49 void append(const String& str); 50 void swap(StringVector* other); 51 52 // std::vector<> compatibility. 53 void push_back(const String& str) { append(str); } 54 void pop() { remove(0U); } 55 56 // The following specializations allow one to add items with 57 // a StringView reference instead, this avoids the need-less 58 // creation of a String instance when one wants to append 59 // a simple C string. 60 void insert(size_t index, const StringView& view); 61 void prepend(const StringView& view); 62 void append(const StringView& view); 63 }; 64 65 } // namespace base 66 } // namespace android 67 68 #endif // ANDROID_BASE_CONTAINERS_STRING_VECTOR_H 69