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.
This commit is contained in:
Sybren A. Stüvel
2021-10-21 15:06:49 +02:00
parent 090be2775e
commit 9a1fce698b
2 changed files with 56 additions and 5 deletions

View File

@@ -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.

View File

@@ -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;