Blender V4.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
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 (auto &[key, item] : children) {
67 callback(item);
68 foreach_item_recursive(item.children_, callback);
69 }
70}
71
73{
74 for (auto &[key, item] : children_) {
75 callback(item);
76 }
77}
78
79/* ---------------------------------------------------------------------- */
80
82{
83 const AssetCatalogTreeItem *parent = nullptr;
84 /* The children for the currently iterated component, where the following component should be
85 * added to (if not there yet). */
86 AssetCatalogTreeItem::ChildMap *current_item_children = &root_items_;
87
88 BLI_assert_msg(!ELEM(catalog.path.str()[0], '/', '\\'),
89 "Malformed catalog path; should not start with a separator");
90
91 const CatalogID nil_id{};
92
93 catalog.path.iterate_components([&](StringRef component_name, const bool is_last_component) {
94 /* Insert new tree element - if no matching one is there yet! */
95 auto [key_and_item, was_inserted] = current_item_children->emplace(
96 component_name,
97 AssetCatalogTreeItem(component_name,
98 is_last_component ? catalog.catalog_id : nil_id,
99 is_last_component ? catalog.simple_name : "",
100 parent));
101 AssetCatalogTreeItem &item = key_and_item->second;
102
103 /* If full path of this catalog already exists as parent path of a previously read catalog,
104 * we can ensure this tree item's UUID is set here. */
105 if (is_last_component) {
106 if (BLI_uuid_is_nil(item.catalog_id_) || catalog.flags.is_first_loaded) {
107 item.catalog_id_ = catalog.catalog_id;
108 }
109 item.has_unsaved_changes_ = catalog.flags.has_unsaved_changes;
110 }
111
112 /* Walk further into the path (no matter if a new item was created or not). */
113 parent = &item;
114 current_item_children = &item.children_;
115 });
116}
117
119{
120 AssetCatalogTreeItem::foreach_item_recursive(root_items_, callback);
121}
122
124{
125 for (auto &[key, item] : root_items_) {
126 callback(item);
127 }
128}
129
131{
132 return root_items_.empty();
133}
134
136{
137 const AssetCatalogTreeItem *result = nullptr;
138 this->foreach_item([&](const AssetCatalogTreeItem &item) {
139 if (result) {
140 /* There is no way to stop iteration. */
141 return;
142 }
143 if (item.catalog_path() == path) {
144 result = &item;
145 }
146 });
147 return result;
148}
149
151{
152 const AssetCatalogTreeItem *result = nullptr;
153 this->foreach_root_item([&](const AssetCatalogTreeItem &item) {
154 if (result) {
155 /* There is no way to stop iteration. */
156 return;
157 }
158 if (item.catalog_path() == path) {
159 result = &item;
160 }
161 });
162 return result;
163}
164
165} // namespace blender::asset_system
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:57
#define ELEM(...)
bool BLI_uuid_is_nil(bUUID uuid)
Definition uuid.cc:79
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)
std::map< std::string, AssetCatalogTreeItem > ChildMap
void foreach_root_item(ItemIterFn callback) const
const AssetCatalogTreeItem * find_item(const AssetCatalogPath &path) const
void foreach_item(ItemIterFn callback) const
void insert_item(const AssetCatalog &catalog)
const AssetCatalogTreeItem * find_root_item(const AssetCatalogPath &path) const
struct blender::asset_system::AssetCatalog::Flags flags
DEGForeachIDComponentCallback callback
Universally Unique Identifier according to RFC4122.