| 1 | /* | 
|---|
| 2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) | 
|---|
| 3 | * | 
|---|
| 4 | * This program is free software: you can redistribute it and/or modify | 
|---|
| 5 | * it under the terms of the GNU General Public License as published by | 
|---|
| 6 | * the Free Software Foundation, either version 3 of the License, or | 
|---|
| 7 | * (at your option) any later version. | 
|---|
| 8 | * | 
|---|
| 9 | * This program is distributed in the hope that it will be useful, | 
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 12 | * GNU General Public License for more details. | 
|---|
| 13 | * | 
|---|
| 14 | * You should have received a copy of the GNU General Public License | 
|---|
| 15 | * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
|---|
| 16 | */ | 
|---|
| 17 | #include "statementmodel.h" | 
|---|
| 18 |  | 
|---|
| 19 | #include <QFile> | 
|---|
| 20 | #include <QTextStream> | 
|---|
| 21 |  | 
|---|
| 22 | StatementModel::StatementModel(QObject *parent) : QObject(parent) | 
|---|
| 23 | { | 
|---|
| 24 | mCount = 0; | 
|---|
| 25 | } | 
|---|
| 26 |  | 
|---|
| 27 | void StatementModel::add(const PStatement& statement) | 
|---|
| 28 | { | 
|---|
| 29 | if (!statement) { | 
|---|
| 30 | return ; | 
|---|
| 31 | } | 
|---|
| 32 | PStatement parent = statement->parentScope.lock(); | 
|---|
| 33 | if (parent) { | 
|---|
| 34 | addMember(parent->children,statement); | 
|---|
| 35 | } else { | 
|---|
| 36 | addMember(mGlobalStatements,statement); | 
|---|
| 37 | } | 
|---|
| 38 | mCount++; | 
|---|
| 39 | #ifdef QT_DEBUG | 
|---|
| 40 | mAllStatements.append(statement); | 
|---|
| 41 | #endif | 
|---|
| 42 |  | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | void StatementModel::deleteStatement(const PStatement& statement) | 
|---|
| 46 | { | 
|---|
| 47 | if (!statement) { | 
|---|
| 48 | return ; | 
|---|
| 49 | } | 
|---|
| 50 | PStatement parent = statement->parentScope.lock(); | 
|---|
| 51 | int count = 0; | 
|---|
| 52 | if (parent) { | 
|---|
| 53 | count = deleteMember(parent->children,statement); | 
|---|
| 54 | } else { | 
|---|
| 55 | count = deleteMember(mGlobalStatements,statement); | 
|---|
| 56 | } | 
|---|
| 57 | mCount -= count; | 
|---|
| 58 | #ifdef QT_DEBUG | 
|---|
| 59 | mAllStatements.removeOne(statement); | 
|---|
| 60 | #endif | 
|---|
| 61 |  | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | const StatementMap &StatementModel::childrenStatements(const PStatement& statement) const | 
|---|
| 65 | { | 
|---|
| 66 | if (!statement) { | 
|---|
| 67 | return mGlobalStatements; | 
|---|
| 68 | } else { | 
|---|
| 69 | return statement->children; | 
|---|
| 70 | } | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | const StatementMap &StatementModel::childrenStatements(std::weak_ptr<Statement> statement) const | 
|---|
| 74 | { | 
|---|
| 75 | PStatement s = statement.lock(); | 
|---|
| 76 | return childrenStatements(s); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | void StatementModel::clear() { | 
|---|
| 80 | mCount=0; | 
|---|
| 81 | mGlobalStatements.clear(); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | void StatementModel::dump(const QString &logFile) | 
|---|
| 85 | { | 
|---|
| 86 | QFile file(logFile); | 
|---|
| 87 | if (file.open(QFile::WriteOnly | QFile::Truncate)) { | 
|---|
| 88 | QTextStream out(&file); | 
|---|
| 89 | dumpStatementMap(mGlobalStatements,out,0); | 
|---|
| 90 | } | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | #ifdef QT_DEBUG | 
|---|
| 94 | void StatementModel::dumpAll(const QString &logFile) | 
|---|
| 95 | { | 
|---|
| 96 | QFile file(logFile); | 
|---|
| 97 | if (file.open(QFile::WriteOnly | QFile::Truncate)) { | 
|---|
| 98 | QTextStream out(&file); | 
|---|
| 99 | for (PStatement statement:mAllStatements) { | 
|---|
| 100 | out<<QString( "%1, %2, %3, %4, %5, %6, %7, %8, %9, %10, %11, %12") | 
|---|
| 101 | .arg(statement->command).arg(int(statement->kind)) | 
|---|
| 102 | .arg(statement->type).arg(statement->fullName) | 
|---|
| 103 | .arg((size_t)(statement->parentScope.lock().get())) | 
|---|
| 104 | .arg((int)statement->classScope) | 
|---|
| 105 | .arg(statement->fileName) | 
|---|
| 106 | .arg(statement->line) | 
|---|
| 107 | .arg(statement->definitionFileName) | 
|---|
| 108 | .arg(statement->definitionLine)<<endl; | 
|---|
| 109 | } | 
|---|
| 110 | } | 
|---|
| 111 | } | 
|---|
| 112 | #endif | 
|---|
| 113 |  | 
|---|
| 114 | void StatementModel::addMember(StatementMap &map, const PStatement& statement) | 
|---|
| 115 | { | 
|---|
| 116 | if (!statement) | 
|---|
| 117 | return ; | 
|---|
| 118 | map.insert(statement->command,statement); | 
|---|
| 119 | //    QList<PStatement> lst = map.values(statement->command); | 
|---|
| 120 | //    if (!lst) { | 
|---|
| 121 | //        lst=std::make_shared<StatementList>(); | 
|---|
| 122 | //        map.insert(,lst); | 
|---|
| 123 | //    } | 
|---|
| 124 | //    lst->append(statement); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | int StatementModel::deleteMember(StatementMap &map, const PStatement& statement) | 
|---|
| 128 | { | 
|---|
| 129 | if (!statement) | 
|---|
| 130 | return 0; | 
|---|
| 131 | return map.remove(statement->command,statement); | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | void StatementModel::dumpStatementMap(StatementMap &map, QTextStream &out, int level) | 
|---|
| 135 | { | 
|---|
| 136 | QString indent(level,'\t'); | 
|---|
| 137 | foreach (const PStatement& statement,map) { | 
|---|
| 138 | out<<indent<<QString( "%1, %2, %3, %4, %5, %6, %7, %8, %9, %10, %11, %12") | 
|---|
| 139 | .arg(statement->command).arg(int(statement->kind)) | 
|---|
| 140 | .arg(statement->type,statement->fullName) | 
|---|
| 141 | .arg(statement->noNameArgs) | 
|---|
| 142 | .arg(statement->args) | 
|---|
| 143 | .arg((size_t)(statement->parentScope.lock().get())) | 
|---|
| 144 | .arg((int)statement->classScope) | 
|---|
| 145 | .arg(statement->fileName) | 
|---|
| 146 | .arg(statement->line) | 
|---|
| 147 | .arg(statement->definitionFileName) | 
|---|
| 148 | .arg(statement->definitionLine); | 
|---|
| 149 | out | 
|---|
| 150 | #if QT_VERSION >= QT_VERSION_CHECK(5,15,0) | 
|---|
| 151 | <<Qt::endl; | 
|---|
| 152 | #else | 
|---|
| 153 | <<endl; | 
|---|
| 154 | #endif | 
|---|
| 155 | if (statement->children.isEmpty()) | 
|---|
| 156 | continue; | 
|---|
| 157 | out<<indent<<statement->command<< " {" | 
|---|
| 158 | #if QT_VERSION >= QT_VERSION_CHECK(5,15,0) | 
|---|
| 159 | <<Qt::endl; | 
|---|
| 160 | #else | 
|---|
| 161 | <<endl; | 
|---|
| 162 | #endif | 
|---|
| 163 | dumpStatementMap(statement->children,out,level+1); | 
|---|
| 164 | out<<indent<< "}" | 
|---|
| 165 | #if QT_VERSION >= QT_VERSION_CHECK(5,15,0) | 
|---|
| 166 | <<Qt::endl; | 
|---|
| 167 | #else | 
|---|
| 168 | <<endl; | 
|---|
| 169 | #endif | 
|---|
| 170 |  | 
|---|
| 171 | } | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|