make sure to include boost posix_time where needed
[ncmpcpp.git] / src / regex_filter.h
blob1970f41e74058cc4ebd69c6a138071a819326d10
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 <boost/regex.hpp>
25 #include "menu.h"
27 template <typename T> struct RegexFilter
29 typedef NC::Menu<T> MenuT;
30 typedef typename NC::Menu<T>::Item Item;
31 typedef std::function<bool(const boost::regex &, const T &)> FilterFunction;
33 RegexFilter(boost::regex rx, FilterFunction filter)
34 : m_rx(rx), m_filter(filter) { }
36 bool operator()(const Item &item) {
37 return m_filter(m_rx, item.value());
40 static std::string currentFilter(MenuT &menu)
42 std::string filter;
43 auto rf = menu.getFilter().template target< RegexFilter<T> >();
44 if (rf)
45 filter = rf->m_rx.str();
46 return filter;
49 private:
50 boost::regex m_rx;
51 FilterFunction m_filter;
54 template <typename T> struct RegexItemFilter
56 typedef NC::Menu<T> MenuT;
57 typedef typename NC::Menu<T>::Item Item;
58 typedef std::function<bool(const boost::regex &, const Item &)> FilterFunction;
60 RegexItemFilter(boost::regex rx, FilterFunction filter)
61 : m_rx(rx), m_filter(filter) { }
63 bool operator()(const Item &item) {
64 return m_filter(m_rx, item);
67 static std::string currentFilter(MenuT &menu)
69 std::string filter;
70 auto rf = menu.getFilter().template target< RegexItemFilter<T> >();
71 if (rf)
72 filter = rf->m_rx.str();
73 return filter;
76 private:
77 boost::regex m_rx;
78 FilterFunction m_filter;
81 #endif // NCMPCPP_REGEX_FILTER_H