song list: get rid of boost::zip_iterator and improve {Const,}SongIterator
[ncmpcpp.git] / src / utility / functional.h
blobda4df3f3c16bf6e8c08935f1128fecfadc7f9a61
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_UTILITY_FUNCTIONAL_H
22 #define NCMPCPP_UTILITY_FUNCTIONAL_H
24 #include <boost/locale/encoding_utf.hpp>
25 #include <utility>
27 template <typename BaseT>
28 struct reversed_iteration
30 reversed_iteration(BaseT &base_)
31 : base(base_) { }
33 BaseT &base;
36 template <typename BaseT>
37 reversed_iteration<BaseT> reverse_iteration(BaseT &base_) {
38 return reversed_iteration<BaseT>(base_);
40 template <typename BaseT>
41 auto begin(reversed_iteration<BaseT> &rev) -> decltype(rev.base.rbegin()) {
42 return rev.base.rbegin();
44 template <typename BaseT>
45 auto begin(reversed_iteration<const BaseT> &rev) -> decltype(rev.base.rbegin()) {
46 return rev.base.rbegin();
48 template <typename BaseT>
49 auto end(reversed_iteration<BaseT> &rev) -> decltype(rev.base.rend()) {
50 return rev.base.rend();
52 template <typename BaseT>
53 auto end(reversed_iteration<const BaseT> &rev) -> decltype(rev.base.rend()) {
54 return rev.base.rend();
57 template <typename ValueT>
58 struct pointer_extractor
60 typedef pointer_extractor type;
61 ValueT *operator()(ValueT &v) const { return &v; }
64 /// Map over the first element in range satisfying the predicate.
65 template <typename InputIterator, typename PredicateT, typename MapT>
66 InputIterator find_map_first(InputIterator first, InputIterator last, PredicateT &&p, MapT &&f)
68 auto it = std::find(first, last, std::forward<PredicateT>(p));
69 if (it != last)
70 f(*it);
71 return it;
74 /// Map over all elements in range satisfying the predicate.
75 template <typename InputIterator, typename PredicateT, typename MapT>
76 void find_map_all(InputIterator first, InputIterator last, PredicateT &&p, MapT &&f)
78 InputIterator it = first;
80 it = find_map_first(it, last, p, f);
81 while (it != last);
84 // identity function object
85 struct id_
87 template <typename ValueT>
88 constexpr auto operator()(ValueT &&v) const noexcept
89 -> decltype(std::forward<ValueT>(v))
91 return std::forward<ValueT>(v);
95 // convert string to appropriate type
96 template <typename TargetT, typename SourceT>
97 struct convertString
99 static std::basic_string<TargetT> apply(const std::basic_string<SourceT> &s)
101 return boost::locale::conv::utf_to_utf<TargetT>(s);
104 template <typename TargetT>
105 struct convertString<TargetT, TargetT>
107 static const std::basic_string<TargetT> &apply(const std::basic_string<TargetT> &s)
109 return s;
114 #endif // NCMPCPP_UTILITY_FUNCTIONAL_H