1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
/** @file
* IPRT - C++ Representational State Transfer (REST) Array Template Class.
*/
/*
* Copyright (C) 2008-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
#ifndef IPRT_INCLUDED_cpp_restarray_h
#define IPRT_INCLUDED_cpp_restarray_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif
#include <iprt/cpp/restbase.h>
/** @defgroup grp_rt_cpp_restarray C++ Representational State Transfer (REST) Array Template Class.
* @ingroup grp_rt_cpp
* @{
*/
/**
* Abstract base class for the RTCRestArray template.
*/
class RT_DECL_CLASS RTCRestArrayBase : public RTCRestObjectBase
{
public:
/** Default destructor. */
RTCRestArrayBase() RT_NOEXCEPT;
/** Destructor. */
virtual ~RTCRestArrayBase();
/* Overridden methods: */
virtual RTCRestObjectBase *baseClone() const RT_NOEXCEPT RT_OVERRIDE;
virtual int resetToDefault() RT_NOEXCEPT RT_OVERRIDE;
virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT RT_OVERRIDE;
virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT RT_OVERRIDE;
virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = kCollectionFormat_Unspecified) const RT_NOEXCEPT RT_OVERRIDE;
virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_NOEXCEPT RT_OVERRIDE;
virtual kTypeClass typeClass(void) const RT_NOEXCEPT RT_OVERRIDE;
virtual const char *typeName(void) const RT_NOEXCEPT RT_OVERRIDE;
/**
* Clear the content of the map.
*/
void clear() RT_NOEXCEPT;
/**
* Check if an list contains any items.
*
* @return True if there is more than zero items, false otherwise.
*/
inline bool isEmpty() const RT_NOEXCEPT
{
return m_cElements == 0;
}
/**
* Gets the number of entries in the map.
*/
inline size_t size() const RT_NOEXCEPT
{
return m_cElements;
}
/**
* Returns the base object pointer at a given index.
*
* @returns The base object at @a a_idx, NULL if out of range.
* @param a_idx The array index.
*/
inline RTCRestObjectBase *atBase(size_t a_idx) RT_NOEXCEPT
{
if (a_idx < m_cElements)
return m_papElements[a_idx];
return NULL;
}
/**
* Returns the const base object pointer at a given index.
*
* @returns The base object at @a a_idx, NULL if out of range.
* @param a_idx The array index.
*/
inline RTCRestObjectBase const *atBase(size_t a_idx) const RT_NOEXCEPT
{
if (a_idx < m_cElements)
return m_papElements[a_idx];
return NULL;
}
/**
* Removes the element at @a a_idx.
* @returns true if @a a_idx is valid, false if out of range.
* @param a_idx The index of the element to remove.
* The value ~(size_t)0 is an alias for the final element.
*/
bool removeAt(size_t a_idx) RT_NOEXCEPT;
/**
* Makes sure the array can hold at the given number of entries.
*
* @returns VINF_SUCCESS or VERR_NO_MEMORY.
* @param a_cEnsureCapacity The number of elements to ensure capacity to hold.
*/
int ensureCapacity(size_t a_cEnsureCapacity) RT_NOEXCEPT;
protected:
/** The array. */
RTCRestObjectBase **m_papElements;
/** Number of elements in the array. */
size_t m_cElements;
/** The number of elements m_papElements can hold.
* The difference between m_cCapacity and m_cElements are all NULLs. */
size_t m_cCapacity;
/**
* Helper for creating a clone.
*
* @returns Pointer to new array on success, NULL if out of memory.
*/
virtual RTCRestArrayBase *createClone(void) const RT_NOEXCEPT = 0;
/**
* Wrapper around the value constructor.
*
* @returns Pointer to new value object on success, NULL if out of memory.
*/
virtual RTCRestObjectBase *createValue(void) RT_NOEXCEPT = 0;
/**
* For accessing the static deserializeInstanceFromJson() method of the value.
*/
virtual int deserializeValueInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT = 0;
/**
* Worker for the copy assignment method and copyArrayWorkerMayThrow().
*
* This will use createEntryCopy to do the copying.
*
* @returns VINF_SUCCESS on success, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
* @param a_rThat The array to copy. Caller makes 100% sure the it has
* the same type as the destination.
*/
int copyArrayWorkerNoThrow(RTCRestArrayBase const &a_rThat) RT_NOEXCEPT;
/**
* Wrapper around copyArrayWorkerNoThrow for the copy constructor and the
* assignment operator.
*/
void copyArrayWorkerMayThrow(RTCRestArrayBase const &a_rThat);
/**
* Worker for performing inserts.
*
* @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
* VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
* @param a_idx Where to insert it. The value ~(size_t)0 is an alias for m_cElements.
* @param a_pValue The value to insert. Ownership is transferred to the map on success.
* @param a_fReplace Whether to replace existing entry rather than insert.
*/
int insertWorker(size_t a_idx, RTCRestObjectBase *a_pValue, bool a_fReplace) RT_NOEXCEPT;
/**
* Worker for performing inserts.
*
* @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
* VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
* @param a_idx Where to insert it. The value ~(size_t)0 is an alias for m_cElements.
* @param a_rValue The value to copy into the map.
* @param a_fReplace Whether to replace existing key-value pair with matching key.
*/
int insertCopyWorker(size_t a_idx, RTCRestObjectBase const &a_rValue, bool a_fReplace) RT_NOEXCEPT;
private:
/** Copy constructor on this class should never be used. */
RTCRestArrayBase(RTCRestArrayBase const &a_rThat);
/** Copy assignment operator on this class should never be used. */
RTCRestArrayBase &operator=(RTCRestArrayBase const &a_rThat);
};
/**
* Limited array class.
*/
template<class ElementType> class RTCRestArray : public RTCRestArrayBase
{
public:
/** Default constructor - empty array. */
RTCRestArray() RT_NOEXCEPT
: RTCRestArrayBase()
{
}
/** Destructor. */
~RTCRestArray()
{
}
/** Copy constructor. */
RTCRestArray(RTCRestArray const &a_rThat)
: RTCRestArrayBase()
{
copyArrayWorkerMayThrow(a_rThat);
}
/** Copy assignment operator. */
inline RTCRestArray &operator=(RTCRestArray const &a_rThat)
{
copyArrayWorkerMayThrow(a_rThat);
return *this;
}
/** Safe copy assignment method. */
inline int assignCopy(RTCRestArray const &a_rThat) RT_NOEXCEPT
{
return copyArrayWorkerNoThrow(a_rThat);
}
/** Make a clone of this object. */
inline RTCRestArray *clone() const RT_NOEXCEPT
{
return (RTCRestArray *)baseClone();
}
/** Factory method. */
static DECLCALLBACK(RTCRestObjectBase *) createInstance(void) RT_NOEXCEPT
{
return new (std::nothrow) RTCRestArray<ElementType>();
}
/** Factory method for elements. */
static DECLCALLBACK(RTCRestObjectBase *) createElementInstance(void) RT_NOEXCEPT
{
return new (std::nothrow) ElementType();
}
/** @copydoc RTCRestObjectBase::FNDESERIALIZEINSTANCEFROMJSON */
static DECLCALLBACK(int) deserializeInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT
{
*a_ppInstance = new (std::nothrow) RTCRestArray<ElementType>();
if (*a_ppInstance)
return (*a_ppInstance)->deserializeFromJson(a_rCursor);
return a_rCursor.m_pPrimary->addError(a_rCursor, VERR_NO_MEMORY, "Out of memory");
}
/**
* Insert the given object at the specified index.
*
* @returns VINF_SUCCESS on success.
* VERR_INVALID_POINTER, VERR_NO_MEMORY, VERR_NO_STR_MEMORY or VERR_OUT_OF_RANGE on failure.
* @param a_idx The insertion index. ~(size_t)0 is an alias for the end.
* @param a_pThat The object to insert. The array takes ownership of the object on success.
*/
inline int insert(size_t a_idx, ElementType *a_pThat) RT_NOEXCEPT
{
return insertWorker(a_idx, a_pThat, false /*a_fReplace*/);
}
/**
* Insert a copy of the object at the specified index.
*
* @returns VINF_SUCCESS on success.
* VERR_NO_MEMORY, VERR_NO_STR_MEMORY or VERR_OUT_OF_RANGE on failure.
* @param a_idx The insertion index. ~(size_t)0 is an alias for the end.
* @param a_rThat The object to insert a copy of.
*/
inline int insertCopy(size_t a_idx, ElementType const &a_rThat) RT_NOEXCEPT
{
return insertCopyWorker(a_idx, a_rThat, false /*a_fReplace*/);
}
/**
* Appends the given object to the array.
*
* @returns VINF_SUCCESS on success.
* VERR_INVALID_POINTER, VERR_NO_MEMORY, VERR_NO_STR_MEMORY or VERR_OUT_OF_RANGE on failure.
* @param a_pThat The object to insert. The array takes ownership of the object on success.
*/
inline int append(ElementType *a_pThat) RT_NOEXCEPT
{
return insertWorker(~(size_t)0, a_pThat, false /*a_fReplace*/);
}
/**
* Appends a copy of the object at the specified index.
*
* @returns VINF_SUCCESS on success.
* VERR_NO_MEMORY, VERR_NO_STR_MEMORY or VERR_OUT_OF_RANGE on failure.
* @param a_rThat The object to insert a copy of.
*/
inline int appendCopy(ElementType const &a_rThat) RT_NOEXCEPT
{
return insertCopyWorker(~(size_t)0, a_rThat, false /*a_fReplace*/);
}
/**
* Prepends the given object to the array.
*
* @returns VINF_SUCCESS on success.
* VERR_INVALID_POINTER, VERR_NO_MEMORY, VERR_NO_STR_MEMORY or VERR_OUT_OF_RANGE on failure.
* @param a_pThat The object to insert. The array takes ownership of the object on success.
*/
inline int prepend(ElementType *a_pThat) RT_NOEXCEPT
{
return insertWorker(0, a_pThat, false /*a_fReplace*/);
}
/**
* Prepends a copy of the object at the specified index.
*
* @returns VINF_SUCCESS on success.
* VERR_NO_MEMORY, VERR_NO_STR_MEMORY or VERR_OUT_OF_RANGE on failure.
* @param a_rThat The object to insert a copy of.
*/
inline int prependCopy(ElementType const &a_rThat) RT_NOEXCEPT
{
return insertCopyWorker(0, a_rThat, false /*a_fReplace*/);
}
/**
* Insert the given object at the specified index.
*
* @returns VINF_SUCCESS on success.
* VERR_INVALID_POINTER, VERR_NO_MEMORY, VERR_NO_STR_MEMORY or VERR_OUT_OF_RANGE on failure.
* @param a_idx The index of the existing object to replace.
* @param a_pThat The replacement object. The array takes ownership of the object on success.
*/
inline int replace(size_t a_idx, ElementType *a_pThat) RT_NOEXCEPT
{
return insertWorker(a_idx, a_pThat, true /*a_fReplace*/);
}
/**
* Insert a copy of the object at the specified index.
*
* @returns VINF_SUCCESS on success.
* VERR_NO_MEMORY, VERR_NO_STR_MEMORY or VERR_OUT_OF_RANGE on failure.
* @param a_idx The index of the existing object to replace.
* @param a_rThat The object to insert a copy of.
*/
inline int replaceCopy(size_t a_idx, ElementType const &a_rThat) RT_NOEXCEPT
{
return insertCopyWorker(a_idx, a_rThat, true /*a_fReplace*/);
}
/**
* Returns the object at a given index.
*
* @returns The object at @a a_idx, NULL if out of range.
* @param a_idx The array index.
*/
inline ElementType *at(size_t a_idx) RT_NOEXCEPT
{
if (a_idx < m_cElements)
return (ElementType *)m_papElements[a_idx];
return NULL;
}
/**
* Returns the object at a given index, const variant.
*
* @returns The object at @a a_idx, NULL if out of range.
* @param a_idx The array index.
*/
inline ElementType const *at(size_t a_idx) const RT_NOEXCEPT
{
if (a_idx < m_cElements)
return (ElementType const *)m_papElements[a_idx];
return NULL;
}
/**
* Returns the first object in the array.
* @returns The first object, NULL if empty.
*/
inline ElementType *first() RT_NOEXCEPT
{
return at(0);
}
/**
* Returns the first object in the array, const variant.
* @returns The first object, NULL if empty.
*/
inline ElementType const *first() const RT_NOEXCEPT
{
return at(0);
}
/**
* Returns the last object in the array.
* @returns The last object, NULL if empty.
*/
inline ElementType *last() RT_NOEXCEPT
{
return at(m_cElements - 1);
}
/**
* Returns the last object in the array, const variant.
* @returns The last object, NULL if empty.
*/
inline ElementType const *last() const RT_NOEXCEPT
{
return at(m_cElements - 1);
}
protected:
virtual RTCRestArrayBase *createClone(void) const RT_NOEXCEPT RT_OVERRIDE
{
return new (std::nothrow) RTCRestArray();
}
virtual RTCRestObjectBase *createValue(void) RT_NOEXCEPT RT_OVERRIDE
{
return new (std::nothrow) ElementType();
}
virtual int deserializeValueInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT RT_OVERRIDE
{
return ElementType::deserializeInstanceFromJson(a_rCursor, a_ppInstance);
}
};
/** @} */
#endif /* !IPRT_INCLUDED_cpp_restarray_h */
|