diff options
author | Sybren A. Stüvel <sybren@blender.org> | 2021-09-23 18:55:43 +0300 |
---|---|---|
committer | Sybren A. Stüvel <sybren@blender.org> | 2021-09-23 18:58:20 +0300 |
commit | bd63944a739b4dcd49e8a65294ffb1b8a0a7b20b (patch) | |
tree | 583d81aa9a9257ef96e69b20d18e760f42ffa839 /source/blender | |
parent | 942fc9f467c104150a474eb61a82957b5c9715f7 (diff) |
UUID: place C++ code in correct namespace
Put the `bUUID` class in the `blender` namespace, instead of the
`blender::bke` namespace.
As a result, some C++ code now correctly uses the C++ class, where
previously it would use the C struct and use implicit casting where
necessary. As a result, support for initializer lists had to be
explicitly coded and in another place an explicit `::bUUID` was
necessary to avoid ambiguity.
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenkernel/intern/asset.cc | 2 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_uuid.h | 9 | ||||
-rw-r--r-- | source/blender/blenlib/intern/uuid.cc | 22 | ||||
-rw-r--r-- | source/blender/blenlib/tests/BLI_uuid_test.cc | 12 |
4 files changed, 35 insertions, 10 deletions
diff --git a/source/blender/blenkernel/intern/asset.cc b/source/blender/blenkernel/intern/asset.cc index ac9bcccc8bd..ae9ded3c754 100644 --- a/source/blender/blenkernel/intern/asset.cc +++ b/source/blender/blenkernel/intern/asset.cc @@ -126,7 +126,7 @@ void BKE_asset_metadata_catalog_id_clear(struct AssetMetaData *asset_data) } void BKE_asset_metadata_catalog_id_set(struct AssetMetaData *asset_data, - const bUUID catalog_id, + const ::bUUID catalog_id, const char *catalog_simple_name) { asset_data->catalog_id = catalog_id; diff --git a/source/blender/blenlib/BLI_uuid.h b/source/blender/blenlib/BLI_uuid.h index 72d95b41329..d21ccd450cc 100644 --- a/source/blender/blenlib/BLI_uuid.h +++ b/source/blender/blenlib/BLI_uuid.h @@ -68,19 +68,24 @@ bool BLI_uuid_parse_string(bUUID *uuid, const char *buffer) ATTR_NONNULL(); #ifdef __cplusplus } +# include <initializer_list> # include <ostream> /** Output the UUID as formatted ASCII string, see #BLI_uuid_format(). */ std::ostream &operator<<(std::ostream &stream, bUUID uuid); -namespace blender::bke { +namespace blender { class bUUID : public ::bUUID { public: bUUID() = default; + /** Initialise from the bUUID DNA struct. */ bUUID(const ::bUUID &struct_uuid); + /** Initialise from 11 integers, 5 for the regular fields and 6 for the `node` array. */ + bUUID(std::initializer_list<uint> field_values); + /** Initialise by parsing the string; undefined behaviour when the string is invalid. */ explicit bUUID(const std::string &string_formatted_uuid); @@ -89,6 +94,6 @@ class bUUID : public ::bUUID { bool operator==(bUUID uuid1, bUUID uuid2); -} // namespace blender::bke +} // namespace blender #endif diff --git a/source/blender/blenlib/intern/uuid.cc b/source/blender/blenlib/intern/uuid.cc index c1f7f8dfecd..cdb430c046a 100644 --- a/source/blender/blenlib/intern/uuid.cc +++ b/source/blender/blenlib/intern/uuid.cc @@ -18,6 +18,7 @@ * \ingroup bli */ +#include "BLI_assert.h" #include "BLI_uuid.h" #include <cstdio> @@ -139,7 +140,22 @@ std::ostream &operator<<(std::ostream &stream, bUUID uuid) return stream; } -namespace blender::bke { +namespace blender { + +bUUID::bUUID(const std::initializer_list<uint> field_values) +{ + BLI_assert_msg(field_values.size() == 11, "bUUID requires 5 regular fields + 6 `node` values"); + + auto field_iter = field_values.begin(); + + this->time_low = static_cast<uint32_t>(*field_iter++); + this->time_mid = static_cast<uint16_t>(*field_iter++); + this->time_hi_and_version = static_cast<uint16_t>(*field_iter++); + this->clock_seq_hi_and_reserved = static_cast<uint8_t>(*field_iter++); + this->clock_seq_low = static_cast<uint8_t>(*field_iter++); + + std::copy(field_iter, field_values.end(), this->node); +} bUUID::bUUID(const std::string &string_formatted_uuid) { @@ -163,9 +179,9 @@ uint64_t bUUID::hash() const return uuid_as_int64[0] ^ uuid_as_int64[1]; } -bool operator==(bUUID uuid1, bUUID uuid2) +bool operator==(const bUUID uuid1, const bUUID uuid2) { return BLI_uuid_equal(uuid1, uuid2); } -} // namespace blender::bke +} // namespace blender diff --git a/source/blender/blenlib/tests/BLI_uuid_test.cc b/source/blender/blenlib/tests/BLI_uuid_test.cc index 731489c6c9e..42aa618963f 100644 --- a/source/blender/blenlib/tests/BLI_uuid_test.cc +++ b/source/blender/blenlib/tests/BLI_uuid_test.cc @@ -18,6 +18,8 @@ #include "BLI_uuid.h" +namespace blender::tests { + TEST(BLI_uuid, generate_random) { const bUUID uuid = BLI_uuid_generate_random(); @@ -51,7 +53,7 @@ TEST(BLI_uuid, generate_many_random) TEST(BLI_uuid, nil_value) { const bUUID nil_uuid = BLI_uuid_nil(); - const bUUID zeroes_uuid = {0, 0, 0, 0, 0, 0}; + const bUUID zeroes_uuid{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; EXPECT_TRUE(BLI_uuid_equal(nil_uuid, zeroes_uuid)); EXPECT_TRUE(BLI_uuid_is_nil(nil_uuid)); @@ -91,13 +93,13 @@ TEST(BLI_uuid, string_formatting) EXPECT_EQ("00000001-0002-0003-0405-060000000007", buffer); /* 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}}; + 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); /* Namespace UUID, example listed in RFC4211. */ const bUUID namespace_dns = { - 0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, {0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}}; + 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); } @@ -139,7 +141,9 @@ TEST(BLI_uuid, string_parsing_fail) TEST(BLI_uuid, stream_operator) { std::stringstream ss; - const bUUID uuid = {3540651616, 5282, 4588, 139, 153, {0xf7, 0x73, 0x69, 0x44, 0xdb, 0x8b}}; + const bUUID uuid = {3540651616, 5282, 4588, 139, 153, 0xf7, 0x73, 0x69, 0x44, 0xdb, 0x8b}; ss << uuid; EXPECT_EQ(ss.str(), "d30a0e60-14a2-11ec-8b99-f7736944db8b"); } + +} // namespace blender::tests |