diff options
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_uuid.h | 15 | ||||
-rw-r--r-- | source/blender/blenlib/intern/uuid.cc | 32 |
2 files changed, 47 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_uuid.h b/source/blender/blenlib/BLI_uuid.h index 9b85f8e65bc..592ac3d4607 100644 --- a/source/blender/blenlib/BLI_uuid.h +++ b/source/blender/blenlib/BLI_uuid.h @@ -73,4 +73,19 @@ bool BLI_uuid_parse_string(bUUID *uuid, const char *buffer) ATTR_NONNULL(); /** Output the UUID as formatted ASCII string, see #BLI_uuid_format(). */ std::ostream &operator<<(std::ostream &stream, bUUID uuid); +namespace blender::bke { + +class bUUID : public ::bUUID { + public: + bUUID() = default; + bUUID(const ::bUUID &struct_uuid); + explicit bUUID(const std::string &string_formatted_uuid); + + uint64_t hash() const; +}; + +bool operator==(bUUID uuid1, bUUID uuid2); + +} // namespace blender::bke + #endif diff --git a/source/blender/blenlib/intern/uuid.cc b/source/blender/blenlib/intern/uuid.cc index ae34bcb3d32..c1f7f8dfecd 100644 --- a/source/blender/blenlib/intern/uuid.cc +++ b/source/blender/blenlib/intern/uuid.cc @@ -24,6 +24,7 @@ #include <cstring> #include <ctime> #include <random> +#include <sstream> #include <string> /* Ensure the UUID struct doesn't have any padding, to be compatible with memcmp(). */ @@ -137,3 +138,34 @@ std::ostream &operator<<(std::ostream &stream, bUUID uuid) stream << buffer; return stream; } + +namespace blender::bke { + +bUUID::bUUID(const std::string &string_formatted_uuid) +{ + const bool parsed_ok = BLI_uuid_parse_string(this, string_formatted_uuid.c_str()); + if (!parsed_ok) { + std::stringstream ss; + ss << "invalid UUID string " << string_formatted_uuid; + throw std::runtime_error(ss.str()); + } +} + +bUUID::bUUID(const ::bUUID &struct_uuid) +{ + *(static_cast<::bUUID *>(this)) = struct_uuid; +} + +uint64_t bUUID::hash() const +{ + /* Convert the struct into two 64-bit numbers, and XOR them to get the hash. */ + const uint64_t *uuid_as_int64 = reinterpret_cast<const uint64_t *>(this); + return uuid_as_int64[0] ^ uuid_as_int64[1]; +} + +bool operator==(bUUID uuid1, bUUID uuid2) +{ + return BLI_uuid_equal(uuid1, uuid2); +} + +} // namespace blender::bke |