File indexing completed on 2024-05-12 05:46:50

0001 /*
0002  *   Copyright (C) 2012 Ivan Cukic <ivan.cukic(at)kde.org>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU Lesser General Public License version 2,
0006  *   or (at your option) any later version, as published by the Free
0007  *   Software Foundation
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU Lesser General Public License for more details
0013  *
0014  *   You should have received a copy of the GNU Lesser General Public
0015  *   License along with this program; if not, write to the
0016  *   Free Software Foundation,3 Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0018  */
0019 
0020 #ifndef UTILS_RANGE_H
0021 #define UTILS_RANGE_H
0022 
0023 #include <boost/range/algorithm/copy.hpp>
0024 #include <boost/range/adaptor/transformed.hpp>
0025 #include <boost/range/adaptor/filtered.hpp>
0026 
0027 /********************************************************************
0028  *  Syntactic sugar for converting ranges to collections            *
0029  ********************************************************************/
0030 
0031 namespace kamd {
0032 namespace utils {
0033 
0034 template <typename Collection, typename Range>
0035 __inline Collection as_collection(Range range)
0036 {
0037     Collection result;
0038 
0039     boost::copy(range, std::back_inserter(result));
0040 
0041     return result;
0042 }
0043 
0044 template <typename Member, typename ...Args>
0045 __inline auto transformed(Member member, Args... args)
0046     -> decltype(boost::adaptors::transformed(
0047                 std::bind(member, args..., std::placeholders::_1)))
0048 {
0049     return boost::adaptors::transformed(
0050         std::bind(member, args..., std::placeholders::_1)
0051     );
0052 
0053 }
0054 
0055 template <typename Member, typename ...Args>
0056 __inline auto filtered(Member member, Args... args)
0057     -> decltype(boost::adaptors::filtered(
0058                 std::bind(member, args..., std::placeholders::_1)))
0059 {
0060     return boost::adaptors::filtered(
0061         std::bind(member, args..., std::placeholders::_1)
0062     );
0063 
0064 }
0065 
0066 template <typename Class, typename Member>
0067 __inline auto filtered(Class *const self, Member member)
0068     -> decltype(boost::adaptors::filtered(
0069                 std::bind(member, self, std::placeholders::_1)))
0070 {
0071     return boost::adaptors::filtered(
0072         std::bind(member, self, std::placeholders::_1)
0073     );
0074 
0075 }
0076 
0077 
0078 } // namespace utils
0079 } // namespace kamd
0080 
0081 #endif // UTILS_RANGE_H