Blender V4.3
COM_BufferRange.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "BLI_assert.h"
8
9#include <iterator>
10
11namespace blender::compositor {
12
13/* Forward declarations. */
14template<typename T> class BufferRangeIterator;
15
19template<typename T> class BufferRange {
20 public:
23
24 private:
25 T *start_;
26 /* Number of elements in the range. */
27 int64_t size_;
28 /* Buffer element stride. */
29 int elem_stride_;
30
31 public:
32 constexpr BufferRange() = default;
33
37 constexpr BufferRange(T *buffer, int64_t start_elem_index, int64_t size, int elem_stride = 1)
38 : start_(buffer + start_elem_index * elem_stride), size_(size), elem_stride_(elem_stride)
39 {
40 }
41
42 constexpr friend bool operator==(const BufferRange &a, const BufferRange &b)
43 {
44 return a.start_ == b.start_ && a.size_ == b.size_ && a.elem_stride_ == b.elem_stride_;
45 }
46
50 constexpr T *operator[](int64_t index) const
51 {
52 BLI_assert(index >= 0);
53 BLI_assert(index < this->size());
54 return start_ + index * elem_stride_;
55 }
56
60 constexpr int64_t size() const
61 {
62 return size_;
63 }
64
65 constexpr Iterator begin()
66 {
67 return begin_iterator<Iterator>();
68 }
69
70 constexpr Iterator end()
71 {
72 return end_iterator<Iterator>();
73 }
74
75 constexpr ConstIterator begin() const
76 {
77 return begin_iterator<ConstIterator>();
78 }
79
80 constexpr ConstIterator end() const
81 {
82 return end_iterator<ConstIterator>();
83 }
84
85 private:
86 template<typename TIterator> constexpr TIterator begin_iterator() const
87 {
88 if (elem_stride_ == 0) {
89 /* Iterate a single element. */
90 return TIterator(start_, 1);
91 }
92
93 return TIterator(start_, elem_stride_);
94 }
95
96 template<typename TIterator> constexpr TIterator end_iterator() const
97 {
98 if (elem_stride_ == 0) {
99 /* Iterate a single element. */
100 return TIterator(start_ + 1, 1);
101 }
102
103 return TIterator(start_ + size_ * elem_stride_, elem_stride_);
104 }
105};
106
107template<typename T> class BufferRangeIterator {
108 public:
109 using iterator_category = std::input_iterator_tag;
110 using value_type = T *;
111 using pointer = T *const *;
112 using reference = T *const &;
113 using difference_type = std::ptrdiff_t;
114
115 private:
116 T *current_;
117 int elem_stride_;
118
119 public:
120 constexpr BufferRangeIterator() = default;
121
122 constexpr BufferRangeIterator(T *current, int elem_stride = 1)
123 : current_(current), elem_stride_(elem_stride)
124 {
125 }
126
128 {
129 current_ += elem_stride_;
130 return *this;
131 }
132
133 constexpr BufferRangeIterator operator++(int) const
134 {
135 BufferRangeIterator copied_iterator = *this;
136 ++copied_iterator;
137 return copied_iterator;
138 }
139
140 constexpr friend bool operator!=(const BufferRangeIterator &a, const BufferRangeIterator &b)
141 {
142 return a.current_ != b.current_;
143 }
144
145 constexpr friend bool operator==(const BufferRangeIterator &a, const BufferRangeIterator &b)
146 {
147 return a.current_ == b.current_;
148 }
149
150 constexpr T *operator*() const
151 {
152 return current_;
153 }
154};
155
156} // namespace blender::compositor
#define BLI_assert(a)
Definition BLI_assert.h:50
constexpr BufferRangeIterator(T *current, int elem_stride=1)
constexpr BufferRangeIterator & operator++()
constexpr friend bool operator==(const BufferRangeIterator &a, const BufferRangeIterator &b)
constexpr friend bool operator!=(const BufferRangeIterator &a, const BufferRangeIterator &b)
constexpr BufferRangeIterator operator++(int) const
constexpr ConstIterator begin() const
constexpr BufferRange()=default
constexpr BufferRange(T *buffer, int64_t start_elem_index, int64_t size, int elem_stride=1)
constexpr int64_t size() const
constexpr T * operator[](int64_t index) const
constexpr friend bool operator==(const BufferRange &a, const BufferRange &b)
constexpr ConstIterator end() const
local_group_size(16, 16) .push_constant(Type b
__int64 int64_t
Definition stdint.h:89