File indexing completed on 2024-04-21 15:08:22

0001 //
0002 // C++ Interface: 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 #ifndef CMACROMANAGER_H
0022 #define CMACROMANAGER_H
0023 
0024 #include <cactionbase.h>
0025 
0026 #include <cvalue.h>
0027 #include <list>
0028 #include <kmuddy_export.h>
0029 
0030 class cActionManager;
0031 class cFunction;
0032 class cMacro;
0033 class cVariableList;
0034 class cCmdQueue;
0035 class cCmdQueueEntry;
0036 struct cMacroManagerPrivate;
0037 
0038 /**
0039 This is the macro manager. It manages and calls macros and functions.
0040 It follows the Singleton pattern.
0041 
0042 @author Tomas Mecir
0043 */
0044 class KMUDDY_EXPORT cMacroManager : public cActionBase
0045 {
0046 public:
0047   /** return an instance */
0048   static cMacroManager *self ();
0049   /** destructor */
0050   ~cMacroManager () override;
0051 
0052   void addMacro (const QString &name, cMacro *macro);
0053   void removeMacro (const QString &name);
0054   /** return the macro with this name */
0055   cMacro *macro (const QString &name);
0056   /** Call a given macro with given parameters. May optionally accept a session number, and
0057   a pointer to the current command stack. */
0058   bool callMacro (const QString &name, const QString &par, int sess = -1, cCmdQueue *queue = nullptr);
0059   void addFunction (const QString &name, cFunction *function);
0060   void removeFunction (const QString &name);
0061   /** Call a given function with given parameters. May optionally accept a session number, and
0062   a pointer to the current command stack. */
0063   cValue callFunction (const QString &name, std::list<cValue> &params, int sess = -1,
0064       cCmdQueue *queue = nullptr);
0065   bool functionExists (const QString &name);
0066 private:
0067   /** constructor */
0068   cMacroManager ();
0069   static cMacroManager *_self;
0070 
0071   cMacroManagerPrivate *d;  
0072 };
0073 
0074 /** The cMacro class represents one macro. */
0075 class KMUDDY_EXPORT cMacro {
0076  public:
0077   cMacro (const QString &name);
0078   virtual ~cMacro ();
0079   virtual void eval (const QString &params, int sess = -1, cCmdQueue *queue = nullptr) = 0;
0080   virtual bool preprocess (cCmdQueue *, cCmdQueueEntry *) { return true; };
0081  protected:
0082   QString n;
0083   // just some convenience aliases ...
0084   cActionManager *am;
0085   cVariableList *varList (int sess);
0086   QString expandVariables (const QString &str, int sess, cCmdQueue *queue);
0087 };
0088 
0089 /** The cFunction class represents one function. */
0090 class KMUDDY_EXPORT cFunction {
0091  public:
0092   cFunction (const QString &name);
0093   virtual ~cFunction ();
0094   virtual cValue eval (std::list<cValue> &params, int sess = -1, cCmdQueue *queue = nullptr) = 0;
0095  protected:
0096   QString n;
0097   // just some convenience aliases ...
0098   cActionManager *am;
0099   cVariableList *varList (int sess);
0100 };
0101 
0102 #endif