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