Blender V4.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
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
63void BKE_viewer_path_blend_write(BlendWriter *writer, const ViewerPath *viewer_path)
64{
65 LISTBASE_FOREACH (ViewerPathElem *, elem, &viewer_path->path) {
66 switch (ViewerPathElemType(elem->type)) {
68 const auto *typed_elem = reinterpret_cast<IDViewerPathElem *>(elem);
69 BLO_write_struct(writer, IDViewerPathElem, typed_elem);
70 break;
71 }
73 const auto *typed_elem = reinterpret_cast<ModifierViewerPathElem *>(elem);
74 BLO_write_struct(writer, ModifierViewerPathElem, typed_elem);
75 BLO_write_string(writer, typed_elem->modifier_name);
76 break;
77 }
79 const auto *typed_elem = reinterpret_cast<GroupNodeViewerPathElem *>(elem);
80 BLO_write_struct(writer, GroupNodeViewerPathElem, typed_elem);
81 break;
82 }
84 const auto *typed_elem = reinterpret_cast<SimulationZoneViewerPathElem *>(elem);
86 break;
87 }
89 const auto *typed_elem = reinterpret_cast<ViewerNodeViewerPathElem *>(elem);
90 BLO_write_struct(writer, ViewerNodeViewerPathElem, typed_elem);
91 break;
92 }
94 const auto *typed_elem = reinterpret_cast<RepeatZoneViewerPathElem *>(elem);
95 BLO_write_struct(writer, RepeatZoneViewerPathElem, typed_elem);
96 break;
97 }
99 const auto *typed_elem = reinterpret_cast<ForeachGeometryElementZoneViewerPathElem *>(
100 elem);
102 break;
103 }
104 }
105 BLO_write_string(writer, elem->ui_name);
106 }
107}
108
110{
111 BLO_read_struct_list(reader, ViewerPathElem, &viewer_path->path);
112 LISTBASE_FOREACH (ViewerPathElem *, elem, &viewer_path->path) {
113 BLO_read_string(reader, &elem->ui_name);
114 switch (ViewerPathElemType(elem->type)) {
121 break;
122 }
124 auto *typed_elem = reinterpret_cast<ModifierViewerPathElem *>(elem);
125 BLO_read_string(reader, &typed_elem->modifier_name);
126 break;
127 }
128 }
129 }
130}
131
133{
134 LISTBASE_FOREACH (ViewerPathElem *, elem, &viewer_path->path) {
135 switch (ViewerPathElemType(elem->type)) {
137 auto *typed_elem = reinterpret_cast<IDViewerPathElem *>(elem);
139 break;
140 }
147 break;
148 }
149 }
150 }
151}
152
154 const blender::bke::id::IDRemapper &mappings)
155{
156 LISTBASE_FOREACH (ViewerPathElem *, elem, &viewer_path->path) {
157 switch (ViewerPathElemType(elem->type)) {
159 auto *typed_elem = reinterpret_cast<IDViewerPathElem *>(elem);
160 mappings.apply(&typed_elem->id, ID_REMAP_APPLY_DEFAULT);
161 break;
162 }
169 break;
170 }
171 }
172 }
173}
174
175template<typename T> static T *make_elem(const ViewerPathElemType type)
176{
177 T *elem = MEM_cnew<T>(__func__);
178 elem->base.type = type;
179 return elem;
180}
181
183{
184 switch (type) {
186 return &make_elem<IDViewerPathElem>(type)->base;
187 }
189 return &make_elem<ModifierViewerPathElem>(type)->base;
190 }
192 return &make_elem<GroupNodeViewerPathElem>(type)->base;
193 }
195 return &make_elem<SimulationZoneViewerPathElem>(type)->base;
196 }
198 return &make_elem<ViewerNodeViewerPathElem>(type)->base;
199 }
201 return &make_elem<RepeatZoneViewerPathElem>(type)->base;
202 }
205 }
206 }
208 return nullptr;
209}
210
215
221
227
233
239
245
251
253{
255 if (src->ui_name) {
256 dst->ui_name = BLI_strdup(src->ui_name);
257 }
258 switch (ViewerPathElemType(src->type)) {
260 const auto *old_elem = reinterpret_cast<const IDViewerPathElem *>(src);
261 auto *new_elem = reinterpret_cast<IDViewerPathElem *>(dst);
262 new_elem->id = old_elem->id;
263 break;
264 }
266 const auto *old_elem = reinterpret_cast<const ModifierViewerPathElem *>(src);
267 auto *new_elem = reinterpret_cast<ModifierViewerPathElem *>(dst);
268 if (old_elem->modifier_name != nullptr) {
269 new_elem->modifier_name = BLI_strdup(old_elem->modifier_name);
270 }
271 break;
272 }
274 const auto *old_elem = reinterpret_cast<const GroupNodeViewerPathElem *>(src);
275 auto *new_elem = reinterpret_cast<GroupNodeViewerPathElem *>(dst);
276 new_elem->node_id = old_elem->node_id;
277 break;
278 }
280 const auto *old_elem = reinterpret_cast<const SimulationZoneViewerPathElem *>(src);
281 auto *new_elem = reinterpret_cast<SimulationZoneViewerPathElem *>(dst);
282 new_elem->sim_output_node_id = old_elem->sim_output_node_id;
283 break;
284 }
286 const auto *old_elem = reinterpret_cast<const ViewerNodeViewerPathElem *>(src);
287 auto *new_elem = reinterpret_cast<ViewerNodeViewerPathElem *>(dst);
288 new_elem->node_id = old_elem->node_id;
289 break;
290 }
292 const auto *old_elem = reinterpret_cast<const RepeatZoneViewerPathElem *>(src);
293 auto *new_elem = reinterpret_cast<RepeatZoneViewerPathElem *>(dst);
294 new_elem->repeat_output_node_id = old_elem->repeat_output_node_id;
295 new_elem->iteration = old_elem->iteration;
296 break;
297 }
299 const auto *old_elem = reinterpret_cast<const ForeachGeometryElementZoneViewerPathElem *>(
300 src);
301 auto *new_elem = reinterpret_cast<ForeachGeometryElementZoneViewerPathElem *>(dst);
302 new_elem->zone_output_node_id = old_elem->zone_output_node_id;
303 new_elem->index = old_elem->index;
304 break;
305 }
306 }
307 return dst;
308}
309
311 const ViewerPathElem *b,
313{
314 if (a->type != b->type) {
315 return false;
316 }
317 switch (ViewerPathElemType(a->type)) {
319 const auto *a_elem = reinterpret_cast<const IDViewerPathElem *>(a);
320 const auto *b_elem = reinterpret_cast<const IDViewerPathElem *>(b);
321 return a_elem->id == b_elem->id;
322 }
324 const auto *a_elem = reinterpret_cast<const ModifierViewerPathElem *>(a);
325 const auto *b_elem = reinterpret_cast<const ModifierViewerPathElem *>(b);
326 return StringRef(a_elem->modifier_name) == StringRef(b_elem->modifier_name);
327 }
329 const auto *a_elem = reinterpret_cast<const GroupNodeViewerPathElem *>(a);
330 const auto *b_elem = reinterpret_cast<const GroupNodeViewerPathElem *>(b);
331 return a_elem->node_id == b_elem->node_id;
332 }
334 const auto *a_elem = reinterpret_cast<const SimulationZoneViewerPathElem *>(a);
335 const auto *b_elem = reinterpret_cast<const SimulationZoneViewerPathElem *>(b);
336 return a_elem->sim_output_node_id == b_elem->sim_output_node_id;
337 }
339 const auto *a_elem = reinterpret_cast<const ViewerNodeViewerPathElem *>(a);
340 const auto *b_elem = reinterpret_cast<const ViewerNodeViewerPathElem *>(b);
341 return a_elem->node_id == b_elem->node_id;
342 }
344 const auto *a_elem = reinterpret_cast<const RepeatZoneViewerPathElem *>(a);
345 const auto *b_elem = reinterpret_cast<const RepeatZoneViewerPathElem *>(b);
346 return a_elem->repeat_output_node_id == b_elem->repeat_output_node_id &&
348 a_elem->iteration == b_elem->iteration);
349 }
351 const auto *a_elem = reinterpret_cast<const ForeachGeometryElementZoneViewerPathElem *>(a);
352 const auto *b_elem = reinterpret_cast<const ForeachGeometryElementZoneViewerPathElem *>(b);
353 return a_elem->zone_output_node_id == b_elem->zone_output_node_id &&
355 a_elem->index == b_elem->index);
356 }
357 }
358 return false;
359}
360
362{
363 switch (ViewerPathElemType(elem->type)) {
370 break;
371 }
373 auto *typed_elem = reinterpret_cast<ModifierViewerPathElem *>(elem);
374 MEM_SAFE_FREE(typed_elem->modifier_name);
375 break;
376 }
377 }
378 if (elem->ui_name) {
379 MEM_freeN(elem->ui_name);
380 }
381 MEM_freeN(elem);
382}
@ 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
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
#define LISTBASE_FOREACH(type, var, list)
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:110
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
Definition string.c:40
#define BLO_write_struct(writer, struct_name, data_ptr)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:4992
void BLO_write_string(BlendWriter *writer, const char *data_ptr)
#define BLO_read_struct_list(reader, struct_name, list)
@ 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_ID
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
IDRemapperApplyResult apply(ID **r_id_ptr, IDRemapperApplyOptions options, ID *id_self=nullptr) const
local_group_size(16, 16) .push_constant(Type b
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
struct ViewerPathElem * next
void BKE_viewer_path_copy(ViewerPath *dst, const ViewerPath *src)
void BKE_viewer_path_id_remap(ViewerPath *viewer_path, const blender::bke::id::IDRemapper &mappings)
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)
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)
uint8_t flag
Definition wm_window.cc:138