File: debug_view.hpp

package info (click to toggle)
range-v3 0.12.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,620 kB
  • sloc: cpp: 76,839; xml: 226; sh: 89; python: 34; makefile: 16; perl: 15
file content (179 lines) | stat: -rw-r--r-- 5,033 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Range v3 library
//
//  Copyright Casey Carter 2017
//
//  Use, modification and distribution is subject to the
//  Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ericniebler/range-v3
//
#ifndef RANGES_TEST_DEBUG_VIEW_HPP
#define RANGES_TEST_DEBUG_VIEW_HPP

#include <cstddef>
#include <atomic>
#include <memory>
#include <range/v3/iterator/operations.hpp>
#include <range/v3/utility/swap.hpp>

template<typename T, bool Sized = true>
struct debug_input_view : ranges::view_base
{
    static_assert(std::is_object<T>::value, "");

    using index_t = std::ptrdiff_t;
    using version_t = long;

    struct data
    {
        T *const first_;
        index_t const n_;
        std::atomic<index_t> offset_ {-1};

        data(T *p, index_t n)
          : first_(p), n_(n)
        {
            RANGES_ENSURE(n >= 0);
            RANGES_ENSURE(p || !n);
        }
    };
    std::shared_ptr<data> data_{};
    version_t version_ = 0;

    debug_input_view() = default;
    debug_input_view(T* p, index_t size)
      : data_(std::make_shared<data>(p, size))
    {}
    template<index_t N>
    debug_input_view(T (&data)[N])
      : debug_input_view{data, N}
    {}
    debug_input_view(debug_input_view const &that) = default;
    debug_input_view &operator=(debug_input_view const &that)
    {
        data_ = that.data_;
        ++version_; // invalidate outstanding iterators
        return *this;
    }

    struct sentinel
    {
        debug_input_view *view_ = nullptr;
        version_t version_ = 0;

        sentinel() = default;
        explicit constexpr sentinel(debug_input_view &view) noexcept
          : view_{&view}, version_{view.version_}
        {}
    };
    struct iterator
    {
        using iterator_category = std::input_iterator_tag;
        using value_type = meta::_t<std::remove_cv<T>>;
        using difference_type = index_t;
        using reference = T &;
        using pointer = T *;

        debug_input_view *view_ = nullptr;
        version_t version_ = 0;
        index_t offset_ = -1;

        iterator() = default;
        explicit constexpr iterator(debug_input_view &view) noexcept
          : view_{&view}, version_{view.version_}
          , offset_{view.data_ ? view.data_->offset_.load() : -1}
        {}

        void check_current() const noexcept
        {
            RANGES_ENSURE(view_);
            RANGES_ENSURE(view_->version_ == version_);
            RANGES_ENSURE(view_->data_);
            RANGES_ENSURE(view_->data_->offset_ == offset_);
        }

        void check_dereferenceable() const noexcept
        {
            check_current();
            RANGES_ENSURE(view_->data_->offset_ < view_->data_->n_);
        }

        reference operator*() const noexcept
        {
            check_dereferenceable();
            return view_->data_->first_[offset_];
        }
        iterator &operator++() noexcept
        {
            check_dereferenceable();
            RANGES_ENSURE(view_->data_->offset_.compare_exchange_strong(offset_, offset_ + 1));
            ++offset_;
            return *this;
        }
        void operator++(int) noexcept
        {
            ++*this;
        }

        friend bool operator==(iterator const &i, sentinel const &s)
        {
            RANGES_ENSURE(i.view_ == s.view_);
            RANGES_ENSURE(i.version_ == s.version_);
            i.check_current();
            return i.offset_ == i.view_->data_->n_;
        }
        friend bool operator==(sentinel const &s, iterator const &i)
        {
            return i == s;
        }
        friend bool operator!=(iterator const &i, sentinel const &s)
        {
            return !(i == s);
        }
        friend bool operator!=(sentinel const &s, iterator const &i)
        {
            return !(i == s);
        }
        CPP_member
        friend auto operator-(sentinel const& s, iterator const& i) ->
            CPP_ret(difference_type)(
                requires Sized)
        {
            RANGES_ENSURE(i.view_ == s.view_);
            RANGES_ENSURE(i.version_ == s.version_);
            i.check_current();
            return i.view_->data_->n_ - i.offset_;
        }
        CPP_member
        friend auto operator-(iterator const& i, sentinel const& s) ->
            CPP_ret(difference_type)(
                requires Sized)
        {
            return -(s - i);
        }
    };
    iterator begin() noexcept
    {
        RANGES_ENSURE(data_);
        index_t tmp = -1;
        RANGES_ENSURE(data_->offset_.compare_exchange_strong(tmp, 0));
        return iterator{*this};
    }
    sentinel end() noexcept
    {
        RANGES_ENSURE(data_);
        return sentinel{*this};
    }
    CPP_member
    auto size() const noexcept -> CPP_ret(std::size_t)(
        requires Sized)
    {
        RANGES_ENSURE(data_);
        RANGES_ENSURE(data_->offset_ == -1);
        return static_cast<std::size_t>(data_->n_);
    }
};

#endif