1  
//
1  
//
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3  
// Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
3  
// Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
4  
//
4  
//
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  
//
7  
//
8  
// Official repository: https://github.com/boostorg/url
8  
// Official repository: https://github.com/boostorg/url
9  
//
9  
//
10  

10  

11  
#ifndef BOOST_URL_IMPL_SEGMENTS_BASE_HPP
11  
#ifndef BOOST_URL_IMPL_SEGMENTS_BASE_HPP
12  
#define BOOST_URL_IMPL_SEGMENTS_BASE_HPP
12  
#define BOOST_URL_IMPL_SEGMENTS_BASE_HPP
13  

13  

14  
#include <boost/url/detail/segments_iter_impl.hpp>
14  
#include <boost/url/detail/segments_iter_impl.hpp>
15  
#include <boost/assert.hpp>
15  
#include <boost/assert.hpp>
16  
#include <iterator>
16  
#include <iterator>
17  

17  

18  
namespace boost {
18  
namespace boost {
19  
namespace urls {
19  
namespace urls {
20  
namespace detail {
20  
namespace detail {
21  
struct segments_iter_access;
21  
struct segments_iter_access;
22  
}
22  
}
23  

23  

24  
class segments_base::iterator
24  
class segments_base::iterator
25  
{
25  
{
26  
    detail::segments_iter_impl it_;
26  
    detail::segments_iter_impl it_;
27  

27  

28  
    friend class segments_base;
28  
    friend class segments_base;
29  
    friend class segments_ref;
29  
    friend class segments_ref;
30  
    friend struct detail::segments_iter_access;
30  
    friend struct detail::segments_iter_access;
31  

31  

32  
    iterator(detail::path_ref const&) noexcept;
32  
    iterator(detail::path_ref const&) noexcept;
33  
    iterator(detail::path_ref const&, int) noexcept;
33  
    iterator(detail::path_ref const&, int) noexcept;
34  

34  

35  
    iterator(
35  
    iterator(
36  
        detail::segments_iter_impl const& it) noexcept
36  
        detail::segments_iter_impl const& it) noexcept
37  
        : it_(it)
37  
        : it_(it)
38  
    {
38  
    {
39  
    }
39  
    }
40  

40  

41  
public:
41  
public:
42  
    using value_type = segments_base::value_type;
42  
    using value_type = segments_base::value_type;
43  
    using reference = segments_base::reference;
43  
    using reference = segments_base::reference;
44  
    using pointer = reference;
44  
    using pointer = reference;
45  
    using difference_type =
45  
    using difference_type =
46  
        segments_base::difference_type;
46  
        segments_base::difference_type;
47  
    using iterator_category =
47  
    using iterator_category =
48  
        std::bidirectional_iterator_tag;
48  
        std::bidirectional_iterator_tag;
49  

49  

50  
    iterator() = default;
50  
    iterator() = default;
51  
    iterator(iterator const&) = default;
51  
    iterator(iterator const&) = default;
52  
    iterator& operator=(
52  
    iterator& operator=(
53  
        iterator const&) noexcept = default;
53  
        iterator const&) noexcept = default;
54  

54  

55  
    BOOST_URL_DECL
55  
    BOOST_URL_DECL
56  
    reference
56  
    reference
57  
    operator*() const;
57  
    operator*() const;
58  

58  

59  
    // the return value is too expensive
59  
    // the return value is too expensive
60  
    pointer operator->() const = delete;
60  
    pointer operator->() const = delete;
61  

61  

62  
    iterator&
62  
    iterator&
63  
    operator++() noexcept
63  
    operator++() noexcept
64  
    {
64  
    {
65  
        it_.increment();
65  
        it_.increment();
66  
        return *this;
66  
        return *this;
67  
    }
67  
    }
68  

68  

69  
    iterator&
69  
    iterator&
70  
    operator--() noexcept
70  
    operator--() noexcept
71  
    {
71  
    {
72  
        it_.decrement();
72  
        it_.decrement();
73  
        return *this;
73  
        return *this;
74  
    }
74  
    }
75  

75  

76  
    iterator
76  
    iterator
77  
    operator++(int) noexcept
77  
    operator++(int) noexcept
78  
    {
78  
    {
79  
        auto tmp = *this;
79  
        auto tmp = *this;
80  
        ++*this;
80  
        ++*this;
81  
        return tmp;
81  
        return tmp;
82  
    }
82  
    }
83  

83  

84  
    iterator
84  
    iterator
85  
    operator--(int) noexcept
85  
    operator--(int) noexcept
86  
    {
86  
    {
87  
        auto tmp = *this;
87  
        auto tmp = *this;
88  
        --*this;
88  
        --*this;
89  
        return tmp;
89  
        return tmp;
90  
    }
90  
    }
91  

91  

92  
    bool
92  
    bool
93  
    operator==(
93  
    operator==(
94  
        iterator const& other) const noexcept
94  
        iterator const& other) const noexcept
95  
    {
95  
    {
96  
        return it_.equal(other.it_);
96  
        return it_.equal(other.it_);
97  
    }
97  
    }
98  

98  

99  
    bool
99  
    bool
100  
    operator!=(
100  
    operator!=(
101  
        iterator const& other) const noexcept
101  
        iterator const& other) const noexcept
102  
    {
102  
    {
103  
        return ! it_.equal(other.it_);
103  
        return ! it_.equal(other.it_);
104  
    }
104  
    }
105  
};
105  
};
106  

106  

107  
//------------------------------------------------
107  
//------------------------------------------------
108  

108  

109  
inline
109  
inline
110  
std::string
110  
std::string
111  
segments_base::
111  
segments_base::
112  
front() const noexcept
112  
front() const noexcept
113  
{
113  
{
114  
    BOOST_ASSERT(! empty());
114  
    BOOST_ASSERT(! empty());
115  
    return *begin();
115  
    return *begin();
116  
}
116  
}
117  

117  

118  
inline
118  
inline
119  
std::string
119  
std::string
120  
segments_base::
120  
segments_base::
121  
back() const noexcept
121  
back() const noexcept
122  
{
122  
{
123  
    BOOST_ASSERT(! empty());
123  
    BOOST_ASSERT(! empty());
124  
    return *--end();
124  
    return *--end();
125  
}
125  
}
126  

126  

127  
} // urls
127  
} // urls
128  
} // boost
128  
} // boost
129  

129  

130  
#endif
130  
#endif