1 #ifndef _DEPOOLSTRING_HPP 2 #define _DEPOOLSTRING_HPP 3 /*------------------------------------------------------------------------- 4 * drawElements C++ Base Library 5 * ----------------------------- 6 * 7 * Copyright 2014 The Android Open Source Project 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * 21 *//*! 22 * \file 23 * \brief Memory pool -backed string. 24 *//*--------------------------------------------------------------------*/ 25 26 #include "deDefs.hpp" 27 #include "deMemPool.hpp" 28 #include "dePoolArray.hpp" 29 30 #include <ostream> 31 #include <string> 32 33 namespace de 34 { 35 36 /*--------------------------------------------------------------------*//*! 37 * \brief String template backed by memory pool 38 * 39 * \note Memory in PoolString is not contiguous so pointer arithmetic 40 * to access next element(s) doesn't work. 41 *//*--------------------------------------------------------------------*/ 42 class PoolString : public PoolArray<char> 43 { 44 public: 45 explicit PoolString(MemPool *pool); 46 PoolString(MemPool *pool, const PoolString &other); 47 ~PoolString(void); 48 49 void toString(std::string &str) const; 50 std::string toString(void) const; 51 52 void append(const char *str); 53 void append(const std::string &str); 54 void append(const PoolString &str); 55 operator =(const char * str)56 PoolString &operator=(const char *str) 57 { 58 clear(); 59 append(str); 60 return *this; 61 } operator =(const std::string & str)62 PoolString &operator=(const std::string &str) 63 { 64 clear(); 65 append(str); 66 return *this; 67 } 68 PoolString &operator=(const PoolString &str); 69 operator +=(const char * str)70 PoolString &operator+=(const char *str) 71 { 72 append(str); 73 return *this; 74 } operator +=(const std::string & str)75 PoolString &operator+=(const std::string &str) 76 { 77 append(str); 78 return *this; 79 } operator +=(const PoolString & str)80 PoolString &operator+=(const PoolString &str) 81 { 82 append(str); 83 return *this; 84 } 85 86 private: 87 PoolString(const PoolString &other); 88 }; 89 90 // Operators. 91 std::ostream &operator<<(std::ostream &stream, const PoolString &string); 92 93 // PoolString inline implementations. 94 PoolString(MemPool * pool)95inline PoolString::PoolString(MemPool *pool) : PoolArray<char>(pool) 96 { 97 } 98 ~PoolString(void)99inline PoolString::~PoolString(void) 100 { 101 } 102 toString(void) const103inline std::string PoolString::toString(void) const 104 { 105 std::string str; 106 toString(str); 107 return str; 108 } 109 operator =(const PoolString & str)110inline PoolString &PoolString::operator=(const PoolString &str) 111 { 112 if (this == &str) 113 return *this; 114 115 clear(); 116 append(str); 117 return *this; 118 } 119 120 } // namespace de 121 122 #endif // _DEPOOLSTRING_HPP 123