Blender  V2.93
FN_generic_virtual_vector_array.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 
27 
29 
30 namespace blender::fn {
31 
32 /* A generically typed version of `VVectorArray`. */
34  protected:
35  const CPPType *type_;
37 
38  public:
40  {
41  }
42 
43  virtual ~GVVectorArray() = default;
44 
45  /* Returns the number of vectors in the vector array. */
46  int64_t size() const
47  {
48  return size_;
49  }
50 
51  /* Returns true when there is no vector in the vector array. */
52  bool is_empty() const
53  {
54  return size_ == 0;
55  }
56 
57  const CPPType &type() const
58  {
59  return *type_;
60  }
61 
62  /* Returns the size of the vector at the given index. */
63  int64_t get_vector_size(const int64_t index) const
64  {
65  BLI_assert(index >= 0);
66  BLI_assert(index < size_);
67  return this->get_vector_size_impl(index);
68  }
69 
70  /* Copies an element from one of the vectors into `r_value`, which is expected to point to
71  * initialized memory. */
72  void get_vector_element(const int64_t index, const int64_t index_in_vector, void *r_value) const
73  {
74  BLI_assert(index >= 0);
75  BLI_assert(index < size_);
76  BLI_assert(index_in_vector >= 0);
77  BLI_assert(index_in_vector < this->get_vector_size(index));
78  this->get_vector_element_impl(index, index_in_vector, r_value);
79  }
80 
81  /* Returns true when the same vector is used at every index. */
82  bool is_single_vector() const
83  {
84  if (size_ == 1) {
85  return true;
86  }
87  return this->is_single_vector_impl();
88  }
89 
90  protected:
91  virtual int64_t get_vector_size_impl(const int64_t index) const = 0;
92 
93  virtual void get_vector_element_impl(const int64_t index,
94  const int64_t index_in_vector,
95  void *r_value) const = 0;
96 
97  virtual bool is_single_vector_impl() const
98  {
99  return false;
100  }
101 };
102 
104  private:
105  const GVVectorArray &vector_array_;
106  const int64_t index_;
107 
108  public:
109  GVArrayForGVVectorArrayIndex(const GVVectorArray &vector_array, const int64_t index)
110  : GVArray(vector_array.type(), vector_array.get_vector_size(index)),
111  vector_array_(vector_array),
112  index_(index)
113  {
114  }
115 
116  protected:
117  void get_impl(const int64_t index_in_vector, void *r_value) const override;
118  void get_to_uninitialized_impl(const int64_t index_in_vector, void *r_value) const override;
119 };
120 
122  private:
123  const GVArray &array_;
124 
125  public:
127  : GVVectorArray(array.type(), size), array_(array)
128  {
129  }
130 
131  protected:
132  int64_t get_vector_size_impl(const int64_t index) const override;
133  void get_vector_element_impl(const int64_t index,
134  const int64_t index_in_vector,
135  void *r_value) const override;
136 
137  bool is_single_vector_impl() const override;
138 };
139 
141  private:
142  const GSpan span_;
143 
144  public:
146  : GVVectorArray(span.type(), size), span_(span)
147  {
148  }
149 
150  protected:
151  int64_t get_vector_size_impl(const int64_t UNUSED(index)) const override;
152  void get_vector_element_impl(const int64_t UNUSED(index),
153  const int64_t index_in_vector,
154  void *r_value) const override;
155 
156  bool is_single_vector_impl() const override;
157 };
158 
159 template<typename T> class VVectorArrayForGVVectorArray : public VVectorArray<T> {
160  private:
161  const GVVectorArray &vector_array_;
162 
163  public:
165  : VVectorArray<T>(vector_array.size()), vector_array_(vector_array)
166  {
167  }
168 
169  protected:
170  int64_t get_vector_size_impl(const int64_t index) const override
171  {
172  return vector_array_.get_vector_size(index);
173  }
174 
175  T get_vector_element_impl(const int64_t index, const int64_t index_in_vector) const override
176  {
177  T value;
178  vector_array_.get_vector_element(index, index_in_vector, &value);
179  return value;
180  }
181 
182  bool is_single_vector_impl() const override
183  {
184  return vector_array_.is_single_vector();
185  }
186 };
187 
188 } // namespace blender::fn
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define UNUSED(x)
GVArrayForGVVectorArrayIndex(const GVVectorArray &vector_array, const int64_t index)
void get_impl(const int64_t index_in_vector, void *r_value) const override
void get_to_uninitialized_impl(const int64_t index_in_vector, void *r_value) const override
const CPPType & type() const
void get_vector_element_impl(const int64_t UNUSED(index), const int64_t index_in_vector, void *r_value) const override
int64_t get_vector_size_impl(const int64_t UNUSED(index)) const override
GVVectorArrayForSingleGSpan(const GSpan span, const int64_t size)
GVVectorArrayForSingleGVArray(const GVArray &array, const int64_t size)
void get_vector_element_impl(const int64_t index, const int64_t index_in_vector, void *r_value) const override
int64_t get_vector_size_impl(const int64_t index) const override
virtual void get_vector_element_impl(const int64_t index, const int64_t index_in_vector, void *r_value) const =0
int64_t get_vector_size(const int64_t index) const
virtual ~GVVectorArray()=default
virtual int64_t get_vector_size_impl(const int64_t index) const =0
void get_vector_element(const int64_t index, const int64_t index_in_vector, void *r_value) const
GVVectorArray(const CPPType &type, const int64_t size)
VVectorArrayForGVVectorArray(const GVVectorArray &vector_array)
int64_t get_vector_size_impl(const int64_t index) const override
T get_vector_element_impl(const int64_t index, const int64_t index_in_vector) const override
#define T
__int64 int64_t
Definition: stdint.h:92