File indexing completed on 2025-01-12 06:47:28
0001 // 0002 // C++ Implementation: cMacroManager, cMacro, cFunction 0003 // 0004 /* 0005 Copyright 2005-2011 Tomas Mecir <kmuddy@kmuddy.com> 0006 0007 This program is free software; you can redistribute it and/or 0008 modify it under the terms of the GNU General Public License as 0009 published by the Free Software Foundation; either version 2 of 0010 the License, or (at your option) any later version. 0011 0012 This program is distributed in the hope that it will be useful, 0013 but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0015 GNU General Public License for more details. 0016 0017 You should have received a copy of the GNU General Public License 0018 along with this program. If not, see <http://www.gnu.org/licenses/>. 0019 */ 0020 0021 #include "cmacromanager.h" 0022 #include "cactionmanager.h" 0023 #include "cvariablelist.h" 0024 0025 #include <map> 0026 0027 using namespace std; 0028 0029 cMacroManager *cMacroManager::_self = nullptr; 0030 0031 struct cMacroManagerPrivate { 0032 map<QString, cMacro *> macros; 0033 map<QString, cFunction *> functions; 0034 }; 0035 0036 cMacroManager * cMacroManager::self() 0037 { 0038 if (!_self) 0039 _self = new cMacroManager; 0040 return _self; 0041 } 0042 0043 cMacroManager::cMacroManager () 0044 : cActionBase ("macros", 0) 0045 { 0046 d = new cMacroManagerPrivate; 0047 } 0048 0049 cMacroManager::~cMacroManager () 0050 { 0051 delete d; 0052 _self = nullptr; 0053 } 0054 0055 void cMacroManager::addMacro (const QString &name, cMacro *macro) 0056 { 0057 if (d->macros.count (name)) return; 0058 if (!macro) return; 0059 d->macros[name] = macro; 0060 } 0061 0062 void cMacroManager::removeMacro (const QString &name) 0063 { 0064 d->macros.erase (name); 0065 } 0066 0067 cMacro *cMacroManager::macro (const QString &name) 0068 { 0069 if (!d->macros.count (name)) return nullptr; 0070 return d->macros[name]; 0071 } 0072 0073 bool cMacroManager::callMacro (const QString &name, const QString ¶ms, 0074 int sess, cCmdQueue *queue) 0075 { 0076 if (!d->macros.count (name)) return false; 0077 d->macros[name]->eval (params, sess, queue); 0078 return true; 0079 } 0080 0081 void cMacroManager::addFunction (const QString &name, cFunction *function) 0082 { 0083 if (d->functions.count (name)) return; 0084 if (!function) return; 0085 d->functions[name] = function; 0086 } 0087 0088 void cMacroManager::removeFunction (const QString &name) 0089 { 0090 d->functions.erase (name); 0091 } 0092 0093 bool cMacroManager::functionExists (const QString &name) 0094 { 0095 return (d->functions.count (name) != 0); 0096 } 0097 0098 cValue cMacroManager::callFunction (const QString &name, std::list<cValue> ¶ms, 0099 int sess, cCmdQueue *queue) 0100 { 0101 cValue empty; 0102 if (!functionExists (name)) return empty; 0103 return d->functions[name]->eval (params, sess, queue); 0104 } 0105 0106 /************************ cMacro ***************************/ 0107 0108 cMacro::cMacro (const QString &name) 0109 { 0110 n = name; 0111 cMacroManager::self()->addMacro (n, this); 0112 am = cActionManager::self(); 0113 } 0114 0115 cMacro::~cMacro () 0116 { 0117 cMacroManager::self()->removeMacro (n); 0118 } 0119 0120 cVariableList *cMacro::varList (int sess) 0121 { 0122 return dynamic_cast<cVariableList *>(am->object ("variables", sess)); 0123 } 0124 0125 QString cMacro::expandVariables (const QString &str, int sess, cCmdQueue *queue) { 0126 return varList(sess)->expandVariables (str, true, queue); 0127 } 0128 0129 /************************ cFunction ***************************/ 0130 0131 cFunction::cFunction (const QString &name) 0132 { 0133 n = name; 0134 cMacroManager::self()->addFunction (n, this); 0135 am = cActionManager::self(); 0136 } 0137 0138 cFunction::~cFunction () 0139 { 0140 cMacroManager::self()->removeFunction (n); 0141 } 0142 0143 cVariableList *cFunction::varList (int sess) 0144 { 0145 return dynamic_cast<cVariableList *>(am->object ("variables", sess)); 0146 } 0147