1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#pragma once
/** \file
* \ingroup fn
*/
#include "FN_cpp_type.hh"
namespace blender::fn::cpp_type_util {
template<typename T> void construct_default_cb(void *ptr)
{
new (ptr) T;
}
template<typename T> void construct_default_n_cb(void *ptr, int64_t n)
{
blender::default_construct_n(static_cast<T *>(ptr), n);
}
template<typename T> void construct_default_indices_cb(void *ptr, IndexMask mask)
{
mask.foreach_index([&](int64_t i) { new (static_cast<T *>(ptr) + i) T; });
}
template<typename T> void destruct_cb(void *ptr)
{
(static_cast<T *>(ptr))->~T();
}
template<typename T> void destruct_n_cb(void *ptr, int64_t n)
{
blender::destruct_n(static_cast<T *>(ptr), n);
}
template<typename T> void destruct_indices_cb(void *ptr, IndexMask mask)
{
T *ptr_ = static_cast<T *>(ptr);
mask.foreach_index([&](int64_t i) { ptr_[i].~T(); });
}
template<typename T> void copy_to_initialized_cb(const void *src, void *dst)
{
*static_cast<T *>(dst) = *static_cast<const T *>(src);
}
template<typename T> void copy_to_initialized_n_cb(const void *src, void *dst, int64_t n)
{
const T *src_ = static_cast<const T *>(src);
T *dst_ = static_cast<T *>(dst);
for (int64_t i = 0; i < n; i++) {
dst_[i] = src_[i];
}
}
template<typename T>
void copy_to_initialized_indices_cb(const void *src, void *dst, IndexMask mask)
{
const T *src_ = static_cast<const T *>(src);
T *dst_ = static_cast<T *>(dst);
mask.foreach_index([&](int64_t i) { dst_[i] = src_[i]; });
}
template<typename T> void copy_to_uninitialized_cb(const void *src, void *dst)
{
blender::uninitialized_copy_n(static_cast<const T *>(src), 1, static_cast<T *>(dst));
}
template<typename T> void copy_to_uninitialized_n_cb(const void *src, void *dst, int64_t n)
{
blender::uninitialized_copy_n(static_cast<const T *>(src), n, static_cast<T *>(dst));
}
template<typename T>
void copy_to_uninitialized_indices_cb(const void *src, void *dst, IndexMask mask)
{
const T *src_ = static_cast<const T *>(src);
T *dst_ = static_cast<T *>(dst);
mask.foreach_index([&](int64_t i) { new (dst_ + i) T(src_[i]); });
}
template<typename T> void move_to_initialized_cb(void *src, void *dst)
{
blender::initialized_move_n(static_cast<T *>(src), 1, static_cast<T *>(dst));
}
template<typename T> void move_to_initialized_n_cb(void *src, void *dst, int64_t n)
{
blender::initialized_move_n(static_cast<T *>(src), n, static_cast<T *>(dst));
}
template<typename T> void move_to_initialized_indices_cb(void *src, void *dst, IndexMask mask)
{
T *src_ = static_cast<T *>(src);
T *dst_ = static_cast<T *>(dst);
mask.foreach_index([&](int64_t i) { dst_[i] = std::move(src_[i]); });
}
template<typename T> void move_to_uninitialized_cb(void *src, void *dst)
{
blender::uninitialized_move_n(static_cast<T *>(src), 1, static_cast<T *>(dst));
}
template<typename T> void move_to_uninitialized_n_cb(void *src, void *dst, int64_t n)
{
blender::uninitialized_move_n(static_cast<T *>(src), n, static_cast<T *>(dst));
}
template<typename T> void move_to_uninitialized_indices_cb(void *src, void *dst, IndexMask mask)
{
T *src_ = static_cast<T *>(src);
T *dst_ = static_cast<T *>(dst);
mask.foreach_index([&](int64_t i) { new (dst_ + i) T(std::move(src_[i])); });
}
template<typename T> void relocate_to_initialized_cb(void *src, void *dst)
{
T *src_ = static_cast<T *>(src);
T *dst_ = static_cast<T *>(dst);
*dst_ = std::move(*src_);
src_->~T();
}
template<typename T> void relocate_to_initialized_n_cb(void *src, void *dst, int64_t n)
{
blender::initialized_relocate_n(static_cast<T *>(src), n, static_cast<T *>(dst));
}
template<typename T> void relocate_to_initialized_indices_cb(void *src, void *dst, IndexMask mask)
{
T *src_ = static_cast<T *>(src);
T *dst_ = static_cast<T *>(dst);
mask.foreach_index([&](int64_t i) {
dst_[i] = std::move(src_[i]);
src_[i].~T();
});
}
template<typename T> void relocate_to_uninitialized_cb(void *src, void *dst)
{
T *src_ = static_cast<T *>(src);
T *dst_ = static_cast<T *>(dst);
new (dst_) T(std::move(*src_));
src_->~T();
}
template<typename T> void relocate_to_uninitialized_n_cb(void *src, void *dst, int64_t n)
{
blender::uninitialized_relocate_n(static_cast<T *>(src), n, static_cast<T *>(dst));
}
template<typename T>
void relocate_to_uninitialized_indices_cb(void *src, void *dst, IndexMask mask)
{
T *src_ = static_cast<T *>(src);
T *dst_ = static_cast<T *>(dst);
mask.foreach_index([&](int64_t i) {
new (dst_ + i) T(std::move(src_[i]));
src_[i].~T();
});
}
template<typename T> void fill_initialized_cb(const void *value, void *dst, int64_t n)
{
const T &value_ = *static_cast<const T *>(value);
T *dst_ = static_cast<T *>(dst);
for (int64_t i = 0; i < n; i++) {
dst_[i] = value_;
}
}
template<typename T> void fill_initialized_indices_cb(const void *value, void *dst, IndexMask mask)
{
const T &value_ = *static_cast<const T *>(value);
T *dst_ = static_cast<T *>(dst);
mask.foreach_index([&](int64_t i) { dst_[i] = value_; });
}
template<typename T> void fill_uninitialized_cb(const void *value, void *dst, int64_t n)
{
const T &value_ = *static_cast<const T *>(value);
T *dst_ = static_cast<T *>(dst);
for (int64_t i = 0; i < n; i++) {
new (dst_ + i) T(value_);
}
}
template<typename T>
void fill_uninitialized_indices_cb(const void *value, void *dst, IndexMask mask)
{
const T &value_ = *static_cast<const T *>(value);
T *dst_ = static_cast<T *>(dst);
mask.foreach_index([&](int64_t i) { new (dst_ + i) T(value_); });
}
template<typename T> void debug_print_cb(const void *value, std::stringstream &ss)
{
const T &value_ = *static_cast<const T *>(value);
ss << value_;
}
template<typename T> bool is_equal_cb(const void *a, const void *b)
{
const T &a_ = *static_cast<const T *>(a);
const T &b_ = *static_cast<const T *>(b);
return a_ == b_;
}
template<typename T> uint64_t hash_cb(const void *value)
{
const T &value_ = *static_cast<const T *>(value);
return DefaultHash<T>{}(value_);
}
} // namespace blender::fn::cpp_type_util
namespace blender::fn {
template<typename T>
inline std::unique_ptr<const CPPType> create_cpp_type(StringRef name, const T &default_value)
{
using namespace cpp_type_util;
const CPPType *type = new CPPType(name,
sizeof(T),
alignof(T),
std::is_trivially_destructible_v<T>,
construct_default_cb<T>,
construct_default_n_cb<T>,
construct_default_indices_cb<T>,
destruct_cb<T>,
destruct_n_cb<T>,
destruct_indices_cb<T>,
copy_to_initialized_cb<T>,
copy_to_initialized_n_cb<T>,
copy_to_initialized_indices_cb<T>,
copy_to_uninitialized_cb<T>,
copy_to_uninitialized_n_cb<T>,
copy_to_uninitialized_indices_cb<T>,
move_to_initialized_cb<T>,
move_to_initialized_n_cb<T>,
move_to_initialized_indices_cb<T>,
move_to_uninitialized_cb<T>,
move_to_uninitialized_n_cb<T>,
move_to_uninitialized_indices_cb<T>,
relocate_to_initialized_cb<T>,
relocate_to_initialized_n_cb<T>,
relocate_to_initialized_indices_cb<T>,
relocate_to_uninitialized_cb<T>,
relocate_to_uninitialized_n_cb<T>,
relocate_to_uninitialized_indices_cb<T>,
fill_initialized_cb<T>,
fill_initialized_indices_cb<T>,
fill_uninitialized_cb<T>,
fill_uninitialized_indices_cb<T>,
debug_print_cb<T>,
is_equal_cb<T>,
hash_cb<T>,
static_cast<const void *>(&default_value));
return std::unique_ptr<const CPPType>(type);
}
} // namespace blender::fn
#define MAKE_CPP_TYPE(IDENTIFIER, TYPE_NAME) \
template<> const blender::fn::CPPType &blender::fn::CPPType::get<TYPE_NAME>() \
{ \
static TYPE_NAME default_value; \
static std::unique_ptr<const CPPType> cpp_type = blender::fn::create_cpp_type<TYPE_NAME>( \
STRINGIFY(IDENTIFIER), default_value); \
return *cpp_type; \
} \
/* Support using `CPPType::get<const T>()`. Otherwise the caller would have to remove const. */ \
template<> const blender::fn::CPPType &blender::fn::CPPType::get<const TYPE_NAME>() \
{ \
return blender::fn::CPPType::get<TYPE_NAME>(); \
}
|