1 /***************************************************************************
2 * Copyright (C) 2008-2017 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_HAVE_FORMAT_H
22 #define NCMPCPP_HAVE_FORMAT_H
24 #include <boost/variant.hpp>
26 #include "curses/menu.h"
32 const unsigned None
= 0;
33 const unsigned Color
= 1;
34 const unsigned Format
= 2;
35 const unsigned OutputSwitch
= 4;
36 const unsigned Tag
= 8;
37 const unsigned All
= Color
| Format
| OutputSwitch
| Tag
;
40 enum class ListType
{ Group
, FirstOf
, AST
};
42 template <ListType
, typename
> struct List
;
43 template <typename CharT
> using Group
= List
<ListType::Group
, CharT
>;
44 template <typename CharT
> using FirstOf
= List
<ListType::FirstOf
, CharT
>;
45 template <typename CharT
> using AST
= List
<ListType::AST
, CharT
>;
47 struct OutputSwitch
{ };
51 SongTag(MPD::Song::GetFunction function_
, unsigned delimiter_
= 0)
52 : m_function(function_
), m_delimiter(delimiter_
)
55 MPD::Song::GetFunction
function() const { return m_function
; }
56 unsigned delimiter() const { return m_delimiter
; }
59 MPD::Song::GetFunction m_function
;
63 inline bool operator==(const SongTag
&lhs
, const SongTag
&rhs
) {
64 return lhs
.function() == rhs
.function()
65 && lhs
.delimiter() == rhs
.delimiter();
67 inline bool operator!=(const SongTag
&lhs
, const SongTag
&rhs
) {
71 template <typename CharT
>
72 using TagVector
= std::vector
<
74 boost::optional
<SongTag
>,
75 std::basic_string
<CharT
>
79 enum class Result
{ Empty
, Missing
, Ok
};
81 template <typename CharT
>
82 using Expression
= boost::variant
<
83 std::basic_string
<CharT
>,
88 boost::recursive_wrapper
<FirstOf
<CharT
>>,
89 boost::recursive_wrapper
<Group
<CharT
>>
92 template <ListType Type
, typename CharT
>
95 typedef std::vector
<Expression
<CharT
>> Base
;
99 : m_base(std::move(base_
))
102 Base
&base() { return m_base
; }
103 const Base
&base() const { return m_base
; }
109 template <typename CharT
, typename VisitorT
>
110 void visit(VisitorT
&visitor
, const AST
<CharT
> &ast
);
112 template <typename CharT
, typename ItemT
>
113 void print(const AST
<CharT
> &ast
, NC::Menu
<ItemT
> &menu
, const MPD::Song
*song
,
114 NC::BasicBuffer
<CharT
> *buffer
, const unsigned flags
= Flags::All
);
116 template <typename CharT
>
117 void print(const AST
<CharT
> &ast
, NC::BasicBuffer
<CharT
> &buffer
,
118 const MPD::Song
*song
, const unsigned flags
= Flags::All
);
120 template <typename CharT
>
121 std::basic_string
<CharT
> stringify(const AST
<CharT
> &ast
, const MPD::Song
*song
);
123 template <typename CharT
>
124 TagVector
<CharT
> flatten(const AST
<CharT
> &ast
, const MPD::Song
&song
);
126 AST
<char> parse(const std::string
&s
, const unsigned flags
= Flags::All
);
127 AST
<wchar_t> parse(const std::wstring
&ws
, const unsigned flags
= Flags::All
);
131 #endif // NCMPCPP_HAVE_FORMAT_H