RedPanda-CPP/RedPandaIDE/parser/statementmodel.cpp

79 lines
1.8 KiB
C++
Raw Normal View History

2021-08-05 23:13:21 +08:00
#include "statementmodel.h"
StatementModel::StatementModel(QObject *parent) : QObject(parent)
{
2021-08-07 14:08:51 +08:00
mCount = 0;
2021-08-07 18:02:57 +08:00
}
void StatementModel::add(PStatement statement)
{
if (!statement) {
return ;
}
PStatement parent = statement->parentScope.lock();
if (parent) {
addMember(parent->children,statement);
} else {
addMember(mGlobalStatements,statement);
}
mCount++;
}
void StatementModel::deleteStatement(PStatement statement)
{
if (!statement) {
return ;
}
PStatement parent = statement->parentScope.lock();
int count = 0;
if (parent) {
count = deleteMember(parent->children,statement);
} else {
count = deleteMember(mGlobalStatements,statement);
}
mCount -= count;
2021-08-07 14:08:51 +08:00
}
2021-08-05 23:13:21 +08:00
2021-08-07 14:08:51 +08:00
const StatementMap &StatementModel::childrenStatements(PStatement statement)
{
if (!statement) {
return mGlobalStatements;
} else {
return statement->children;
}
}
const StatementMap &StatementModel::childrenStatements(std::weak_ptr<Statement> statement)
{
PStatement s = statement.lock();
return childrenStatements(s);
2021-08-05 23:13:21 +08:00
}
2021-08-07 18:02:57 +08:00
void StatementModel::clear() {
mCount=0;
mGlobalStatements.clear();
}
void StatementModel::addMember(StatementMap &map, PStatement statement)
{
if (!statement)
return ;
PStatementList lst = map.value(statement->command, PStatementList());
if (!lst) {
lst=std::make_shared<StatementList>();
map.insert(statement->command,lst);
}
lst->append(statement);
}
int StatementModel::deleteMember(StatementMap &map, PStatement statement)
{
if (!statement)
return 0;
PStatementList lst = map.value(statement->command, PStatementList());
if (!lst) {
return 0;
}
return lst->removeAll(statement);
}