Cleanup: Add method to get UUID as std::string

Avoids having to use the C-style `BLI_uuid_format()` function with
manual buffer management, and makes it easy to get a `std::string` from
a UUID.
This commit is contained in:
Julian Eisel 2024-02-20 15:08:43 +01:00
parent 447bb9a4b9
commit 99673edd85
5 changed files with 17 additions and 6 deletions

View File

@ -83,6 +83,9 @@ class bUUID : public ::bUUID {
/** Initialize by parsing the string; undefined behavior when the string is invalid. */
explicit bUUID(const StringRefNull string_formatted_uuid);
/** Return the UUID as formatted ASCII string, see #BLI_uuid_format(). */
std::string str() const;
uint64_t hash() const;
}; // namespace blender

View File

@ -168,6 +168,13 @@ bUUID::bUUID(const ::bUUID &struct_uuid)
*(static_cast<::bUUID *>(this)) = struct_uuid;
}
std::string bUUID::str() const
{
std::string string(36, '\0');
BLI_uuid_format(string.data(), *this);
return string;
}
uint64_t bUUID::hash() const
{
/* Convert the struct into two 64-bit numbers, and XOR them to get the hash. */

View File

@ -94,6 +94,7 @@ TEST(BLI_uuid, string_formatting)
memset(&uuid, 0, sizeof(uuid));
BLI_uuid_format(buffer.data(), uuid);
EXPECT_EQ("00000000-0000-0000-0000-000000000000", buffer);
EXPECT_EQ("00000000-0000-0000-0000-000000000000", uuid.str());
/* Demo of where the bits end up in the formatted string. */
uuid.time_low = 1;
@ -105,17 +106,20 @@ TEST(BLI_uuid, string_formatting)
uuid.node[5] = 7;
BLI_uuid_format(buffer.data(), uuid);
EXPECT_EQ("00000001-0002-0003-0405-060000000007", buffer);
EXPECT_EQ("00000001-0002-0003-0405-060000000007", uuid.str());
/* Somewhat more complex bit patterns. This is a version 1 UUID generated from Python. */
const bUUID uuid1 = {3540651616, 5282, 4588, 139, 153, 0xf7, 0x73, 0x69, 0x44, 0xdb, 0x8b};
BLI_uuid_format(buffer.data(), uuid1);
EXPECT_EQ("d30a0e60-14a2-11ec-8b99-f7736944db8b", buffer);
EXPECT_EQ("d30a0e60-14a2-11ec-8b99-f7736944db8b", uuid1.str());
/* Namespace UUID, example listed in RFC4211. */
const bUUID namespace_dns = {
0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8};
BLI_uuid_format(buffer.data(), namespace_dns);
EXPECT_EQ("6ba7b810-9dad-11d1-80b4-00c04fd430c8", buffer);
EXPECT_EQ("6ba7b810-9dad-11d1-80b4-00c04fd430c8", namespace_dns.str());
}
TEST(BLI_uuid, string_parsing_ok)

View File

@ -272,9 +272,8 @@ struct AssetEntryWriter {
void add_catalog_id(const CatalogID &catalog_id)
{
char catalog_id_str[UUID_STRING_SIZE];
BLI_uuid_format(catalog_id_str, catalog_id);
attributes.append_as(std::pair(ATTRIBUTE_ENTRIES_CATALOG_ID, new StringValue(catalog_id_str)));
attributes.append_as(
std::pair(ATTRIBUTE_ENTRIES_CATALOG_ID, new StringValue(catalog_id.str())));
}
void add_catalog_name(const StringRefNull catalog_name)

View File

@ -306,8 +306,6 @@ void AssetCatalogTreeViewItem::build_context_menu(bContext &C, uiLayout &column)
&props);
RNA_string_set(&props, "parent_path", catalog_item_.catalog_path().c_str());
char catalog_id_str_buffer[UUID_STRING_SIZE] = "";
BLI_uuid_format(catalog_id_str_buffer, catalog_item_.get_catalog_id());
uiItemFullO(&column,
"ASSET_OT_catalog_delete",
IFACE_("Delete Catalog"),
@ -316,7 +314,7 @@ void AssetCatalogTreeViewItem::build_context_menu(bContext &C, uiLayout &column)
WM_OP_INVOKE_DEFAULT,
UI_ITEM_NONE,
&props);
RNA_string_set(&props, "catalog_id", catalog_id_str_buffer);
RNA_string_set(&props, "catalog_id", catalog_item_.get_catalog_id().str().c_str());
uiItemO(&column, IFACE_("Rename"), ICON_NONE, "UI_OT_view_item_rename");
/* Doesn't actually exist right now, but could be defined in Python. Reason that this isn't done