Blender  V2.93
FN_cpp_type_make.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 "FN_cpp_type.hh"
24 
26 
27 template<typename T> void construct_default_cb(void *ptr)
28 {
29  new (ptr) T;
30 }
31 template<typename T> void construct_default_n_cb(void *ptr, int64_t n)
32 {
33  blender::default_construct_n(static_cast<T *>(ptr), n);
34 }
35 template<typename T> void construct_default_indices_cb(void *ptr, IndexMask mask)
36 {
37  mask.foreach_index([&](int64_t i) { new (static_cast<T *>(ptr) + i) T; });
38 }
39 
40 template<typename T> void destruct_cb(void *ptr)
41 {
42  (static_cast<T *>(ptr))->~T();
43 }
44 template<typename T> void destruct_n_cb(void *ptr, int64_t n)
45 {
46  blender::destruct_n(static_cast<T *>(ptr), n);
47 }
48 template<typename T> void destruct_indices_cb(void *ptr, IndexMask mask)
49 {
50  T *ptr_ = static_cast<T *>(ptr);
51  mask.foreach_index([&](int64_t i) { ptr_[i].~T(); });
52 }
53 
54 template<typename T> void copy_to_initialized_cb(const void *src, void *dst)
55 {
56  *static_cast<T *>(dst) = *static_cast<const T *>(src);
57 }
58 template<typename T> void copy_to_initialized_n_cb(const void *src, void *dst, int64_t n)
59 {
60  const T *src_ = static_cast<const T *>(src);
61  T *dst_ = static_cast<T *>(dst);
62 
63  for (int64_t i = 0; i < n; i++) {
64  dst_[i] = src_[i];
65  }
66 }
67 template<typename T>
68 void copy_to_initialized_indices_cb(const void *src, void *dst, IndexMask mask)
69 {
70  const T *src_ = static_cast<const T *>(src);
71  T *dst_ = static_cast<T *>(dst);
72 
73  mask.foreach_index([&](int64_t i) { dst_[i] = src_[i]; });
74 }
75 
76 template<typename T> void copy_to_uninitialized_cb(const void *src, void *dst)
77 {
78  blender::uninitialized_copy_n(static_cast<const T *>(src), 1, static_cast<T *>(dst));
79 }
80 template<typename T> void copy_to_uninitialized_n_cb(const void *src, void *dst, int64_t n)
81 {
82  blender::uninitialized_copy_n(static_cast<const T *>(src), n, static_cast<T *>(dst));
83 }
84 template<typename T>
85 void copy_to_uninitialized_indices_cb(const void *src, void *dst, IndexMask mask)
86 {
87  const T *src_ = static_cast<const T *>(src);
88  T *dst_ = static_cast<T *>(dst);
89 
90  mask.foreach_index([&](int64_t i) { new (dst_ + i) T(src_[i]); });
91 }
92 
93 template<typename T> void move_to_initialized_cb(void *src, void *dst)
94 {
95  blender::initialized_move_n(static_cast<T *>(src), 1, static_cast<T *>(dst));
96 }
97 template<typename T> void move_to_initialized_n_cb(void *src, void *dst, int64_t n)
98 {
99  blender::initialized_move_n(static_cast<T *>(src), n, static_cast<T *>(dst));
100 }
101 template<typename T> void move_to_initialized_indices_cb(void *src, void *dst, IndexMask mask)
102 {
103  T *src_ = static_cast<T *>(src);
104  T *dst_ = static_cast<T *>(dst);
105 
106  mask.foreach_index([&](int64_t i) { dst_[i] = std::move(src_[i]); });
107 }
108 
109 template<typename T> void move_to_uninitialized_cb(void *src, void *dst)
110 {
111  blender::uninitialized_move_n(static_cast<T *>(src), 1, static_cast<T *>(dst));
112 }
113 template<typename T> void move_to_uninitialized_n_cb(void *src, void *dst, int64_t n)
114 {
115  blender::uninitialized_move_n(static_cast<T *>(src), n, static_cast<T *>(dst));
116 }
117 template<typename T> void move_to_uninitialized_indices_cb(void *src, void *dst, IndexMask mask)
118 {
119  T *src_ = static_cast<T *>(src);
120  T *dst_ = static_cast<T *>(dst);
121 
122  mask.foreach_index([&](int64_t i) { new (dst_ + i) T(std::move(src_[i])); });
123 }
124 
125 template<typename T> void relocate_to_initialized_cb(void *src, void *dst)
126 {
127  T *src_ = static_cast<T *>(src);
128  T *dst_ = static_cast<T *>(dst);
129 
130  *dst_ = std::move(*src_);
131  src_->~T();
132 }
133 template<typename T> void relocate_to_initialized_n_cb(void *src, void *dst, int64_t n)
134 {
135  blender::initialized_relocate_n(static_cast<T *>(src), n, static_cast<T *>(dst));
136 }
137 template<typename T> void relocate_to_initialized_indices_cb(void *src, void *dst, IndexMask mask)
138 {
139  T *src_ = static_cast<T *>(src);
140  T *dst_ = static_cast<T *>(dst);
141 
142  mask.foreach_index([&](int64_t i) {
143  dst_[i] = std::move(src_[i]);
144  src_[i].~T();
145  });
146 }
147 
148 template<typename T> void relocate_to_uninitialized_cb(void *src, void *dst)
149 {
150  T *src_ = static_cast<T *>(src);
151  T *dst_ = static_cast<T *>(dst);
152 
153  new (dst_) T(std::move(*src_));
154  src_->~T();
155 }
156 template<typename T> void relocate_to_uninitialized_n_cb(void *src, void *dst, int64_t n)
157 {
158  blender::uninitialized_relocate_n(static_cast<T *>(src), n, static_cast<T *>(dst));
159 }
160 template<typename T>
162 {
163  T *src_ = static_cast<T *>(src);
164  T *dst_ = static_cast<T *>(dst);
165 
166  mask.foreach_index([&](int64_t i) {
167  new (dst_ + i) T(std::move(src_[i]));
168  src_[i].~T();
169  });
170 }
171 
172 template<typename T> void fill_initialized_cb(const void *value, void *dst, int64_t n)
173 {
174  const T &value_ = *static_cast<const T *>(value);
175  T *dst_ = static_cast<T *>(dst);
176 
177  for (int64_t i = 0; i < n; i++) {
178  dst_[i] = value_;
179  }
180 }
181 template<typename T> void fill_initialized_indices_cb(const void *value, void *dst, IndexMask mask)
182 {
183  const T &value_ = *static_cast<const T *>(value);
184  T *dst_ = static_cast<T *>(dst);
185 
186  mask.foreach_index([&](int64_t i) { dst_[i] = value_; });
187 }
188 
189 template<typename T> void fill_uninitialized_cb(const void *value, void *dst, int64_t n)
190 {
191  const T &value_ = *static_cast<const T *>(value);
192  T *dst_ = static_cast<T *>(dst);
193 
194  for (int64_t i = 0; i < n; i++) {
195  new (dst_ + i) T(value_);
196  }
197 }
198 template<typename T>
199 void fill_uninitialized_indices_cb(const void *value, void *dst, IndexMask mask)
200 {
201  const T &value_ = *static_cast<const T *>(value);
202  T *dst_ = static_cast<T *>(dst);
203 
204  mask.foreach_index([&](int64_t i) { new (dst_ + i) T(value_); });
205 }
206 
207 template<typename T> void debug_print_cb(const void *value, std::stringstream &ss)
208 {
209  const T &value_ = *static_cast<const T *>(value);
210  ss << value_;
211 }
212 
213 template<typename T> bool is_equal_cb(const void *a, const void *b)
214 {
215  const T &a_ = *static_cast<const T *>(a);
216  const T &b_ = *static_cast<const T *>(b);
217  return a_ == b_;
218 }
219 
220 template<typename T> uint64_t hash_cb(const void *value)
221 {
222  const T &value_ = *static_cast<const T *>(value);
223  return get_default_hash(value_);
224 }
225 
226 } // namespace blender::fn::cpp_type_util
227 
228 namespace blender::fn {
229 
230 template<typename T>
231 inline std::unique_ptr<const CPPType> create_cpp_type(StringRef name, const T &default_value)
232 {
233  using namespace cpp_type_util;
234  const CPPType *type = new CPPType(name,
235  sizeof(T),
236  alignof(T),
237  std::is_trivially_destructible_v<T>,
238  construct_default_cb<T>,
239  construct_default_n_cb<T>,
240  construct_default_indices_cb<T>,
241  destruct_cb<T>,
242  destruct_n_cb<T>,
243  destruct_indices_cb<T>,
244  copy_to_initialized_cb<T>,
245  copy_to_initialized_n_cb<T>,
246  copy_to_initialized_indices_cb<T>,
247  copy_to_uninitialized_cb<T>,
248  copy_to_uninitialized_n_cb<T>,
249  copy_to_uninitialized_indices_cb<T>,
250  move_to_initialized_cb<T>,
251  move_to_initialized_n_cb<T>,
252  move_to_initialized_indices_cb<T>,
253  move_to_uninitialized_cb<T>,
254  move_to_uninitialized_n_cb<T>,
255  move_to_uninitialized_indices_cb<T>,
256  relocate_to_initialized_cb<T>,
257  relocate_to_initialized_n_cb<T>,
258  relocate_to_initialized_indices_cb<T>,
259  relocate_to_uninitialized_cb<T>,
260  relocate_to_uninitialized_n_cb<T>,
261  relocate_to_uninitialized_indices_cb<T>,
262  fill_initialized_cb<T>,
263  fill_initialized_indices_cb<T>,
264  fill_uninitialized_cb<T>,
265  fill_uninitialized_indices_cb<T>,
266  debug_print_cb<T>,
267  is_equal_cb<T>,
268  hash_cb<T>,
269  static_cast<const void *>(&default_value));
270  return std::unique_ptr<const CPPType>(type);
271 }
272 
273 } // namespace blender::fn
274 
275 #define MAKE_CPP_TYPE(IDENTIFIER, TYPE_NAME) \
276  template<> const blender::fn::CPPType &blender::fn::CPPType::get<TYPE_NAME>() \
277  { \
278  static TYPE_NAME default_value; \
279  static std::unique_ptr<const CPPType> cpp_type = blender::fn::create_cpp_type<TYPE_NAME>( \
280  STRINGIFY(IDENTIFIER), default_value); \
281  return *cpp_type; \
282  } \
283  /* Support using `CPPType::get<const T>()`. Otherwise the caller would have to remove const. */ \
284  template<> const blender::fn::CPPType &blender::fn::CPPType::get<const TYPE_NAME>() \
285  { \
286  return blender::fn::CPPType::get<TYPE_NAME>(); \
287  }
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
#define T
static unsigned a[3]
Definition: RandGen.cpp:92
void copy_to_initialized_n_cb(const void *src, void *dst, int64_t n)
void copy_to_uninitialized_indices_cb(const void *src, void *dst, IndexMask mask)
void move_to_uninitialized_cb(void *src, void *dst)
void relocate_to_initialized_cb(void *src, void *dst)
void relocate_to_uninitialized_n_cb(void *src, void *dst, int64_t n)
void copy_to_initialized_cb(const void *src, void *dst)
void copy_to_initialized_indices_cb(const void *src, void *dst, IndexMask mask)
void relocate_to_initialized_indices_cb(void *src, void *dst, IndexMask mask)
void copy_to_uninitialized_cb(const void *src, void *dst)
void construct_default_cb(void *ptr)
void fill_initialized_indices_cb(const void *value, void *dst, IndexMask mask)
void fill_initialized_cb(const void *value, void *dst, int64_t n)
bool is_equal_cb(const void *a, const void *b)
void move_to_uninitialized_indices_cb(void *src, void *dst, IndexMask mask)
void destruct_indices_cb(void *ptr, IndexMask mask)
void move_to_initialized_indices_cb(void *src, void *dst, IndexMask mask)
void debug_print_cb(const void *value, std::stringstream &ss)
void move_to_initialized_n_cb(void *src, void *dst, int64_t n)
void move_to_uninitialized_n_cb(void *src, void *dst, int64_t n)
void relocate_to_initialized_n_cb(void *src, void *dst, int64_t n)
uint64_t hash_cb(const void *value)
void relocate_to_uninitialized_indices_cb(void *src, void *dst, IndexMask mask)
void fill_uninitialized_cb(const void *value, void *dst, int64_t n)
void destruct_n_cb(void *ptr, int64_t n)
void fill_uninitialized_indices_cb(const void *value, void *dst, IndexMask mask)
void move_to_initialized_cb(void *src, void *dst)
void construct_default_n_cb(void *ptr, int64_t n)
void construct_default_indices_cb(void *ptr, IndexMask mask)
void copy_to_uninitialized_n_cb(const void *src, void *dst, int64_t n)
void relocate_to_uninitialized_cb(void *src, void *dst)
std::unique_ptr< const CPPType > create_cpp_type(StringRef name, const T &default_value)
void default_construct_n(T *ptr, int64_t n)
void initialized_relocate_n(T *src, int64_t n, T *dst)
void initialized_move_n(T *src, int64_t n, T *dst)
uint64_t get_default_hash(const T &v)
Definition: BLI_hash.hh:209
void uninitialized_relocate_n(T *src, int64_t n, T *dst)
void destruct_n(T *ptr, int64_t n)
void uninitialized_copy_n(const T *src, int64_t n, T *dst)
void uninitialized_move_n(T *src, int64_t n, T *dst)
__int64 int64_t
Definition: stdint.h:92
unsigned __int64 uint64_t
Definition: stdint.h:93
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
PointerRNA * ptr
Definition: wm_files.c:3157