2021-02-15 21:11:13 +01:00
|
|
|
#include "objects.hpp"
|
|
|
|
#include "globals.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <fancy.hpp>
|
|
|
|
#include <functional>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
namespace Soundux::Objects
|
|
|
|
{
|
2021-02-16 15:40:26 +01:00
|
|
|
Tab Data::addTab(Tab tab)
|
2021-02-15 21:11:13 +01:00
|
|
|
{
|
|
|
|
tab.id = tabs.size();
|
|
|
|
tabs.push_back(tab);
|
|
|
|
std::unique_lock lock(Globals::gSoundsMutex);
|
|
|
|
|
2021-02-16 00:50:27 +01:00
|
|
|
for (auto &sound : tabs.back().sounds)
|
2021-02-15 21:11:13 +01:00
|
|
|
{
|
2021-02-16 15:40:26 +01:00
|
|
|
sound.id = soundIdCounter++;
|
2021-02-15 21:11:13 +01:00
|
|
|
Globals::gSounds.insert({sound.id, sound});
|
|
|
|
}
|
2021-02-16 15:40:26 +01:00
|
|
|
|
|
|
|
return tabs.back();
|
2021-02-15 21:11:13 +01:00
|
|
|
}
|
|
|
|
void Data::removeTabById(const std::uint32_t &index)
|
|
|
|
{
|
|
|
|
std::unique_lock lock(Globals::gSoundsMutex);
|
|
|
|
if (tabs.size() > index)
|
|
|
|
{
|
|
|
|
auto &tab = tabs.at(index);
|
|
|
|
for (auto &sound : tab.sounds)
|
|
|
|
{
|
|
|
|
Globals::gSounds.erase(sound.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
tabs.erase(tabs.begin() + index);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
|
|
|
Globals::gSounds.clear();
|
2021-02-16 00:50:27 +01:00
|
|
|
for (auto &tab : tabs)
|
2021-02-15 21:11:13 +01:00
|
|
|
{
|
2021-02-16 00:50:27 +01:00
|
|
|
for (auto &sound : tab.sounds)
|
2021-02-15 21:11:13 +01:00
|
|
|
{
|
|
|
|
Globals::gSounds.insert({sound.id, sound});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2021-02-16 00:50:27 +01:00
|
|
|
std::optional<std::reference_wrapper<Sound>> Data::getSound(const std::uint32_t &id)
|
2021-02-15 21:11:13 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
} // namespace Soundux::Objects
|