File indexing completed on 2025-01-19 06:54:52
0001 // 0002 // C++ Interface: cValue, cValueList 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 CVALUE_H 0022 #define CVALUE_H 0023 0024 class cValueData; 0025 class QXmlStreamWriter; 0026 class QXmlStreamReader; 0027 0028 #include <qstring.h> 0029 #include <kmuddy_export.h> 0030 0031 /** 0032 Class cValue holds one value, used in variables and in function evaluator. 0033 0034 @author Tomas Mecir 0035 */ 0036 class KMUDDY_EXPORT cValue { 0037 public: 0038 /** constructor */ 0039 cValue (); 0040 cValue (const cValue &val); 0041 cValue (const QString &val); 0042 cValue (int val); 0043 cValue (double val); 0044 cValue (bool val); 0045 0046 /** destructor */ 0047 ~cValue (); 0048 0049 /** Load data into a variable. Returns variable name, if given. */ 0050 QString load (QXmlStreamReader *reader); 0051 void save (QXmlStreamWriter *writer, const QString &name = QString()); 0052 0053 /** assignment operator */ 0054 cValue &operator= (const cValue &a); 0055 0056 /** set value to nothing */ 0057 void setValue (); 0058 /** set value to a string */ 0059 void setValue (const QString &val); 0060 /** set value to an integer */ 0061 void setValue (int val); 0062 /** set value to a number */ 0063 void setValue (double val); 0064 /** set value to a copy of a given value */ 0065 void setValue (const cValue &val); 0066 void setAsMarker (); 0067 /** set one particular item in an array */ 0068 void setItem (int index, const QString &value); 0069 /** remove an item from an array */ 0070 void removeItem (int index); 0071 /** add an item to the list */ 0072 void addToList (const QString &item); 0073 /** remove an item from the list */ 0074 void removeFromList (const QString &item); 0075 /** does the list contain a given string ? */ 0076 bool listContains (const QString &item); 0077 0078 QString asString () const; 0079 int asInteger () const; 0080 double asDouble () const; 0081 /** Return array item with the given index, if any. Arrays only. */ 0082 QString item (int index) const; 0083 /** Does the list contain a given value ? Lists only.*/ 0084 bool contains (const QString &item) const; 0085 /** Number of entries in a list/array. */ 0086 int size () const; 0087 0088 /** join a list into a string, using the given separator */ 0089 QString listJoin (const QString &sep) const; 0090 /** convert a string to a list, using the given separator */ 0091 cValue toList (const QString &sep) const; 0092 0093 bool isEmpty() const; 0094 bool isString() const; 0095 bool isInteger() const; 0096 bool isDouble() const; 0097 bool isArray() const; 0098 bool isList() const; 0099 bool isMarker() const; 0100 0101 /// some operators ... 0102 cValue operator[] (int index) const; 0103 0104 static cValue empty () { return _empty; }; 0105 protected: 0106 0107 /** detach from current value, deleting it if no one else uses it */ 0108 void detachValue (); 0109 /** ensure that no one else uses our value */ 0110 void unique (); 0111 0112 cValueData *d; 0113 0114 static cValue _empty; 0115 }; 0116 0117 /// some external operators ... 0118 cValue operator+ (const cValue &a, const cValue &b); 0119 cValue operator- (const cValue &a, const cValue &b); 0120 cValue operator* (const cValue &a, const cValue &b); 0121 cValue operator/ (const cValue &a, const cValue &b); 0122 0123 0124 #endif