Created transparent form of Azure::Core::Uuid (#4534)
* Created transparent form of Azure::Core::Uuid * Pull request feedback * Update sdk/core/azure-core/CHANGELOG.md Co-authored-by: Ahson Khan <ahson_ahmedk@yahoo.com> * Update sdk/core/azure-core/inc/azure/core/uuid.hpp Co-authored-by: Ahson Khan <ahson_ahmedk@yahoo.com> --------- Co-authored-by: Ahson Khan <ahson_ahmedk@yahoo.com>
This commit is contained in:
parent
fe7c7f971f
commit
99a17a8263
@ -4,6 +4,9 @@
|
||||
|
||||
### Features Added
|
||||
|
||||
- Added `Azure::Core::Uuid::AsArray()` and `Azure::Core::Uuid::FromArray()` to enable reading or writing from an existing UUID.
|
||||
This is useful when the UUID was generated outside the Azure SDK, or needs to be used from a component outside the Azure SDK.
|
||||
|
||||
### Breaking Changes
|
||||
|
||||
### Bugs Fixed
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
|
||||
#include "azure/core/platform.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
@ -22,7 +23,7 @@ namespace Azure { namespace Core {
|
||||
private:
|
||||
static constexpr size_t UuidSize = 16;
|
||||
|
||||
uint8_t m_uuid[UuidSize];
|
||||
std::array<uint8_t, UuidSize> m_uuid;
|
||||
// The UUID reserved variants.
|
||||
static constexpr uint8_t ReservedNCS = 0x80;
|
||||
static constexpr uint8_t ReservedRFC4122 = 0x40;
|
||||
@ -30,7 +31,7 @@ namespace Azure { namespace Core {
|
||||
static constexpr uint8_t ReservedFuture = 0x00;
|
||||
|
||||
private:
|
||||
Uuid(uint8_t const uuid[UuidSize]) { std::memcpy(m_uuid, uuid, UuidSize); }
|
||||
Uuid(uint8_t const uuid[UuidSize]) { std::memcpy(m_uuid.data(), uuid, UuidSize); }
|
||||
|
||||
public:
|
||||
/**
|
||||
@ -39,10 +40,23 @@ namespace Azure { namespace Core {
|
||||
*/
|
||||
std::string ToString();
|
||||
|
||||
/**
|
||||
* @brief Returns the binary value of the Uuid for consumption by clients who need non-string
|
||||
* representation of the Uuid
|
||||
* @returns An array with the binary representation of the Uuid.
|
||||
*/
|
||||
std::array<uint8_t, UuidSize> const& AsArray() const { return m_uuid; }
|
||||
|
||||
/**
|
||||
* @brief Creates a new random UUID.
|
||||
*
|
||||
*/
|
||||
static Uuid CreateUuid();
|
||||
|
||||
/**
|
||||
* @brief Construct a Uuid from an existing UUID represented as an array of bytes.
|
||||
* @details Creates a Uuid from a UUID created in an external scope.
|
||||
*/
|
||||
static Uuid FromArray(std::array<uint8_t, UuidSize> const& uuid);
|
||||
};
|
||||
}} // namespace Azure::Core
|
||||
|
||||
@ -70,11 +70,17 @@ namespace Azure { namespace Core {
|
||||
// SetVariant to ReservedRFC4122
|
||||
uuid[8] = (uuid[8] | ReservedRFC4122) & 0x7F;
|
||||
|
||||
constexpr uint8_t version = 4;
|
||||
constexpr uint8_t version = 4; // Version 4: Pseudo-random number
|
||||
|
||||
uuid[6] = (uuid[6] & 0xF) | (version << 4);
|
||||
|
||||
return Uuid(uuid);
|
||||
}
|
||||
|
||||
Uuid Uuid::FromArray(std::array<uint8_t, UuidSize> const& uuid)
|
||||
{
|
||||
Uuid rv{uuid.data()};
|
||||
return rv;
|
||||
}
|
||||
|
||||
}} // namespace Azure::Core
|
||||
|
||||
@ -14,6 +14,14 @@ TEST(Uuid, Basic)
|
||||
EXPECT_EQ(uuid.ToString().length(), 36);
|
||||
}
|
||||
|
||||
TEST(Uuid, Transparent)
|
||||
{
|
||||
auto uuid1 = Uuid::CreateUuid();
|
||||
auto arrayUuid1(uuid1.AsArray());
|
||||
auto uuid2 = Azure::Core::Uuid::FromArray(arrayUuid1);
|
||||
EXPECT_EQ(uuid1.ToString(), uuid2.ToString());
|
||||
}
|
||||
|
||||
TEST(Uuid, Randomness)
|
||||
{
|
||||
const int size = 100000;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user