1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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. *
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. *
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>
30 /// Buffer template class that stores text
31 /// along with its properties (colors/formatting).
32 template <typename CharT
> class BasicBuffer
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
)
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
++);
99 BasicBuffer
<CharT
> &operator<<(int n
)
101 m_string
+= boost::lexical_cast
<StringType
>(n
);
105 BasicBuffer
<CharT
> &operator<<(long int n
)
107 m_string
+= boost::lexical_cast
<StringType
>(n
);
111 BasicBuffer
<CharT
> &operator<<(unsigned int n
)
113 m_string
+= boost::lexical_cast
<StringType
>(n
);
117 BasicBuffer
<CharT
> &operator<<(unsigned long int n
)
119 m_string
+= boost::lexical_cast
<StringType
>(n
);
123 BasicBuffer
<CharT
> &operator<<(CharT c
)
129 BasicBuffer
<CharT
> &operator<<(const CharT
*s
)
135 BasicBuffer
<CharT
> &operator<<(const StringType
&s
)
141 BasicBuffer
<CharT
> &operator<<(Color color
)
143 addProperty(m_string
.size(), color
);
147 BasicBuffer
<CharT
> &operator<<(Format format
)
149 addProperty(m_string
.size(), format
);
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
)
159 result
.construct(std::forward
<Args
>(args
)...);
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
)...);
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())
186 auto &s
= buffer
.str();
187 auto &ps
= buffer
.properties();
189 for (size_t i
= 0;; ++i
)
191 for (; p
!= ps
.end() && p
->first
== i
; ++p
)
204 #endif // NCMPCPP_STRBUFFER_H