From 014311fdbfcd1d7393fb34cb8588a03901a1eb52 Mon Sep 17 00:00:00 2001 From: jules Date: Thu, 4 May 2017 12:02:37 +0100 Subject: [PATCH] Added some comparison and getter methods to Uuid. --- modules/juce_core/misc/juce_Uuid.cpp | 25 +++++++++++++++++++++++-- modules/juce_core/misc/juce_Uuid.h | 19 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/modules/juce_core/misc/juce_Uuid.cpp b/modules/juce_core/misc/juce_Uuid.cpp index f8f0e4e89f..03f911cbf2 100644 --- a/modules/juce_core/misc/juce_Uuid.cpp +++ b/modules/juce_core/misc/juce_Uuid.cpp @@ -48,6 +48,20 @@ Uuid& Uuid::operator= (const Uuid& other) noexcept bool Uuid::operator== (const Uuid& other) const noexcept { return memcmp (uuid, other.uuid, sizeof (uuid)) == 0; } bool Uuid::operator!= (const Uuid& other) const noexcept { return ! operator== (other); } +bool Uuid::operator< (const Uuid& other) const noexcept { return compare (other) < 0; } +bool Uuid::operator> (const Uuid& other) const noexcept { return compare (other) > 0; } +bool Uuid::operator<= (const Uuid& other) const noexcept { return compare (other) <= 0; } +bool Uuid::operator>= (const Uuid& other) const noexcept { return compare (other) >= 0; } + +int Uuid::compare (Uuid other) const noexcept +{ + for (size_t i = 0; i < sizeof (uuid); ++i) + if (int diff = uuid[i] - (int) other.uuid[i]) + return diff > 0 ? 1 : -1; + + return 0; +} + Uuid Uuid::null() noexcept { return Uuid ((const uint8*) nullptr); @@ -55,8 +69,8 @@ Uuid Uuid::null() noexcept bool Uuid::isNull() const noexcept { - for (size_t i = 0; i < sizeof (uuid); ++i) - if (uuid[i] != 0) + for (auto i : uuid) + if (i != 0) return false; return true; @@ -109,3 +123,10 @@ Uuid& Uuid::operator= (const uint8* const rawData) noexcept return *this; } + +uint32 Uuid::getTimeLow() const noexcept { return ByteOrder::bigEndianInt (uuid); } +uint16 Uuid::getTimeMid() const noexcept { return ByteOrder::bigEndianShort (uuid + 4); } +uint16 Uuid::getTimeHighAndVersion() const noexcept { return ByteOrder::bigEndianShort (uuid + 6); } +uint8 Uuid::getClockSeqAndReserved() const noexcept { return uuid[8]; } +uint8 Uuid::getClockSeqLow() const noexcept { return uuid[9]; } +uint64 Uuid::getNode() const noexcept { return (((uint64) ByteOrder::bigEndianShort (uuid + 10)) << 32) + ByteOrder::bigEndianInt (uuid + 12); } diff --git a/modules/juce_core/misc/juce_Uuid.h b/modules/juce_core/misc/juce_Uuid.h index 2c1b80df96..cab55d8dc7 100644 --- a/modules/juce_core/misc/juce_Uuid.h +++ b/modules/juce_core/misc/juce_Uuid.h @@ -58,6 +58,10 @@ public: bool operator== (const Uuid&) const noexcept; bool operator!= (const Uuid&) const noexcept; + bool operator< (const Uuid&) const noexcept; + bool operator> (const Uuid&) const noexcept; + bool operator<= (const Uuid&) const noexcept; + bool operator>= (const Uuid&) const noexcept; //============================================================================== /** Returns a stringified version of this UUID. @@ -85,6 +89,20 @@ public: Uuid& operator= (const String& uuidString); + //============================================================================== + /** Returns the time-low section of the UUID. */ + uint32 getTimeLow() const noexcept; + /** Returns the time-mid section of the UUID. */ + uint16 getTimeMid() const noexcept; + /** Returns the time-high-and-version section of the UUID. */ + uint16 getTimeHighAndVersion() const noexcept; + /** Returns the clock-seq-and-reserved section of the UUID. */ + uint8 getClockSeqAndReserved() const noexcept; + /** Returns the clock-seq-low section of the UUID. */ + uint8 getClockSeqLow() const noexcept; + /** Returns the node section of the UUID. */ + uint64 getNode() const noexcept; + //============================================================================== /** Returns a pointer to the internal binary representation of the ID. @@ -106,6 +124,7 @@ private: //============================================================================== uint8 uuid[16]; String getHexRegion (int, int) const; + int compare (Uuid) const noexcept; JUCE_LEAK_DETECTOR (Uuid) };