Blender V5.3
asset_catalog_tree.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
10
11namespace blender::asset_system {
12
14 CatalogID catalog_id,
15 StringRef simple_name,
16 const AssetCatalogTreeItem *parent)
17 : name_(name), catalog_id_(catalog_id), simple_name_(simple_name), parent_(parent)
18{
19}
20
22{
23 return catalog_id_;
24}
25
27{
28 return name_;
29}
30
32{
33 return simple_name_;
34}
36{
37 return has_unsaved_changes_;
38}
39
41{
42 AssetCatalogPath current_path = name_;
43 for (const AssetCatalogTreeItem *parent = parent_; parent; parent = parent->parent_) {
44 current_path = AssetCatalogPath(parent->name_) / current_path;
45 }
46 return current_path;
47}
48
50{
51 int i = 0;
52 for (const AssetCatalogTreeItem *parent = parent_; parent; parent = parent->parent_) {
53 i++;
54 }
55 return i;
56}
57
59{
60 return !children_.empty();
61}
62
63void AssetCatalogTreeItem::foreach_item_recursive(const AssetCatalogTreeItem::ChildMap &children,
64 const ItemIterFn callback)
65{
66 for (const auto &[key, item] : children) {
67 callback(item);
68 foreach_item_recursive(item.children_, callback);
69 }
70}
71
73{
74 for (const auto &[key, item] : children_) {
75 callback(item);
76 }
77}
78
80{
81 AssetCatalogTreeItem::foreach_item_recursive(children_, callback);
82}
83
84/* ---------------------------------------------------------------------- */
85
87 const std::optional<StringRef> skip_prefix)
88{
89 const AssetCatalogTreeItem *parent = nullptr;
90 /* The children for the currently iterated component, where the following component should be
91 * added to (if not there yet). */
92 AssetCatalogTreeItem::ChildMap *current_item_children = &root_items_;
93
94 BLI_assert_msg(!ELEM(catalog.path.str()[0], '/', '\\'),
95 "Malformed catalog path; should not start with a separator");
96
97 const CatalogID nil_id{};
98
99 std::optional<StringRef> skip_prefix_tmp = skip_prefix;
100
101 catalog.path.iterate_components([&](StringRef component_name, const bool is_last_component) {
102 if (skip_prefix_tmp && skip_prefix_tmp->startswith(component_name)) {
103 if (skip_prefix_tmp->size() == component_name.size() ||
104 (*skip_prefix)[component_name.size()] == AssetCatalogPath::SEPARATOR)
105 {
106 skip_prefix_tmp = skip_prefix_tmp->drop_prefix(component_name.size() + 1);
107 return;
108 }
109 }
110
111 /* Insert new tree element - if no matching one is there yet! */
112 auto [key_and_item, was_inserted] = current_item_children->emplace(
113 component_name,
114 AssetCatalogTreeItem(component_name,
115 is_last_component ? catalog.catalog_id : nil_id,
116 is_last_component ? catalog.simple_name : "",
117 parent));
118 AssetCatalogTreeItem &item = key_and_item->second;
119
120 /* If full path of this catalog already exists as parent path of a previously read catalog,
121 * we can ensure this tree item's UUID is set here. */
122 if (is_last_component) {
123 if (BLI_uuid_is_nil(item.catalog_id_) || catalog.flags.is_first_loaded) {
124 item.catalog_id_ = catalog.catalog_id;
125 }
126 item.has_unsaved_changes_ = catalog.flags.has_unsaved_changes;
127 }
128
129 /* Walk further into the path (no matter if a new item was created or not). */
130 parent = &item;
131 current_item_children = &item.children_;
132 });
133}
134
136{
137 AssetCatalogTreeItem::foreach_item_recursive(root_items_, callback);
138}
139
140void AssetCatalogTree::foreach_root_item(const ItemIterFn callback) const
141{
142 for (const auto &[key, item] : root_items_) {
143 callback(item);
144 }
145}
146
148{
149 return root_items_.empty();
150}
151
153{
154 const AssetCatalogTreeItem *result = nullptr;
155 this->foreach_item([&](const AssetCatalogTreeItem &item) {
156 if (result) {
157 /* There is no way to stop iteration. */
158 return;
159 }
160 if (item.catalog_path() == path) {
161 result = &item;
162 }
163 });
164 return result;
165}
166
168{
169 const AssetCatalogTreeItem *result = nullptr;
170 this->foreach_root_item([&](const AssetCatalogTreeItem &item) {
171 if (result) {
172 /* There is no way to stop iteration. */
173 return;
174 }
175 if (item.catalog_path() == path) {
176 result = &item;
177 }
178 });
179 return result;
180}
181
182} // namespace blender::asset_system
#define ELEM(...)
#define BLI_assert_msg(a, msg)
void iterate_components(ComponentIteratorFn callback) const
void foreach_child(ItemIterFn callback) const
AssetCatalogTreeItem(StringRef name, CatalogID catalog_id, StringRef simple_name, const AssetCatalogTreeItem *parent=nullptr)
FunctionRef< void(const AssetCatalogTreeItem &)> ItemIterFn
std::map< std::string, AssetCatalogTreeItem > ChildMap
void foreach_item(ItemIterFn callback) const
void insert_item(const AssetCatalog &catalog, std::optional< StringRef > skip_prefix=std::nullopt)
void foreach_root_item(ItemIterFn callback) const
const AssetCatalogTreeItem * find_item(const AssetCatalogPath &path) const
void foreach_item(ItemIterFn callback) const
const AssetCatalogTreeItem * find_root_item(const AssetCatalogPath &path) const
struct blender::asset_system::AssetCatalog::Flags flags
constexpr int64_t size() const
bool BLI_uuid_is_nil(bUUID uuid)
Definition uuid.cc:72