split PressSpace action into modular pieces
[ncmpcpp.git] / src / regex_filter.h
bloba4abd1f452f130b9a1e100918a57638960b768be
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_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>
34 namespace Regex {
36 typedef
37 #ifdef BOOST_REGEX_ICU
38 boost::u32regex
39 #else
40 boost::regex
41 #endif // BOOST_REGEX_ICU
42 Regex;
44 template <typename StringT>
45 inline Regex make(StringT &&s, boost::regex_constants::syntax_option_type flags)
47 return
48 # ifdef BOOST_REGEX_ICU
49 boost::make_u32regex
50 # else
51 boost::regex
52 # endif // BOOST_REGEX_ICU
53 (std::forward<StringT>(s), flags);
56 template <typename StringT>
57 inline bool search(StringT &&s, const Regex &rx)
59 return
60 # ifdef BOOST_REGEX_ICU
61 boost::u32regex_search
62 # else
63 boost::regex_search
64 # endif // BOOST_REGEX_ICU
65 (std::forward<StringT>(s), rx);
68 template <typename T>
69 struct Filter
71 typedef NC::Menu<T> MenuT;
72 typedef typename NC::Menu<T>::Item Item;
73 typedef std::function<bool(const Regex &, const T &)> FilterFunction;
75 Filter() { }
76 Filter(Regex rx, FilterFunction filter)
77 : m_rx(std::move(rx)), m_filter(std::move(filter)) { }
79 void clear()
81 m_filter = nullptr;
84 bool operator()(const Item &item) const {
85 assert(defined());
86 return m_filter(m_rx, item.value());
89 bool defined() const
91 return m_filter.operator bool();
94 private:
95 Regex m_rx;
96 FilterFunction m_filter;
99 template <typename T> struct ItemFilter
101 typedef NC::Menu<T> MenuT;
102 typedef typename NC::Menu<T>::Item Item;
103 typedef std::function<bool(const Regex &, const Item &)> FilterFunction;
105 ItemFilter() { }
106 ItemFilter(Regex rx, FilterFunction filter)
107 : m_rx(std::move(rx)), m_filter(std::move(filter)) { }
109 void clear()
111 m_filter = nullptr;
114 bool operator()(const Item &item) {
115 return m_filter(m_rx, item);
118 bool defined() const
120 return m_filter.operator bool();
123 private:
124 Regex m_rx;
125 FilterFunction m_filter;
130 #endif // NCMPCPP_REGEX_FILTER_H