From 029d042e8518c53dffe2471d113d5daf4acf97d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Mon, 20 Sep 2021 12:15:37 +0200 Subject: UUID: add nil value for UUIDs Add `BLI_uuid_nil()` that returns the nil UUID (used to indicate "not set") and `BLI_uuid_is_nil(uuid)` to do an equality test with the nil value. --- source/blender/blenlib/BLI_uuid.h | 8 ++++++++ source/blender/blenlib/intern/uuid.cc | 11 +++++++++++ source/blender/blenlib/tests/BLI_uuid_test.cc | 13 +++++++++++++ 3 files changed, 32 insertions(+) (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/BLI_uuid.h b/source/blender/blenlib/BLI_uuid.h index 5440e9426bf..15913cc1017 100644 --- a/source/blender/blenlib/BLI_uuid.h +++ b/source/blender/blenlib/BLI_uuid.h @@ -37,6 +37,14 @@ extern "C" { * This function is not thread-safe. */ UUID BLI_uuid_generate_random(void); +/** + * Return the UUID nil value, consisting of all-zero fields. + */ +UUID BLI_uuid_nil(void); + +/** Return true iff this is the nil UUID. */ +bool BLI_uuid_is_nil(UUID uuid); + /** Compare two UUIDs, return true if they are equal. */ bool BLI_uuid_equal(UUID uuid1, UUID uuid2); diff --git a/source/blender/blenlib/intern/uuid.cc b/source/blender/blenlib/intern/uuid.cc index 5d4f7e1a520..f5edb356acc 100644 --- a/source/blender/blenlib/intern/uuid.cc +++ b/source/blender/blenlib/intern/uuid.cc @@ -78,6 +78,17 @@ UUID BLI_uuid_generate_random() return uuid; } +UUID BLI_uuid_nil(void) +{ + const UUID nil = {0, 0, 0, 0, 0, 0}; + return nil; +} + +bool BLI_uuid_is_nil(UUID uuid) +{ + return BLI_uuid_equal(BLI_uuid_nil(), uuid); +} + bool BLI_uuid_equal(const UUID uuid1, const UUID uuid2) { return std::memcmp(&uuid1, &uuid2, sizeof(uuid1)) == 0; diff --git a/source/blender/blenlib/tests/BLI_uuid_test.cc b/source/blender/blenlib/tests/BLI_uuid_test.cc index bb5eb3817af..31c69002c1c 100644 --- a/source/blender/blenlib/tests/BLI_uuid_test.cc +++ b/source/blender/blenlib/tests/BLI_uuid_test.cc @@ -48,6 +48,19 @@ TEST(BLI_uuid, generate_many_random) } } +TEST(BLI_uuid, nil_value) +{ + const UUID nil_uuid = BLI_uuid_nil(); + const UUID zeroes_uuid = {0, 0, 0, 0, 0, 0}; + + EXPECT_TRUE(BLI_uuid_equal(nil_uuid, zeroes_uuid)); + EXPECT_TRUE(BLI_uuid_is_nil(nil_uuid)); + + std::string buffer(36, '\0'); + BLI_uuid_format(buffer.data(), nil_uuid); + EXPECT_EQ("00000000-0000-0000-0000-000000000000", buffer); +} + TEST(BLI_uuid, equality) { const UUID uuid1 = BLI_uuid_generate_random(); -- cgit v1.2.3