2022-02-10 23:07:11 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-02-07 16:29:02 +01:00
|
|
|
#pragma once
|
2016-06-07 08:07:13 +02:00
|
|
|
|
2019-02-17 22:08:12 +01:00
|
|
|
/** \file
|
|
|
|
* \ingroup bke
|
2020-02-10 15:05:54 +01:00
|
|
|
*
|
|
|
|
* API to generate and use a mapping from [ID type & name] to [id pointer], within a given Main
|
|
|
|
* data-base.
|
|
|
|
*
|
|
|
|
* \note `BKE_main` files are for operations over the Main database itself, or generating extra
|
|
|
|
* temp data to help working with it. Those should typically not affect the data-blocks themselves.
|
|
|
|
*
|
|
|
|
* \section Function Names
|
|
|
|
*
|
|
|
|
* - `BKE_main_idmap_` Should be used for functions in that file.
|
2016-06-07 08:07:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "BLI_compiler_attrs.h"
|
|
|
|
|
2020-03-02 15:07:49 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-06-07 08:07:13 +02:00
|
|
|
struct ID;
|
|
|
|
struct IDNameLib_Map;
|
2019-01-28 11:08:24 +01:00
|
|
|
struct Main;
|
2016-06-07 08:07:13 +02:00
|
|
|
|
2020-03-05 16:17:14 +01:00
|
|
|
enum {
|
|
|
|
MAIN_IDMAP_TYPE_NAME = 1 << 0,
|
|
|
|
MAIN_IDMAP_TYPE_UUID = 1 << 1,
|
|
|
|
};
|
|
|
|
|
2021-12-07 07:19:15 +01:00
|
|
|
/**
|
|
|
|
* Generate mapping from ID type/name to ID pointer for given \a bmain.
|
|
|
|
*
|
|
|
|
* \note When used during undo/redo, there is no guaranty that ID pointers from UI area are not
|
|
|
|
* pointing to freed memory (when some IDs have been deleted). To avoid crashes in those cases, one
|
|
|
|
* can provide the 'old' (aka current) Main database as reference. #BKE_main_idmap_lookup_id will
|
|
|
|
* then check that given ID does exist in \a old_bmain before trying to use it.
|
|
|
|
*
|
|
|
|
* \param create_valid_ids_set: If \a true, generate a reference to prevent freed memory accesses.
|
|
|
|
* \param old_bmain: If not NULL, its IDs will be added the valid references set.
|
|
|
|
*/
|
2016-06-07 08:07:13 +02:00
|
|
|
struct IDNameLib_Map *BKE_main_idmap_create(struct Main *bmain,
|
2022-01-07 01:38:08 +01:00
|
|
|
bool create_valid_ids_set,
|
2020-03-05 16:17:14 +01:00
|
|
|
struct Main *old_bmain,
|
2022-01-07 01:38:08 +01:00
|
|
|
int idmap_types) ATTR_WARN_UNUSED_RESULT
|
Fix (unreported) crash when undoing after ID deletion.
Yes, we do can undo an ID deletion now.
However, this requires extra care in UI 'remapping' to new IDs step
(when undoing, we do not fully reload the UI from saved .blend).
Otherwise, new UI (i.e. one from saved .blend file) might reference
IDs that where freed in old bmain (the one before the undo), we cannot
use those to get ID name then, that would be a nasty use-after-free!
To prevent this, we generate a GSet of all valid ID pointers at that
time (i.e. those found in both old and new Main's), and ensure any ID
we try to remap by its name is in that GSet. Otherwise, there is no
possible remapping, just return NULL.
2019-02-08 18:45:57 +01:00
|
|
|
ATTR_NONNULL(1);
|
2020-09-04 20:59:13 +02:00
|
|
|
void BKE_main_idmap_destroy(struct IDNameLib_Map *id_map) ATTR_NONNULL();
|
2021-06-30 15:19:35 +02:00
|
|
|
|
|
|
|
void BKE_main_idmap_insert_id(struct IDNameLib_Map *id_map, struct ID *id) ATTR_NONNULL();
|
|
|
|
void BKE_main_idmap_remove_id(struct IDNameLib_Map *id_map, struct ID *id) ATTR_NONNULL();
|
|
|
|
|
2020-09-04 20:59:13 +02:00
|
|
|
struct Main *BKE_main_idmap_main_get(struct IDNameLib_Map *id_map) ATTR_WARN_UNUSED_RESULT
|
2016-06-07 08:07:13 +02:00
|
|
|
ATTR_NONNULL();
|
2021-06-30 15:19:35 +02:00
|
|
|
|
2020-09-04 20:59:13 +02:00
|
|
|
struct ID *BKE_main_idmap_lookup_name(struct IDNameLib_Map *id_map,
|
2020-03-05 16:17:14 +01:00
|
|
|
short id_type,
|
|
|
|
const char *name,
|
|
|
|
const struct Library *lib) ATTR_WARN_UNUSED_RESULT
|
2016-06-07 08:07:13 +02:00
|
|
|
ATTR_NONNULL(1, 3);
|
2020-09-04 20:59:13 +02:00
|
|
|
struct ID *BKE_main_idmap_lookup_id(struct IDNameLib_Map *id_map,
|
2016-06-07 08:07:13 +02:00
|
|
|
const struct ID *id) ATTR_WARN_UNUSED_RESULT
|
|
|
|
ATTR_NONNULL(1, 2);
|
|
|
|
|
2020-09-04 20:59:13 +02:00
|
|
|
struct ID *BKE_main_idmap_lookup_uuid(struct IDNameLib_Map *id_map,
|
2022-01-07 01:38:08 +01:00
|
|
|
uint session_uuid) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
|
2020-03-05 16:17:14 +01:00
|
|
|
|
2020-03-02 15:07:49 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|