paludis Version 3.0.2
The one true package mangler
iterator_range.hh
1/* vim: set sw=4 sts=4 et foldmethod=syntax : */
2/*
3 * Copyright (c) 2014 Saleem Abdulrasool
4 *
5 * This file is part of the Paludis package manager. Paludis is free software;
6 * you can redistribute it and/or modify it under the terms of the GNU General
7 * Public License version 2, as published by the Free Software Foundation.
8 *
9 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 * details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19#ifndef PALUDIS_GUARD_PALUDIS_UTIL_ITERATOR_RANGE_HH
20#define PALUDIS_GUARD_PALUDIS_UTIL_ITERATOR_RANGE_HH 1
21
22#include <iterator>
23#include <utility>
24
25namespace paludis
26{
27 template <typename Iterator_>
28 class IteratorRange
29 {
30 private:
31 Iterator_ _begin, _end;
32
33 public:
34 using const_iterator = Iterator_;
35 using value_type = typename Iterator_::value_type;
36
37 // TODO(compnerd) use SFINAE to ensure that the container's
38 // iterators match the range's iterator
39 template <typename Container_>
40 IteratorRange(Container_ && container)
41 : _begin(std::begin(container)), _end(std::end(container))
42 {
43 }
44
45 IteratorRange(Iterator_ begin, Iterator_ end)
46 : _begin(std::move(begin)), _end(std::move(end))
47 {
48 }
49
50 Iterator_ begin() const
51 {
52 return _begin;
53 }
54
55 Iterator_ end() const
56 {
57 return _end;
58 }
59
60 size_t size() const
61 {
62 return std::distance(_begin, _end);
63 }
64
65 bool empty() const
66 {
67 return _begin == _end;
68 }
69 };
70
71 template <typename Iterator_>
72 IteratorRange<Iterator_> make_range(Iterator_ begin, Iterator_ end)
73 {
74 return IteratorRange<Iterator_>(std::move(begin), std::move(end));
75 }
76
77 template <typename Iterator_>
78 IteratorRange<Iterator_> make_range(std::pair<Iterator_, Iterator_> range)
79 {
80 return IteratorRange<Iterator_>(std::move(range.first),
81 std::move(range.second));
82 }
83}
84
85#endif
86
Definition iterator_range.hh:29
Definition about_metadata-fwd.hh:24