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