qmjsonarray.h
Go to the documentation of this file.
1 //
2 // QtMark JSON Library
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Rodney Forbes <forbesr@ainfosec.com>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 #ifndef QMJSONARRAY_H
23 #define QMJSONARRAY_H
24 
25 // ============================================================================
26 // Libraries
27 // ============================================================================
28 
29 #include <qmjsonvalue.h>
30 
31 // ============================================================================
32 // Class Definition
33 // ============================================================================
34 
35 class QM_JSON_EXPORT QMJsonArray : public QObject
36 {
37  Q_OBJECT
38 
39 public:
40 
41  QMJsonArray();
42  explicit QMJsonArray(const QList<QMPointer<QMJsonValue> > &list);
43  virtual ~QMJsonArray();
44 
45  virtual void reserve(int32_t alloc);
46 
47  virtual void clear(void);
48 
49  virtual int32_t count(void) const;
50  virtual int32_t length(void) const;
51  virtual int32_t size(void) const;
52 
53  virtual bool isEmpty(void) const;
54  virtual bool empty(void) const;
55 
56  virtual bool contains(const QMPointer<QMJsonValue> &value) const;
57  virtual int32_t indexOf(const QMPointer<QMJsonValue> &value) const;
58  virtual int32_t lastIndexOf(const QMPointer<QMJsonValue> &value, int32_t from = -1) const;
59 
60  virtual bool endsWith(const QMPointer<QMJsonValue> &value) const;
61  virtual bool startsWith(const QMPointer<QMJsonValue> &value) const;
62 
63  virtual void prepend(const QMPointer<QMJsonValue> &value);
64  virtual void append(const QMPointer<QMJsonValue> &value);
65  virtual void insert(int32_t index, const QMPointer<QMJsonValue> &value);
66  template<class T> void prepend(const T &value);
67  template<class T> void append(const T &value);
68  template<class T> void insert(int32_t index, const T &value);
69 
70  virtual void unite(const QMPointer<QMJsonArray> &array, QMJsonArrayUnitePolicy policy = QMJsonArrayUnitePolicy_Append);
71 
72  virtual void removeAll(const QMPointer<QMJsonValue> &value);
73  virtual void removeOne(const QMPointer<QMJsonValue> &value);
74  virtual void removeAt(int32_t index);
75  virtual void removeFirst(void);
76  virtual void removeLast(void);
77 
78  virtual QMPointer<QMJsonValue> takeFirst(void);
79  virtual QMPointer<QMJsonValue> takeLast(void);
80  virtual QMPointer<QMJsonValue> takeAt(int32_t index);
81  virtual QMPointer<QMJsonValue> takeAt(int32_t index, const QMPointer<QMJsonValue> &defaultValue);
82  template<class T> QMPointer<QMJsonValue> takeAt(int32_t index, const T &defaultValue);
83 
84  virtual const QMPointer<QMJsonValue> &front(void) const;
85  virtual const QMPointer<QMJsonValue> &back(void) const;
86  virtual const QMPointer<QMJsonValue> &value(int32_t index) const;
87  virtual const QMPointer<QMJsonValue> &value(int32_t index, const QMPointer<QMJsonValue> &defaultValue) const;
88  template<class T> QMPointer<QMJsonValue> value(int32_t index, const T &defaultValue) const;
89 
90  virtual QList<QMPointer<QMJsonValue> > values(void) const;
91  virtual QList<QMPointer<QMJsonValue> > mid(int32_t pos, int32_t length = -1) const;
92 
93  virtual void move(int32_t from, int32_t to);
94  virtual void replace(int32_t index, const QMPointer<QMJsonValue> &value);
95  template<class T> void replace(int32_t index, const T &value);
96 
97  virtual bool isNull(int32_t index) const;
98  virtual bool isBool(int32_t index) const;
99  virtual bool isDouble(int32_t index) const;
100  virtual bool isString(int32_t index) const;
101  virtual bool isArray(int32_t index) const;
102  virtual bool isObject(int32_t index) const;
103  template<class T> bool is(int32_t index) const;
104 
105  virtual bool toBool(int32_t index) const;
106  virtual double toDouble(int32_t index) const;
107  virtual QString toString(int32_t index) const;
108  virtual const QMPointer<QMJsonArray> &toArray(int32_t index) const;
109  virtual const QMPointer<QMJsonObject> &toObject(int32_t index) const;
110 
111  virtual bool toBool(int32_t index, bool defaultValue) const;
112  virtual double toDouble(int32_t index, double defaultValue) const;
113  virtual const QString &toString(int32_t index, const QString &defaultValue) const;
114  virtual const QMPointer<QMJsonArray> &toArray(int32_t index, const QMPointer<QMJsonArray> &defaultValue) const;
115  virtual const QMPointer<QMJsonObject> &toObject(int32_t index, const QMPointer<QMJsonObject> &defaultValue) const;
116  template<class T> const T &to(int32_t index, const T &defaultValue) const;
117 
118  virtual bool fromBool(int32_t index, bool value);
119  virtual bool fromDouble(int32_t index, double value);
120  virtual bool fromString(int32_t index, const QString &value);
121  virtual bool fromArray(int32_t index, const QMPointer<QMJsonArray> &value);
122  virtual bool fromObject(int32_t index, const QMPointer<QMJsonObject> &value);
123  virtual bool from(int32_t index, const QMPointer<QMJsonValue> &value);
124  template <class T> bool from(int32_t index, const T &value);
125 
126 signals:
127 
128  void itemAdded(int32_t index, const QMPointer<QMJsonValue> &value);
129  void itemRemoved(int32_t index, const QMPointer<QMJsonValue> &value);
130 
131 private:
132 
133  Q_DISABLE_COPY(QMJsonArray);
134 
135  QList<QMPointer<QMJsonValue> > mList;
136 };
137 
138 QDebug QM_JSON_EXPORT operator<<(QDebug dbg, const QMJsonArray &array);
139 QDebug QM_JSON_EXPORT operator<<(QDebug dbg, const QMPointer<QMJsonArray> &value);
140 
141 // ============================================================================
142 // Class Implementation
143 // ============================================================================
144 
145 template<class T>
146 void QMJsonArray::prepend(const T &value)
147 {
148  this->prepend(QMPointer<QMJsonValue>(new QMJsonValue(value)));
149 }
150 
151 template<class T>
152 void QMJsonArray::append(const T &value)
153 {
154  this->append(QMPointer<QMJsonValue>(new QMJsonValue(value)));
155 }
156 
157 template<class T>
158 void QMJsonArray::insert(int32_t index, const T &value)
159 {
160  this->insert(index, QMPointer<QMJsonValue>(new QMJsonValue(value)));
161 }
162 
163 template<class T>
164 QMPointer<QMJsonValue> QMJsonArray::takeAt(int32_t index, const T &defaultValue)
165 {
166  if(index < 0 || index >= mList.count())
167  return QMPointer<QMJsonValue>(new QMJsonValue(defaultValue));
168 
169  auto value = mList.takeAt(index);
170 
171  emit itemRemoved(index, value);
172  return value;
173 }
174 
175 template<class T>
176 QMPointer<QMJsonValue> QMJsonArray::value(int32_t index, const T &defaultValue) const
177 {
178  if(index < 0 || index >= mList.count())
179  return QMPointer<QMJsonValue>(new QMJsonValue(defaultValue));
180 
181  return mList.at(index);
182 }
183 
184 template<class T>
185 void QMJsonArray::replace(int32_t index, const T &value)
186 {
187  this->replace(index, QMPointer<QMJsonValue>(new QMJsonValue(value)));
188 }
189 
190 template<class T>
191 bool QMJsonArray::is(int32_t index) const
192 {
193  if(index < 0 || index >= this->count())
194  return false;
195 
196  return mList.at(index)->is<T>();
197 }
198 
199 template<class T>
200 const T &QMJsonArray::to(int32_t index, const T &defaultValue) const
201 {
202  return this->value(index)->to<T>(defaultValue);
203 }
204 
205 template<class T>
206 bool QMJsonArray::from(int32_t index, const T &value)
207 {
208  return this->value(index)->from<T>(value);
209 }
210 
211 #endif // QMJSONARRAY_H
212 
virtual void append(const QMPointer< QMJsonValue > &value)
bool is(int32_t index) const
Definition: qmjsonarray.h:191
virtual QMPointer< QMJsonValue > takeAt(int32_t index)
#define QM_JSON_EXPORT
Definition: qmjsontype.h:36
virtual void replace(int32_t index, const QMPointer< QMJsonValue > &value)
void itemRemoved(int32_t index, const QMPointer< QMJsonValue > &value)
QMJsonArrayUnitePolicy
Definition: qmjsontype.h:75
virtual void prepend(const QMPointer< QMJsonValue > &value)
virtual const QMPointer< QMJsonValue > & value(int32_t index) const
virtual bool from(int32_t index, const QMPointer< QMJsonValue > &value)
virtual void insert(int32_t index, const QMPointer< QMJsonValue > &value)
virtual int32_t count(void) const
Definition: qmjsonarray.cpp:72
const T & to(int32_t index, const T &defaultValue) const
Definition: qmjsonarray.h:200
QDebug QM_JSON_EXPORT operator<<(QDebug dbg, const QMJsonArray &array)