From 6a4b1c5186b31d37d8f04ca41b007a3e451eb98a Mon Sep 17 00:00:00 2001 From: Nicolas Pope <nwpope@utu.fi> Date: Sun, 7 Jun 2020 21:35:22 +0300 Subject: [PATCH] Implement swaps --- .../structures/include/ftl/data/new_frame.hpp | 19 +++++++++- components/structures/src/new_frame.cpp | 35 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/components/structures/include/ftl/data/new_frame.hpp b/components/structures/include/ftl/data/new_frame.hpp index 68960b975..11cc40864 100644 --- a/components/structures/include/ftl/data/new_frame.hpp +++ b/components/structures/include/ftl/data/new_frame.hpp @@ -23,6 +23,21 @@ class Frame { explicit Frame(Frame *parent) : parent_(parent) {}; ~Frame() { flush(); }; + Frame(Frame &&f) { + f.swapTo(*this); + f.reset(); + } + + Frame &operator=(Frame &&f) { + f.swapTo(*this); + f.reset(); + return *this; + } + + // Prevent frame copy, instead use a move. + Frame(const Frame &)=delete; + Frame &operator=(const Frame &)=delete; + inline bool has(ftl::codecs::Channel c) { return data_.find(c) != data_.end() || (parent_ && parent_->has(c)); } @@ -79,7 +94,9 @@ class Frame { void merge(Frame &); - void swap(Frame &); + void swapTo(Frame &); + + void swapChanged(Frame &); void swapChannel(ftl::codecs::Channel, Frame &); diff --git a/components/structures/src/new_frame.cpp b/components/structures/src/new_frame.cpp index 52f40ad7e..9d030ed8c 100644 --- a/components/structures/src/new_frame.cpp +++ b/components/structures/src/new_frame.cpp @@ -30,3 +30,38 @@ bool Frame::flush() { changed_.clear(); return true; } + +void Frame::merge(Frame &f) { + for (auto x : f) { + data_[x.first] = std::move(x.second); + touch(x.first); + } +} + +void Frame::swapTo(Frame &f) { + for (auto x : *this) { + f.data_[x.first].swap(x.second); + f.changed_.emplace(x.first); + changed_.emplace(x.first); + } +} + +void Frame::swapChanged(Frame &f) { + for (auto x : changed_) { + f.data_[x].swap(data_[x]); + f.changed_.emplace(x); + } +} + +void Frame::swapChannel(ftl::codecs::Channel c, Frame &f) { + if (has(c)) { + f.data_[c].swap(data_[c]); + f.changed_.emplace(c); + changed_.emplace(c); + } +} + +void Frame::clear() { + changed_.clear(); + data_.clear(); +} -- GitLab