From f77e73c01a15426bcc6e3d7fe5826d2a741fed38 Mon Sep 17 00:00:00 2001 From: hnOsmium0001 Date: Mon, 18 Apr 2022 20:18:09 -0700 Subject: Add Uid for IresObject --- source/Uid.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 source/Uid.cpp (limited to 'source/Uid.cpp') diff --git a/source/Uid.cpp b/source/Uid.cpp new file mode 100644 index 0000000..2520d1e --- /dev/null +++ b/source/Uid.cpp @@ -0,0 +1,47 @@ +#include "Uid.hpp" + +#include "RapidJsonHelper.hpp" + +#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::Read(const rapidjson::Value& value) { + assert(value.IsArray()); + 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(); +} + +void Uid::WriteInto(rapidjson::Value& value, rapidjson::Document& root) { + value.Reserve(2, root.GetAllocator()); + value.PushBack((uint64_t)upper, root.GetAllocator()); + value.PushBack((uint64_t)lower, root.GetAllocator()); +} + +rapidjson::Value Uid::Write(rapidjson::Document& root) { + rapidjson::Value result(rapidjson::kArrayType); + WriteInto(result, root); + return result; +} -- cgit v1.2.3-70-g09d2