ArnLib  4.0.x
Active Registry Network
XStringMap.hpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2022 Michael Wiklund.
2 // All rights reserved.
3 // Contact: arnlib@wiklunden.se
4 //
5 // This file is part of the ArnLib - Active Registry Network.
6 // Parts of ArnLib depend on Qt and/or other libraries that have their own
7 // licenses. Usage of these other libraries is subject to their respective
8 // license agreements.
9 //
10 // The MIT License (MIT) Usage
11 // Permission is hereby granted, free of charge, to any person obtaining a
12 // copy of this file to deal in its contained Software without restriction,
13 // including without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to permit
15 // persons to whom the Software is furnished to do so, subject to the
16 // following conditions:
17 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software in this file.
19 //
20 // GNU Lesser General Public License Usage
21 // This file may be used under the terms of the GNU Lesser General Public
22 // License version 2.1 as published by the Free Software Foundation and
23 // appearing in the file LICENSE_LGPL.txt included in the packaging of this
24 // file. In addition, as a special exception, you may use the rights described
25 // in the Nokia Qt LGPL Exception version 1.1, included in the file
26 // LGPL_EXCEPTION.txt in this package.
27 //
28 // GNU General Public License Usage
29 // Alternatively, this file may be used under the terms of the GNU General Public
30 // License version 3.0 as published by the Free Software Foundation and appearing
31 // in the file LICENSE_GPL.txt included in the packaging of this file.
32 //
33 // Other Usage
34 // Alternatively, this file may be used in accordance with the terms and conditions
35 // contained in a signed written agreement between you and Michael Wiklund.
36 //
37 // This program is distributed in the hope that it will be useful, but WITHOUT ANY
38 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
39 // PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
40 //
41 
42 #ifndef ARN_XSTRINGMAP_HPP
43 #define ARN_XSTRINGMAP_HPP
44 
45 #include "ArnLib_global.hpp"
46 #include "MQFlagsBase.hpp"
47 #include <QVector>
48 #include <QByteArray>
49 #include <QStringList>
50 #include <QVariant>
51 
52 #define ARNXSTRINGMAP_VER "3.0"
53 
54 typedef QMultiMap<QString,QVariant> MQVariantMap;
55 
56 namespace Arn {
57 
59  Q_GADGET
60  Q_ENUMS(E)
61 public:
62  enum E {
63  None = 0x00,
64  NullTilde = 0x01,
65  RepeatLen = 0x02,
66  Frame = 0x04,
67  AnyKey = 0x08,
69  Supported = 0x0f
70  };
72 };
73 MQ_DECLARE_OPERATORS_FOR_FLAGS( XStringMapOptions)
74 
75 
76 
108 {
109 public:
111 
112  XStringMap();
114  XStringMap( const XStringMap& other);
115  explicit XStringMap( const QByteArray& xString);
116  explicit XStringMap( const QVariantMap& variantMap);
117  ~XStringMap();
119  XStringMap& operator=( const XStringMap& other);
120 
121  int size() const { return _size; }
122  void clear( bool freeMem = false);
123  void squeeze();
124  const Options& options() const;
125  void setOptions( const Options& newOptions);
126 
127  int indexOf( const char* key, int from = 0) const;
128  int indexOf( const QByteArray& key, int from = 0) const;
129  int indexOf( const QString& key, int from = 0) const;
130  int indexOfValue( const QByteArray& value, int from = 0) const;
131  int indexOfValue( const QString& value, int from = 0) const;
132  int maxEnumOf( const char* keyPrefix) const;
133 
134  XStringMap& add( const char* key, const QByteArray& val);
135  XStringMap& add( const char* key, const char* val);
136  XStringMap& add( const char* keyPrefix, uint eNum, const QByteArray& val);
137  XStringMap& add( const QByteArray& key, const QByteArray& val);
138  XStringMap& add( const char* key, const QString& val);
139  XStringMap& add( const char* keyPrefix, uint eNum, const QString& val);
140  XStringMap& add( const QByteArray& key, const QString& val);
141  XStringMap& add( const QString& key, const QString& val);
142  XStringMap& add( const XStringMap& other);
143  XStringMap& add( const QVariantMap& variantMap);
144 
145  XStringMap& addNum( const char* key, int val);
146  XStringMap& addNum( const QByteArray& key, int val);
147  XStringMap& addNum( const QString& key, int val);
148  XStringMap& addNum( const char* key, uint val);
149  XStringMap& addNum( const QByteArray& key, uint val);
150  XStringMap& addNum( const QString& key, uint val);
151  XStringMap& addNum( const char* key, double val, int precision = -1);
152  XStringMap& addNum( const QByteArray& key, double val, int precision = -1);
153  XStringMap& addNum( const QString& key, double val, int precision = -1);
154 
155  XStringMap& addValues( const QStringList& stringList);
156 
157  XStringMap& set( int i, const QByteArray& val);
158  XStringMap& set( int i, const QString& val);
159  XStringMap& set( const char* key, const QByteArray& val);
160  XStringMap& set( const char* key, const char* val);
161  XStringMap& set( const QByteArray& key, const QByteArray& val);
162  XStringMap& set( const char* key, const QString& val);
163  XStringMap& set( const QByteArray& key, const QString& val);
164  XStringMap& set( const QString& key, const QString& val);
165 
166  XStringMap& setKey( int i, const QByteArray& key);
167 
168  const QByteArray& keyRef( int i) const;
169  QByteArray key( int i, const char* def = arnNullptr) const;
170  QByteArray key( const QByteArray& value, const char* def = arnNullptr) const;
171  QByteArray key( const QString& value, const char* def = arnNullptr) const;
172  QString keyString( int i, const QString& def = QString()) const;
173  QString keyString( const QString& value, const QString& def = QString()) const;
174 
175  const QByteArray& valueRef( int i) const;
176  QByteArray value( int i, const char* def = arnNullptr) const;
177  QByteArray value( const char* key, const char* def = arnNullptr) const;
178  QByteArray value( const char* keyPrefix, uint eNum, const char* def = arnNullptr) const;
179  QByteArray value( const QByteArray& key, const char* def = arnNullptr) const;
180  QByteArray value( const QByteArray& key, const QByteArray& def) const;
181  QString valueString( int i, const QString& def = QString()) const;
182  QString valueString( const char* key, const QString& def = QString()) const;
183  QString valueString( const char* keyPrefix, uint eNum, const QString& def = QString()) const;
184  QString valueString( const QByteArray& key, const QString& def = QString()) const;
185  QString valueString( const QString& key, const QString& def = QString()) const;
186 
187  XStringMap& remove( int index);
188  XStringMap& remove( const char* key);
189  XStringMap& remove( const QByteArray& key);
190  XStringMap& remove( const QString& key);
191  XStringMap& removeValue( const QByteArray& val);
192  XStringMap& removeValue( const QString& val);
193 
194  QByteArray toXString() const;
195  QString toXStringString() const;
196  bool fromXString( const QByteArray& inXString, int size=-1);
197  bool fromXString( const QString& inXString);
198 
199  void setEmptyKeysToValue();
200  void reverseOrder();
201  QStringList keys() const;
202  QStringList values( const char* keyPrefix = arnNullptr) const;
203  MQVariantMap toVariantMap( bool useStringVal) const;
204 
205  void stringCode( QByteArray& dst, const QByteArray& src) const;
206  void stringDecode( QByteArray& dst, const QByteArray& src) const;
207 
208  inline void append( const char* key, const QByteArray& val)
209  {add( key, val);}
210  inline void append( const char* key, const char* val)
211  {add( key, val);}
212  inline void append( const char* keyPrefix, uint eNum, const QByteArray& val)
213  {add( keyPrefix, eNum, val);}
214  inline void append( const QByteArray& key, const QByteArray& val)
215  {add( key, val);}
216  inline void append( const char* key, const QString& val)
217  {add( key, val);}
218  inline void append( const char* keyPrefix, uint eNum, const QString& val)
219  {add( keyPrefix, eNum, val);}
220  inline void append( const QByteArray& key, const QString& val)
221  {add( key, val);}
222  inline void append( const QString& key, const QString& val)
223  {add( key, val);}
224  inline void append( const XStringMap& other)
225  {add( other);}
226  inline void append( const QVariantMap& other)
227  {add( other);}
228 
229  XStringMap& operator+=( const XStringMap& other);
230  XStringMap& operator+=( const QVariantMap& other);
231 
232  QByteArray info();
233 
234 private:
235  void init();
236  void checkSpace();
237 
238  QVector<QByteArray> _keyList;
239  QVector<QByteArray> _valList;
240  int _size;
241  Options _options;
242  mutable bool _hasBinCode;
243  mutable bool _hasChgCode;
244  mutable bool _hasEqChar;
245  static QByteArray _nullValue;
246 };
247 
248 } // Arn::
249 
250 #endif // ARN_XSTRINGMAP_HPP
XStringMapOptions Options
Definition: XStringMap.hpp:110
void append(const QByteArray &key, const QString &val)
Definition: XStringMap.hpp:220
QMultiMap< QString, QVariant > MQVariantMap
Definition: XStringMap.hpp:54
void append(const char *keyPrefix, uint eNum, const QByteArray &val)
Definition: XStringMap.hpp:212
Container class with string representation for serialized data.
Definition: XStringMap.hpp:107
void append(const char *key, const char *val)
Definition: XStringMap.hpp:210
#define MQ_DECLARE_FLAGS(FEStruct)
Flags.
Definition: MQFlagsBase.hpp:49
void append(const QString &key, const QString &val)
Definition: XStringMap.hpp:222
void append(const QVariantMap &other)
Definition: XStringMap.hpp:226
#define MQ_DECLARE_OPERATORS_FOR_FLAGS(FEStruct)
Definition: MQFlagsBase.hpp:65
void append(const char *key, const QByteArray &val)
Definition: XStringMap.hpp:208
Definition: Arn.cpp:43
void append(const XStringMap &other)
Definition: XStringMap.hpp:224
#define ARNLIBSHARED_EXPORT
void append(const QByteArray &key, const QByteArray &val)
Definition: XStringMap.hpp:214
int size() const
Definition: XStringMap.hpp:121
void append(const char *keyPrefix, uint eNum, const QString &val)
Definition: XStringMap.hpp:218
void append(const char *key, const QString &val)
Definition: XStringMap.hpp:216