summaryrefslogtreecommitdiff
path: root/src/strbuffer.h
blob: 29923e078ae3d02b0815d085abdf26b57afa3fcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/***************************************************************************
 *   Copyright (C) 2008-2013 by Andrzej Rybczak                            *
 *   electricityispower@gmail.com                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 ***************************************************************************/

#ifndef NCMPCPP_STRBUFFER_H
#define NCMPCPP_STRBUFFER_H

#include <boost/lexical_cast.hpp>
#include <set>
#include "window.h"

namespace NC {//

/// Buffer template class that stores text
/// along with its properties (colors/formatting).
template <typename CharT> class BasicBuffer
{
	struct Property
	{
		enum class Type { Color, Format };
		
		Property(size_t position_, NC::Color color_, int id_)
		: m_type(Type::Color), m_position(position_), m_color(color_), m_id(id_) { }
		Property(size_t position_, NC::Format format_, int id_)
		: m_type(Type::Format), m_position(position_), m_format(format_), m_id(id_) { }
		
		size_t position() const { return m_position; }
		size_t id() const { return m_id; }
		
		bool operator<(const Property &rhs) const
		{
			if (m_position != rhs.m_position)
				return m_position < rhs.m_position;
			if (m_type != rhs.m_type)
				return m_type < rhs.m_type;
			switch (m_type)
			{
				case Type::Color:
					if (m_color != rhs.m_color)
						return m_color < rhs.m_color;
					break;
				case Type::Format:
					if (m_format != rhs.m_format)
						return m_format < rhs.m_format;
					break;
			}
			return m_id < rhs.m_id;
		}
		
		friend Window &operator<<(Window &w, const Property &p)
		{
			switch (p.m_type)
			{
				case Type::Color:
					w << p.m_color;
					break;
				case Type::Format:
					w << p.m_format;
					break;
			}
			return w;
		}
		
	private:
		Type m_type;
		size_t m_position;
		Color m_color;
		Format m_format;
		size_t m_id;
	};
	
public:
	typedef std::basic_string<CharT> StringType;
	typedef std::set<Property> Properties;
	
	BasicBuffer() { }
	
	const StringType &str() const { return m_string; }
	const Properties &properties() const { return m_properties; }
	
	template <typename PropertyT>
	void setProperty(size_t position, PropertyT property, size_t id = -1)
	{
		m_properties.insert(Property(position, property, id));
	}
	
	template <typename PropertyT>
	bool removeProperty(size_t position, PropertyT property, size_t id = -1)
	{
		auto it = m_properties.find(Property(position, property, id));
		bool found = it != m_properties.end();
		if (found)
			m_properties.erase(it);
		return found;
	}
	
	void removeProperties(size_t id = -1)
	{
		auto it = m_properties.begin();
		while (it != m_properties.end())
		{
			if (it->id() == id)
				m_properties.erase(it++);
			else
				++it;
		}
	}
	
	void clear()
	{
		m_string.clear();
		m_properties.clear();
	}
	
	BasicBuffer<CharT> &operator<<(int n)
	{
		m_string += boost::lexical_cast<StringType>(n);
		return *this;
	}
	
	BasicBuffer<CharT> &operator<<(long int n)
	{
		m_string += boost::lexical_cast<StringType>(n);
		return *this;
	}
	
	BasicBuffer<CharT> &operator<<(unsigned int n)
	{
		m_string += boost::lexical_cast<StringType>(n);
		return *this;
	}
	
	BasicBuffer<CharT> &operator<<(unsigned long int n)
	{
		m_string += boost::lexical_cast<StringType>(n);
		return *this;
	}
	
	BasicBuffer<CharT> &operator<<(CharT c)
	{
		m_string += c;
		return *this;
	}

	BasicBuffer<CharT> &operator<<(const CharT *s)
	{
		m_string += s;
		return *this;
	}
	
	BasicBuffer<CharT> &operator<<(const StringType &s)
	{
		m_string += s;
		return *this;
	}
	
	BasicBuffer<CharT> &operator<<(Color color)
	{
		setProperty(m_string.size(), color);
		return *this;
	}
	
	BasicBuffer<CharT> &operator<<(Format format)
	{
		setProperty(m_string.size(), format);
		return *this;
	}
	
private:
	StringType m_string;
	Properties m_properties;
};

typedef BasicBuffer<char> Buffer;
typedef BasicBuffer<wchar_t> WBuffer;

template <typename CharT>
Window &operator<<(Window &w, const BasicBuffer<CharT> &buffer)
{
	if (buffer.properties().empty())
		w << buffer.str();
	else
	{
		auto &s = buffer.str();
		auto &ps = buffer.properties();
		auto p = ps.begin();
		for (size_t i = 0; i < s.size(); ++i)
		{
			for (; p != ps.end() && p->position() == i; ++p)
				w << *p;
			w << s[i];
		}
		// load remaining properties
		for (; p != ps.end(); ++p)
			w << *p;
	}
	return w;
}

}

#endif // NCMPCPP_STRBUFFER_H