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