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

0001 /***************************************************************************
0002                           cvariablelist.h  -  list of variables
0003                              -------------------
0004     begin                : Po sep 8 2003
0005     copyright            : (C) 2003-2008 by Tomas Mecir
0006     email                : kmuddy@kmuddy.com
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #ifndef CVARIABLELIST_H
0019 #define CVARIABLELIST_H
0020 
0021 #include <map>
0022 
0023 #include <cactionbase.h>
0024 #include <kmuddy_export.h>
0025 
0026 class cCmdQueue;
0027 class cVariable;
0028 class cValue;
0029 /**
0030 This class represents the list of variables.
0031   *@author Tomas Mecir
0032   */
0033 
0034 class KMUDDY_EXPORT cVariableList : public cActionBase {
0035 public:
0036   cVariableList (int sess);
0037   ~cVariableList () override;
0038   QString getValue (const QString &varname);
0039   int getIntValue (const QString &varname);
0040   /** Retrieve a variable value. If queue is given, attempts to get local
0041   variable value first, if one exists. */
0042   cValue *value (const QString &varname, cCmdQueue *queue = nullptr);
0043   /** as above, but if value doesn't exist, it gets created */
0044   cValue *valueNotEmpty (const QString &varname, cCmdQueue *queue = nullptr);
0045   bool exists (const QString &varname);
0046   void set (const QString &varname, const QString &value);
0047   void set (const QString &varname, cValue *value);
0048   void unset (const QString &varname);
0049   /** increases value by delta */
0050   void inc (const QString &varname, double delta);
0051   /** decreases value by delta; value can become negative */
0052   void dec (const QString &varname, double delta);
0053   /** resource support; provides one unit of a resource */
0054   void provideResource (const QString &varname);
0055   /** resource support; requests one resource, if available */
0056   bool requestResource (const QString &varname);
0057   QStringList getList ();
0058   /** expand all variables in the string and return the result */
0059   QString expandVariables (const QString &string, bool recursive = true, cCmdQueue *queue = nullptr);
0060 
0061   void save ();
0062 private:
0063   void load ();
0064 
0065   QString doExpandVariables (const QString &string, int recursionCounter, cCmdQueue *queue);
0066   QString processVariable (const QString &varname, cCmdQueue *queue = nullptr);
0067   /** variable mapping, used to store the variables */
0068   std::map<QString, cVariable *> vars;
0069 };
0070 
0071 #endif