#pragma once #include #include #include #include #include "common/UniqueAccess.hpp" #include "messages/Emote.hpp" namespace chatterino { template class MapReplacement { public: MapReplacement(std::unordered_map &items) : oldItems_(items) { } void add(const TKey &key, const Emote &data) { this->add(key, Emote(data)); } void add(const TKey &key, Emote &&data) { auto it = this->oldItems_.find(key); if (it != this->oldItems_.end() && *it->second == data) { this->newItems_[key] = it->second; } else { this->newItems_[key] = std::make_shared(std::move(data)); } } void apply() { this->oldItems_ = std::move(this->newItems_); } private: std::unordered_map &oldItems_; std::unordered_map newItems_; }; template class EmoteCache { public: using Iterator = typename std::unordered_map::iterator; using ConstIterator = typename std::unordered_map::iterator; Iterator begin() { return this->items_.begin(); } ConstIterator begin() const { return this->items_.begin(); } Iterator end() { return this->items_.end(); } ConstIterator end() const { return this->items_.end(); } boost::optional get(const TKey &key) const { auto it = this->items_.find(key); if (it == this->items_.end()) return boost::none; else return it->second; } MapReplacement makeReplacment() { return MapReplacement(this->items_); } private: std::unordered_map items_; }; } // namespace chatterino