##// END OF EJS Templates
commit
commit

File last commit:

r684:90684a2a46fa
r684:90684a2a46fa
Show More
VariableSynchronizer.cpp
104 lines | 3.2 KiB | text/x-c | CppLexer
/ core / src / Variable / VariableSynchronizer.cpp
#include "Variable/VariableSynchronizer.h"
#include "Variable/Variable.h"
Q_LOGGING_CATEGORY(LOG_VariableSynchronizer, "VariableSynchronizer")
namespace {
using GroupId = VariableSynchronizer::GroupId;
struct Group {
GroupId m_Id;
std::set<std::shared_ptr<Variable> > m_Variables;
};
} // namespace
struct VariableSynchronizer::VariableSynchronizerPrivate {
std::map<GroupId, Group> m_Groups;
std::map<std::shared_ptr<Variable>, Group *> m_GroupsIndex;
};
VariableSynchronizer::VariableSynchronizer(QObject *parent)
: QObject{parent}, impl{spimpl::make_unique_impl<VariableSynchronizerPrivate>()}
{
}
void VariableSynchronizer::addGroup(GroupId groupId) noexcept
{
if (impl->m_Groups.count(groupId) == 1) {
qCWarning(LOG_VariableSynchronizer())
<< tr("Can't create new synchronization group: a "
"group already exists under the passed identifier");
return;
}
impl->m_Groups.insert(std::make_pair(groupId, Group{}));
}
void VariableSynchronizer::addVariable(std::shared_ptr<Variable> variable, GroupId groupId) noexcept
{
if (impl->m_GroupsIndex.count(variable) == 1) {
qCWarning(LOG_VariableSynchronizer())
<< tr("Can't add variable to a new synchronization group: the variable is already in a "
"synchronization group");
return;
}
auto groupIt = impl->m_Groups.find(groupId);
if (groupIt == impl->m_Groups.end()) {
qCWarning(LOG_VariableSynchronizer())
<< tr("Can't add variable to the synchronization group: no group exists under the "
"passed identifier");
return;
}
// Registers variable
groupIt->second.m_Variables.insert(variable);
// Creates index for variable
impl->m_GroupsIndex.insert(std::make_pair(variable, &groupIt->second));
}
void VariableSynchronizer::removeGroup(GroupId groupId) noexcept
{
auto groupIt = impl->m_Groups.find(groupId);
if (groupIt == impl->m_Groups.end()) {
qCWarning(LOG_VariableSynchronizer()) << tr(
"Can't remove synchronization group: no group exists under the passed identifier");
return;
}
// Removes indexes
for (const auto &variable : groupIt->second.m_Variables) {
impl->m_GroupsIndex.erase(variable);
}
// Removes group
impl->m_Groups.erase(groupIt);
}
void VariableSynchronizer::removeVariable(std::shared_ptr<Variable> variable) noexcept
{
// Finds group in which the variable is
auto variableGroupIt = impl->m_GroupsIndex.find(variable);
if (variableGroupIt != impl->m_GroupsIndex.end()) {
auto groupId = variableGroupIt->second->m_Id;
// Removes variable index
impl->m_GroupsIndex.erase(variableGroupIt);
// Removes variable from group
impl->m_Groups[groupId].m_Variables.erase(variable);
}
}
std::set<std::shared_ptr<Variable> >
VariableSynchronizer::synchronizedVariables(std::shared_ptr<Variable> variable) const noexcept
{
auto groupIndexIt = impl->m_GroupsIndex.find(variable);
return groupIndexIt != impl->m_GroupsIndex.end() ? groupIndexIt->second->m_Variables
: std::set<std::shared_ptr<Variable> >{};
}