Compile cleanly with GCC 4.9.4
[ncmpcpp.git] / src / regex_filter.h
blob398b0e05a4d2437c8587bcaeef750c45e6ddb5af
1 /***************************************************************************
2 * Copyright (C) 2008-2016 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_REGEX_FILTER_H
22 #define NCMPCPP_REGEX_FILTER_H
24 #include "config.h"
26 #ifdef BOOST_REGEX_ICU
27 # include <boost/regex/icu.hpp>
28 #else
29 # include <boost/regex.hpp>
30 #endif // BOOST_REGEX_ICU
32 #include <cassert>
33 #include <iostream>
35 namespace Regex {
37 typedef
38 #ifdef BOOST_REGEX_ICU
39 boost::u32regex
40 #else
41 boost::regex
42 #endif // BOOST_REGEX_ICU
43 Regex;
45 template <typename StringT>
46 inline Regex make(StringT &&s, boost::regex_constants::syntax_option_type flags)
48 return
49 # ifdef BOOST_REGEX_ICU
50 boost::make_u32regex
51 # else
52 boost::regex
53 # endif // BOOST_REGEX_ICU
54 (std::forward<StringT>(s), flags);
57 template <typename StringT>
58 inline bool search(StringT &&s, const Regex &rx)
60 try {
61 return
62 # ifdef BOOST_REGEX_ICU
63 boost::u32regex_search
64 # else
65 boost::regex_search
66 # endif // BOOST_REGEX_ICU
67 (std::forward<StringT>(s), rx);
68 } catch (std::out_of_range &e) {
69 // Invalid UTF-8 sequence, ignore the string.
70 std::cerr << "Regex::search: error while processing \"" << s << "\": " << e.what() << "\n";
71 return false;
75 template <typename T>
76 struct Filter
78 typedef NC::Menu<T> MenuT;
79 typedef typename NC::Menu<T>::Item Item;
80 typedef std::function<bool(const Regex &, const T &)> FilterFunction;
82 Filter() { }
84 template <typename FilterT>
85 Filter(const std::string &constraint_,
86 boost::regex_constants::syntax_option_type flags,
87 FilterT &&filter)
88 : m_rx(make(constraint_, flags))
89 , m_constraint(constraint_)
90 , m_filter(std::forward<FilterT>(filter))
91 { }
93 void clear()
95 m_filter = nullptr;
98 const std::string &constraint() const {
99 return m_constraint;
102 bool operator()(const Item &item) const {
103 assert(defined());
104 return m_filter(m_rx, item.value());
107 bool defined() const
109 return m_filter.operator bool();
112 private:
113 Regex m_rx;
114 std::string m_constraint;
115 FilterFunction m_filter;
118 template <typename T> struct ItemFilter
120 typedef NC::Menu<T> MenuT;
121 typedef typename NC::Menu<T>::Item Item;
122 typedef std::function<bool(const Regex &, const Item &)> FilterFunction;
124 ItemFilter() { }
126 template <typename FilterT>
127 ItemFilter(const std::string &constraint_,
128 boost::regex_constants::syntax_option_type flags,
129 FilterT &&filter)
130 : m_rx(make(constraint_, flags))
131 , m_constraint(constraint_)
132 , m_filter(std::forward<FilterT>(filter))
135 void clear()
137 m_filter = nullptr;
140 const std::string &constraint() const {
141 return m_constraint;
144 bool operator()(const Item &item) {
145 return m_filter(m_rx, item);
148 bool defined() const
150 return m_filter.operator bool();
153 private:
154 Regex m_rx;
155 std::string m_constraint;
156 FilterFunction m_filter;
161 #endif // NCMPCPP_REGEX_FILTER_H