Blender V5.3
viewer_path.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_lib_query.hh"
10#include "BKE_lib_remap.hh"
11#include "BKE_viewer_path.hh"
12
13#include "BLI_index_range.hh"
14#include "BLI_listbase.hh"
15#include "BLI_string.hh"
16#include "BLI_string_ref.hh"
17
18#include "MEM_guardedalloc.h"
19
20#include "BLO_read_write.hh"
21
22namespace blender {
23
25{
26 viewer_path->path.clear_no_delete();
27}
28
30{
31 for (ViewerPathElem &elem : viewer_path->path.items_mutable()) {
33 }
34 viewer_path->path.clear_no_delete();
35}
36
38{
40 for (const ViewerPathElem &src_elem : src->path) {
41 ViewerPathElem *new_elem = BKE_viewer_path_elem_copy(&src_elem);
42 BLI_addtail(&dst->path, new_elem);
43 }
44}
45
47 const ViewerPath *b,
48 const ViewerPathEqualFlag flag)
49{
50 const ViewerPathElem *elem_a = a->path.first();
51 const ViewerPathElem *elem_b = b->path.first();
52
53 while (elem_a != nullptr && elem_b != nullptr) {
54 if (!BKE_viewer_path_elem_equal(elem_a, elem_b, flag)) {
55 return false;
56 }
57 elem_a = elem_a->next;
58 elem_b = elem_b->next;
59 }
60 if (elem_a == nullptr && elem_b == nullptr) {
61 return true;
62 }
63 return false;
64}
65
67{
68 uint64_t hash = 0;
69 for (ViewerPathElem &elem : viewer_path.path) {
70 const uint64_t elem_hash = BKE_viewer_path_elem_hash(elem);
71 hash = get_default_hash(hash, elem_hash);
72 }
73 return hash;
74}
75
76void BKE_viewer_path_blend_write(BlendWriter *writer, const ViewerPath *viewer_path)
77{
78 for (ViewerPathElem &elem : viewer_path->path) {
79 switch (ViewerPathElemType(elem.type)) {
81 const auto *typed_elem = reinterpret_cast<IDViewerPathElem *>(&elem);
82 writer->write_struct(typed_elem);
83 break;
84 }
86 const auto *typed_elem = reinterpret_cast<ModifierViewerPathElem *>(&elem);
87 writer->write_struct(typed_elem);
88 break;
89 }
91 const auto *typed_elem = reinterpret_cast<GroupNodeViewerPathElem *>(&elem);
92 writer->write_struct(typed_elem);
93 break;
94 }
96 const auto *typed_elem = reinterpret_cast<SimulationZoneViewerPathElem *>(&elem);
97 writer->write_struct(typed_elem);
98 break;
99 }
101 const auto *typed_elem = reinterpret_cast<ViewerNodeViewerPathElem *>(&elem);
102 writer->write_struct(typed_elem);
103 break;
104 }
106 const auto *typed_elem = reinterpret_cast<RepeatZoneViewerPathElem *>(&elem);
107 writer->write_struct(typed_elem);
108 break;
109 }
111 const auto *typed_elem = reinterpret_cast<ForeachGeometryElementZoneViewerPathElem *>(
112 &elem);
113 writer->write_struct(typed_elem);
114 break;
115 }
117 const auto *typed_elem = reinterpret_cast<EvaluateClosureNodeViewerPathElem *>(&elem);
118 writer->write_struct(typed_elem);
119 break;
120 }
121 }
122 writer->write_string(elem.ui_name);
123 }
124}
125
148
150{
152 reader, &viewer_path->path, [](const ViewerPathElem &elem) {
153 return viewer_path_elem_struct_size(elem.type);
154 });
155 for (ViewerPathElem &elem : viewer_path->path) {
156 BLO_read_string(reader, &elem.ui_name);
157 switch (ViewerPathElemType(elem.type)) {
166 break;
167 }
168 }
169 }
170}
171
173{
174 for (ViewerPathElem &elem : viewer_path->path) {
175 switch (ViewerPathElemType(elem.type)) {
177 auto *typed_elem = reinterpret_cast<IDViewerPathElem *>(&elem);
179 break;
180 }
182 auto *typed_elem = reinterpret_cast<EvaluateClosureNodeViewerPathElem *>(&elem);
184 data, typed_elem->source_node_tree, IDWALK_CB_DIRECT_WEAK_LINK);
185 break;
186 }
193 break;
194 }
195 }
196 }
197}
198
199void BKE_viewer_path_id_remap(ViewerPath *viewer_path, const bke::id::IDRemapper &mappings)
200{
201 for (ViewerPathElem &elem : viewer_path->path) {
202 switch (ViewerPathElemType(elem.type)) {
204 auto *typed_elem = reinterpret_cast<IDViewerPathElem *>(&elem);
205 mappings.apply(&typed_elem->id, ID_REMAP_APPLY_DEFAULT);
206 break;
207 }
215 break;
216 }
217 }
218 }
219}
220
221template<typename T> static T *make_elem(const ViewerPathElemType type)
222{
223 T *elem = MEM_new<T>(__func__);
224 elem->base.type = type;
225 return elem;
226}
227
229{
230 switch (type) {
232 return &make_elem<IDViewerPathElem>(type)->base;
233 }
235 return &make_elem<ModifierViewerPathElem>(type)->base;
236 }
238 return &make_elem<GroupNodeViewerPathElem>(type)->base;
239 }
241 return &make_elem<SimulationZoneViewerPathElem>(type)->base;
242 }
244 return &make_elem<ViewerNodeViewerPathElem>(type)->base;
245 }
247 return &make_elem<RepeatZoneViewerPathElem>(type)->base;
248 }
251 }
254 }
255 }
257 return nullptr;
258}
259
264
270
276
282
288
294
300
306
308{
310 if (src->ui_name) {
311 dst->ui_name = BLI_strdup(src->ui_name);
312 }
313 switch (ViewerPathElemType(src->type)) {
315 const auto *old_elem = reinterpret_cast<const IDViewerPathElem *>(src);
316 auto *new_elem = reinterpret_cast<IDViewerPathElem *>(dst);
317 new_elem->id = old_elem->id;
318 break;
319 }
321 const auto *old_elem = reinterpret_cast<const ModifierViewerPathElem *>(src);
322 auto *new_elem = reinterpret_cast<ModifierViewerPathElem *>(dst);
323 new_elem->modifier_uid = old_elem->modifier_uid;
324 break;
325 }
327 const auto *old_elem = reinterpret_cast<const GroupNodeViewerPathElem *>(src);
328 auto *new_elem = reinterpret_cast<GroupNodeViewerPathElem *>(dst);
329 new_elem->node_id = old_elem->node_id;
330 break;
331 }
333 const auto *old_elem = reinterpret_cast<const SimulationZoneViewerPathElem *>(src);
334 auto *new_elem = reinterpret_cast<SimulationZoneViewerPathElem *>(dst);
335 new_elem->sim_output_node_id = old_elem->sim_output_node_id;
336 break;
337 }
339 const auto *old_elem = reinterpret_cast<const ViewerNodeViewerPathElem *>(src);
340 auto *new_elem = reinterpret_cast<ViewerNodeViewerPathElem *>(dst);
341 new_elem->node_id = old_elem->node_id;
342 break;
343 }
345 const auto *old_elem = reinterpret_cast<const RepeatZoneViewerPathElem *>(src);
346 auto *new_elem = reinterpret_cast<RepeatZoneViewerPathElem *>(dst);
347 new_elem->repeat_output_node_id = old_elem->repeat_output_node_id;
348 new_elem->iteration = old_elem->iteration;
349 break;
350 }
352 const auto *old_elem = reinterpret_cast<const ForeachGeometryElementZoneViewerPathElem *>(
353 src);
354 auto *new_elem = reinterpret_cast<ForeachGeometryElementZoneViewerPathElem *>(dst);
355 new_elem->zone_output_node_id = old_elem->zone_output_node_id;
356 new_elem->index = old_elem->index;
357 break;
358 }
360 const auto *old_elem = reinterpret_cast<const EvaluateClosureNodeViewerPathElem *>(src);
361 auto *new_elem = reinterpret_cast<EvaluateClosureNodeViewerPathElem *>(dst);
362 new_elem->source_output_node_id = old_elem->source_output_node_id;
363 new_elem->evaluate_node_id = old_elem->evaluate_node_id;
364 new_elem->source_node_tree = old_elem->source_node_tree;
365 break;
366 }
367 }
368 return dst;
369}
370
372 const ViewerPathElem *b,
373 const ViewerPathEqualFlag flag)
374{
375 if (a->type != b->type) {
376 return false;
377 }
379 if (StringRef(a->ui_name) != StringRef(b->ui_name)) {
380 return false;
381 }
382 }
383 switch (ViewerPathElemType(a->type)) {
385 const auto *a_elem = reinterpret_cast<const IDViewerPathElem *>(a);
386 const auto *b_elem = reinterpret_cast<const IDViewerPathElem *>(b);
387 return a_elem->id == b_elem->id;
388 }
390 const auto *a_elem = reinterpret_cast<const ModifierViewerPathElem *>(a);
391 const auto *b_elem = reinterpret_cast<const ModifierViewerPathElem *>(b);
392 return a_elem->modifier_uid == b_elem->modifier_uid;
393 }
395 const auto *a_elem = reinterpret_cast<const GroupNodeViewerPathElem *>(a);
396 const auto *b_elem = reinterpret_cast<const GroupNodeViewerPathElem *>(b);
397 return a_elem->node_id == b_elem->node_id;
398 }
400 const auto *a_elem = reinterpret_cast<const SimulationZoneViewerPathElem *>(a);
401 const auto *b_elem = reinterpret_cast<const SimulationZoneViewerPathElem *>(b);
402 return a_elem->sim_output_node_id == b_elem->sim_output_node_id;
403 }
405 const auto *a_elem = reinterpret_cast<const ViewerNodeViewerPathElem *>(a);
406 const auto *b_elem = reinterpret_cast<const ViewerNodeViewerPathElem *>(b);
407 return a_elem->node_id == b_elem->node_id;
408 }
410 const auto *a_elem = reinterpret_cast<const RepeatZoneViewerPathElem *>(a);
411 const auto *b_elem = reinterpret_cast<const RepeatZoneViewerPathElem *>(b);
412 return a_elem->repeat_output_node_id == b_elem->repeat_output_node_id &&
414 a_elem->iteration == b_elem->iteration);
415 }
417 const auto *a_elem = reinterpret_cast<const ForeachGeometryElementZoneViewerPathElem *>(a);
418 const auto *b_elem = reinterpret_cast<const ForeachGeometryElementZoneViewerPathElem *>(b);
419 return a_elem->zone_output_node_id == b_elem->zone_output_node_id &&
421 a_elem->index == b_elem->index);
422 }
424 const auto *a_elem = reinterpret_cast<const EvaluateClosureNodeViewerPathElem *>(a);
425 const auto *b_elem = reinterpret_cast<const EvaluateClosureNodeViewerPathElem *>(b);
426 return a_elem->source_output_node_id == b_elem->source_output_node_id &&
427 a_elem->evaluate_node_id == b_elem->evaluate_node_id &&
428 a_elem->source_node_tree == b_elem->source_node_tree;
429 }
430 }
431 return false;
432}
433
435{
436 switch (ViewerPathElemType(elem.type)) {
438 const auto &typed_elem = reinterpret_cast<const IDViewerPathElem &>(elem);
439 return get_default_hash(elem.type, typed_elem.id ? typed_elem.id->session_uid : 0);
440 }
442 const auto &typed_elem = reinterpret_cast<const ModifierViewerPathElem &>(elem);
443 return get_default_hash(elem.type, typed_elem.modifier_uid);
444 }
446 const auto &typed_elem = reinterpret_cast<const GroupNodeViewerPathElem &>(elem);
447 return get_default_hash(elem.type, typed_elem.node_id);
448 }
450 const auto &typed_elem = reinterpret_cast<const SimulationZoneViewerPathElem &>(elem);
451 return get_default_hash(elem.type, typed_elem.sim_output_node_id);
452 }
454 const auto &typed_elem = reinterpret_cast<const ViewerNodeViewerPathElem &>(elem);
455 return get_default_hash(elem.type, typed_elem.node_id);
456 }
458 const auto &typed_elem = reinterpret_cast<const RepeatZoneViewerPathElem &>(elem);
459 return get_default_hash(elem.type, typed_elem.repeat_output_node_id, typed_elem.iteration);
460 }
462 const auto &typed_elem = reinterpret_cast<const ForeachGeometryElementZoneViewerPathElem &>(
463 elem);
464 return get_default_hash(elem.type, typed_elem.zone_output_node_id, typed_elem.index);
465 }
467 const auto &typed_elem = reinterpret_cast<const EvaluateClosureNodeViewerPathElem &>(elem);
468 return get_default_hash(
469 elem.type,
470 typed_elem.evaluate_node_id,
471 typed_elem.source_output_node_id,
472 typed_elem.source_node_tree ?
473 reinterpret_cast<const ID *>(typed_elem.source_node_tree)->session_uid :
474 0);
475 }
476 }
477 return 0;
478}
479
499
500} // namespace blender
#define BKE_LIB_FOREACHID_PROCESS_ID(data_, id_, cb_flag_)
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
constexpr StringRef()
IDRemapperApplyResult apply(ID **r_id_ptr, IDRemapperApplyOptions options, ID *id_self=nullptr) const
#define T
BMesh Iterators.
unsigned __int64 uint64_t
Definition BKE_text.h:91
void BKE_viewer_path_id_remap(ViewerPath *viewer_path, const bke::id::IDRemapper &mappings)
GroupNodeViewerPathElem * BKE_viewer_path_elem_new_group_node()
ViewerNodeViewerPathElem * BKE_viewer_path_elem_new_viewer_node()
void BKE_viewer_path_init(ViewerPath *viewer_path)
void BKE_viewer_path_blend_read_data(BlendDataReader *reader, ViewerPath *viewer_path)
SimulationZoneViewerPathElem * BKE_viewer_path_elem_new_simulation_zone()
IDViewerPathElem * BKE_viewer_path_elem_new_id()
ModifierViewerPathElem * BKE_viewer_path_elem_new_modifier()
uint64_t BKE_viewer_path_hash(const ViewerPath &viewer_path)
void BKE_viewer_path_foreach_id(LibraryForeachIDData *data, ViewerPath *viewer_path)
ForeachGeometryElementZoneViewerPathElem * BKE_viewer_path_elem_new_foreach_geometry_element_zone()
BMesh const char void * data
bool BKE_viewer_path_elem_equal(const ViewerPathElem *a, const ViewerPathElem *b, ViewerPathEqualFlag flag=ViewerPathEqualFlag(0))
@ VIEWER_PATH_EQUAL_FLAG_IGNORE_ITERATION
@ VIEWER_PATH_EQUAL_FLAG_CONSIDER_UI_NAME
void BKE_viewer_path_copy(ViewerPath *dst, const ViewerPath *src)
@ VIEWER_PATH_ELEM_TYPE_REPEAT_ZONE
@ VIEWER_PATH_ELEM_TYPE_SIMULATION_ZONE
@ VIEWER_PATH_ELEM_TYPE_GROUP_NODE
@ VIEWER_PATH_ELEM_TYPE_VIEWER_NODE
@ VIEWER_PATH_ELEM_TYPE_EVALUATE_CLOSURE
@ VIEWER_PATH_ELEM_TYPE_FOREACH_GEOMETRY_ELEMENT_ZONE
uint64_t BKE_viewer_path_elem_hash(const ViewerPathElem &elem)
void BLO_read_struct_list_with_type_size(BlendDataReader *reader, size_t base_size, ListBase *list, FunctionRef< size_t(const void *data)> type_size_fn)
Definition readfile.cc:6591
ViewerPathElem * BKE_viewer_path_elem_copy(const ViewerPathElem *src)
static size_t viewer_path_elem_struct_size(const ViewerPathElemType type)
void BKE_viewer_path_blend_write(BlendWriter *writer, const ViewerPath *viewer_path)
bool BKE_viewer_path_equal(const ViewerPath *a, const ViewerPath *b, ViewerPathEqualFlag flag=ViewerPathEqualFlag(0))
RepeatZoneViewerPathElem * BKE_viewer_path_elem_new_repeat_zone()
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
static T * make_elem(const ViewerPathElemType type)
@ ID_REMAP_APPLY_DEFAULT
@ IDWALK_CB_DIRECT_WEAK_LINK
void BKE_viewer_path_elem_free(ViewerPathElem *elem)
void BKE_viewer_path_clear(ViewerPath *viewer_path)
EvaluateClosureNodeViewerPathElem * BKE_viewer_path_elem_new_evaluate_closure()
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
ViewerPathElem * BKE_viewer_path_elem_new(ViewerPathElemType type)
#define hash
Definition noise_c.cc:156
void write_string(const char *data)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
unsigned int session_uid
Definition DNA_ID.h:463
struct ViewerPathElem * next
ListBaseT< ViewerPathElem > path