VirtualBox

source: vbox/trunk/include/iprt/cpp/restarray.h@ 74402

最後變更 在這個檔案從74402是 74402,由 vboxsync 提交於 7 年 前

IPRT/rest: Implemented array and string-map support for deserializating polymorphic objects. bugref:9167

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette