Blender V4.5
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
5#include "BKE_lib_query.hh"
6#include "BKE_lib_remap.hh"
7#include "BKE_viewer_path.hh"
8
9#include "BLI_index_range.hh"
10#include "BLI_listbase.h"
11#include "BLI_string.h"
12#include "BLI_string_ref.hh"
13
14#include "MEM_guardedalloc.h"
15
16#include "BLO_read_write.hh"
17
20
22{
23 BLI_listbase_clear(&viewer_path->path);
24}
25
27{
28 LISTBASE_FOREACH_MUTABLE (ViewerPathElem *, elem, &viewer_path->path) {
30 }
31 BLI_listbase_clear(&viewer_path->path);
32}
33
35{
37 LISTBASE_FOREACH (const ViewerPathElem *, src_elem, &src->path) {
38 ViewerPathElem *new_elem = BKE_viewer_path_elem_copy(src_elem);
39 BLI_addtail(&dst->path, new_elem);
40 }
41}
42
44 const ViewerPath *b,
46{
47 const ViewerPathElem *elem_a = static_cast<const ViewerPathElem *>(a->path.first);
48 const ViewerPathElem *elem_b = static_cast<const ViewerPathElem *>(b->path.first);
49
50 while (elem_a != nullptr && elem_b != nullptr) {
51 if (!BKE_viewer_path_elem_equal(elem_a, elem_b, flag)) {
52 return false;
53 }
54 elem_a = elem_a->next;
55 elem_b = elem_b->next;
56 }
57 if (elem_a == nullptr && elem_b == nullptr) {
58 return true;
59 }
60 return false;
61}
62
64{
65 uint64_t hash = 0;
66 LISTBASE_FOREACH (ViewerPathElem *, elem, &viewer_path.path) {
67 const uint64_t elem_hash = BKE_viewer_path_elem_hash(*elem);
69 }
70 return hash;
71}
72
73void BKE_viewer_path_blend_write(BlendWriter *writer, const ViewerPath *viewer_path)
74{
75 LISTBASE_FOREACH (ViewerPathElem *, elem, &viewer_path->path) {
76 switch (ViewerPathElemType(elem->type)) {
78 const auto *typed_elem = reinterpret_cast<IDViewerPathElem *>(elem);
79 BLO_write_struct(writer, IDViewerPathElem, typed_elem);
80 break;
81 }
83 const auto *typed_elem = reinterpret_cast<ModifierViewerPathElem *>(elem);
84 BLO_write_struct(writer, ModifierViewerPathElem, typed_elem);
85 break;
86 }
88 const auto *typed_elem = reinterpret_cast<GroupNodeViewerPathElem *>(elem);
89 BLO_write_struct(writer, GroupNodeViewerPathElem, typed_elem);
90 break;
91 }
93 const auto *typed_elem = reinterpret_cast<SimulationZoneViewerPathElem *>(elem);
95 break;
96 }
98 const auto *typed_elem = reinterpret_cast<ViewerNodeViewerPathElem *>(elem);
99 BLO_write_struct(writer, ViewerNodeViewerPathElem, typed_elem);
100 break;
101 }
103 const auto *typed_elem = reinterpret_cast<RepeatZoneViewerPathElem *>(elem);
104 BLO_write_struct(writer, RepeatZoneViewerPathElem, typed_elem);
105 break;
106 }
108 const auto *typed_elem = reinterpret_cast<ForeachGeometryElementZoneViewerPathElem *>(
109 elem);
111 break;
112 }
114 const auto *typed_elem = reinterpret_cast<EvaluateClosureNodeViewerPathElem *>(elem);
116 break;
117 }
118 }
119 BLO_write_string(writer, elem->ui_name);
120 }
121}
122
145
147{
149 reader, &viewer_path->path, [](const ViewerPathElem &elem) {
150 return viewer_path_elem_struct_size(ViewerPathElemType(elem.type));
151 });
152 LISTBASE_FOREACH (ViewerPathElem *, elem, &viewer_path->path) {
153 BLO_read_string(reader, &elem->ui_name);
154 switch (ViewerPathElemType(elem->type)) {
163 break;
164 }
165 }
166 }
167}
168
170{
171 LISTBASE_FOREACH (ViewerPathElem *, elem, &viewer_path->path) {
172 switch (ViewerPathElemType(elem->type)) {
174 auto *typed_elem = reinterpret_cast<IDViewerPathElem *>(elem);
176 break;
177 }
179 auto *typed_elem = reinterpret_cast<EvaluateClosureNodeViewerPathElem *>(elem);
181 data, typed_elem->source_node_tree, IDWALK_CB_DIRECT_WEAK_LINK);
182 break;
183 }
190 break;
191 }
192 }
193 }
194}
195
197 const blender::bke::id::IDRemapper &mappings)
198{
199 LISTBASE_FOREACH (ViewerPathElem *, elem, &viewer_path->path) {
200 switch (ViewerPathElemType(elem->type)) {
202 auto *typed_elem = reinterpret_cast<IDViewerPathElem *>(elem);
203 mappings.apply(&typed_elem->id, ID_REMAP_APPLY_DEFAULT);
204 break;
205 }
213 break;
214 }
215 }
216 }
217}
218
219template<typename T> static T *make_elem(const ViewerPathElemType type)
220{
221 T *elem = MEM_callocN<T>(__func__);
222 elem->base.type = type;
223 return elem;
224}
225
227{
228 switch (type) {
230 return &make_elem<IDViewerPathElem>(type)->base;
231 }
233 return &make_elem<ModifierViewerPathElem>(type)->base;
234 }
236 return &make_elem<GroupNodeViewerPathElem>(type)->base;
237 }
239 return &make_elem<SimulationZoneViewerPathElem>(type)->base;
240 }
242 return &make_elem<ViewerNodeViewerPathElem>(type)->base;
243 }
245 return &make_elem<RepeatZoneViewerPathElem>(type)->base;
246 }
249 }
252 }
253 }
255 return nullptr;
256}
257
262
268
274
280
286
292
298
304
306{
308 if (src->ui_name) {
309 dst->ui_name = BLI_strdup(src->ui_name);
310 }
311 switch (ViewerPathElemType(src->type)) {
313 const auto *old_elem = reinterpret_cast<const IDViewerPathElem *>(src);
314 auto *new_elem = reinterpret_cast<IDViewerPathElem *>(dst);
315 new_elem->id = old_elem->id;
316 break;
317 }
319 const auto *old_elem = reinterpret_cast<const ModifierViewerPathElem *>(src);
320 auto *new_elem = reinterpret_cast<ModifierViewerPathElem *>(dst);
321 new_elem->modifier_uid = old_elem->modifier_uid;
322 break;
323 }
325 const auto *old_elem = reinterpret_cast<const GroupNodeViewerPathElem *>(src);
326 auto *new_elem = reinterpret_cast<GroupNodeViewerPathElem *>(dst);
327 new_elem->node_id = old_elem->node_id;
328 break;
329 }
331 const auto *old_elem = reinterpret_cast<const SimulationZoneViewerPathElem *>(src);
332 auto *new_elem = reinterpret_cast<SimulationZoneViewerPathElem *>(dst);
333 new_elem->sim_output_node_id = old_elem->sim_output_node_id;
334 break;
335 }
337 const auto *old_elem = reinterpret_cast<const ViewerNodeViewerPathElem *>(src);
338 auto *new_elem = reinterpret_cast<ViewerNodeViewerPathElem *>(dst);
339 new_elem->node_id = old_elem->node_id;
340 break;
341 }
343 const auto *old_elem = reinterpret_cast<const RepeatZoneViewerPathElem *>(src);
344 auto *new_elem = reinterpret_cast<RepeatZoneViewerPathElem *>(dst);
345 new_elem->repeat_output_node_id = old_elem->repeat_output_node_id;
346 new_elem->iteration = old_elem->iteration;
347 break;
348 }
350 const auto *old_elem = reinterpret_cast<const ForeachGeometryElementZoneViewerPathElem *>(
351 src);
352 auto *new_elem = reinterpret_cast<ForeachGeometryElementZoneViewerPathElem *>(dst);
353 new_elem->zone_output_node_id = old_elem->zone_output_node_id;
354 new_elem->index = old_elem->index;
355 break;
356 }
358 const auto *old_elem = reinterpret_cast<const EvaluateClosureNodeViewerPathElem *>(src);
359 auto *new_elem = reinterpret_cast<EvaluateClosureNodeViewerPathElem *>(dst);
360 new_elem->source_output_node_id = old_elem->source_output_node_id;
361 new_elem->evaluate_node_id = old_elem->evaluate_node_id;
362 new_elem->source_node_tree = old_elem->source_node_tree;
363 break;
364 }
365 }
366 return dst;
367}
368
370 const ViewerPathElem *b,
372{
373 if (a->type != b->type) {
374 return false;
375 }
377 if (StringRef(a->ui_name) != StringRef(b->ui_name)) {
378 return false;
379 }
380 }
381 switch (ViewerPathElemType(a->type)) {
383 const auto *a_elem = reinterpret_cast<const IDViewerPathElem *>(a);
384 const auto *b_elem = reinterpret_cast<const IDViewerPathElem *>(b);
385 return a_elem->id == b_elem->id;
386 }
388 const auto *a_elem = reinterpret_cast<const ModifierViewerPathElem *>(a);
389 const auto *b_elem = reinterpret_cast<const ModifierViewerPathElem *>(b);
390 return a_elem->modifier_uid == b_elem->modifier_uid;
391 }
393 const auto *a_elem = reinterpret_cast<const GroupNodeViewerPathElem *>(a);
394 const auto *b_elem = reinterpret_cast<const GroupNodeViewerPathElem *>(b);
395 return a_elem->node_id == b_elem->node_id;
396 }
398 const auto *a_elem = reinterpret_cast<const SimulationZoneViewerPathElem *>(a);
399 const auto *b_elem = reinterpret_cast<const SimulationZoneViewerPathElem *>(b);
400 return a_elem->sim_output_node_id == b_elem->sim_output_node_id;
401 }
403 const auto *a_elem = reinterpret_cast<const ViewerNodeViewerPathElem *>(a);
404 const auto *b_elem = reinterpret_cast<const ViewerNodeViewerPathElem *>(b);
405 return a_elem->node_id == b_elem->node_id;
406 }
408 const auto *a_elem = reinterpret_cast<const RepeatZoneViewerPathElem *>(a);
409 const auto *b_elem = reinterpret_cast<const RepeatZoneViewerPathElem *>(b);
410 return a_elem->repeat_output_node_id == b_elem->repeat_output_node_id &&
412 a_elem->iteration == b_elem->iteration);
413 }
415 const auto *a_elem = reinterpret_cast<const ForeachGeometryElementZoneViewerPathElem *>(a);
416 const auto *b_elem = reinterpret_cast<const ForeachGeometryElementZoneViewerPathElem *>(b);
417 return a_elem->zone_output_node_id == b_elem->zone_output_node_id &&
419 a_elem->index == b_elem->index);
420 }
422 const auto *a_elem = reinterpret_cast<const EvaluateClosureNodeViewerPathElem *>(a);
423 const auto *b_elem = reinterpret_cast<const EvaluateClosureNodeViewerPathElem *>(b);
424 return a_elem->source_output_node_id == b_elem->source_output_node_id &&
425 a_elem->evaluate_node_id == b_elem->evaluate_node_id &&
426 a_elem->source_node_tree == b_elem->source_node_tree;
427 }
428 }
429 return false;
430}
431
433{
435 switch (ViewerPathElemType(elem.type)) {
437 const auto &typed_elem = reinterpret_cast<const IDViewerPathElem &>(elem);
438 return get_default_hash(elem.type, typed_elem.id ? typed_elem.id->session_uid : 0);
439 }
441 const auto &typed_elem = reinterpret_cast<const ModifierViewerPathElem &>(elem);
442 return get_default_hash(elem.type, typed_elem.modifier_uid);
443 }
445 const auto &typed_elem = reinterpret_cast<const GroupNodeViewerPathElem &>(elem);
446 return get_default_hash(elem.type, typed_elem.node_id);
447 }
449 const auto &typed_elem = reinterpret_cast<const SimulationZoneViewerPathElem &>(elem);
450 return get_default_hash(elem.type, typed_elem.sim_output_node_id);
451 }
453 const auto &typed_elem = reinterpret_cast<const ViewerNodeViewerPathElem &>(elem);
454 return get_default_hash(elem.type, typed_elem.node_id);
455 }
457 const auto &typed_elem = reinterpret_cast<const RepeatZoneViewerPathElem &>(elem);
458 return get_default_hash(elem.type, typed_elem.repeat_output_node_id, typed_elem.iteration);
459 }
461 const auto &typed_elem = reinterpret_cast<const ForeachGeometryElementZoneViewerPathElem &>(
462 elem);
463 return get_default_hash(elem.type, typed_elem.zone_output_node_id, typed_elem.index);
464 }
466 const auto &typed_elem = reinterpret_cast<const EvaluateClosureNodeViewerPathElem &>(elem);
467 return get_default_hash(
468 elem.type,
469 typed_elem.evaluate_node_id,
470 typed_elem.source_output_node_id,
471 typed_elem.source_node_tree ?
472 reinterpret_cast<const ID *>(typed_elem.source_node_tree)->session_uid :
473 0);
474 }
475 }
476 return 0;
477}
478
@ IDWALK_CB_DIRECT_WEAK_LINK
#define BKE_LIB_FOREACHID_PROCESS_ID(data_, id_, cb_flag_)
@ ID_REMAP_APPLY_DEFAULT
ViewerPathEqualFlag
@ VIEWER_PATH_EQUAL_FLAG_IGNORE_ITERATION
@ VIEWER_PATH_EQUAL_FLAG_CONSIDER_UI_NAME
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define LISTBASE_FOREACH(type, var, list)
BLI_INLINE void BLI_listbase_clear(ListBase *lb)
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
std::string data
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
Definition string.cc:41
#define BLO_write_struct(writer, struct_name, data_ptr)
void BLO_read_struct_list_with_type_size(BlendDataReader *reader, size_t base_size, ListBase *list, blender::FunctionRef< size_t(const void *data)> type_size_fn)
Definition readfile.cc:5647
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:5783
void BLO_write_string(BlendWriter *writer, const char *data_ptr)
@ VIEWER_PATH_ELEM_TYPE_GROUP_NODE
@ VIEWER_PATH_ELEM_TYPE_SIMULATION_ZONE
@ VIEWER_PATH_ELEM_TYPE_FOREACH_GEOMETRY_ELEMENT_ZONE
@ VIEWER_PATH_ELEM_TYPE_VIEWER_NODE
@ VIEWER_PATH_ELEM_TYPE_REPEAT_ZONE
@ VIEWER_PATH_ELEM_TYPE_MODIFIER
@ VIEWER_PATH_ELEM_TYPE_EVALUATE_CLOSURE
@ VIEWER_PATH_ELEM_TYPE_ID
struct ViewerNodeViewerPathElem ViewerNodeViewerPathElem
struct GroupNodeViewerPathElem GroupNodeViewerPathElem
struct IDViewerPathElem IDViewerPathElem
struct ViewerPathElem ViewerPathElem
struct SimulationZoneViewerPathElem SimulationZoneViewerPathElem
struct ForeachGeometryElementZoneViewerPathElem ForeachGeometryElementZoneViewerPathElem
struct EvaluateClosureNodeViewerPathElem EvaluateClosureNodeViewerPathElem
struct ModifierViewerPathElem ModifierViewerPathElem
struct RepeatZoneViewerPathElem RepeatZoneViewerPathElem
Read Guarded memory(de)allocation.
IDRemapperApplyResult apply(ID **r_id_ptr, IDRemapperApplyOptions options, ID *id_self=nullptr) const
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:233
#define hash
Definition noise_c.cc:154
unsigned __int64 uint64_t
Definition stdint.h:90
Definition DNA_ID.h:404
unsigned int session_uid
Definition DNA_ID.h:444
void * first
struct ViewerPathElem * next
void BKE_viewer_path_copy(ViewerPath *dst, const ViewerPath *src)
static size_t viewer_path_elem_struct_size(const ViewerPathElemType type)
void BKE_viewer_path_id_remap(ViewerPath *viewer_path, const blender::bke::id::IDRemapper &mappings)
uint64_t BKE_viewer_path_elem_hash(const ViewerPathElem &elem)
EvaluateClosureNodeViewerPathElem * BKE_viewer_path_elem_new_evaluate_closure()
ViewerPathElem * BKE_viewer_path_elem_copy(const ViewerPathElem *src)
ViewerNodeViewerPathElem * BKE_viewer_path_elem_new_viewer_node()
ViewerPathElem * BKE_viewer_path_elem_new(const ViewerPathElemType type)
void BKE_viewer_path_elem_free(ViewerPathElem *elem)
void BKE_viewer_path_foreach_id(LibraryForeachIDData *data, ViewerPath *viewer_path)
void BKE_viewer_path_clear(ViewerPath *viewer_path)
void BKE_viewer_path_init(ViewerPath *viewer_path)
SimulationZoneViewerPathElem * BKE_viewer_path_elem_new_simulation_zone()
ForeachGeometryElementZoneViewerPathElem * BKE_viewer_path_elem_new_foreach_geometry_element_zone()
GroupNodeViewerPathElem * BKE_viewer_path_elem_new_group_node()
static T * make_elem(const ViewerPathElemType type)
IDViewerPathElem * BKE_viewer_path_elem_new_id()
bool BKE_viewer_path_elem_equal(const ViewerPathElem *a, const ViewerPathElem *b, const ViewerPathEqualFlag flag)
ModifierViewerPathElem * BKE_viewer_path_elem_new_modifier()
void BKE_viewer_path_blend_read_data(BlendDataReader *reader, ViewerPath *viewer_path)
uint64_t BKE_viewer_path_hash(const ViewerPath &viewer_path)
bool BKE_viewer_path_equal(const ViewerPath *a, const ViewerPath *b, const ViewerPathEqualFlag flag)
RepeatZoneViewerPathElem * BKE_viewer_path_elem_new_repeat_zone()
void BKE_viewer_path_blend_write(BlendWriter *writer, const ViewerPath *viewer_path)