From f138311d2d2e0cc9ba0496d523bb46f2c1c9fb73 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Wed, 20 Sep 2023 23:58:58 -0700 Subject: Copy from the PlasticSCM repo, replace vendored glm wtih conan --- source/10-common/Uid.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 source/10-common/Uid.cpp (limited to 'source/10-common/Uid.cpp') diff --git a/source/10-common/Uid.cpp b/source/10-common/Uid.cpp new file mode 100644 index 0000000..58dfffd --- /dev/null +++ b/source/10-common/Uid.cpp @@ -0,0 +1,70 @@ +#include "Uid.hpp" + +#include "RapidJsonHelper.hpp" + +#include +#include +#include + +Uid Uid::Create() { + std::random_device rd; + std::mt19937_64 gen(rd()); + std::uniform_int_distribution dist( + std::numeric_limits::min(), + std::numeric_limits::max()); + + Uid uid; + uid.upper = dist(gen); + uid.lower = dist(gen); + return uid; +} + +bool Uid::IsNull() const { + return upper == 0 && lower == 0; +} + +void Uid::ReadString(std::string_view str) { + sscanf(str.data(), BRUSSEL_Uid_SCAN_STR, &upper, &lower); +} + +std::string Uid::WriteString() { + char buf[256]; + snprintf(buf, sizeof(buf), BRUSSEL_Uid_FORMAT_STR, upper, lower); + return std::string(buf); +} + +void Uid::Read(const rapidjson::Value& value) { + if (value.IsString()) { + ReadString(rapidjson::AsStringView(value)); + } else if (value.IsArray()) { + // Compatibility support + assert(value.Size() == 2); + auto& upper = value[0]; + assert(upper.IsUint64()); + auto& lower = value[1]; + assert(lower.IsUint64()); + + this->upper = upper.GetUint64(); + this->lower = lower.GetUint64(); + } else { + assert(false); + } +} + +void Uid::WriteInto(rapidjson::Value& value, rapidjson::Document& root) const { +#if BRUSSEL_Uid_WRITE_USE_ARRAY + value.Reserve(2, root.GetAllocator()); + value.PushBack((uint64_t)upper, root.GetAllocator()); + value.PushBack((uint64_t)lower, root.GetAllocator()); +#else + char buf[256]; + int len = snprintf(buf, sizeof(buf), BRUSSEL_Uid_FORMAT_STR, upper, lower); + value.SetString(buf, len, root.GetAllocator()); +#endif +} + +rapidjson::Value Uid::Write(rapidjson::Document& root) const { + rapidjson::Value result(rapidjson::kArrayType); + WriteInto(result, root); + return result; +} -- cgit v1.2.3-70-g09d2