diff options
author | Sybren A. Stüvel <sybren@blender.org> | 2021-10-21 16:06:49 +0300 |
---|---|---|
committer | Sybren A. Stüvel <sybren@blender.org> | 2021-10-21 16:53:16 +0300 |
commit | 9a1fce698bc6dd51c66464f9dcccfb89d0432823 (patch) | |
tree | baf16b2137a3f36a4f1ded27de26c507b11bfb02 /source | |
parent | 090be2775e3d7c0dc3f7956b05c7fb9a72c4bdfc (diff) |
Cleanup: rename & restructure `AssetCatalogPathCmp`
Rename `AssetCatalogPathCmp` to `AssetCatalogLessThan`:
- it compares more than paths (so no more `Path` in the name), and
- performs a less-than operation (so no more `Cmp` in the name).
Also restructure its code to make an extra upcoming comparison easier to
add.
No functional changes.
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenkernel/BKE_asset_catalog.hh | 17 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/asset_catalog_test.cc | 44 |
2 files changed, 56 insertions, 5 deletions
diff --git a/source/blender/blenkernel/BKE_asset_catalog.hh b/source/blender/blenkernel/BKE_asset_catalog.hh index f7896e0f51e..ca4517707e3 100644 --- a/source/blender/blenkernel/BKE_asset_catalog.hh +++ b/source/blender/blenkernel/BKE_asset_catalog.hh @@ -395,6 +395,12 @@ class AssetCatalog { /* Treat this catalog as deleted. Keeping deleted catalogs around is necessary to support * merging of on-disk changes with in-memory changes. */ bool is_deleted = false; + + /* Sort this catalog first when there are multiple catalogs with the same catalog path. This + * ensures that in a situation where missing catalogs were auto-created, and then + * load-and-merged with a file that also has these catalogs, the first one in that file is + * always sorted first, regardless of the sort order of its UUID. */ + bool is_first_loaded = false; } flags; /** @@ -411,20 +417,21 @@ class AssetCatalog { }; /** Comparator for asset catalogs, ordering by (path, UUID). */ -struct AssetCatalogPathCmp { +struct AssetCatalogLessThan { bool operator()(const AssetCatalog *lhs, const AssetCatalog *rhs) const { - if (lhs->path == rhs->path) { - return lhs->catalog_id < rhs->catalog_id; + if (lhs->path != rhs->path) { + return lhs->path < rhs->path; } - return lhs->path < rhs->path; + + return lhs->catalog_id < rhs->catalog_id; } }; /** * Set that stores catalogs ordered by (path, UUID). * Being a set, duplicates are removed. The catalog's simple name is ignored in this. */ -using AssetCatalogOrderedSet = std::set<const AssetCatalog *, AssetCatalogPathCmp>; +using AssetCatalogOrderedSet = std::set<const AssetCatalog *, AssetCatalogLessThan>; /** * Filter that can determine whether an asset should be visible or not, based on its catalog ID. diff --git a/source/blender/blenkernel/intern/asset_catalog_test.cc b/source/blender/blenkernel/intern/asset_catalog_test.cc index d743d250c1d..478d2d2b31e 100644 --- a/source/blender/blenkernel/intern/asset_catalog_test.cc +++ b/source/blender/blenkernel/intern/asset_catalog_test.cc @@ -954,6 +954,50 @@ TEST_F(AssetCatalogTest, order_by_path) } } +TEST_F(AssetCatalogTest, order_by_path_and_first_seen) +{ + AssetCatalogService service; + service.load_from_disk(asset_library_root_); + + const bUUID first_seen_uuid("3d451c87-27d1-40fd-87fc-f4c9e829c848"); + const bUUID first_sorted_uuid("00000000-0000-0000-0000-000000000001"); + const bUUID last_sorted_uuid("ffffffff-ffff-ffff-ffff-ffffffffffff"); + + AssetCatalog first_seen_cat(first_seen_uuid, "simple/path/child", ""); + const AssetCatalog first_sorted_cat(first_sorted_uuid, "simple/path/child", ""); + const AssetCatalog last_sorted_cat(last_sorted_uuid, "simple/path/child", ""); + + /* Mimick that this catalog was first-seen when loading from disk. */ + first_seen_cat.flags.is_first_loaded = true; + + /* Just an assertion of the defaults; this is more to avoid confusing errors later on than an + * actual test of these defaults. */ + ASSERT_FALSE(first_sorted_cat.flags.is_first_loaded); + ASSERT_FALSE(last_sorted_cat.flags.is_first_loaded); + + AssetCatalogOrderedSet by_path; + by_path.insert(&first_seen_cat); + by_path.insert(&first_sorted_cat); + by_path.insert(&last_sorted_cat); + + AssetCatalogOrderedSet::const_iterator set_iter = by_path.begin(); + + EXPECT_EQ(1, by_path.count(&first_seen_cat)); + EXPECT_EQ(1, by_path.count(&first_sorted_cat)); + EXPECT_EQ(1, by_path.count(&last_sorted_cat)); + ASSERT_EQ(3, by_path.size()); + + EXPECT_EQ(first_seen_uuid, (*(set_iter++))->catalog_id); + EXPECT_EQ(first_sorted_uuid, (*(set_iter++))->catalog_id); + EXPECT_EQ(last_sorted_uuid, (*(set_iter++))->catalog_id); + + if (set_iter != by_path.end()) { + const AssetCatalog *next_cat = *set_iter; + FAIL() << "Did not expect more items in the set, had at least " << next_cat->catalog_id << ":" + << next_cat->path; + } +} + TEST_F(AssetCatalogTest, create_missing_catalogs) { TestableAssetCatalogService new_service; |