mirror of
https://onedev.site.tesses.net/tesses-framework
synced 2026-03-26 08:53:21 +00:00
Revamp uuid code
This commit is contained in:
@@ -46,13 +46,11 @@ class BitConverter {
|
|||||||
static uint32_t ToUint32LE(uint8_t& b);
|
static uint32_t ToUint32LE(uint8_t& b);
|
||||||
static uint16_t ToUint16LE(uint8_t& b);
|
static uint16_t ToUint16LE(uint8_t& b);
|
||||||
|
|
||||||
static Uuid ToUuidBE(uint8_t& b);
|
static Uuid ToUuid(uint8_t& b);
|
||||||
|
|
||||||
static Uuid ToUuidMS(uint8_t& b);
|
|
||||||
|
|
||||||
static void ToUuidBE(uint8_t& b, Uuid& uuid);
|
static void ToUuid(uint8_t& b, Uuid& uuid);
|
||||||
|
|
||||||
static void ToUuidMS(uint8_t& b, Uuid& uuid);
|
|
||||||
|
|
||||||
static void FromDoubleBE(uint8_t& b, double v);
|
static void FromDoubleBE(uint8_t& b, double v);
|
||||||
static void FromUint64BE(uint8_t& b, uint64_t v);
|
static void FromUint64BE(uint8_t& b, uint64_t v);
|
||||||
@@ -63,9 +61,8 @@ class BitConverter {
|
|||||||
static void FromUint32LE(uint8_t& b, uint32_t v);
|
static void FromUint32LE(uint8_t& b, uint32_t v);
|
||||||
static void FromUint16LE(uint8_t& b, uint16_t v);
|
static void FromUint16LE(uint8_t& b, uint16_t v);
|
||||||
|
|
||||||
static void FromUuidBE(uint8_t& b, const Uuid& uuid);
|
static void FromUuid(uint8_t& b, const Uuid& uuid);
|
||||||
|
|
||||||
static void FromUuidMS(uint8_t& b, const Uuid& uuid);
|
|
||||||
|
|
||||||
|
|
||||||
static inline bool IsLittleEndian()
|
static inline bool IsLittleEndian()
|
||||||
|
|||||||
@@ -27,9 +27,7 @@ namespace Tesses::Framework::Streams
|
|||||||
float ReadF32LE();
|
float ReadF32LE();
|
||||||
double ReadF64BE();
|
double ReadF64BE();
|
||||||
double ReadF64LE();
|
double ReadF64LE();
|
||||||
Uuid ReadUuidBE();
|
Uuid ReadUuid();
|
||||||
Uuid ReadUuidMS();
|
void ReadUuid(Uuid& uuid);
|
||||||
void ReadUuidBE(Uuid& uuid);
|
|
||||||
void ReadUuidMS(Uuid& uuid);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -27,7 +27,6 @@ namespace Tesses::Framework::Streams
|
|||||||
void WriteF32LE(float v);
|
void WriteF32LE(float v);
|
||||||
void WriteF64BE(double v);
|
void WriteF64BE(double v);
|
||||||
void WriteF64LE(double v);
|
void WriteF64LE(double v);
|
||||||
void WriteUuidBE(const Uuid& uuid);
|
void WriteUuid(const Uuid& uuid);
|
||||||
void WriteUuidMS(const Uuid& uuid);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -27,6 +27,11 @@ namespace Tesses::Framework {
|
|||||||
|
|
||||||
static bool TryParse(std::string text, Uuid& uuid);
|
static bool TryParse(std::string text, Uuid& uuid);
|
||||||
|
|
||||||
std::string ToString(UuidStringifyConfig cfg = UuidStringifyConfig::UppercaseCurly);
|
std::string ToString(UuidStringifyConfig cfg = UuidStringifyConfig::UppercaseCurly) const;
|
||||||
|
|
||||||
|
bool IsEmpty() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
bool operator==(const Uuid& left, const Uuid& right);
|
||||||
|
bool operator!=(const Uuid& left, const Uuid& right);
|
||||||
}
|
}
|
||||||
@@ -157,7 +157,7 @@ namespace Tesses::Framework::Serialization
|
|||||||
b2[0] = (uint8_t)v;
|
b2[0] = (uint8_t)v;
|
||||||
b2[1] = (uint8_t)(v >> 8);
|
b2[1] = (uint8_t)(v >> 8);
|
||||||
}
|
}
|
||||||
void BitConverter::FromUuidBE(uint8_t& b, const Uuid& uuid)
|
void BitConverter::FromUuid(uint8_t& b, const Uuid& uuid)
|
||||||
{
|
{
|
||||||
uint8_t* b2 = &b;
|
uint8_t* b2 = &b;
|
||||||
FromUint32BE(b2[0], uuid.time_low);
|
FromUint32BE(b2[0], uuid.time_low);
|
||||||
@@ -169,35 +169,17 @@ namespace Tesses::Framework::Serialization
|
|||||||
b2[i+10] = uuid.node[i];
|
b2[i+10] = uuid.node[i];
|
||||||
|
|
||||||
}
|
}
|
||||||
void BitConverter::FromUuidMS(uint8_t& b, const Uuid& uuid)
|
|
||||||
{
|
|
||||||
uint8_t* b2 = &b;
|
|
||||||
FromUint32LE(b2[0], uuid.time_low);
|
|
||||||
FromUint32LE(b2[4], uuid.time_mid);
|
|
||||||
FromUint32LE(b2[6], uuid.time_hi_and_version);
|
|
||||||
|
|
||||||
b2[8] = uuid.clock_seq_hi_and_reserved;
|
|
||||||
b2[9] = uuid.clock_seq_low;
|
|
||||||
for(size_t i = 0; i < 6; i++)
|
|
||||||
b2[i+10] = uuid.node[i];
|
|
||||||
|
|
||||||
}
|
Uuid BitConverter::ToUuid(uint8_t& b)
|
||||||
|
|
||||||
Uuid BitConverter::ToUuidBE(uint8_t& b)
|
|
||||||
{
|
{
|
||||||
Uuid uuid;
|
Uuid uuid;
|
||||||
BitConverter::ToUuidBE(b,uuid);
|
BitConverter::ToUuid(b,uuid);
|
||||||
return uuid;
|
|
||||||
}
|
|
||||||
Uuid BitConverter::ToUuidMS(uint8_t& b)
|
|
||||||
{
|
|
||||||
Uuid uuid;
|
|
||||||
BitConverter::ToUuidMS(b,uuid);
|
|
||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void BitConverter::ToUuidBE(uint8_t& b, Uuid& uuid)
|
void BitConverter::ToUuid(uint8_t& b, Uuid& uuid)
|
||||||
{
|
{
|
||||||
uint8_t* b2 = &b;
|
uint8_t* b2 = &b;
|
||||||
uuid.time_low = ToUint32BE(b2[0]);
|
uuid.time_low = ToUint32BE(b2[0]);
|
||||||
@@ -213,16 +195,5 @@ namespace Tesses::Framework::Serialization
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitConverter::ToUuidMS(uint8_t& b, Uuid& uuid)
|
|
||||||
{
|
|
||||||
uint8_t* b2 = &b;
|
|
||||||
uuid.time_low = ToUint32LE(b2[0]);
|
|
||||||
uuid.time_mid = ToUint16LE(b2[4]);
|
|
||||||
uuid.time_hi_and_version = ToUint16LE(b2[6]);
|
|
||||||
|
|
||||||
uuid.clock_seq_hi_and_reserved = b2[8];
|
|
||||||
uuid.clock_seq_low = b2[9];
|
|
||||||
for(size_t i = 0; i < 6; i++)
|
|
||||||
uuid.node[i]= b2[i+10];
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
@@ -151,32 +151,19 @@ namespace Tesses::Framework::Streams
|
|||||||
return *(double*)&v;
|
return *(double*)&v;
|
||||||
}
|
}
|
||||||
|
|
||||||
Uuid ByteReader::ReadUuidBE()
|
Uuid ByteReader::ReadUuid()
|
||||||
{
|
{
|
||||||
uint8_t data[16];
|
uint8_t data[16];
|
||||||
if(this->strm->ReadBlock(data, 16) != 16)
|
if(this->strm->ReadBlock(data, 16) != 16)
|
||||||
throw std::runtime_error("End of file");
|
throw std::runtime_error("End of file");
|
||||||
return Serialization::BitConverter::ToUuidBE(data[0]);
|
return Serialization::BitConverter::ToUuid(data[0]);
|
||||||
}
|
}
|
||||||
Uuid ByteReader::ReadUuidMS()
|
void ByteReader::ReadUuid(Uuid& uuid)
|
||||||
{
|
{
|
||||||
uint8_t data[16];
|
uint8_t data[16];
|
||||||
if(this->strm->ReadBlock(data, 16) != 16)
|
if(this->strm->ReadBlock(data, 16) != 16)
|
||||||
throw std::runtime_error("End of file");
|
throw std::runtime_error("End of file");
|
||||||
return Serialization::BitConverter::ToUuidMS(data[0]);
|
Serialization::BitConverter::ToUuid(data[0],uuid);
|
||||||
}
|
|
||||||
void ByteReader::ReadUuidBE(Uuid& uuid)
|
|
||||||
{
|
|
||||||
uint8_t data[16];
|
|
||||||
if(this->strm->ReadBlock(data, 16) != 16)
|
|
||||||
throw std::runtime_error("End of file");
|
|
||||||
Serialization::BitConverter::ToUuidBE(data[0],uuid);
|
|
||||||
}
|
|
||||||
void ByteReader::ReadUuidMS(Uuid& uuid)
|
|
||||||
{
|
|
||||||
uint8_t data[16];
|
|
||||||
if(this->strm->ReadBlock(data, 16) != 16)
|
|
||||||
throw std::runtime_error("End of file");
|
|
||||||
Serialization::BitConverter::ToUuidMS(data[0],uuid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -130,16 +130,11 @@ namespace Tesses::Framework::Streams
|
|||||||
uint64_t data = *(uint64_t*)&v;
|
uint64_t data = *(uint64_t*)&v;
|
||||||
WriteU64LE(data);
|
WriteU64LE(data);
|
||||||
}
|
}
|
||||||
void ByteWriter::WriteUuidBE(const Uuid& uuid)
|
void ByteWriter::WriteUuid(const Uuid& uuid)
|
||||||
{
|
{
|
||||||
uint8_t data[16];
|
uint8_t data[16];
|
||||||
Serialization::BitConverter::FromUuidBE(data[0], uuid);
|
Serialization::BitConverter::FromUuid(data[0], uuid);
|
||||||
this->strm->WriteBlock(data, 16);
|
|
||||||
}
|
|
||||||
void ByteWriter::WriteUuidMS(const Uuid& uuid)
|
|
||||||
{
|
|
||||||
uint8_t data[16];
|
|
||||||
Serialization::BitConverter::FromUuidMS(data[0], uuid);
|
|
||||||
this->strm->WriteBlock(data, 16);
|
this->strm->WriteBlock(data, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
69
src/Uuid.cpp
69
src/Uuid.cpp
@@ -61,39 +61,39 @@ namespace Tesses::Framework {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t b = hex_digits[0] >> 4 | hex_digits[1];
|
uint8_t b = hex_digits[0] << 4 | hex_digits[1];
|
||||||
uuid.time_low = (uint32_t)b;
|
uuid.time_low = (uint32_t)b;
|
||||||
b = hex_digits[2] >> 4 | hex_digits[3];
|
b = hex_digits[2] << 4 | hex_digits[3];
|
||||||
uuid.time_low |= (uint32_t)b << 8;
|
uuid.time_low |= (uint32_t)b << 8;
|
||||||
b = hex_digits[4] >> 4 | hex_digits[5];
|
b = hex_digits[4] << 4 | hex_digits[5];
|
||||||
uuid.time_low |= (uint32_t)b << 16;
|
uuid.time_low |= (uint32_t)b << 16;
|
||||||
b = hex_digits[6] >> 4 | hex_digits[7];
|
b = hex_digits[6] << 4 | hex_digits[7];
|
||||||
uuid.time_low |= (uint32_t)b << 24;
|
uuid.time_low |= (uint32_t)b << 24;
|
||||||
|
|
||||||
b = hex_digits[8] >> 4 | hex_digits[9];
|
b = hex_digits[8] << 4 | hex_digits[9];
|
||||||
uuid.time_mid = (uint16_t)b;
|
uuid.time_mid = (uint16_t)b;
|
||||||
b = hex_digits[10] >> 4 | hex_digits[11];
|
b = hex_digits[10] << 4 | hex_digits[11];
|
||||||
uuid.time_mid |= (uint16_t)b << 8;
|
uuid.time_mid |= (uint16_t)b << 8;
|
||||||
|
|
||||||
b = hex_digits[12] >> 4 | hex_digits[13];
|
b = hex_digits[12] << 4 | hex_digits[13];
|
||||||
uuid.time_hi_and_version = (uint16_t)b;
|
uuid.time_hi_and_version = (uint16_t)b;
|
||||||
b = hex_digits[14] >> 4 | hex_digits[15];
|
b = hex_digits[14] << 4 | hex_digits[15];
|
||||||
uuid.time_hi_and_version |= (uint16_t)b << 8;
|
uuid.time_hi_and_version |= (uint16_t)b << 8;
|
||||||
|
|
||||||
uuid.clock_seq_hi_and_reserved = hex_digits[16] >> 4 | hex_digits[17];
|
uuid.clock_seq_hi_and_reserved = hex_digits[16] << 4 | hex_digits[17];
|
||||||
|
|
||||||
uuid.clock_seq_low = hex_digits[18] >> 4 | hex_digits[19];
|
uuid.clock_seq_low = hex_digits[18] << 4 | hex_digits[19];
|
||||||
|
|
||||||
for(size_t i = 0; i < 6; i++)
|
for(size_t i = 0; i < 6; i++)
|
||||||
{
|
{
|
||||||
uuid.node[i] = hex_digits[20+(i*2)] >> 4 | hex_digits[21+(i*2)];
|
uuid.node[i] = hex_digits[20+(i*2)] << 4 | hex_digits[21+(i*2)];
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//9c4994e7-3c82-4c30-a459-8fdcd960b4ac
|
//9c4994e7-3c82-4c30-a459-8fdcd960b4ac
|
||||||
|
|
||||||
std::string Uuid::ToString(UuidStringifyConfig cfg)
|
std::string Uuid::ToString(UuidStringifyConfig cfg) const
|
||||||
{
|
{
|
||||||
bool hasCurly = ((int)cfg & (int)UuidStringifyConfig::HasCurly) != 0;
|
bool hasCurly = ((int)cfg & (int)UuidStringifyConfig::HasCurly) != 0;
|
||||||
bool isUppercase = ((int)cfg & (int)UuidStringifyConfig::IsUppercase) != 0;
|
bool isUppercase = ((int)cfg & (int)UuidStringifyConfig::IsUppercase) != 0;
|
||||||
@@ -166,4 +166,49 @@ namespace Tesses::Framework {
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Uuid::IsEmpty() const
|
||||||
|
{
|
||||||
|
return this->time_low == 0 &&
|
||||||
|
this->time_mid == 0 &&
|
||||||
|
this->time_hi_and_version == 0 &&
|
||||||
|
this->clock_seq_hi_and_reserved == 0 &&
|
||||||
|
this->clock_seq_low == 0 &&
|
||||||
|
this->node[0] == 0 &&
|
||||||
|
this->node[1] == 0 &&
|
||||||
|
this->node[2] == 0 &&
|
||||||
|
this->node[3] == 0 &&
|
||||||
|
this->node[4] == 0 &&
|
||||||
|
this->node[5] == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const Uuid& left, const Uuid& right)
|
||||||
|
{
|
||||||
|
return left.time_low == right.time_low &&
|
||||||
|
left.time_mid == right.time_mid &&
|
||||||
|
left.time_hi_and_version == right.time_hi_and_version &&
|
||||||
|
left.clock_seq_hi_and_reserved == right.clock_seq_hi_and_reserved &&
|
||||||
|
left.clock_seq_low == right.clock_seq_low &&
|
||||||
|
left.node[0] == right.node[0] &&
|
||||||
|
left.node[1] == right.node[1] &&
|
||||||
|
left.node[2] == right.node[2] &&
|
||||||
|
left.node[3] == right.node[3] &&
|
||||||
|
left.node[4] == right.node[4] &&
|
||||||
|
left.node[5] == right.node[5];
|
||||||
|
|
||||||
|
}
|
||||||
|
bool operator!=(const Uuid& left, const Uuid& right)
|
||||||
|
{
|
||||||
|
return left.time_low != right.time_low &&
|
||||||
|
left.time_mid != right.time_mid &&
|
||||||
|
left.time_hi_and_version != right.time_hi_and_version &&
|
||||||
|
left.clock_seq_hi_and_reserved != right.clock_seq_hi_and_reserved &&
|
||||||
|
left.clock_seq_low != right.clock_seq_low &&
|
||||||
|
left.node[0] != right.node[0] &&
|
||||||
|
left.node[1] != right.node[1] &&
|
||||||
|
left.node[2] != right.node[2] &&
|
||||||
|
left.node[3] != right.node[3] &&
|
||||||
|
left.node[4] != right.node[4] &&
|
||||||
|
left.node[5] != right.node[5];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user