Blender  V2.93
FN_generic_span.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
23 #include "BLI_span.hh"
24 
25 #include "FN_cpp_type.hh"
26 
27 namespace blender::fn {
28 
32 class GSpan {
33  private:
34  const CPPType *type_;
35  const void *data_;
36  int64_t size_;
37 
38  public:
39  GSpan(const CPPType &type, const void *buffer, int64_t size)
40  : type_(&type), data_(buffer), size_(size)
41  {
42  BLI_assert(size >= 0);
43  BLI_assert(buffer != nullptr || size == 0);
45  }
46 
47  GSpan(const CPPType &type) : GSpan(type, nullptr, 0)
48  {
49  }
50 
51  template<typename T>
53  : GSpan(CPPType::get<T>(), static_cast<const void *>(array.data()), array.size())
54  {
55  }
56 
57  const CPPType &type() const
58  {
59  return *type_;
60  }
61 
62  bool is_empty() const
63  {
64  return size_ == 0;
65  }
66 
67  int64_t size() const
68  {
69  return size_;
70  }
71 
72  const void *data() const
73  {
74  return data_;
75  }
76 
77  const void *operator[](int64_t index) const
78  {
79  BLI_assert(index < size_);
80  return POINTER_OFFSET(data_, type_->size() * index);
81  }
82 
83  template<typename T> Span<T> typed() const
84  {
85  BLI_assert(type_->is<T>());
86  return Span<T>(static_cast<const T *>(data_), size_);
87  }
88 };
89 
94 class GMutableSpan {
95  private:
96  const CPPType *type_;
97  void *data_;
98  int64_t size_;
99 
100  public:
102  : type_(&type), data_(buffer), size_(size)
103  {
104  BLI_assert(size >= 0);
105  BLI_assert(buffer != nullptr || size == 0);
107  }
108 
109  GMutableSpan(const CPPType &type) : GMutableSpan(type, nullptr, 0)
110  {
111  }
112 
113  template<typename T>
115  : GMutableSpan(CPPType::get<T>(), static_cast<void *>(array.begin()), array.size())
116  {
117  }
118 
119  operator GSpan() const
120  {
121  return GSpan(*type_, data_, size_);
122  }
123 
124  const CPPType &type() const
125  {
126  return *type_;
127  }
128 
129  bool is_empty() const
130  {
131  return size_ == 0;
132  }
133 
134  int64_t size() const
135  {
136  return size_;
137  }
138 
139  void *data() const
140  {
141  return data_;
142  }
143 
144  void *operator[](int64_t index) const
145  {
146  BLI_assert(index >= 0);
147  BLI_assert(index < size_);
148  return POINTER_OFFSET(data_, type_->size() * index);
149  }
150 
151  template<typename T> MutableSpan<T> typed() const
152  {
153  BLI_assert(type_->is<T>());
154  return MutableSpan<T>(static_cast<T *>(data_), size_);
155  }
156 };
157 
158 } // namespace blender::fn
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define POINTER_OFFSET(v, ofs)
bool pointer_has_valid_alignment(const void *ptr) const
Definition: FN_cpp_type.hh:311
int64_t size() const
Definition: FN_cpp_type.hh:280
MutableSpan< T > typed() const
const CPPType & type() const
void * operator[](int64_t index) const
GMutableSpan(const CPPType &type)
GMutableSpan(const CPPType &type, void *buffer, int64_t size)
GMutableSpan(MutableSpan< T > array)
const void * operator[](int64_t index) const
GSpan(const CPPType &type, const void *buffer, int64_t size)
GSpan(Span< T > array)
GSpan(const CPPType &type)
const void * data() const
const CPPType & type() const
Span< T > typed() const
bool is_empty() const
int64_t size() const
T * data_
__kernel void ccl_constant KernelData ccl_global void ccl_global char ccl_global int ccl_global char ccl_global unsigned int ccl_global float * buffer
#define T
__int64 int64_t
Definition: stdint.h:92