diff options
Diffstat (limited to 'src/zencore/compactbinarypackage.cpp')
| -rw-r--r-- | src/zencore/compactbinarypackage.cpp | 108 |
1 files changed, 53 insertions, 55 deletions
diff --git a/src/zencore/compactbinarypackage.cpp b/src/zencore/compactbinarypackage.cpp index a4fa38a1d..ffe64f2e9 100644 --- a/src/zencore/compactbinarypackage.cpp +++ b/src/zencore/compactbinarypackage.cpp @@ -3,40 +3,56 @@ #include "zencore/compactbinarypackage.h" #include <zencore/compactbinarybuilder.h> #include <zencore/compactbinaryvalidation.h> +#include <zencore/eastlutil.h> #include <zencore/endian.h> #include <zencore/stream.h> #include <zencore/testing.h> +#include <EASTL/span.h> + namespace zen { /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -CbAttachment::CbAttachment(const CompressedBuffer& InValue, const IoHash& Hash) : CbAttachment(InValue.MakeOwned(), Hash) +CbAttachment::CbAttachment(const CbObject& InValue, const IoHash* const InHash) { -} + auto SetValue = [&](const CbObject& ValueToSet) { + if (InHash) + { + Value.emplace<CbObject>(ValueToSet); + Hash = *InHash; + } + else + { + Value.emplace<CbObject>(ValueToSet); + Hash = ValueToSet.GetHash(); + } + }; -CbAttachment::CbAttachment(const SharedBuffer& InValue) : CbAttachment(CompositeBuffer(InValue)) -{ + MemoryView View; + if (!InValue.IsOwned() || !InValue.TryGetSerializedView(View)) + { + SetValue(CbObject::Clone(InValue)); + } + else + { + SetValue(InValue); + } } -CbAttachment::CbAttachment(const SharedBuffer& InValue, const IoHash& InHash) : CbAttachment(CompositeBuffer(InValue), InHash) +CbAttachment::CbAttachment(const CompressedBuffer& InValue, const IoHash& Hash) : Hash(Hash), Value(InValue) { + ZEN_ASSERT(!std::get<CompressedBuffer>(Value).IsNull()); } -CbAttachment::CbAttachment(const CompositeBuffer& InValue) -: Hash(InValue.IsNull() ? IoHash::Zero : IoHash::HashBuffer(InValue)) -, Value(InValue) +CbAttachment::CbAttachment(CompressedBuffer&& InValue, const IoHash& InHash) : Hash(InHash), Value(std::move(InValue)) { - if (std::get<CompositeBuffer>(Value).IsNull()) - { - Value.emplace<std::nullptr_t>(); - } + ZEN_ASSERT(!std::get<CompressedBuffer>(Value).IsNull()); } CbAttachment::CbAttachment(CompositeBuffer&& InValue) : Hash(InValue.IsNull() ? IoHash::Zero : IoHash::HashBuffer(InValue)) , Value(std::move(InValue)) - { if (std::get<CompositeBuffer>(Value).IsNull()) { @@ -44,7 +60,7 @@ CbAttachment::CbAttachment(CompositeBuffer&& InValue) } } -CbAttachment::CbAttachment(CompositeBuffer&& InValue, const IoHash& InHash) : Hash(InHash), Value(InValue) +CbAttachment::CbAttachment(CompositeBuffer&& InValue, const IoHash& InHash) : Hash(InHash), Value(std::move(InValue)) { if (std::get<CompositeBuffer>(Value).IsNull()) { @@ -52,40 +68,6 @@ CbAttachment::CbAttachment(CompositeBuffer&& InValue, const IoHash& InHash) : Ha } } -CbAttachment::CbAttachment(CompressedBuffer&& InValue, const IoHash& InHash) : Hash(InHash), Value(InValue) -{ - if (std::get<CompressedBuffer>(Value).IsNull()) - { - Value.emplace<std::nullptr_t>(); - } -} - -CbAttachment::CbAttachment(const CbObject& InValue, const IoHash* const InHash) -{ - auto SetValue = [&](const CbObject& ValueToSet) { - if (InHash) - { - Value.emplace<CbObject>(ValueToSet); - Hash = *InHash; - } - else - { - Value.emplace<CbObject>(ValueToSet); - Hash = ValueToSet.GetHash(); - } - }; - - MemoryView View; - if (!InValue.IsOwned() || !InValue.TryGetSerializedView(View)) - { - SetValue(CbObject::Clone(InValue)); - } - else - { - SetValue(InValue); - } -} - bool CbAttachment::TryLoad(IoBuffer& InBuffer, BufferAllocator Allocator) { @@ -186,7 +168,7 @@ TryLoad_ArchiveFieldIntoAttachment(CbAttachment& TargetAttachment, CbField&& Fie { return false; } - TargetAttachment = CbAttachment(CompositeBuffer(Buffer), BinaryAttachmentHash); + TargetAttachment = CbAttachment(std::move(Buffer), BinaryAttachmentHash); } else if (SharedBuffer Buffer = Field.AsBinary(); !Field.HasError()) { @@ -201,7 +183,7 @@ TryLoad_ArchiveFieldIntoAttachment(CbAttachment& TargetAttachment, CbField&& Fie else { // Is an uncompressed empty binary blob - TargetAttachment = CbAttachment(CompositeBuffer(Buffer), IoHash::HashBuffer(nullptr, 0)); + TargetAttachment = CbAttachment(std::move(Buffer), IoHash::HashBuffer(nullptr, 0)); } } else @@ -282,7 +264,7 @@ CbAttachment::GetHash() const return Hash; } -CompositeBuffer +const CompositeBuffer& CbAttachment::AsCompositeBinary() const { if (const CompositeBuffer* BinValue = std::get_if<CompositeBuffer>(&Value)) @@ -304,7 +286,7 @@ CbAttachment::AsBinary() const return {}; } -CompressedBuffer +const CompressedBuffer& CbAttachment::AsCompressedBinary() const { if (const CompressedBuffer* CompValue = std::get_if<CompressedBuffer>(&Value)) @@ -329,6 +311,11 @@ CbAttachment::AsObject() const /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +CbPackage::CbPackage() +{ + Attachments.reserve(16); +} + void CbPackage::SetObject(CbObject InObject, const IoHash* InObjectHash, AttachmentResolver* InResolver) { @@ -357,6 +344,12 @@ CbPackage::SetObject(CbObject InObject, const IoHash* InObjectHash, AttachmentRe } void +CbPackage::ReserveAttachments(size_t Count) +{ + Attachments.reserve(Count); +} + +void CbPackage::AddAttachment(const CbAttachment& Attachment, AttachmentResolver* Resolver) { if (!Attachment.IsNull()) @@ -386,17 +379,22 @@ CbPackage::AddAttachments(std::span<const CbAttachment> InAttachments) { return; } + for (const CbAttachment& Attachment : InAttachments) + { + ZEN_ASSERT(!Attachment.IsNull()); + } + // Assume we have no duplicates! Attachments.insert(Attachments.end(), InAttachments.begin(), InAttachments.end()); std::sort(Attachments.begin(), Attachments.end()); - ZEN_ASSERT_SLOW(std::unique(Attachments.begin(), Attachments.end()) == Attachments.end()); + ZEN_ASSERT_SLOW(eastl::unique(Attachments.begin(), Attachments.end()) == Attachments.end()); } int32_t CbPackage::RemoveAttachment(const IoHash& Hash) { return gsl::narrow_cast<int32_t>( - std::erase_if(Attachments, [&Hash](const CbAttachment& Attachment) -> bool { return Attachment.GetHash() == Hash; })); + erase_if(Attachments, [&Hash](const CbAttachment& Attachment) -> bool { return Attachment.GetHash() == Hash; })); } bool @@ -741,7 +739,7 @@ namespace legacy { } else { - Package.AddAttachment(CbAttachment(CompositeBuffer(std::move(Buffer)), Hash)); + Package.AddAttachment(CbAttachment(std::move(Buffer), Hash)); } } } |