change version to 0.7.3
[ncmpcpp.git] / src / strbuffer.h
blobc5b59be66f7a419203505c3f7c5bf0a28fb2a4f4
1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #ifndef NCMPCPP_STRBUFFER_H
22 #define NCMPCPP_STRBUFFER_H
24 #include <boost/lexical_cast.hpp>
25 #include <map>
26 #include "window.h"
28 namespace NC {
30 /// Buffer template class that stores text
31 /// along with its properties (colors/formatting).
32 template <typename CharT> class BasicBuffer
34 struct Property
36 enum class Type { Color, Format };
38 Property(NC::Color color_, size_t id_)
39 : m_type(Type::Color), m_color(std::move(color_)), m_id(id_) { }
40 Property(NC::Format format_, size_t id_)
41 : m_type(Type::Format), m_format(format_), m_id(id_) { }
43 size_t id() const { return m_id; }
45 template <typename OutputStreamT>
46 friend OutputStreamT &operator<<(OutputStreamT &os, const Property &p)
48 switch (p.m_type)
50 case Type::Color:
51 os << p.m_color;
52 break;
53 case Type::Format:
54 os << p.m_format;
55 break;
57 return os;
60 private:
61 Type m_type;
62 Color m_color;
63 Format m_format;
64 size_t m_id;
67 public:
68 typedef std::basic_string<CharT> StringType;
69 typedef std::multimap<size_t, Property> Properties;
71 const StringType &str() const { return m_string; }
72 const Properties &properties() const { return m_properties; }
74 template <typename PropertyT>
75 void addProperty(size_t position, PropertyT &&property, size_t id = -1)
77 assert(position <= m_string.size());
78 m_properties.emplace(position, Property(std::forward<PropertyT>(property), id));
81 void removeProperties(size_t id = -1)
83 auto it = m_properties.begin();
84 while (it != m_properties.end())
86 if (it->second.id() == id)
87 m_properties.erase(it++);
88 else
89 ++it;
93 void clear()
95 m_string.clear();
96 m_properties.clear();
99 BasicBuffer<CharT> &operator<<(int n)
101 m_string += boost::lexical_cast<StringType>(n);
102 return *this;
105 BasicBuffer<CharT> &operator<<(long int n)
107 m_string += boost::lexical_cast<StringType>(n);
108 return *this;
111 BasicBuffer<CharT> &operator<<(unsigned int n)
113 m_string += boost::lexical_cast<StringType>(n);
114 return *this;
117 BasicBuffer<CharT> &operator<<(unsigned long int n)
119 m_string += boost::lexical_cast<StringType>(n);
120 return *this;
123 BasicBuffer<CharT> &operator<<(CharT c)
125 m_string += c;
126 return *this;
129 BasicBuffer<CharT> &operator<<(const CharT *s)
131 m_string += s;
132 return *this;
135 BasicBuffer<CharT> &operator<<(const StringType &s)
137 m_string += s;
138 return *this;
141 BasicBuffer<CharT> &operator<<(Color color)
143 addProperty(m_string.size(), color);
144 return *this;
147 BasicBuffer<CharT> &operator<<(Format format)
149 addProperty(m_string.size(), format);
150 return *this;
153 // static variadic initializer. used instead of a proper constructor because
154 // it's too polymorphic and would end up invoked as a copy/move constructor.
155 template <typename... Args>
156 static BasicBuffer init(Args&&... args)
158 BasicBuffer result;
159 result.construct(std::forward<Args>(args)...);
160 return result;
163 private:
164 void construct() { }
165 template <typename ArgT, typename... Args>
166 void construct(ArgT &&arg, Args&&... args)
168 *this << std::forward<ArgT>(arg);
169 construct(std::forward<Args>(args)...);
172 StringType m_string;
173 Properties m_properties;
176 typedef BasicBuffer<char> Buffer;
177 typedef BasicBuffer<wchar_t> WBuffer;
179 template <typename OutputStreamT, typename CharT>
180 OutputStreamT &operator<<(OutputStreamT &os, const BasicBuffer<CharT> &buffer)
182 if (buffer.properties().empty())
183 os << buffer.str();
184 else
186 auto &s = buffer.str();
187 auto &ps = buffer.properties();
188 auto p = ps.begin();
189 for (size_t i = 0;; ++i)
191 for (; p != ps.end() && p->first == i; ++p)
192 os << p->second;
193 if (i < s.size())
194 os << s[i];
195 else
196 break;
199 return os;
204 #endif // NCMPCPP_STRBUFFER_H