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)			{ clear();	append(str);	return *this;	}
operator =(const std::string & str)57 	PoolString&		operator=			(const std::string& str)	{ clear();	append(str);	return *this;	}
58 	PoolString&		operator=			(const PoolString& str);
59 
operator +=(const char * str)60 	PoolString&		operator+=			(const char* str)			{ append(str);	return *this;	}
operator +=(const std::string & str)61 	PoolString&		operator+=			(const std::string& str)	{ append(str);	return *this;	}
operator +=(const PoolString & str)62 	PoolString&		operator+=			(const PoolString& str)		{ append(str);	return *this;	}
63 
64 private:
65 					PoolString			(const PoolString& other);
66 };
67 
68 // Operators.
69 std::ostream&	operator<<	(std::ostream& stream, const PoolString& string);
70 
71 // PoolString inline implementations.
72 
PoolString(MemPool * pool)73 inline PoolString::PoolString (MemPool* pool)
74 	: PoolArray<char>(pool)
75 {
76 }
77 
~PoolString(void)78 inline PoolString::~PoolString (void)
79 {
80 }
81 
toString(void) const82 inline std::string PoolString::toString (void) const
83 {
84 	std::string str;
85 	toString(str);
86 	return str;
87 }
88 
operator =(const PoolString & str)89 inline PoolString& PoolString::operator= (const PoolString& str)
90 {
91 	if (this == &str)
92 		return *this;
93 
94 	clear();
95 	append(str);
96 	return *this;
97 }
98 
99 } // de
100 
101 #endif // _DEPOOLSTRING_HPP
102