Blender V5.0
lib_id_remapper.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "DNA_ID.h"
6
7#include "BKE_idtype.hh"
8#include "BKE_lib_id.hh"
9#include "BKE_lib_remap.hh"
10
11namespace blender::bke::id {
12
13void IDRemapper::add(ID *old_id, ID *new_id)
14{
15 BLI_assert(old_id != nullptr);
16 BLI_assert(new_id == nullptr || this->allow_idtype_mismatch ||
17 (GS(old_id->name) == GS(new_id->name)));
19
20 mappings_.add(old_id, new_id);
21 source_types_ |= BKE_idtype_idcode_to_idfilter(GS(old_id->name));
22}
23
24void IDRemapper::add_overwrite(ID *old_id, ID *new_id)
25{
26 BLI_assert(old_id != nullptr);
27 BLI_assert(new_id == nullptr || this->allow_idtype_mismatch ||
28 (GS(old_id->name) == GS(new_id->name)));
30
31 mappings_.add_overwrite(old_id, new_id);
32 source_types_ |= BKE_idtype_idcode_to_idfilter(GS(old_id->name));
33}
34
37 const ID *id_self) const
38{
39 if (!mappings_.contains(id)) {
41 }
42 const ID *new_id = mappings_.lookup(id);
43 if ((options & ID_REMAP_APPLY_UNMAP_WHEN_REMAPPING_TO_SELF) != 0 && id_self == new_id) {
44 new_id = nullptr;
45 }
46 if (new_id == nullptr) {
48 }
49
51}
52
55 ID *id_self) const
56{
57 BLI_assert(r_id_ptr != nullptr);
59 ((options & ID_REMAP_APPLY_UNMAP_WHEN_REMAPPING_TO_SELF) == 0 || id_self != nullptr),
60 "ID_REMAP_APPLY_WHEN_REMAPPING_TO_SELF requires a non-null `id_self` parameter.");
61
62 if (*r_id_ptr == nullptr) {
64 }
65
66 ID *const *new_id = mappings_.lookup_ptr(*r_id_ptr);
67 if (new_id == nullptr) {
69 }
70
72 id_us_min(*r_id_ptr);
73 }
74
75 *r_id_ptr = *new_id;
76 if (options & ID_REMAP_APPLY_UNMAP_WHEN_REMAPPING_TO_SELF && *r_id_ptr == id_self) {
77 *r_id_ptr = nullptr;
78 }
79 if (*r_id_ptr == nullptr) {
81 }
82
84 /* Do not handle ID_TAG_INDIRECT/ID_TAG_EXTERN here. */
85 id_us_plus_no_lib(*r_id_ptr);
86 }
87
89 id_us_ensure_real(*r_id_ptr);
90 }
92}
93
95{
96 switch (result) {
98 return "not_mappable";
100 return "unavailable";
102 return "unassigned";
104 return "remapped";
105 }
107 return "";
108}
109
111{
112 this->iter([](ID *old_id, ID *new_id) {
113 if (old_id != nullptr && new_id != nullptr) {
114 printf("Remap %s(%p) to %s(%p)\n", old_id->name, old_id, new_id->name, new_id);
115 }
116 if (old_id != nullptr && new_id == nullptr) {
117 printf("Unassign %s(%p)\n", old_id->name, old_id);
118 }
119 });
120}
121
122} // namespace blender::bke::id
uint64_t BKE_idtype_idcode_to_idfilter(short idcode)
Definition idtype.cc:363
void id_us_ensure_real(ID *id)
Definition lib_id.cc:313
void id_us_plus_no_lib(ID *id)
Definition lib_id.cc:342
void id_us_min(ID *id)
Definition lib_id.cc:366
IDRemapperApplyResult
@ ID_REMAP_RESULT_SOURCE_REMAPPED
@ ID_REMAP_RESULT_SOURCE_UNASSIGNED
@ ID_REMAP_RESULT_SOURCE_NOT_MAPPABLE
@ ID_REMAP_RESULT_SOURCE_UNAVAILABLE
IDRemapperApplyOptions
@ ID_REMAP_APPLY_UPDATE_REFCOUNT
@ ID_REMAP_APPLY_UNMAP_WHEN_REMAPPING_TO_SELF
@ ID_REMAP_APPLY_ENSURE_REAL
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
ID and Library types, which are fundamental for SDNA.
IDRemapperApplyResult get_mapping_result(ID *id, IDRemapperApplyOptions options, const ID *id_self) const
IDRemapperApplyResult apply(ID **r_id_ptr, IDRemapperApplyOptions options, ID *id_self=nullptr) const
static StringRefNull result_to_string(const IDRemapperApplyResult result)
void iter(FunctionRef< void(ID *old_id, ID *new_id)> func) const
void add(ID *old_id, ID *new_id)
void add_overwrite(ID *old_id, ID *new_id)
CCL_NAMESPACE_BEGIN struct Options options
#define GS(x)
#define printf(...)
Definition DNA_ID.h:414
char name[258]
Definition DNA_ID.h:432