Blender V4.3
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
11#include "MEM_guardedalloc.h"
12
13namespace blender::bke::id {
14
15void IDRemapper::add(ID *old_id, ID *new_id)
16{
17 BLI_assert(old_id != nullptr);
18 BLI_assert(new_id == nullptr || this->allow_idtype_mismatch ||
19 (GS(old_id->name) == GS(new_id->name)));
21
22 mappings_.add(old_id, new_id);
23 source_types_ |= BKE_idtype_idcode_to_idfilter(GS(old_id->name));
24}
25
26void IDRemapper::add_overwrite(ID *old_id, ID *new_id)
27{
28 BLI_assert(old_id != nullptr);
29 BLI_assert(new_id == nullptr || this->allow_idtype_mismatch ||
30 (GS(old_id->name) == GS(new_id->name)));
32
33 mappings_.add_overwrite(old_id, new_id);
34 source_types_ |= BKE_idtype_idcode_to_idfilter(GS(old_id->name));
35}
36
39 const ID *id_self) const
40{
41 if (!mappings_.contains(id)) {
43 }
44 const ID *new_id = mappings_.lookup(id);
45 if ((options & ID_REMAP_APPLY_UNMAP_WHEN_REMAPPING_TO_SELF) != 0 && id_self == new_id) {
46 new_id = nullptr;
47 }
48 if (new_id == nullptr) {
50 }
51
53}
54
57 ID *id_self) const
58{
59 BLI_assert(r_id_ptr != nullptr);
61 ((options & ID_REMAP_APPLY_UNMAP_WHEN_REMAPPING_TO_SELF) == 0 || id_self != nullptr),
62 "ID_REMAP_APPLY_WHEN_REMAPPING_TO_SELF requires a non-null `id_self` parameter.");
63
64 if (*r_id_ptr == nullptr) {
66 }
67
68 ID *const *new_id = mappings_.lookup_ptr(*r_id_ptr);
69 if (new_id == nullptr) {
71 }
72
74 id_us_min(*r_id_ptr);
75 }
76
77 *r_id_ptr = *new_id;
78 if (options & ID_REMAP_APPLY_UNMAP_WHEN_REMAPPING_TO_SELF && *r_id_ptr == id_self) {
79 *r_id_ptr = nullptr;
80 }
81 if (*r_id_ptr == nullptr) {
83 }
84
86 /* Do not handle ID_TAG_INDIRECT/ID_TAG_EXTERN here. */
87 id_us_plus_no_lib(*r_id_ptr);
88 }
89
91 id_us_ensure_real(*r_id_ptr);
92 }
94}
95
97{
98 switch (result) {
100 return "not_mappable";
102 return "unavailable";
104 return "unassigned";
106 return "remapped";
107 }
109 return "";
110}
111
113{
114 this->iter([](ID *old_id, ID *new_id) {
115 if (old_id != nullptr && new_id != nullptr) {
116 printf("Remap %s(%p) to %s(%p)\n", old_id->name, old_id, new_id->name, new_id);
117 }
118 if (old_id != nullptr && new_id == nullptr) {
119 printf("Unassign %s(%p)\n", old_id->name, old_id);
120 }
121 });
122}
123
124} // namespace blender::bke::id
uint64_t BKE_idtype_idcode_to_idfilter(short idcode)
Definition idtype.cc:369
void id_us_ensure_real(ID *id)
Definition lib_id.cc:306
void id_us_plus_no_lib(ID *id)
Definition lib_id.cc:335
void id_us_min(ID *id)
Definition lib_id.cc:359
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:97
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:57
ID and Library types, which are fundamental for SDNA.
Read Guarded memory(de)allocation.
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:484
bool add_overwrite(const Key &key, const Value &value)
Definition BLI_map.hh:301
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:271
const Value & lookup(const Key &key) const
Definition BLI_map.hh:506
bool contains(const Key &key) const
Definition BLI_map.hh:329
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
void iter(FunctionRef< void(ID *old_id, ID *new_id)> func) const
static const StringRefNull result_to_string(const IDRemapperApplyResult result)
void add(ID *old_id, ID *new_id)
void add_overwrite(ID *old_id, ID *new_id)
#define printf
CCL_NAMESPACE_BEGIN struct Options options
#define GS(x)
Definition iris.cc:202
Definition DNA_ID.h:413
char name[66]
Definition DNA_ID.h:425