File indexing completed on 2024-03-24 15:43:41

0001 //
0002 // C++ Interface: cCmdQueues
0003 //
0004 // Description: one command queue
0005 //
0006 /*
0007 Copyright 2005-2011 Tomas Mecir <kmuddy@kmuddy.com>
0008 
0009 This program is free software; you can redistribute it and/or
0010 modify it under the terms of the GNU General Public License as
0011 published by the Free Software Foundation; either version 2 of 
0012 the License, or (at your option) any later version.
0013 
0014 This program is distributed in the hope that it will be useful,
0015 but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017 GNU General Public License for more details.
0018 
0019 You should have received a copy of the GNU General Public License
0020 along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #ifndef CCMDQUEUE_H
0024 #define CCMDQUEUE_H
0025 
0026 #include <qstring.h>
0027 #include <list>
0028 #include <map>
0029 
0030 #include <kmuddy_export.h>
0031 
0032 struct KMUDDY_EXPORT cCmdQueueEntry {
0033   bool isMacro, canParse;
0034   QString command, macroName, macroParams;
0035 };
0036 
0037 class cConnPrefs;
0038 class cCmdParser;
0039 class cMacro;
0040 class cValue;
0041 class cExecStack;
0042 class cExecStackItem;
0043 class cPattern;
0044 
0045 /**
0046 One command queue. Stores, parses and executes commands. Also calls macros.
0047 All sent commands must go through this.
0048 
0049 @author Tomas Mecir
0050 */
0051 class KMUDDY_EXPORT cCmdQueue
0052 {
0053  public:
0054   cCmdQueue (int _sess);
0055   ~cCmdQueue ();
0056   
0057   /** is this queue finished ? */
0058   bool finished ();
0059   /** is this queue waiting for something ? */
0060   bool waiting ();
0061   
0062   /** Add a new command to the queue, using the given parsing and alias expansion settings.
0063       Parameter expandAliases is ignored if parsing is false. */
0064   void addCommand (const QString &command, bool parsing = true, bool expandAliases = true);
0065   
0066   /** remove all commands from the queue */
0067   void deleteAllCommands ();
0068   
0069   bool varExists (const QString &name);
0070   QString getValue (const QString &name);
0071   /** This returns a pointer to the value, which can them be manipulated. Note that this
0072   pointer should not be stored anywhere, and only manipulated from within a single function,
0073   as setting the value using setValue might cause this instance to be deleted. */
0074   cValue *value (const QString &name);
0075   void setValue (const QString &name, const QString &value);
0076   void setValue (const QString &name, const cValue &value);
0077   void delValue (const QString &name);
0078   
0079   /** delete all variables from the queue */
0080   void deleteAllVariables ();
0081   
0082   /** Fill the queue from this pattern.  */
0083   void fillFromPattern (const cPattern *p);
0084 
0085   /** the execution stack */
0086   cExecStack *execStack (const QString &name);
0087 
0088   /** add a new proprocessor macro */
0089   void addPreprocessMacro (cMacro *macro);
0090   /** remove a preprocessor command */
0091   void removePreprocessMacro (cMacro *macro);
0092   /** is this preprocess macro registered ? */
0093   bool hasPreprocessMacro (cMacro *macro);
0094  protected:
0095   /** execute the next command in this queue - protected, so only cCmdQueues can use it */
0096   void executeNext ();
0097   /** call preprocessor commands on this queue entry */
0098   bool preprocess (cCmdQueueEntry *qe);
0099   
0100   int sess;
0101   
0102   std::list<cCmdQueueEntry *> commands;
0103   /** local variables */
0104   std::map<QString, cValue *> variables;
0105   /** execution stacks */
0106   std::map<QString, cExecStack *> stacks;
0107   /** pre-process commands */
0108   std::list<cMacro *> *preproc;
0109   
0110   cCmdParser *parser;
0111 
0112   friend class cCmdQueues;
0113 };
0114 
0115 
0116 /** cExecStack class - manages the execution stack */
0117 
0118 class KMUDDY_EXPORT cExecStack {
0119  public:
0120   cExecStack ();
0121   ~cExecStack ();
0122   /** adds an item to the exec-stack */
0123   void push (const cExecStackItem &item);
0124   /** removes the top item from the exec-stack */
0125   cExecStackItem pop ();
0126   /** retrieves the top item of the exec-stack without removing it */
0127   cExecStackItem top ();
0128   /** is the stack empty ? */
0129   bool empty ();
0130   /** clear the stack */
0131   void clear ();
0132   protected:
0133    std::list<cExecStackItem> stack; 
0134 };
0135 
0136 /** cExecstackItem class - one item on the exec stack */
0137 
0138 class KMUDDY_EXPORT cExecStackItem {
0139  public:
0140   cExecStackItem ();
0141   virtual ~cExecStackItem ();
0142   /** attribute of the item */
0143   int attrib (const QString &name);
0144   /** set an attribute of the item */
0145   void setAttrib (const QString &name, int val);
0146  protected:
0147   std::map<QString, int> attribs;
0148 };
0149 
0150 
0151 #endif