Soundux/src/core/global/objects.cpp

204 lines
5.7 KiB
C++
Raw Normal View History

#include "objects.hpp"
#include "globals.hpp"
#include <algorithm>
#include <fancy.hpp>
#include <functional>
#include <optional>
namespace Soundux::Objects
{
Tab Data::addTab(Tab tab)
{
tab.id = tabs.size();
tabs.emplace_back(tab);
std::unique_lock lock(Globals::gSoundsMutex);
std::unique_lock favLock(Globals::gFavoritesMutex);
for (auto &sound : tabs.back().sounds)
{
Globals::gSounds.insert({sound.id, sound});
if (sound.isFavorite)
2021-03-09 20:38:09 +01:00
{
Globals::gFavorites.insert({sound.id, sound});
2021-03-09 20:38:09 +01:00
}
}
return tabs.back();
}
void Data::removeTabById(const std::uint32_t &index)
{
std::unique_lock lock(Globals::gSoundsMutex);
std::unique_lock favLock(Globals::gFavoritesMutex);
if (tabs.size() > index)
{
auto &tab = tabs.at(index);
for (auto &sound : tab.sounds)
{
Globals::gSounds.erase(sound.id);
if (sound.isFavorite)
2021-03-09 20:38:09 +01:00
{
Globals::gFavorites.erase(sound.id);
2021-03-09 20:38:09 +01:00
}
}
tabs.erase(tabs.begin() + index);
2021-02-19 14:08:16 +01:00
2021-03-04 10:36:10 +01:00
for (std::size_t i = 0; tabs.size() > i; i++)
2021-02-19 14:08:16 +01:00
{
tabs.at(i).id = i;
}
}
else
{
Fancy::fancy.logTime().warning() << "Tried to remove non existant tab" << std::endl;
}
}
void Data::setTabs(const std::vector<Tab> &newTabs)
{
tabs = newTabs;
std::unique_lock lock(Globals::gSoundsMutex);
std::unique_lock favLock(Globals::gFavoritesMutex);
Globals::gSounds.clear();
Globals::gFavorites.clear();
2021-03-04 10:36:10 +01:00
for (std::size_t i = 0; tabs.size() > i; i++)
{
2021-02-19 14:08:16 +01:00
auto &tab = tabs.at(i);
tab.id = i;
for (auto &sound : tab.sounds)
{
Globals::gSounds.insert({sound.id, sound});
if (sound.isFavorite)
2021-03-09 20:38:09 +01:00
{
Globals::gFavorites.insert({sound.id, sound});
2021-03-09 20:38:09 +01:00
}
}
}
}
std::vector<Tab> Data::getTabs() const
{
return tabs;
}
2021-02-16 23:21:23 +01:00
std::optional<Tab> Data::getTab(const std::uint32_t &id) const
{
if (tabs.size() > id)
{
return tabs.at(id);
}
Fancy::fancy.logTime().warning() << "Tried to access non existant tab " << id << std::endl;
return std::nullopt;
}
std::optional<std::reference_wrapper<Sound>> Data::getSound(const std::uint32_t &id)
{
std::shared_lock lock(Globals::gSoundsMutex);
if (Globals::gSounds.find(id) != Globals::gSounds.end())
{
return Globals::gSounds.at(id);
}
Fancy::fancy.logTime().warning() << "Tried to access non existant sound " << id << std::endl;
return std::nullopt;
}
2021-02-19 14:08:16 +01:00
std::optional<Tab> Data::setTab(const std::uint32_t &id, const Tab &tab)
{
if (tabs.size() > id)
{
auto &realTab = tabs.at(id);
std::unique_lock lock(Globals::gSoundsMutex);
std::unique_lock favLock(Globals::gFavoritesMutex);
2021-02-19 14:08:16 +01:00
for (const auto &sound : realTab.sounds)
{
Globals::gSounds.erase(sound.id);
if (sound.isFavorite)
2021-03-09 20:38:09 +01:00
{
Globals::gFavorites.erase(sound.id);
2021-03-09 20:38:09 +01:00
}
2021-02-19 14:08:16 +01:00
}
realTab = tab;
for (auto &sound : realTab.sounds)
{
Globals::gSounds.insert({sound.id, sound});
if (sound.isFavorite)
2021-03-09 20:38:09 +01:00
{
Globals::gFavorites.insert({sound.id, sound});
2021-03-09 20:38:09 +01:00
}
2021-02-19 14:08:16 +01:00
}
return realTab;
}
Fancy::fancy.logTime().warning() << "Tried to access non existant Tab " << id << std::endl;
return std::nullopt;
}
void Data::set(const Data &other)
{
tabs = other.tabs;
width = other.width;
height = other.height;
soundIdCounter = other.soundIdCounter;
std::unique_lock lock(Globals::gSoundsMutex);
std::unique_lock favLock(Globals::gFavoritesMutex);
Globals::gSounds.clear();
Globals::gFavorites.clear();
2021-03-04 10:36:10 +01:00
for (std::size_t i = 0; tabs.size() > i; i++)
{
auto &tab = tabs.at(i);
tab.id = i;
for (auto &sound : tab.sounds)
{
Globals::gSounds.insert({sound.id, sound});
if (sound.isFavorite)
2021-03-09 20:38:09 +01:00
{
Globals::gFavorites.insert({sound.id, sound});
2021-03-09 20:38:09 +01:00
}
}
}
}
std::optional<Sound> Data::markFavorite(const std::uint32_t &id, bool favourite)
2021-03-09 20:38:09 +01:00
{
auto sound = getSound(id);
if (!sound)
{
return std::nullopt;
}
sound->get().isFavorite = favourite;
2021-03-09 20:38:09 +01:00
if (favourite)
{
std::unique_lock lock(Globals::gFavoritesMutex);
Globals::gFavorites.insert({id, sound->get()});
2021-03-09 20:38:09 +01:00
}
else
{
std::unique_lock lock(Globals::gFavoritesMutex);
if (Globals::gFavorites.find(id) != Globals::gFavorites.end())
2021-03-09 20:38:09 +01:00
{
Globals::gFavorites.erase(id);
}
}
2021-03-09 20:38:09 +01:00
return sound;
}
std::vector<Sound> Data::getFavorites()
2021-03-09 20:38:09 +01:00
{
std::shared_lock lock(Globals::gFavoritesMutex);
2021-03-09 20:38:09 +01:00
std::vector<Sound> rtn;
rtn.reserve(Globals::gFavorites.size());
2021-03-09 20:38:09 +01:00
for (const auto &sound : Globals::gFavorites)
2021-03-09 20:38:09 +01:00
{
rtn.emplace_back(sound.second);
}
return rtn;
}
} // namespace Soundux::Objects