Blender V5.3
NOD_geometry_nodes_bundle.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BKE_node.hh"
13
15#include "BLI_ustring.hh"
16
20
21#include "DNA_node_types.h"
22
23namespace blender::nodes {
24
30
35 public:
37 virtual StringRefNull type_name() const = 0;
38};
39
43
45 std::variant<BundleItemSocketValue, BundleItemInternalValue> value;
46
50 std::optional<bke::SocketValueVariant> as_socket_value(
51 const bke::bNodeSocketType &dst_socket_type) const;
52 template<typename T>
53 std::optional<T> as_socket_value(const bke::bNodeSocketType &socket_type) const;
54 template<typename T> std::optional<T> as() const;
55
59 template<typename T> T *as_pointer();
60 template<typename T> const T *as_pointer() const;
61};
62
63class BundleKey {
64 private:
65 UString key_;
66
67 BundleKey(UString key);
68
69 public:
70 static std::optional<BundleKey> from_ustr(UString key);
71 static std::optional<BundleKey> from_str(StringRef key);
72
73 uint64_t hash() const;
74 bool operator==(const BundleKey &other) const = default;
75
76 UString ustr() const;
77
78 /* Disallow certain characters so that we can use them to e.g. build a bundle path or
79 * expressions referencing multiple bundle items. We might not need all of them in the future,
80 * but better reserve them now while we still can. */
81 static constexpr StringRefNull forbidden_key_chars = "/*&|\"^~!,{}()+$#@[];:?<>.-%\\=";
82 static bool is_valid_key(StringRef key);
83};
84
91 public:
93
94 private:
95 BundleItemMap items_;
96
97 public:
99 static BundlePtr create();
100
101 bool add(BundleKey key, const BundleItemValue &value);
102 bool add(BundleKey key, BundleItemValue &&value);
103 void add_new(BundleKey key, const BundleItemValue &value);
104 void add_new(BundleKey key, BundleItemValue &&value);
105 void add_override(BundleKey key, const BundleItemValue &value);
106 bool add_path(StringRef path, const BundleItemValue &value);
107 void add_path_new(StringRef path, const BundleItemValue &value);
108 void add_path_override(StringRef path, const BundleItemValue &value);
109 void add_path_override(Span<BundleKey> path, const BundleItemValue &value);
110
111 template<typename T>
112 requires(!std::is_same_v<std::decay_t<T>, BundleItemValue>)
113 void add(BundleKey key, T &&value);
114 template<typename T>
115 requires(!std::is_same_v<std::decay_t<T>, BundleItemValue>)
116 void add_new(BundleKey key, T &&value);
117 template<typename T>
118 requires(!std::is_same_v<std::decay_t<T>, BundleItemValue>)
119 void add_override(BundleKey key, T &&value);
120 template<typename T>
121 requires(!std::is_same_v<std::decay_t<T>, BundleItemValue>)
122 void add_path(StringRef path, T &&value);
123 template<typename T>
124 requires(!std::is_same_v<std::decay_t<T>, BundleItemValue>)
125 void add_path_override(Span<BundleKey> path, T &&value);
126 template<typename T>
127 requires(!std::is_same_v<std::decay_t<T>, BundleItemValue>)
128 void add_path_override(StringRef path, T &&value);
129
130 bool remove(BundleKey key);
131 bool remove_path(StringRef path);
132 bool remove_path(Span<BundleKey> path);
133 bool contains(BundleKey key) const;
134 bool contains_path(StringRef path) const;
135 bool contains_path(Span<BundleKey> path) const;
136
137 const BundleItemValue *lookup(BundleKey key) const;
140 const BundleItemValue *lookup_path(StringRef path) const;
143 template<typename T> std::optional<T> lookup(BundleKey key) const;
144 template<typename T> std::optional<T> lookup_path(Span<BundleKey> path) const;
145 template<typename T> std::optional<T> lookup_path(StringRef path) const;
146 template<typename T> T *lookup_ptr(BundleKey key);
147 template<typename T> const T *lookup_ptr(BundleKey key) const;
148 template<typename T> const T *lookup_path_ptr(StringRef path) const;
149 template<typename T> const T *lookup_path_ptr(Span<BundleKey> path) const;
150 template<typename T> T *lookup_path_for_write_ptr(StringRef path);
151 template<typename T> T *lookup_path_for_write_ptr(Span<BundleKey> path);
152
154
155 void merge(const Bundle &other);
156 void merge_override(const Bundle &other);
157
158 bool is_empty() const;
159 int64_t size() const;
160
161 void clear();
162
163 std::optional<StringRef> type() const;
164
167 bool owns_direct_data() const;
168
169 BundleItemMap::ItemIterator items() const;
170 BundleItemMap::MutableItemIterator items();
171
172 BundlePtr copy() const;
173
174 void delete_self() override;
175
176 void count_memory(MemoryCounter &memory) const;
177
179
181 static std::string combine_path(Span<StringRef> path);
182 static std::string combine_path(Span<BundleKey> path);
183
184 static bool is_valid_path(StringRef path);
185 static std::optional<Vector<BundleKey>> split_path(StringRef path);
186};
187
193
195 const Bundle &bundle, FunctionRef<bool(StringRef type)> type_filter_fn);
197 eNodeSocketDatatype data_type);
198
200 const Bundle &bundle,
201 FunctionRef<void(Span<BundleKey> path, const BundleItemValue &value)> fn);
202
203template<typename T>
204inline std::optional<T> BundleItemValue::as_socket_value(
205 const bke::bNodeSocketType &dst_socket_type) const
206{
207 if (const std::optional<bke::SocketValueVariant> value = this->as_socket_value(dst_socket_type))
208 {
209 return *value->get_if<T>();
210 }
211 return std::nullopt;
212}
213
214template<typename T> inline T *BundleItemValue::as_pointer()
215{
216 return const_cast<T *>(std::as_const(*this).as_pointer<T>());
217}
218template<typename T> inline const T *BundleItemValue::as_pointer() const
219{
220 const BundleItemSocketValue *socket_value = std::get_if<BundleItemSocketValue>(&this->value);
221 if (!socket_value) {
222 return nullptr;
223 }
224 const GPointer ptr = socket_value->value.get();
225 if (!ptr.is_type<T>()) {
226 return nullptr;
227 }
228 return ptr.get<T>();
229}
230
231template<typename T> inline const bke::bNodeSocketType *socket_type_info_by_static_type()
232{
233 if constexpr (fn::is_field_v<T>) {
234 const std::optional<eNodeSocketDatatype> socket_type =
236 BLI_assert(socket_type);
237 const bke::bNodeSocketType *socket_type_info = bke::node_socket_type_find_static(*socket_type);
238 BLI_assert(socket_type_info);
239 return socket_type_info;
240 }
241 else {
242 const std::optional<eNodeSocketDatatype> socket_type =
244 if (!socket_type) {
245 return nullptr;
246 }
247 return bke::node_socket_type_find_static(*socket_type);
248 }
249}
250
251template<typename T> constexpr bool is_valid_internal_bundle_item_type()
252{
253 if constexpr (is_ImplicitSharingPtr_strong_v<T>) {
254 if constexpr (std::is_base_of_v<BundleItemInternalValueMixin, typename T::element_type>) {
255 return true;
256 }
257 }
258 return false;
259}
260
261template<typename T> inline std::optional<T> BundleItemValue::as() const
262{
264 using SharingInfoT = typename T::element_type;
265 const auto *internal_value = std::get_if<BundleItemInternalValue>(&this->value);
266 if (!internal_value) {
267 return std::nullopt;
268 }
269 const BundleItemInternalValueMixin *sharing_info = internal_value->value.get();
270 const SharingInfoT *converted_value = dynamic_cast<const SharingInfoT *>(sharing_info);
271 if (!converted_value) {
272 return std::nullopt;
273 }
274 sharing_info->add_user();
275 return ImplicitSharingPtr<SharingInfoT>{converted_value};
276 }
277 else if constexpr (std::is_same_v<T, bke::SocketValueVariant>) {
278 if (const BundleItemSocketValue *socket_value = std::get_if<BundleItemSocketValue>(
279 &this->value))
280 {
281 return socket_value->value;
282 }
283 return std::nullopt;
284 }
285 else if constexpr (std::is_same_v<T, GListPtr>) {
286 const BundleItemSocketValue *socket_value = std::get_if<BundleItemSocketValue>(&this->value);
287 if (!socket_value) {
288 return std::nullopt;
289 }
290 if (socket_value->value.get().is_type<GListPtr>()) {
291 return *socket_value->value.get().get<GListPtr>();
292 }
293 return std::nullopt;
294 }
295 else if (const bke::bNodeSocketType *dst_socket_type = socket_type_info_by_static_type<T>()) {
296 return this->as_socket_value<T>(*dst_socket_type);
297 }
298 /* Can't lookup this type directly currently. */
300 return std::nullopt;
301}
302
303template<typename T> inline std::optional<T> Bundle::lookup(const BundleKey key) const
304{
305 const BundleItemValue *item = this->lookup(key);
306 if (!item) {
307 return std::nullopt;
308 }
309 return item->as<T>();
310}
311
312template<typename T> inline T *Bundle::lookup_ptr(const BundleKey key)
313{
314 BundleItemValue *item = this->lookup(key);
315 return item ? item->as_pointer<T>() : nullptr;
316}
317
318template<typename T> inline const T *Bundle::lookup_path_ptr(const StringRef path) const
319{
320 const BundleItemValue *item = this->lookup_path(path);
321 return item ? item->as_pointer<T>() : nullptr;
322}
323
324template<typename T> inline const T *Bundle::lookup_path_ptr(const Span<BundleKey> path) const
325{
326 const BundleItemValue *item = this->lookup_path(path);
327 return item ? item->as_pointer<T>() : nullptr;
328}
329
330template<typename T> inline const T *Bundle::lookup_ptr(const BundleKey key) const
331{
332 const BundleItemValue *item = this->lookup(key);
333 return item ? item->as_pointer<T>() : nullptr;
334}
335
336template<typename T> inline T *Bundle::lookup_path_for_write_ptr(const Span<BundleKey> path)
337{
338 BundleItemValue *item = this->lookup_path_for_write(path);
339 return item ? item->as_pointer<T>() : nullptr;
340}
341
342template<typename T> inline T *Bundle::lookup_path_for_write_ptr(const StringRef path)
343{
344 BundleItemValue *item = this->lookup_path_for_write(path);
345 return item ? item->as_pointer<T>() : nullptr;
346}
347
348template<typename T> inline std::optional<T> Bundle::lookup_path(const Span<BundleKey> path) const
349{
350 const BundleItemValue *item = this->lookup_path(path);
351 if (!item) {
352 return std::nullopt;
353 }
354 return item->as<T>();
355}
356
357template<typename T> inline std::optional<T> Bundle::lookup_path(const StringRef path) const
358{
359 const BundleItemValue *item = this->lookup_path(path);
360 if (!item) {
361 return std::nullopt;
362 }
363 return item->as<T>();
364}
365
367{
368 const CPPType *base_type;
369 if (value.is_field()) {
370 base_type = &value.get_if<fn::GField>()->cpp_type();
371 }
372 else if (value.is_list()) {
373 const GListPtr &list = *value.get_if<GListPtr>();
375 base_type = &list->cpp_type();
376 }
377 else {
378 base_type = value.get().type();
379 }
380 const std::optional<eNodeSocketDatatype> socket_type =
382 BLI_assert(socket_type);
383 return *socket_type;
384}
385
386template<typename T, typename Fn> inline void to_stored_type(T &&value, Fn &&fn)
387{
388 using DecayT = std::decay_t<T>;
389 if constexpr (std::is_same_v<DecayT, BundleItemValue>) {
390 fn(std::forward<T>(value));
391 }
392 else if constexpr (std::is_same_v<DecayT, BundleItemSocketValue>) {
393 fn(BundleItemValue{std::forward<T>(value)});
394 }
395 else if constexpr (std::is_same_v<DecayT, BundleItemInternalValue>) {
396 fn(BundleItemValue{std::forward<T>(value)});
397 }
398 else if constexpr (std::is_same_v<DecayT, bke::SocketValueVariant>) {
399 const eNodeSocketDatatype socket_type = socket_type_for_value_variant(value);
400 const bke::bNodeSocketType *socket_type_info = bke::node_socket_type_find_static(socket_type);
401 fn(BundleItemValue{BundleItemSocketValue{socket_type_info, std::forward<T>(value)}});
402 }
403 else if constexpr (is_valid_internal_bundle_item_type<DecayT>()) {
404 const BundleItemInternalValueMixin *sharing_info = value.get();
405 if (sharing_info) {
406 sharing_info->add_user();
407 }
409 }
410 else if (const bke::bNodeSocketType *socket_type = socket_type_info_by_static_type<DecayT>()) {
411 auto value_variant = bke::SocketValueVariant::from(std::forward<T>(value));
412 fn(BundleItemValue{BundleItemSocketValue{socket_type, value_variant}});
413 }
414 else {
415 /* All allowed types should be handled above already. */
417 }
418}
419
420template<typename T>
421 requires(!std::is_same_v<std::decay_t<T>, BundleItemValue>)
422inline void Bundle::add(const BundleKey key, T &&value)
423{
424 to_stored_type(std::forward<T>(value),
425 [&]<typename U>(U &&item_value) { this->add(key, std::forward<U>(item_value)); });
426}
427
428template<typename T>
429 requires(!std::is_same_v<std::decay_t<T>, BundleItemValue>)
430inline void Bundle::add_new(const BundleKey key, T &&value)
431{
432 to_stored_type(std::forward<T>(value), [&]<typename U>(U &&item_value) {
433 this->add_new(key, std::forward<U>(item_value));
434 });
435}
436
437template<typename T>
438 requires(!std::is_same_v<std::decay_t<T>, BundleItemValue>)
439inline void Bundle::add_path(const StringRef path, T &&value)
440{
441 to_stored_type(std::forward<T>(value), [&]<typename U>(U &&item_value) {
442 this->add_path(path, std::forward<U>(item_value));
443 });
444}
445
446template<typename T>
447 requires(!std::is_same_v<std::decay_t<T>, BundleItemValue>)
448inline void Bundle::add_override(const BundleKey key, T &&value)
449{
450 to_stored_type(std::forward<T>(value), [&]<typename U>(U &&item_value) {
451 this->add_override(key, std::forward<U>(item_value));
452 });
453}
454
455template<typename T>
456 requires(!std::is_same_v<std::decay_t<T>, BundleItemValue>)
457inline void Bundle::add_path_override(const StringRef path, T &&value)
458{
459 to_stored_type(std::forward<T>(value), [&]<typename U>(U &&item_value) {
460 this->add_path_override(path, std::forward<U>(item_value));
461 });
462}
463
464template<typename T>
465 requires(!std::is_same_v<std::decay_t<T>, BundleItemValue>)
467{
468 to_stored_type(std::forward<T>(value), [&]<typename U>(U &&item_value) {
469 this->add_path_override(path, std::forward<U>(item_value));
470 });
471}
472
473inline Bundle::BundleItemMap::ItemIterator Bundle::items() const
474{
475 return items_.items();
476}
477
478inline Bundle::BundleItemMap::MutableItemIterator Bundle::items()
479{
480 return items_.items();
481}
482
483inline bool Bundle::is_empty() const
484{
485 return items_.is_empty();
486}
487
488inline int64_t Bundle::size() const
489{
490 return items_.size();
491}
492
493inline BundleKey::BundleKey(const UString key) : key_(key) {}
494
496{
497 return get_default_hash(key_);
498}
499
500std::optional<BundleKey> inline BundleKey::from_ustr(const UString key)
501{
502 if (is_valid_key(key.ref())) {
503 return BundleKey(key);
504 }
505 return std::nullopt;
506}
507
508std::optional<BundleKey> inline BundleKey::from_str(const StringRef key)
509{
510 /* Check validity before converting to UString. */
511 if (is_valid_key(key)) {
512 return BundleKey(UString(key));
513 }
514 return std::nullopt;
515}
516
518{
519 return key_;
520}
521
522inline std::string_view format_as(const BundleKey key)
523{
524 return key.ustr().ref();
525}
526
527} // namespace blender::nodes
#define BLI_assert_unreachable()
static const CPPType & get()
const CPPType * type() const
const void * get() const
StringRefNull ref() const
UString()=default
static SocketValueVariant from(T &&value)
static std::optional< BundleKey > from_ustr(UString key)
static std::optional< BundleKey > from_str(StringRef key)
virtual StringRefNull type_name() const =0
bool operator==(const BundleKey &other) const =default
static bool is_valid_key(StringRef key)
static constexpr StringRefNull forbidden_key_chars
static std::optional< BundleKey > from_ustr(UString key)
static std::optional< BundleKey > from_str(StringRef key)
void add_path_new(StringRef path, const BundleItemValue &value)
void merge_override(const Bundle &other)
Vector< std::string > gather_paths(FunctionRef< bool(const Bundle &bundle)> fn) const
void count_memory(MemoryCounter &memory) const
bool add(BundleKey key, const BundleItemValue &value)
static std::string combine_path(Span< StringRef > path)
static bool is_valid_path(StringRef path)
bool contains(BundleKey key) const
bool contains_path(StringRef path) const
const T * lookup_path_ptr(StringRef path) const
std::optional< StringRef > type() const
BundleItemValue * lookup_path_for_write(Span< BundleKey > path)
void add_override(BundleKey key, const BundleItemValue &value)
void add_path_override(StringRef path, const BundleItemValue &value)
Map< BundleKey, BundleItemValue > BundleItemMap
bool remove_path(StringRef path)
void merge(const Bundle &other)
const BundleItemValue * lookup_path(Span< BundleKey > path) const
const BundleItemValue * lookup(BundleKey key) const
static std::optional< Vector< BundleKey > > split_path(StringRef path)
Bundle & ensure_nested_bundle(StringRef path)
bool add_path(StringRef path, const BundleItemValue &value)
void add_new(BundleKey key, const BundleItemValue &value)
T * lookup_path_for_write_ptr(StringRef path)
BundleItemMap::ItemIterator items() const
#define T
std::optional< eNodeSocketDatatype > geo_nodes_base_cpp_type_to_socket_type(const CPPType &type)
Definition node.cc:6008
bNodeSocketType * node_socket_type_find_static(int type, int subtype=0)
Definition node.cc:2975
constexpr bool is_field_v
Definition FN_field.hh:425
const bke::bNodeSocketType * socket_type_info_by_static_type()
std::string_view format_as(const BundleKey key)
constexpr bool is_valid_internal_bundle_item_type()
Vector< std::string > gather_bundle_paths_by_bundle_type(const Bundle &bundle, const FunctionRef< bool(StringRef type)> type_filter_fn)
void to_stored_type(T &&value, Fn &&fn)
eNodeSocketDatatype socket_type_for_value_variant(const bke::SocketValueVariant &value)
Vector< std::string > gather_bundle_paths_by_data_type(const Bundle &bundle, const eNodeSocketDatatype data_type)
void foreach_nested_bundle_item(const Bundle &bundle, const FunctionRef< void(Span< BundleKey >, const BundleItemValue &value)> fn)
ImplicitSharingPtr< Bundle > BundlePtr
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
Defines a socket type.
Definition BKE_node.hh:163
ImplicitSharingPtr< BundleItemInternalValueMixin > value
std::variant< BundleItemSocketValue, BundleItemInternalValue > value
std::optional< bke::SocketValueVariant > as_socket_value(const bke::bNodeSocketType &dst_socket_type) const