diff options
| author | Stefan Boberg <[email protected]> | 2021-09-02 13:01:52 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-09-02 13:01:52 +0200 |
| commit | 709c2f8afebe9151aa947ec53ab801c2e5be4746 (patch) | |
| tree | f64fb6137580d21d116563776b4708207e4c352d /zencore/include | |
| parent | Added HashMemory() function accepting CompositeBuffer argument (diff) | |
| download | zen-709c2f8afebe9151aa947ec53ab801c2e5be4746.tar.xz zen-709c2f8afebe9151aa947ec53ab801c2e5be4746.zip | |
Introduced support for compressed buffer attachments
Diffstat (limited to 'zencore/include')
| -rw-r--r-- | zencore/include/zencore/compactbinarybuilder.h | 7 | ||||
| -rw-r--r-- | zencore/include/zencore/compactbinarypackage.h | 60 | ||||
| -rw-r--r-- | zencore/include/zencore/compactbinaryvalidation.h | 4 |
3 files changed, 46 insertions, 25 deletions
diff --git a/zencore/include/zencore/compactbinarybuilder.h b/zencore/include/zencore/compactbinarybuilder.h index f2bf773bf..f7f2bbfd3 100644 --- a/zencore/include/zencore/compactbinarybuilder.h +++ b/zencore/include/zencore/compactbinarybuilder.h @@ -210,6 +210,13 @@ public: ZENCORE_API void AddBinary(IoBuffer Value); ZENCORE_API void AddBinary(SharedBuffer Value); + inline void AddBinary(std::string_view Name, const CompositeBuffer& Buffer) + { + SetName(Name); + AddBinary(Buffer); + } + ZENCORE_API void AddBinary(const CompositeBuffer& Buffer); + /** Write a string field by copying the UTF-8 value. */ inline void AddString(std::string_view Name, std::string_view Value) { diff --git a/zencore/include/zencore/compactbinarypackage.h b/zencore/include/zencore/compactbinarypackage.h index 66bace294..181bf1818 100644 --- a/zencore/include/zencore/compactbinarypackage.h +++ b/zencore/include/zencore/compactbinarypackage.h @@ -5,10 +5,12 @@ #include <zencore/zencore.h> #include <zencore/compactbinary.h> +#include <zencore/compress.h> #include <zencore/iohash.h> #include <functional> #include <span> +#include <variant> namespace zen { @@ -36,16 +38,20 @@ public: CbAttachment() = default; /** Construct a compact binary attachment. Value is cloned if not owned. */ - inline explicit CbAttachment(CbObject Value) : CbAttachment(std::move(Value), nullptr) {} + inline explicit CbAttachment(const CbObject& Value) : CbAttachment(Value, nullptr) {} /** Construct a compact binary attachment. Value is cloned if not owned. Hash must match Value. */ - inline explicit CbAttachment(CbObject Value, const IoHash& Hash) : CbAttachment(std::move(Value), &Hash) {} + inline explicit CbAttachment(const CbObject& Value, const IoHash& Hash) : CbAttachment(Value, &Hash) {} /** Construct a binary attachment. Value is cloned if not owned. */ - inline explicit CbAttachment(SharedBuffer Value) : CbAttachment(std::move(Value), nullptr) {} + ZENCORE_API explicit CbAttachment(const SharedBuffer& Value); /** Construct a binary attachment. Value is cloned if not owned. Hash must match Value. */ - inline explicit CbAttachment(SharedBuffer Value, const IoHash& Hash) : CbAttachment(std::move(Value), &Hash) {} + ZENCORE_API explicit CbAttachment(const SharedBuffer& Value, const IoHash& Hash); + + /** Construct a binary attachment. Value is cloned if not owned. */ + ZENCORE_API explicit CbAttachment(const CompressedBuffer& InValue); + ZENCORE_API explicit CbAttachment(CompressedBuffer&& InValue); /** Reset this to a null attachment. */ inline void Reset() { *this = CbAttachment(); } @@ -54,27 +60,30 @@ public: inline explicit operator bool() const { return !IsNull(); } /** Whether the attachment has a value. */ - inline bool IsNull() const { return !Buffer; } + ZENCORE_API [[nodiscard]] bool IsNull() const; /** Access the attachment as binary. Defaults to a null buffer on error. */ - inline SharedBuffer AsBinary() const { return Buffer; } + ZENCORE_API [[nodiscard]] SharedBuffer AsBinary() const; + + /** Access the attachment as compressed binary. Defaults to a null buffer if the attachment is null. */ + ZENCORE_API [[nodiscard]] CompressedBuffer AsCompressedBinary() const; /** Access the attachment as compact binary. Defaults to a field iterator with no value on error. */ - inline CbObject AsObject() const { return Object ? CbObject(CbFieldView(Object).AsObjectView(), Buffer) : CbObject{}; } + ZENCORE_API [[nodiscard]] CbObject AsObject() const; - /** Returns whether the attachment is binary or compact binary. */ - inline bool IsBinary() const { return !Buffer.IsNull(); } + /** Returns true if the attachment is either binary or an object */ + inline [[nodiscard]] bool IsBinary() const { return !IsNull(); } - /** Returns whether the attachment is compact binary. */ - inline bool IsObject() const { return Object.IsObject(); } + /** Returns whether the attachment is an object. */ + ZENCORE_API [[nodiscard]] bool IsObject() const; /** Returns the hash of the attachment value. */ - inline const IoHash& GetHash() const { return Hash; } + ZENCORE_API [[nodiscard]] IoHash GetHash() const; /** Compares attachments by their hash. Any discrepancy in type must be handled externally. */ - inline bool operator==(const CbAttachment& Attachment) const { return Hash == Attachment.Hash; } - inline bool operator!=(const CbAttachment& Attachment) const { return Hash != Attachment.Hash; } - inline bool operator<(const CbAttachment& Attachment) const { return Hash < Attachment.Hash; } + inline bool operator==(const CbAttachment& Attachment) const { return GetHash() == Attachment.GetHash(); } + inline bool operator!=(const CbAttachment& Attachment) const { return GetHash() != Attachment.GetHash(); } + inline bool operator<(const CbAttachment& Attachment) const { return GetHash() < Attachment.GetHash(); } /** * Load the attachment from compact binary as written by Save. @@ -102,15 +111,18 @@ public: ZENCORE_API void Save(BinaryWriter& Writer) const; private: - ZENCORE_API CbAttachment(CbObject Value, const IoHash* Hash); - ZENCORE_API CbAttachment(SharedBuffer Value, const IoHash* Hash); - - /** An owned buffer containing the binary or compact binary data. */ - SharedBuffer Buffer; - /** A field iterator that is valid only for compact binary attachments. */ - CbFieldView Object; - /** A hash of the attachment value. */ - IoHash Hash; + ZENCORE_API CbAttachment(const CbObject& Value, const IoHash* Hash); + + struct CbObjectValue + { + CbObject Object; + IoHash Hash; + + CbObjectValue(const CbObject& InObject, const IoHash& InHash) : Object(InObject), Hash(InHash) {} + CbObjectValue(CbObject&& InObject, const IoHash& InHash) : Object(std::move(InObject)), Hash(InHash) {} + }; + + std::variant<CompressedBuffer, CbObjectValue> Value; }; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/zencore/include/zencore/compactbinaryvalidation.h b/zencore/include/zencore/compactbinaryvalidation.h index 656eb3d96..9799c594a 100644 --- a/zencore/include/zencore/compactbinaryvalidation.h +++ b/zencore/include/zencore/compactbinaryvalidation.h @@ -62,8 +62,10 @@ enum class CbValidateMode : uint32_t */ Package = 1 << 4, + PackageHash = 1 << 5, + /** Perform all validation described above. */ - All = Default | Names | Format | Padding | Package, + All = Default | Names | Format | Padding | Package | PackageHash, }; ENUM_CLASS_FLAGS(CbValidateMode); |