2018-08-02 14:23:27 +02:00
|
|
|
#include "Emote.hpp"
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
bool operator==(const Emote &a, const Emote &b)
|
|
|
|
{
|
|
|
|
return std::tie(a.homePage, a.name, a.tooltip, a.images) ==
|
|
|
|
std::tie(b.homePage, b.name, b.tooltip, b.images);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Emote &a, const Emote &b)
|
|
|
|
{
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
2018-08-11 14:20:53 +02:00
|
|
|
EmotePtr cachedOrMakeEmotePtr(Emote &&emote, const EmoteMap &cache)
|
|
|
|
{
|
|
|
|
// reuse old shared_ptr if nothing changed
|
|
|
|
auto it = cache.find(emote.name);
|
2018-10-21 13:43:02 +02:00
|
|
|
if (it != cache.end() && *it->second == emote)
|
|
|
|
return it->second;
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2018-08-11 14:20:53 +02:00
|
|
|
return std::make_shared<Emote>(std::move(emote));
|
|
|
|
}
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2018-08-11 17:15:17 +02:00
|
|
|
EmotePtr cachedOrMakeEmotePtr(
|
|
|
|
Emote &&emote,
|
|
|
|
std::unordered_map<EmoteId, std::weak_ptr<const Emote>> &cache,
|
|
|
|
std::mutex &mutex, const EmoteId &id)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(mutex);
|
|
|
|
|
|
|
|
auto shared = cache[id].lock();
|
2018-10-21 13:43:02 +02:00
|
|
|
if (shared && *shared == emote)
|
|
|
|
{
|
2018-08-11 17:15:17 +02:00
|
|
|
// reuse old shared_ptr if nothing changed
|
|
|
|
return shared;
|
2018-10-21 13:43:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-11 17:15:17 +02:00
|
|
|
shared = std::make_shared<Emote>(std::move(emote));
|
|
|
|
cache[id] = shared;
|
|
|
|
return shared;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 12:07:41 +01:00
|
|
|
EmoteMap::const_iterator EmoteMap::findEmote(const QString &emoteNameHint,
|
|
|
|
const QString &emoteID) const
|
|
|
|
{
|
|
|
|
auto it = this->end();
|
|
|
|
if (!emoteNameHint.isEmpty())
|
|
|
|
{
|
|
|
|
it = this->find(EmoteName{emoteNameHint});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (it == this->end() || it->second->id.string != emoteID)
|
|
|
|
{
|
|
|
|
it = std::find_if(this->begin(), this->end(),
|
|
|
|
[emoteID](const auto entry) {
|
|
|
|
return entry.second->id.string == emoteID;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
} // namespace chatterino
|