Blender V5.3
BKE_node_socket_value.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include <type_traits>
8#include <utility>
9
10#include "BLI_any.hh"
11#include "BLI_cpp_type.hh"
14
16
17namespace blender::bke {
18
20
21namespace detail {
22
25
27 const CPPType &type;
28 bool (*convert_to)(const CPPType &dst_type, SocketValueVariantAny &value);
29 bool (*is_interpretable_as)(const CPPType &dst_type, const SocketValueVariantAny &value);
30
31 template<typename T> static SocketValueVariantTypeInfo get()
32 {
35 .convert_to = try_convert_fn<T>,
36 .is_interpretable_as = is_interpretable_as_fn<T>,
37 };
38 }
39
40 template<typename T>
41 static bool try_convert_fn(const CPPType &dst_type, SocketValueVariantAny &value);
42
43 template<typename T>
44 static bool is_interpretable_as_fn(const CPPType &dst_type, const SocketValueVariantAny &value);
45};
46
47template<typename T>
48concept has_generic_type = requires { typename T::generic_type; };
49template<typename T> struct storage_type {
50 using type = T;
51};
52template<has_generic_type T> struct storage_type<T> {
53 using type = typename T::generic_type;
54};
55
56inline const CPPType &to_storage_type(const CPPType &type)
57{
58 if (type.generic_type) {
59 return *type.generic_type;
60 }
61 return type; // NOLINT
62}
63
64}; // namespace detail
65
84 private:
86
88
94 template<typename T> using to_storage_type = typename detail::storage_type<T>::type;
95
96 public:
101
106 template<typename T>
107 explicit SocketValueVariant(T &&value)
108 /* Required to avoid overriding the copy/move-constructors. */
109 requires(std::is_trivial_v<std::decay_t<T>> || is_same_any_v<std::decay_t<T>, std::string>);
110
112 template<typename T> static SocketValueVariant &construct_in(void *ptr, T &&value);
113
115 template<typename T> static SocketValueVariant from(T &&value);
116
118 template<typename T, typename... Args> T &emplace(Args &&...args);
119
124 template<typename T> T *try_convert();
125 void *try_convert(const CPPType &type);
126
131 template<typename T> T &ensure_type();
132 void *ensure_type(const CPPType &type);
133
135 template<typename T> T copy_as() const;
136
144 template<typename T> T extract();
145
149 template<typename T> const T *get_if() const;
150 template<typename T> T *get_if();
151 const void *get_if(const CPPType &type) const;
152 void *get_if(const CPPType &type);
153
155 GPointer get() const;
157
161 bool is_context_dependent_field() const;
162
166 bool is_field() const;
167
171 bool is_volume_grid() const;
172
176 bool is_single() const;
177
181 bool is_list() const;
182
185 bool owns_direct_data() const;
186
191 void *allocate_single(const CPPType &type);
192
193 void count_memory(MemoryCounter &memory) const;
194
195 private:
196 template<typename T> T &init_default();
197 void *init_default(const CPPType &type);
198
199 public:
200 template<typename T> static T &init_default(detail::SocketValueVariantAny &value);
201 static void *init_default(const CPPType &type, detail::SocketValueVariantAny &value);
202 static void *allocate(const CPPType &type, detail::SocketValueVariantAny &value);
203};
204
205template<typename T>
207 requires(std::is_trivial_v<std::decay_t<T>> || is_same_any_v<std::decay_t<T>, std::string>)
208{
209 this->emplace<std::decay_t<T>>(std::forward<T>(value));
210}
211
212template<typename T>
214{
215 SocketValueVariant *value_variant = new (ptr) SocketValueVariant();
216 value_variant->emplace<std::decay_t<T>>(std::forward<T>(value));
217 return *value_variant;
218}
219
220template<typename T> inline SocketValueVariant SocketValueVariant::from(T &&value)
221{
222 SocketValueVariant value_variant;
223 value_variant.emplace<std::decay_t<T>>(std::forward<T>(value));
224 return value_variant;
225}
226
227template<typename T, typename... Args> inline T &SocketValueVariant::emplace(Args &&...args)
228{
229 using StorageT = to_storage_type<T>;
230 StorageT &value = value_.emplace<StorageT>(T(std::forward<Args>(args)...));
231 static_assert(sizeof(T) == sizeof(StorageT));
232 return reinterpret_cast<T &>(value);
233}
234
235template<typename T> T *SocketValueVariant::try_convert()
236{
237 using StorageT = to_storage_type<T>;
238 if (!value_.has_value()) {
239 return nullptr;
240 }
241 const Info &info = value_.extra_info();
242 const CPPType &requested_type = CPPType::get<T>();
243 if (info.type == requested_type) {
244 return &reinterpret_cast<T &>(value_.get<StorageT>());
245 }
246 if (info.is_interpretable_as(requested_type, value_)) {
247 return &reinterpret_cast<T &>(value_.get<StorageT>());
248 }
249 if (!info.convert_to(requested_type, value_)) {
250 return nullptr;
251 }
252 BLI_assert(value_.extra_info().is_interpretable_as(requested_type, value_));
253 return &reinterpret_cast<T &>(value_.get<StorageT>());
254}
255
257{
258 if (!value_.has_value()) {
259 return nullptr;
260 }
261 const Info &info = value_.extra_info();
262 if (info.type == type) {
263 return value_.get();
264 }
265 if (!info.convert_to(type, value_)) {
266 return nullptr;
267 }
268 return value_.get();
269}
270
271template<typename T> inline T &SocketValueVariant::ensure_type()
272{
273 if (!this->try_convert<T>()) {
274 return this->init_default<T>();
275 }
276 return *this->get_if<T>();
277}
278
280{
281 if (!this->try_convert(type)) {
282 return this->init_default(type);
283 }
284 return value_.get();
285}
286
287template<typename T> T SocketValueVariant::copy_as() const
288{
289 SocketValueVariant copy(*this);
290 return copy.extract<T>();
291}
292
293template<typename T> inline const T *SocketValueVariant::get_if() const
294{
295 if (!value_) {
296 return nullptr;
297 }
298 const Info &info = value_.extra_info();
299 const CPPType &requested_type = CPPType::get<T>();
300 if (info.type == requested_type) {
301 return &value_.get<T>();
302 }
303 if (info.is_interpretable_as(requested_type, value_)) {
304 if constexpr (detail::has_generic_type<T>) {
305 using GenericT = T::generic_type;
306 return reinterpret_cast<const T *>(&value_.get<GenericT>());
307 }
308 else {
309 return &value_.get<T>();
310 }
311 }
312 return nullptr;
313}
314
315template<typename T> inline T *SocketValueVariant::get_if()
316{
317 return const_cast<T *>(std::as_const(*this).get_if<T>());
318}
319
320inline const void *SocketValueVariant::get_if(const CPPType &type) const
321{
322 if (!value_) {
323 return nullptr;
324 }
325 const Info &info = value_.extra_info();
326 if (info.type == type) {
327 return value_.get();
328 }
329 if (info.is_interpretable_as(type, value_)) {
330 return value_.get();
331 }
332 return nullptr;
333}
334
335inline void *SocketValueVariant::get_if(const CPPType &type)
336{
337 return const_cast<void *>(std::as_const(*this).get_if(type));
338}
339
341{
342 if (!value_) {
343 return {};
344 }
345 const Info &info = value_.extra_info();
346 return {info.type, value_.get()};
347}
348
350{
351 if (!value_) {
352 return {};
353 }
354 const Info &info = value_.extra_info();
355 return {info.type, value_.get()};
356}
357
358template<typename T> inline T &SocketValueVariant::init_default()
359{
360 return SocketValueVariant::init_default<T>(value_);
361}
362
363inline void *SocketValueVariant::init_default(const CPPType &type)
364{
365 return SocketValueVariant::init_default(type, value_);
366}
367
368template<typename T> inline T SocketValueVariant::extract()
369{
370 return std::move(this->ensure_type<T>());
371}
372
373} // namespace blender::bke
static const CPPType & get()
const CPPType * generic_type
static const CPPType * get_pre_register()
void * allocate_single(const CPPType &type)
void count_memory(MemoryCounter &memory) const
static SocketValueVariant & construct_in(void *ptr, T &&value)
static SocketValueVariant from(T &&value)
static void * allocate(const CPPType &type, detail::SocketValueVariantAny &value)
#define T
Any< SocketValueVariantTypeInfo, 32, 16 > SocketValueVariantAny
const CPPType & to_storage_type(const CPPType &type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
constexpr bool is_same_any_v
static bool try_convert_fn(const CPPType &dst_type, SocketValueVariantAny &value)
bool(*) is_interpretable_as(const CPPType &dst_type, const SocketValueVariantAny &value)
bool(*) convert_to(const CPPType &dst_type, SocketValueVariantAny &value)
static bool is_interpretable_as_fn(const CPPType &dst_type, const SocketValueVariantAny &value)