Debian Codegraph @

common/main/segiter.h

raw
1 #pragma once
2
3 #include "dsx-ns.h"
4 #ifdef DXX_BUILD_DESCENT
5 #include <iterator>
6 #include <type_traits>
7 #include "dxxsconf.h"
8 #include "object.h"
9 #include "segment.h"
10
11 #ifndef DXX_SEGITER_DEBUG_OBJECT_LINKAGE
12 # ifdef NDEBUG
13 # define DXX_SEGITER_DEBUG_OBJECT_LINKAGE 0
14 # else
15 # define DXX_SEGITER_DEBUG_OBJECT_LINKAGE 1
16 # endif
17 #endif
18
19 #if DXX_SEGITER_DEBUG_OBJECT_LINKAGE
20 #include <cassert>
21 #endif
22
23 namespace dsx {
24
25 template <typename iterator_value_type>
26 class objects_in
27 {
28 /* These are never defined, and exist only for use in the decltype expression. */
29 static valptridx<object>::vcptridx iterator_base_dispatch(const object_base &);
30 static valptridx<object>::vmptridx iterator_base_dispatch(object_base &);
31 static valptridx<object>::vcptridx iterator_base_dispatch(valptridx<object>::vcptridx);
32 static valptridx<object>::vmptridx iterator_base_dispatch(valptridx<object>::vmptridx);
33 using iterator_base_type = decltype(iterator_base_dispatch(std::declval<iterator_value_type &>()));
34 class iterator;
35 class sentinel
36 {
37 };
38 const iterator b;
39 public:
40 [[nodiscard]]
41 objects_in(const unique_segment &s, auto &object_factory, auto &segment_factory) :
42 b{construct(s, object_factory, segment_factory)}
43 {
44 }
45 const iterator &begin() const { return b; }
46 static sentinel end() { return {}; }
47 static iterator_base_type construct(const unique_segment &s, auto &of, auto &sf)
48 requires(
49 std::convertible_to<decltype(of(s.objects)), const object_base &> &&
50 requires(const object_base o) {
51 /* Require that the supplied `sf` can produce a value whose address
52 * can be compared to the address of a `const unique_segment &`.
53 *
54 * In !NDEBUG builds, this is required as a consequence of the
55 * assert conditions. In NDEBUG builds, it would not be checked,
56 * so an erroneous factory would compile successfully. Use the
57 * `requires` clause to catch this error when assertions are not
58 * enabled.
59 */
60 &*sf(o.segnum) == &s;
61 }
62 )
63 {
64 if (s.objects == object_none)
65 return iterator_base_type{object_none, typename iterator_base_type::allow_none_construction{}};
66 iterator_base_type opi{of(s.objects)};
67 #if DXX_SEGITER_DEBUG_OBJECT_LINKAGE
68 const object_base &o = opi;
69 /* Assert that the first object in the segment claims to be
70 * in the segment that claims to have that object.
71 */
72 assert(&*sf(o.segnum) == &s);
73 /* Assert that the first object in the segment agrees that there
74 * are no objects before it in the segment.
75 */
76 assert(o.prev == object_none);
77 #endif
78 return opi;
79 }
80 };
81
82 template <typename iterator_value_type>
83 class objects_in<iterator_value_type>::iterator :
84 objects_in<iterator_value_type>::iterator_base_type
85 {
86 using iterator_base_type = objects_in<iterator_value_type>::iterator_base_type;
87 using iterator_base_type::m_ptr;
88 using iterator_base_type::m_idx;
89 public:
90 using iterator_category = std::forward_iterator_tag;
91 using value_type = iterator_value_type;
92 using difference_type = std::ptrdiff_t;
93 using pointer = void;
94 /* If the result of `operator*()` is a reference, set `reference` as a type
95 * alias for it.
96 * If the result of `operator*()` is not a reference, set `reference` to
97 * `void` to prevent using algorithms that may behave badly when given a
98 * proxy object.
99 */
100 using reference = typename std::conditional_t<std::is_same_v<iterator_base_type, iterator_value_type>, void, value_type &>;
101 [[nodiscard]]
102 constexpr iterator(iterator_base_type o) :
103 iterator_base_type{std::move(o)}
104 {
105 }
106 /* If `std::is_same_v` is true, then return a copy of the underlying
107 * iterator, so that the caller cannot modify the iterator of `this`. If
108 * `std::is_same_v` is false, then return the result of converting `this`
109 * to `value_type &`, which will typically be `const object &` or
110 * `object &`.
111 */
112 [[nodiscard]]
113 typename std::conditional_t<std::is_same_v<iterator_base_type, value_type>, value_type, value_type &> operator *() const { return *this; }
114 iterator &operator++()
115 {
116 const auto oi{m_idx};
117 const auto op{m_ptr};
118 const auto ni{op->next};
119 m_idx = ni;
120 /* If ni == object_none, then the iterator is at end() and there should
121 * be no further reads of m_ptr. Optimizing compilers will typically
122 * recognize that setting m_ptr=nullptr in this case is unnecessary and
123 * omit the store. Omit the nullptr assignment so that less optimizing
124 * compilers do not generate a useless branch.
125 */
126 m_ptr += static_cast<std::size_t>(ni) - static_cast<std::size_t>(oi);
127 if (ni != object_none)
128 {
129 /* If ni == object_none, then `m_ptr` is now invalid and these
130 * tests would be undefined.
131 */
132 #if DXX_SEGITER_DEBUG_OBJECT_LINKAGE
133 /* Assert that the next object in the segment agrees that
134 * the preceding object is the previous object.
135 */
136 assert(oi == m_ptr->prev);
137 /* Assert that the old object and new object are in the same
138 * segment.
139 */
140 assert(op->segnum == m_ptr->segnum);
141 #endif
142 }
143 return *this;
144 }
145 [[nodiscard]]
146 constexpr bool operator==(const iterator &rhs) const
147 {
148 return m_idx == rhs.m_idx;
149 }
150 [[nodiscard]]
151 constexpr bool operator==(const sentinel &) const
152 {
153 return m_idx == object_none;
154 }
155 };
156
157 static_assert(std::same_as<decltype(std::declval<objects_in<object>>().begin().operator *()), object &>);
158 static_assert(std::same_as<decltype(std::declval<objects_in<const object>>().begin().operator *()), const object &>);
159 static_assert(std::same_as<decltype(std::declval<objects_in<vcobjptridx_t>>().begin().operator *()), vcobjptridx_t>);
160 static_assert(std::same_as<decltype(std::declval<objects_in<vmobjptridx_t>>().begin().operator *()), vmobjptridx_t>);
161
162 objects_in(const unique_segment &s, auto &object_factory, auto & /* segment_factory */) -> objects_in<decltype(object_factory(s.objects))>;
163
164 }
165 #endif