diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-20 14:33:09 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-20 14:33:09 +1000 |
commit | 6352e8e3196f78388b6c771073f9e03eaa612673 (patch) | |
tree | e23772f79a7fbc41bc9108951e9e136857484bf4 /source/core/StarUuid.cpp | |
parent | 6741a057e5639280d85d0f88ba26f000baa58f61 (diff) |
everything everywhere
all at once
Diffstat (limited to 'source/core/StarUuid.cpp')
-rw-r--r-- | source/core/StarUuid.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/source/core/StarUuid.cpp b/source/core/StarUuid.cpp new file mode 100644 index 0000000..11121b6 --- /dev/null +++ b/source/core/StarUuid.cpp @@ -0,0 +1,72 @@ +#include "StarUuid.hpp" +#include "StarRandom.hpp" +#include "StarFormat.hpp" +#include "StarEncode.hpp" + +namespace Star { + +Uuid::Uuid() : Uuid(Random::randBytes(UuidSize)) {} + +Uuid::Uuid(ByteArray const& bytes) { + if (bytes.size() != UuidSize) + throw UuidException(strf("Size mismatch in reading Uuid from ByteArray: %s vs %s", bytes.size(), UuidSize)); + + bytes.copyTo(m_data.ptr(), UuidSize); +} + +Uuid::Uuid(String const& hex) : Uuid(hexDecode(hex)) {} + +char const* Uuid::ptr() const { + return m_data.ptr(); +} + +ByteArray Uuid::bytes() const { + return ByteArray(m_data.ptr(), UuidSize); +} + +String Uuid::hex() const { + return hexEncode(m_data.ptr(), UuidSize); +} + +bool Uuid::operator==(Uuid const& u) const { + return m_data == u.m_data; +} + +bool Uuid::operator!=(Uuid const& u) const { + return m_data != u.m_data; +} + +bool Uuid::operator<(Uuid const& u) const { + return m_data < u.m_data; +} + +bool Uuid::operator<=(Uuid const& u) const { + return m_data <= u.m_data; +} + +bool Uuid::operator>(Uuid const& u) const { + return m_data > u.m_data; +} + +bool Uuid::operator>=(Uuid const& u) const { + return m_data >= u.m_data; +} + +size_t hash<Uuid>::operator()(Uuid const& u) const { + size_t hashval = 0; + for (size_t i = 0; i < UuidSize; ++i) + hashCombine(hashval, u.ptr()[i]); + return hashval; +} + +DataStream& operator>>(DataStream& ds, Uuid& uuid) { + uuid = Uuid(ds.readBytes(UuidSize)); + return ds; +} + +DataStream& operator<<(DataStream& ds, Uuid const& uuid) { + ds.writeData(uuid.ptr(), UuidSize); + return ds; +} + +} |