Blender  V3.3
BLI_generic_span.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
9 #include "BLI_cpp_type.hh"
10 #include "BLI_span.hh"
11 
12 namespace blender {
13 
17 class GSpan {
18  protected:
19  const CPPType *type_ = nullptr;
20  const void *data_ = nullptr;
22 
23  public:
24  GSpan() = default;
25 
26  GSpan(const CPPType *type, const void *buffer, int64_t size)
28  {
29  BLI_assert(size >= 0);
30  BLI_assert(buffer != nullptr || size == 0);
31  BLI_assert(size == 0 || type != nullptr);
33  }
34 
35  GSpan(const CPPType &type, const void *buffer, int64_t size) : GSpan(&type, buffer, size)
36  {
37  }
38 
39  GSpan(const CPPType &type) : type_(&type)
40  {
41  }
42 
44  {
45  }
46 
47  template<typename T>
49  : GSpan(CPPType::get<T>(), static_cast<const void *>(array.data()), array.size())
50  {
51  }
52 
53  const CPPType &type() const
54  {
55  BLI_assert(type_ != nullptr);
56  return *type_;
57  }
58 
59  const CPPType *type_ptr() const
60  {
61  return type_;
62  }
63 
64  bool is_empty() const
65  {
66  return size_ == 0;
67  }
68 
69  int64_t size() const
70  {
71  return size_;
72  }
73 
74  const void *data() const
75  {
76  return data_;
77  }
78 
79  const void *operator[](int64_t index) const
80  {
81  BLI_assert(index < size_);
82  return POINTER_OFFSET(data_, type_->size() * index);
83  }
84 
85  template<typename T> Span<T> typed() const
86  {
87  BLI_assert(type_->is<T>());
88  return Span<T>(static_cast<const T *>(data_), size_);
89  }
90 
91  GSpan slice(const int64_t start, int64_t size) const
92  {
93  BLI_assert(start >= 0);
94  BLI_assert(size >= 0);
95  const int64_t new_size = std::max<int64_t>(0, std::min(size, size_ - start));
96  return GSpan(type_, POINTER_OFFSET(data_, type_->size() * start), new_size);
97  }
98 
99  GSpan slice(const IndexRange range) const
100  {
101  return this->slice(range.start(), range.size());
102  }
103 };
104 
110  protected:
111  const CPPType *type_ = nullptr;
112  void *data_ = nullptr;
114 
115  public:
116  GMutableSpan() = default;
117 
120  {
121  BLI_assert(size >= 0);
122  BLI_assert(buffer != nullptr || size == 0);
123  BLI_assert(size == 0 || type != nullptr);
125  }
126 
128  {
129  }
130 
132  {
133  }
134 
136  {
137  }
138 
139  template<typename T>
141  : GMutableSpan(CPPType::get<T>(), static_cast<void *>(array.begin()), array.size())
142  {
143  }
144 
145  operator GSpan() const
146  {
147  return GSpan(type_, data_, size_);
148  }
149 
150  const CPPType &type() const
151  {
152  BLI_assert(type_ != nullptr);
153  return *type_;
154  }
155 
156  const CPPType *type_ptr() const
157  {
158  return type_;
159  }
160 
161  bool is_empty() const
162  {
163  return size_ == 0;
164  }
165 
166  int64_t size() const
167  {
168  return size_;
169  }
170 
171  void *data() const
172  {
173  return data_;
174  }
175 
176  void *operator[](int64_t index) const
177  {
178  BLI_assert(index >= 0);
179  BLI_assert(index < size_);
180  return POINTER_OFFSET(data_, type_->size() * index);
181  }
182 
183  template<typename T> MutableSpan<T> typed() const
184  {
185  BLI_assert(type_->is<T>());
186  return MutableSpan<T>(static_cast<T *>(data_), size_);
187  }
188 
189  GMutableSpan slice(const int64_t start, int64_t size) const
190  {
191  BLI_assert(start >= 0);
192  BLI_assert(size >= 0);
193  const int64_t new_size = std::max<int64_t>(0, std::min(size, size_ - start));
194  return GMutableSpan(*type_, POINTER_OFFSET(data_, type_->size() * start), new_size);
195  }
196 
198  {
199  return this->slice(range.start(), range.size());
200  }
201 
207  void copy_from(GSpan values)
208  {
209  BLI_assert(type_ == &values.type());
210  BLI_assert(size_ == values.size());
211  type_->copy_assign_n(values.data(), data_, size_);
212  }
213 };
214 
215 } // namespace blender
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define POINTER_OFFSET(v, ofs)
bool is() const
void copy_assign_n(const void *src, void *dst, int64_t n) const
bool pointer_has_valid_alignment(const void *ptr) const
int64_t size() const
const CPPType & type() const
GMutableSpan(const CPPType *type)
void copy_from(GSpan values)
GMutableSpan(MutableSpan< T > array)
const CPPType * type_ptr() const
GMutableSpan slice(const int64_t start, int64_t size) const
void * operator[](int64_t index) const
MutableSpan< T > typed() const
GMutableSpan(const CPPType &type)
GMutableSpan(const CPPType *type, void *buffer, int64_t size)
GMutableSpan(const CPPType &type, void *buffer, int64_t size)
GMutableSpan slice(IndexRange range) const
const CPPType * type_ptr() const
GSpan(const CPPType *type, const void *buffer, int64_t size)
const void * data() const
const CPPType & type() const
const void * operator[](int64_t index) const
GSpan slice(const IndexRange range) const
GSpan(Span< T > array)
bool is_empty() const
GSpan(const CPPType &type, const void *buffer, int64_t size)
GSpan(const CPPType &type)
GSpan slice(const int64_t start, int64_t size) const
const void * data_
GSpan()=default
GSpan(const CPPType *type)
const CPPType * type_
Span< T > typed() const
int64_t size() const
constexpr int64_t size() const
constexpr int64_t start() const
SyclQueue void void size_t num_bytes void
ccl_global float * buffer
#define T
#define min(a, b)
Definition: sort.c:35
__int64 int64_t
Definition: stdint.h:89