summaryrefslogtreecommitdiff
path: root/lib/persistence.h
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-02-21 00:03:08 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2023-02-21 00:36:06 +0000
commitbf0a1a219e48007ca51e23ea45d08d295e398fcd (patch)
tree0c85f949ca0feaa346290169875928b3f7e7dad1 /lib/persistence.h
parentAccept a CSV for glm::vec data (diff)
downloadilt-bf0a1a219e48007ca51e23ea45d08d295e398fcd.tar.bz2
ilt-bf0a1a219e48007ca51e23ea45d08d295e398fcd.tar.xz
ilt-bf0a1a219e48007ca51e23ea45d08d295e398fcd.zip
Add helper Selection to insert into a map based on a member value as key
Diffstat (limited to 'lib/persistence.h')
-rw-r--r--lib/persistence.h17
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/persistence.h b/lib/persistence.h
index 01b2a7e..d55062a 100644
--- a/lib/persistence.h
+++ b/lib/persistence.h
@@ -256,6 +256,23 @@ namespace Persistence {
}
};
+ template<typename Map, typename Type = typename Map::mapped_type, auto Key = &Type::id>
+ struct MapByMember : public Persistence::SelectionT<std::shared_ptr<Type>> {
+ MapByMember(Map & m) : Persistence::SelectionT<std::shared_ptr<Type>> {s}, map {m} { }
+
+ using Persistence::SelectionT<std::shared_ptr<Type>>::SelectionT;
+ void
+ endObject(Persistence::Stack & stk) override
+ {
+ map.emplace(std::invoke(Key, s), s);
+ stk.pop();
+ }
+
+ private:
+ std::shared_ptr<Type> s;
+ Map & map;
+ };
+
struct Persistable {
Persistable() = default;
virtual ~Persistable() = default;