From e7d6aafd3c9353d9e6169ca775cf1dae618238cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 17 Jun 2023 21:58:32 +0200 Subject: Storage: iterable type for record item list --- include/erebos/storage.h | 187 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 137 insertions(+), 50 deletions(-) (limited to 'include') diff --git a/include/erebos/storage.h b/include/erebos/storage.h index 3a3073d..32049db 100644 --- a/include/erebos/storage.h +++ b/include/erebos/storage.h @@ -40,8 +40,12 @@ template class Head; using std::bind; using std::call_once; using std::make_unique; +using std::monostate; using std::move; +using std::optional; +using std::shared_ptr; using std::string; +using std::variant; using std::vector; class PartialStorage @@ -222,54 +226,8 @@ template class RecordT { public: - class Item { - public: - struct UnknownType - { - std::string type; - std::string value; - }; - - struct Empty {}; - - typedef std::variant< - std::monostate, - Empty, - int, - std::string, - std::vector, - ZonedTime, - UUID, - typename S::Ref, - UnknownType> Variant; - - Item(const std::string & name): - Item(name, std::monostate()) {} - Item(const std::string & name, Variant value): - name(name), value(value) {} - template - Item(const std::string & name, const Stored & value): - Item(name, value.ref()) {} - - Item(const Item &) = default; - Item & operator=(const Item &) = delete; - - operator bool() const; - - std::optional asEmpty() const; - std::optional asInteger() const; - std::optional asText() const; - std::optional> asBinary() const; - std::optional asDate() const; - std::optional asUUID() const; - std::optional asRef() const; - std::optional asUnknown() const; - - template std::optional> as() const; - - const std::string name; - const Variant value; - }; + class Item; + class Items; private: RecordT(const std::shared_ptr> & ptr): @@ -280,10 +238,10 @@ public: RecordT(std::vector &&); std::vector encode() const; - const std::vector & items() const; + Items items() const; Item item(const std::string & name) const; Item operator[](const std::string & name) const; - std::vector items(const std::string & name) const; + Items items(const std::string & name) const; private: friend ObjectT; @@ -295,6 +253,123 @@ private: const std::shared_ptr> ptr; }; +template +class RecordT::Item +{ +public: + struct UnknownType + { + string type; + string value; + }; + + struct Empty {}; + + using Integer = int; + using Text = string; + using Binary = vector; + using Date = ZonedTime; + using UUID = erebos::UUID; + using Ref = typename S::Ref; + + using Variant = variant< + monostate, + Empty, + int, + string, + vector, + ZonedTime, + UUID, + typename S::Ref, + UnknownType>; + + Item(const string & name): + Item(name, monostate()) {} + Item(const string & name, Variant value): + name(name), value(value) {} + template + Item(const string & name, const Stored & value): + Item(name, value.ref()) {} + + Item(const Item &) = default; + Item & operator=(const Item &) = delete; + + operator bool() const; + + optional asEmpty() const; + optional asInteger() const; + optional asText() const; + optional asBinary() const; + optional asDate() const; + optional asUUID() const; + optional asRef() const; + optional asUnknown() const; + + template optional> as() const; + + const string name; + const Variant value; +}; + +template +class RecordT::Items +{ +public: + using Empty = typename Item::Empty; + using Integer = typename Item::Integer; + using Text = typename Item::Text; + using Binary = typename Item::Binary; + using Date = typename Item::Date; + using UUID = typename Item::UUID; + using Ref = typename Item::Ref; + using UnknownType = typename Item::UnknownType; + + Items(shared_ptr> items); + Items(shared_ptr> items, string filter); + + class Iterator + { + Iterator(const Items & source, size_t idx); + friend Items; + public: + using iterator_category = std::forward_iterator_tag; + using value_type = Item; + using difference_type = ssize_t; + using pointer = const Item *; + using reference = const Item &; + + Iterator(const Iterator &) = default; + ~Iterator() = default; + Iterator & operator=(const Iterator &) = default; + Iterator & operator++(); + value_type operator*() const { return (*source.items)[idx]; } + bool operator==(const Iterator & other) const { return idx == other.idx; } + bool operator!=(const Iterator & other) const { return idx != other.idx; } + + private: + const Items & source; + size_t idx; + }; + + Iterator begin() const; + Iterator end() const; + + vector asEmpty() const; + vector asInteger() const; + vector asText() const; + vector asBinary() const; + vector asDate() const; + vector asUUID() const; + vector asRef() const; + vector asUnknown() const; + + template vector> as() const; + +private: + const shared_ptr> items; + const optional filter; +}; + extern template class RecordT; extern template class RecordT; @@ -367,6 +442,18 @@ std::optional> RecordT::Item::as() const return std::nullopt; } +template +template +vector> RecordT::Items::as() const +{ + auto refs = asRef(); + vector> res; + res.reserve(refs.size()); + for (const auto & ref : refs) + res.push_back(Stored::load(ref)); + return res; +} + class Generation { public: -- cgit v1.2.3