VirtualBox

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

Last change on this file since 74732 was 74425, checked in by vboxsync, 6 years ago

IPRT/rest: Missed RT_NOEXCEPT in two place. Went wild adding RT_NOEXCEPT everywhere possible. bugref:9167

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.8 KB
Line 
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.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_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() RT_NOEXCEPT;
45 /** Destructor. */
46 virtual ~RTCRestArrayBase();
47
48 /* Overridden methods: */
49 virtual RTCRestObjectBase *baseClone() const RT_NOEXCEPT RT_OVERRIDE;
50 virtual int resetToDefault() RT_NOEXCEPT RT_OVERRIDE;
51 virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT RT_OVERRIDE;
52 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT RT_OVERRIDE;
53 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = kCollectionFormat_Unspecified) const RT_NOEXCEPT 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_NOEXCEPT RT_OVERRIDE;
56 virtual kTypeClass typeClass(void) const RT_NOEXCEPT RT_OVERRIDE;
57 virtual const char *typeName(void) const RT_NOEXCEPT RT_OVERRIDE;
58
59 /**
60 * Clear the content of the map.
61 */
62 void clear() RT_NOEXCEPT;
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 RT_NOEXCEPT
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 RT_NOEXCEPT
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) RT_NOEXCEPT
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 RT_NOEXCEPT
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) RT_NOEXCEPT;
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) RT_NOEXCEPT;
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 RT_NOEXCEPT = 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) RT_NOEXCEPT = 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) RT_NOEXCEPT = 0;
152
153 /**
154 * Worker for the copy assignment method and copyArrayWorkerMayThrow().
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 */
162 int copyArrayWorkerNoThrow(RTCRestArrayBase const &a_rThat) RT_NOEXCEPT;
163
164 /**
165 * Wrapper around copyArrayWorkerNoThrow for the copy constructor and the
166 * assignment operator.
167 */
168 void copyArrayWorkerMayThrow(RTCRestArrayBase const &a_rThat);
169
170 /**
171 * Worker for performing inserts.
172 *
173 * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
174 * VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
175 * @param a_idx Where to insert it. The value ~(size_t)0 is an alias for m_cElements.
176 * @param a_pValue The value to insert. Ownership is transferred to the map on success.
177 * @param a_fReplace Whether to replace existing entry rather than insert.
178 */
179 int insertWorker(size_t a_idx, RTCRestObjectBase *a_pValue, bool a_fReplace) RT_NOEXCEPT;
180
181 /**
182 * Worker for performing inserts.
183 *
184 * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
185 * VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
186 * @param a_idx Where to insert it. The value ~(size_t)0 is an alias for m_cElements.
187 * @param a_rValue The value to copy into the map.
188 * @param a_fReplace Whether to replace existing key-value pair with matching key.
189 */
190 int insertCopyWorker(size_t a_idx, RTCRestObjectBase const &a_rValue, bool a_fReplace) RT_NOEXCEPT;
191
192private:
193 /** Copy constructor on this class should never be used. */
194 RTCRestArrayBase(RTCRestArrayBase const &a_rThat);
195 /** Copy assignment operator on this class should never be used. */
196 RTCRestArrayBase &operator=(RTCRestArrayBase const &a_rThat);
197};
198
199
200
201/**
202 * Limited array class.
203 */
204template<class ElementType> class RTCRestArray : public RTCRestArrayBase
205{
206public:
207 /** Default constructor - empty array. */
208 RTCRestArray() RT_NOEXCEPT
209 : RTCRestArrayBase()
210 {
211 }
212
213 /** Destructor. */
214 ~RTCRestArray()
215 {
216 }
217
218 /** Copy constructor. */
219 RTCRestArray(RTCRestArray const &a_rThat)
220 : RTCRestArrayBase()
221 {
222 copyArrayWorkerMayThrow(a_rThat);
223 }
224
225 /** Copy assignment operator. */
226 inline RTCRestArray &operator=(RTCRestArray const &a_rThat)
227 {
228 copyArrayWorkerMayThrow(a_rThat);
229 return *this;
230 }
231
232 /** Safe copy assignment method. */
233 inline int assignCopy(RTCRestArray const &a_rThat) RT_NOEXCEPT
234 {
235 return copyArrayWorkerNoThrow(a_rThat);
236 }
237
238 /** Make a clone of this object. */
239 inline RTCRestArray *clone() const RT_NOEXCEPT
240 {
241 return (RTCRestArray *)baseClone();
242 }
243
244 /** Factory method. */
245 static DECLCALLBACK(RTCRestObjectBase *) createInstance(void) RT_NOEXCEPT
246 {
247 return new (std::nothrow) RTCRestArray<ElementType>();
248 }
249
250 /** Factory method for elements. */
251 static DECLCALLBACK(RTCRestObjectBase *) createElementInstance(void) RT_NOEXCEPT
252 {
253 return new (std::nothrow) ElementType();
254 }
255
256 /** @copydoc RTCRestObjectBase::FNDESERIALIZEINSTANCEFROMJSON */
257 static DECLCALLBACK(int) deserializeInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT
258 {
259 *a_ppInstance = new (std::nothrow) RTCRestArray<ElementType>();
260 if (*a_ppInstance)
261 return (*a_ppInstance)->deserializeFromJson(a_rCursor);
262 return a_rCursor.m_pPrimary->addError(a_rCursor, VERR_NO_MEMORY, "Out of memory");
263 }
264
265
266 /**
267 * Insert the given object at the specified index.
268 *
269 * @returns VINF_SUCCESS on success.
270 * VERR_INVALID_POINTER, VERR_NO_MEMORY, VERR_NO_STR_MEMORY or VERR_OUT_OF_RANGE on failure.
271 * @param a_idx The insertion index. ~(size_t)0 is an alias for the end.
272 * @param a_pThat The object to insert. The array takes ownership of the object on success.
273 */
274 inline int insert(size_t a_idx, ElementType *a_pThat) RT_NOEXCEPT
275 {
276 return insertWorker(a_idx, a_pThat, false /*a_fReplace*/);
277 }
278
279 /**
280 * Insert a copy of the object at the specified index.
281 *
282 * @returns VINF_SUCCESS on success.
283 * VERR_NO_MEMORY, VERR_NO_STR_MEMORY or VERR_OUT_OF_RANGE on failure.
284 * @param a_idx The insertion index. ~(size_t)0 is an alias for the end.
285 * @param a_rThat The object to insert a copy of.
286 */
287 inline int insertCopy(size_t a_idx, ElementType const &a_rThat) RT_NOEXCEPT
288 {
289 return insertCopyWorker(a_idx, a_rThat, false /*a_fReplace*/);
290 }
291
292 /**
293 * Appends the given object to the array.
294 *
295 * @returns VINF_SUCCESS on success.
296 * VERR_INVALID_POINTER, VERR_NO_MEMORY, VERR_NO_STR_MEMORY or VERR_OUT_OF_RANGE on failure.
297 * @param a_pThat The object to insert. The array takes ownership of the object on success.
298 */
299 inline int append(ElementType *a_pThat) RT_NOEXCEPT
300 {
301 return insertWorker(~(size_t)0, a_pThat, false /*a_fReplace*/);
302 }
303
304 /**
305 * Appends a copy of the object at the specified index.
306 *
307 * @returns VINF_SUCCESS on success.
308 * VERR_NO_MEMORY, VERR_NO_STR_MEMORY or VERR_OUT_OF_RANGE on failure.
309 * @param a_rThat The object to insert a copy of.
310 */
311 inline int appendCopy(ElementType const &a_rThat) RT_NOEXCEPT
312 {
313 return insertCopyWorker(~(size_t)0, a_rThat, false /*a_fReplace*/);
314 }
315
316 /**
317 * Prepends the given object to the array.
318 *
319 * @returns VINF_SUCCESS on success.
320 * VERR_INVALID_POINTER, VERR_NO_MEMORY, VERR_NO_STR_MEMORY or VERR_OUT_OF_RANGE on failure.
321 * @param a_pThat The object to insert. The array takes ownership of the object on success.
322 */
323 inline int prepend(ElementType *a_pThat) RT_NOEXCEPT
324 {
325 return insertWorker(0, a_pThat, false /*a_fReplace*/);
326 }
327
328 /**
329 * Prepends a copy of the object at the specified index.
330 *
331 * @returns VINF_SUCCESS on success.
332 * VERR_NO_MEMORY, VERR_NO_STR_MEMORY or VERR_OUT_OF_RANGE on failure.
333 * @param a_rThat The object to insert a copy of.
334 */
335 inline int prependCopy(ElementType const &a_rThat) RT_NOEXCEPT
336 {
337 return insertCopyWorker(0, a_rThat, false /*a_fReplace*/);
338 }
339
340 /**
341 * Insert the given object at the specified index.
342 *
343 * @returns VINF_SUCCESS on success.
344 * VERR_INVALID_POINTER, VERR_NO_MEMORY, VERR_NO_STR_MEMORY or VERR_OUT_OF_RANGE on failure.
345 * @param a_idx The index of the existing object to replace.
346 * @param a_pThat The replacement object. The array takes ownership of the object on success.
347 */
348 inline int replace(size_t a_idx, ElementType *a_pThat) RT_NOEXCEPT
349 {
350 return insertWorker(a_idx, a_pThat, true /*a_fReplace*/);
351 }
352
353 /**
354 * Insert a copy of the object at the specified index.
355 *
356 * @returns VINF_SUCCESS on success.
357 * VERR_NO_MEMORY, VERR_NO_STR_MEMORY or VERR_OUT_OF_RANGE on failure.
358 * @param a_idx The index of the existing object to replace.
359 * @param a_rThat The object to insert a copy of.
360 */
361 inline int replaceCopy(size_t a_idx, ElementType const &a_rThat) RT_NOEXCEPT
362 {
363 return insertCopyWorker(a_idx, a_rThat, true /*a_fReplace*/);
364 }
365
366 /**
367 * Returns the object at a given index.
368 *
369 * @returns The object at @a a_idx, NULL if out of range.
370 * @param a_idx The array index.
371 */
372 inline ElementType *at(size_t a_idx) RT_NOEXCEPT
373 {
374 if (a_idx < m_cElements)
375 return (ElementType *)m_papElements[a_idx];
376 return NULL;
377 }
378
379 /**
380 * Returns the object at a given index, const variant.
381 *
382 * @returns The object at @a a_idx, NULL if out of range.
383 * @param a_idx The array index.
384 */
385 inline ElementType const *at(size_t a_idx) const RT_NOEXCEPT
386 {
387 if (a_idx < m_cElements)
388 return (ElementType const *)m_papElements[a_idx];
389 return NULL;
390 }
391
392 /**
393 * Returns the first object in the array.
394 * @returns The first object, NULL if empty.
395 */
396 inline ElementType *first() RT_NOEXCEPT
397 {
398 return at(0);
399 }
400
401 /**
402 * Returns the first object in the array, const variant.
403 * @returns The first object, NULL if empty.
404 */
405 inline ElementType const *first() const RT_NOEXCEPT
406 {
407 return at(0);
408 }
409
410 /**
411 * Returns the last object in the array.
412 * @returns The last object, NULL if empty.
413 */
414 inline ElementType *last() RT_NOEXCEPT
415 {
416 return at(m_cElements - 1);
417 }
418
419 /**
420 * Returns the last object in the array, const variant.
421 * @returns The last object, NULL if empty.
422 */
423 inline ElementType const *last() const RT_NOEXCEPT
424 {
425 return at(m_cElements - 1);
426 }
427
428
429protected:
430 virtual RTCRestArrayBase *createClone(void) const RT_NOEXCEPT RT_OVERRIDE
431 {
432 return new (std::nothrow) RTCRestArray();
433 }
434
435 virtual RTCRestObjectBase *createValue(void) RT_NOEXCEPT RT_OVERRIDE
436 {
437 return new (std::nothrow) ElementType();
438 }
439
440 virtual int deserializeValueInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT RT_OVERRIDE
441 {
442 return ElementType::deserializeInstanceFromJson(a_rCursor, a_ppInstance);
443 }
444};
445
446
447/** @} */
448
449#endif
450
Note: See TracBrowser for help on using the repository browser.

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