Blender V5.0
asset_weak_reference.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
8
9#include <memory>
10
11#include "BLI_listbase.h"
12#include "BLI_path_utils.hh"
13#include "BLI_string.h"
14
15#include "AS_asset_library.hh"
16
17#include "BKE_asset.hh"
18
19#include "BLO_read_write.hh"
20
21#include "DNA_asset_types.h"
22
23#include "MEM_guardedalloc.h"
24
25using namespace blender;
26
27/* #AssetWeakReference -------------------------------------------- */
28
29AssetWeakReference::AssetWeakReference()
30 : asset_library_type(0), asset_library_identifier(nullptr), relative_asset_identifier(nullptr)
31{
32}
33
34AssetWeakReference::AssetWeakReference(const AssetWeakReference &other)
35 : asset_library_type(other.asset_library_type),
36 asset_library_identifier(BLI_strdup_null(other.asset_library_identifier)),
37 relative_asset_identifier(BLI_strdup_null(other.relative_asset_identifier))
38{
39}
40
41AssetWeakReference::AssetWeakReference(AssetWeakReference &&other)
42 : asset_library_type(other.asset_library_type),
43 asset_library_identifier(other.asset_library_identifier),
44 relative_asset_identifier(other.relative_asset_identifier)
45{
46 other.asset_library_type = 0; /* Not a valid type. */
47 other.asset_library_identifier = nullptr;
48 other.relative_asset_identifier = nullptr;
49}
50
51AssetWeakReference::~AssetWeakReference()
52{
53 MEM_delete(asset_library_identifier);
54 MEM_delete(relative_asset_identifier);
55}
56
57AssetWeakReference &AssetWeakReference::operator=(const AssetWeakReference &other)
58{
59 if (this == &other) {
60 return *this;
61 }
62 std::destroy_at(this);
63 new (this) AssetWeakReference(other);
64 return *this;
65}
66
67AssetWeakReference &AssetWeakReference::operator=(AssetWeakReference &&other)
68{
69 if (this == &other) {
70 return *this;
71 }
72 std::destroy_at(this);
73 new (this) AssetWeakReference(std::move(other));
74 return *this;
75}
76
78{
79 if (a.asset_library_type != b.asset_library_type) {
80 return false;
81 }
82
83 const char *a_lib_idenfifier = a.asset_library_identifier ? a.asset_library_identifier : "";
84 const char *b_lib_idenfifier = b.asset_library_identifier ? b.asset_library_identifier : "";
85 if (BLI_path_cmp_normalized(a_lib_idenfifier, b_lib_idenfifier) != 0) {
86 return false;
87 }
88 const char *a_asset_idenfifier = a.relative_asset_identifier ? a.relative_asset_identifier : "";
89 const char *b_asset_idenfifier = b.relative_asset_identifier ? b.relative_asset_identifier : "";
90 if (BLI_path_cmp_normalized(a_asset_idenfifier, b_asset_idenfifier) != 0) {
91 return false;
92 }
93 return true;
94}
95
96AssetWeakReference AssetWeakReference::make_reference(const asset_system::AssetLibrary &library,
97 const StringRef library_relative_identifier)
98{
99 AssetWeakReference weak_ref{};
100
101 weak_ref.asset_library_type = library.library_type();
102 StringRefNull name = library.name();
103 if (!name.is_empty()) {
104 weak_ref.asset_library_identifier = BLI_strdupn(name.c_str(), name.size());
105 }
106
107 weak_ref.relative_asset_identifier = BLI_strdupn(library_relative_identifier.data(),
108 library_relative_identifier.size());
109
110 return weak_ref;
111}
112
114{
115 BLO_write_struct(writer, AssetWeakReference, weak_ref);
118}
119
125
127{
128 LISTBASE_FOREACH_MUTABLE (AssetCatalogPathLink *, catalog_path, &catalog_path_list) {
129 MEM_delete(catalog_path->path);
130 BLI_freelinkN(&catalog_path_list, catalog_path);
131 }
132 BLI_assert(BLI_listbase_is_empty(&catalog_path_list));
133}
134
136{
137 ListBase duplicated_list = {nullptr};
138
139 LISTBASE_FOREACH (AssetCatalogPathLink *, catalog_path, &catalog_path_list) {
141 copied_path->path = BLI_strdup(catalog_path->path);
142
143 BLI_addtail(&duplicated_list, copied_path);
144 }
145
146 return duplicated_list;
147}
148
150 const ListBase &catalog_path_list)
151{
152 LISTBASE_FOREACH (const AssetCatalogPathLink *, catalog_path, &catalog_path_list) {
153 BLO_write_struct(writer, AssetCatalogPathLink, catalog_path);
154 BLO_write_string(writer, catalog_path->path);
155 }
156}
157
159 ListBase &catalog_path_list)
160{
161 BLO_read_struct_list(reader, AssetCatalogPathLink, &catalog_path_list);
162 LISTBASE_FOREACH (AssetCatalogPathLink *, catalog_path, &catalog_path_list) {
163 BLO_read_string(reader, &catalog_path->path);
164 }
165}
166
167bool BKE_asset_catalog_path_list_has_path(const ListBase &catalog_path_list,
168 const char *catalog_path)
169{
170 return BLI_findstring_ptr(
171 &catalog_path_list, catalog_path, offsetof(AssetCatalogPathLink, path)) != nullptr;
172}
173
174void BKE_asset_catalog_path_list_add_path(ListBase &catalog_path_list, const char *catalog_path)
175{
177 new_path->path = BLI_strdup(catalog_path);
178 BLI_addtail(&catalog_path_list, new_path);
179}
#define BLI_assert(a)
Definition BLI_assert.h:46
#define LISTBASE_FOREACH(type, var, list)
BLI_INLINE bool BLI_listbase_is_empty(const ListBase *lb)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:270
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
void * BLI_findstring_ptr(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:651
int BLI_path_cmp_normalized(const char *p1, const char *p2) ATTR_NONNULL(1
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
Definition string.cc:41
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition string.cc:30
char * BLI_strdup_null(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_MALLOC
Definition string.cc:46
#define BLO_write_struct(writer, struct_name, data_ptr)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:5828
void BLO_write_string(BlendWriter *writer, const char *data_ptr)
#define BLO_read_struct_list(reader, struct_name, list)
struct AssetWeakReference AssetWeakReference
Read Guarded memory(de)allocation.
void BKE_asset_catalog_path_list_free(ListBase &catalog_path_list)
void BKE_asset_weak_reference_read(BlendDataReader *reader, AssetWeakReference *weak_ref)
ListBase BKE_asset_catalog_path_list_duplicate(const ListBase &catalog_path_list)
void BKE_asset_weak_reference_write(BlendWriter *writer, const AssetWeakReference *weak_ref)
void BKE_asset_catalog_path_list_blend_write(BlendWriter *writer, const ListBase &catalog_path_list)
bool BKE_asset_catalog_path_list_has_path(const ListBase &catalog_path_list, const char *catalog_path)
void BKE_asset_catalog_path_list_blend_read_data(BlendDataReader *reader, ListBase &catalog_path_list)
bool operator==(const AssetWeakReference &a, const AssetWeakReference &b)
void BKE_asset_catalog_path_list_add_path(ListBase &catalog_path_list, const char *catalog_path)
constexpr int64_t size() const
constexpr const char * data() const
eAssetLibraryType library_type() const
#define offsetof(t, d)
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
const char * name
const char * relative_asset_identifier
const char * asset_library_identifier