From 6cf5244bc514042fe419fffe1cd26a7f5e3c778f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Thu, 16 Apr 2020 21:42:45 +0200 Subject: Remove optional from load result Makes loading of data well-defined for arbitrary object contents. Introduce zero reference and object to represent missing or mismatched parts. --- include/erebos/message.h | 6 +++--- include/erebos/storage.h | 16 +++++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) (limited to 'include/erebos') diff --git a/include/erebos/message.h b/include/erebos/message.h index 0b5e257..c94e8c3 100644 --- a/include/erebos/message.h +++ b/include/erebos/message.h @@ -14,9 +14,9 @@ class Identity; class DirectMessage { public: - const Identity & from() const; - const struct ZonedTime & time() const; - const std::string & text() const; + const std::optional & from() const; + const std::optional & time() const; + std::string text() const; private: friend class DirectMessageThread; diff --git a/include/erebos/storage.h b/include/erebos/storage.h index d5ae45a..d46c056 100644 --- a/include/erebos/storage.h +++ b/include/erebos/storage.h @@ -72,6 +72,7 @@ public: PartialStorage derivePartialStorage() const; std::optional ref(const Digest &) const; + Ref zref() const; std::optional loadObject(const Digest &) const; Ref storeObject(const Object &) const; @@ -103,6 +104,7 @@ public: explicit Digest(std::array value): value(value) {} explicit Digest(const std::string &); explicit operator std::string() const; + bool isZero() const; const std::array & arr() const { return value; } @@ -151,6 +153,7 @@ public: Ref & operator=(Ref &&) = default; static std::optional create(Storage, const Digest &); + static Ref zcreate(Storage); constexpr operator bool() const { return true; } const Object operator*() const; @@ -265,7 +268,8 @@ class ObjectT public: typedef std::variant< RecordT, - Blob> Variants; + Blob, + std::monostate> Variants; ObjectT(const ObjectT &) = default; ObjectT(Variants content): content(content) {} @@ -282,7 +286,7 @@ public: std::vector::const_iterator); static std::vector> decodeMany(const S &, const std::vector &); std::vector encode() const; - static std::optional> load(const typename S::Ref &); + static ObjectT load(const typename S::Ref &); std::optional> asRecord() const; std::optional asBlob() const; @@ -317,7 +321,7 @@ public: Stored & operator=(const Stored &) = default; Stored & operator=(Stored &&) = default; - static std::optional> load(const Ref &); + static Stored load(const Ref &); Ref store(const Storage &) const; bool operator==(const Stored & other) const @@ -354,11 +358,9 @@ Stored Storage::store(const T & val) const } template -std::optional> Stored::load(const Ref & ref) +Stored Stored::load(const Ref & ref) { - if (auto val = T::load(ref)) - return Stored(ref, std::make_shared(val.value())); - return std::nullopt; + return Stored(ref, std::make_shared(T::load(ref))); } template -- cgit v1.2.3