VirtualBox

source: vbox/trunk/include/iprt/cpp/reststringmap.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: 17.7 KB
Line 
1/** @file
2 * IPRT - C++ Representational State Transfer (REST) String Map Template.
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_reststringmap_h
27#define ___iprt_cpp_reststringmap_h
28
29#include <iprt/list.h>
30#include <iprt/string.h>
31#include <iprt/cpp/restbase.h>
32
33
34/** @defgroup grp_rt_cpp_reststingmap C++ Representational State Transfer (REST) String Map Template
35 * @ingroup grp_rt_cpp
36 * @{
37 */
38
39/**
40 * Abstract base class for the RTCRestStringMap template.
41 */
42class RT_DECL_CLASS RTCRestStringMapBase : public RTCRestObjectBase
43{
44public:
45 /** Default destructor. */
46 RTCRestStringMapBase() RT_NOEXCEPT;
47 /** Copy constructor. */
48 RTCRestStringMapBase(RTCRestStringMapBase const &a_rThat);
49 /** Destructor. */
50 virtual ~RTCRestStringMapBase();
51 /** Copy assignment operator. */
52 RTCRestStringMapBase &operator=(RTCRestStringMapBase const &a_rThat);
53
54 /* Overridden methods: */
55 virtual RTCRestObjectBase *baseClone() const RT_NOEXCEPT RT_OVERRIDE;
56 virtual int resetToDefault() RT_NOEXCEPT RT_OVERRIDE;
57 virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT RT_OVERRIDE;
58 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT RT_OVERRIDE;
59 // later?
60 //virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = kCollectionFormat_Unspecified) const RT_NOEXCEPT RT_OVERRIDE;
61 //virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
62 // uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_NOEXCEPT RT_OVERRIDE;
63 virtual kTypeClass typeClass(void) const RT_NOEXCEPT RT_OVERRIDE;
64 virtual const char *typeName(void) const RT_NOEXCEPT RT_OVERRIDE;
65
66 /**
67 * Clear the content of the map.
68 */
69 void clear() RT_NOEXCEPT;
70
71 /**
72 * Checks if the map is empty.
73 */
74 inline bool isEmpty() const RT_NOEXCEPT { return m_cEntries == 0; }
75
76 /**
77 * Gets the number of entries in the map.
78 */
79 size_t size() const RT_NOEXCEPT;
80
81 /**
82 * Checks if the map contains the given key.
83 * @returns true if key found, false if not.
84 * @param a_pszKey The key to check fo.
85 */
86 bool containsKey(const char *a_pszKey) const RT_NOEXCEPT;
87
88 /**
89 * Checks if the map contains the given key.
90 * @returns true if key found, false if not.
91 * @param a_rStrKey The key to check fo.
92 */
93 bool containsKey(RTCString const &a_rStrKey) const RT_NOEXCEPT;
94
95 /**
96 * Remove any key-value pair with the given key.
97 * @returns true if anything was removed, false if not found.
98 * @param a_pszKey The key to remove.
99 */
100 bool remove(const char *a_pszKey) RT_NOEXCEPT;
101
102 /**
103 * Remove any key-value pair with the given key.
104 * @returns true if anything was removed, false if not found.
105 * @param a_rStrKey The key to remove.
106 */
107 bool remove(RTCString const &a_rStrKey) RT_NOEXCEPT;
108
109 /**
110 * Creates a new value and inserts it under the given key, returning the new value.
111 *
112 * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
113 * VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
114 * @param a_ppValue Where to return the pointer to the value.
115 * @param a_pszKey The key to put it under.
116 * @param a_cchKey The length of the key. Default is the entire string.
117 * @param a_fReplace Whether to replace or fail on key collision.
118 */
119 int putNewValue(RTCRestObjectBase **a_ppValue, const char *a_pszKey, size_t a_cchKey = RTSTR_MAX, bool a_fReplace = false) RT_NOEXCEPT;
120
121 /**
122 * Creates a new value and inserts it under the given key, returning the new value.
123 *
124 * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
125 * VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
126 * @param a_ppValue Where to return the pointer to the value.
127 * @param a_rStrKey The key to put it under.
128 * @param a_fReplace Whether to replace or fail on key collision.
129 */
130 int putNewValue(RTCRestObjectBase **a_ppValue, RTCString const &a_rStrKey, bool a_fReplace = false) RT_NOEXCEPT;
131
132protected:
133 /** Map entry. */
134 typedef struct MapEntry
135 {
136 /** String space core. */
137 RTSTRSPACECORE Core;
138 /** List node for enumeration. */
139 RTLISTNODE ListEntry;
140 /** The key.
141 * @remarks Core.pszString points to the value of this object. So, consider it const. */
142 RTCString strKey;
143 /** The value. */
144 RTCRestObjectBase *pValue;
145 } MapEntry;
146 /** The map tree. */
147 RTSTRSPACE m_Map;
148 /** The enumeration list head (MapEntry). */
149 RTLISTANCHOR m_ListHead;
150 /** Number of map entries. */
151 size_t m_cEntries;
152
153public:
154 /** @name Map Iteration
155 * @{ */
156 /** Const iterator. */
157 class ConstIterator
158 {
159 private:
160 MapEntry *m_pCur;
161 ConstIterator() RT_NOEXCEPT;
162 protected:
163 ConstIterator(MapEntry *a_pEntry) RT_NOEXCEPT : m_pCur(a_pEntry) { }
164 public:
165 ConstIterator(ConstIterator const &a_rThat) RT_NOEXCEPT : m_pCur(a_rThat.m_pCur) { }
166
167 /** Gets the key string. */
168 inline RTCString const &getKey() RT_NOEXCEPT { return m_pCur->strKey; }
169 /** Gets poitner to the value object. */
170 inline RTCRestObjectBase const *getValue() RT_NOEXCEPT { return m_pCur->pValue; }
171
172 /** Advance to the next map entry. */
173 inline ConstIterator &operator++() RT_NOEXCEPT
174 {
175 m_pCur = RTListNodeGetNextCpp(&m_pCur->ListEntry, MapEntry, ListEntry);
176 return *this;
177 }
178
179 /** Advance to the previous map entry. */
180 inline ConstIterator &operator--() RT_NOEXCEPT
181 {
182 m_pCur = RTListNodeGetPrevCpp(&m_pCur->ListEntry, MapEntry, ListEntry);
183 return *this;
184 }
185
186 /** Compare equal. */
187 inline bool operator==(ConstIterator const &a_rThat) RT_NOEXCEPT { return m_pCur == a_rThat.m_pCur; }
188 /** Compare not equal. */
189 inline bool operator!=(ConstIterator const &a_rThat) RT_NOEXCEPT { return m_pCur != a_rThat.m_pCur; }
190
191 /* Map class must be friend so it can use the MapEntry constructor. */
192 friend class RTCRestStringMapBase;
193 };
194
195 /** Returns iterator for the first map entry (unless it's empty and it's also the end). */
196 inline ConstIterator begin() const RT_NOEXCEPT
197 {
198 if (!RTListIsEmpty(&m_ListHead))
199 return ConstIterator(RTListNodeGetNextCpp(&m_ListHead, MapEntry, ListEntry));
200 return end();
201 }
202 /** Returns iterator for the last map entry (unless it's empty and it's also the end). */
203 inline ConstIterator last() const RT_NOEXCEPT
204 {
205 if (!RTListIsEmpty(&m_ListHead))
206 return ConstIterator(RTListNodeGetPrevCpp(&m_ListHead, MapEntry, ListEntry));
207 return end();
208 }
209 /** Returns the end iterator. This does not ever refer to an actual map entry. */
210 inline ConstIterator end() const RT_NOEXCEPT
211 {
212 return ConstIterator(RT_FROM_CPP_MEMBER(&m_ListHead, MapEntry, ListEntry));
213 }
214 /** @} */
215
216
217protected:
218 /**
219 * Helper for creating a clone.
220 *
221 * @returns Pointer to new map object on success, NULL if out of memory.
222 */
223 virtual RTCRestStringMapBase *createClone(void) const RT_NOEXCEPT = 0;
224
225 /**
226 * Wrapper around the value constructor.
227 *
228 * @returns Pointer to new value object on success, NULL if out of memory.
229 */
230 virtual RTCRestObjectBase *createValue(void) RT_NOEXCEPT = 0;
231
232 /**
233 * For accessing the static deserializeInstanceFromJson() method of the value.
234 */
235 virtual int deserializeValueInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT = 0;
236
237 /**
238 * Worker for the copy assignment method and copyMapWorkerMayThrow.
239 *
240 * This will use createEntryCopy to do the copying.
241 *
242 * @returns VINF_SUCCESS on success, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
243 * @param a_rThat The map to copy. Caller makes 100% sure the it has
244 * the same type as the destination.
245 */
246 int copyMapWorkerNoThrow(RTCRestStringMapBase const &a_rThat) RT_NOEXCEPT;
247
248 /**
249 * Wrapper around copyMapWorkerNoThrow() that throws allocation errors, making
250 * it suitable for copy constructors and assignment operators.
251 */
252 void copyMapWorkerMayThrow(RTCRestStringMapBase const &a_rThat);
253
254 /**
255 * Worker for performing inserts.
256 *
257 * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
258 * VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
259 * @param a_pszKey The key.
260 * @param a_pValue The value to insert. Ownership is transferred to the map on success.
261 * @param a_fReplace Whether to replace existing key-value pair with matching key.
262 * @param a_cchKey The key length, the whole string by default.
263 */
264 int putWorker(const char *a_pszKey, RTCRestObjectBase *a_pValue, bool a_fReplace, size_t a_cchKey = RTSTR_MAX) RT_NOEXCEPT;
265
266 /**
267 * Worker for performing inserts.
268 *
269 * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
270 * VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
271 * @param a_pszKey The key.
272 * @param a_rValue The value to copy into the map.
273 * @param a_fReplace Whether to replace existing key-value pair with matching key.
274 * @param a_cchKey The key length, the whole string by default.
275 */
276 int putCopyWorker(const char *a_pszKey, RTCRestObjectBase const &a_rValue, bool a_fReplace, size_t a_cchKey = RTSTR_MAX) RT_NOEXCEPT;
277
278 /**
279 * Worker for getting the value corresponding to the given key.
280 *
281 * @returns Pointer to the value object if found, NULL if key not in the map.
282 * @param a_pszKey The key which value to look up.
283 */
284 RTCRestObjectBase *getWorker(const char *a_pszKey) RT_NOEXCEPT;
285
286 /**
287 * Worker for getting the value corresponding to the given key, const variant.
288 *
289 * @returns Pointer to the value object if found, NULL if key not in the map.
290 * @param a_pszKey The key which value to look up.
291 */
292 RTCRestObjectBase const *getWorker(const char *a_pszKey) const RT_NOEXCEPT;
293
294private:
295 static DECLCALLBACK(int) stringSpaceDestructorCallback(PRTSTRSPACECORE pStr, void *pvUser) RT_NOEXCEPT;
296};
297
298
299/**
300 * Limited map class.
301 */
302template<class ValueType> class RTCRestStringMap : public RTCRestStringMapBase
303{
304public:
305 /** Default constructor, creates emtpy map. */
306 RTCRestStringMap() RT_NOEXCEPT
307 : RTCRestStringMapBase()
308 {}
309
310 /** Copy constructor. */
311 RTCRestStringMap(RTCRestStringMap const &a_rThat)
312 : RTCRestStringMapBase()
313 {
314 copyMapWorkerMayThrow(a_rThat);
315 }
316
317 /** Destructor. */
318 virtual ~RTCRestStringMap()
319 {
320 /* nothing to do here. */
321 }
322
323 /** Copy assignment operator. */
324 RTCRestStringMap &operator=(RTCRestStringMap const &a_rThat)
325 {
326 copyMapWorkerMayThrow(a_rThat);
327 return *this;
328 }
329
330 /** Safe copy assignment method. */
331 int assignCopy(RTCRestStringMap const &a_rThat) RT_NOEXCEPT
332 {
333 return copyMapWorkerNoThrow(a_rThat);
334 }
335
336 /** Make a clone of this object. */
337 inline RTCRestStringMap *clone() const RT_NOEXCEPT
338 {
339 return (RTCRestStringMap *)baseClone();
340 }
341
342 /** Factory method. */
343 static DECLCALLBACK(RTCRestObjectBase *) createInstance(void) RT_NOEXCEPT
344 {
345 return new (std::nothrow) RTCRestStringMap<ValueType>();
346 }
347
348 /** Factory method for values. */
349 static DECLCALLBACK(RTCRestObjectBase *) createValueInstance(void) RT_NOEXCEPT
350 {
351 return new (std::nothrow) ValueType();
352 }
353
354 /** @copydoc RTCRestObjectBase::FNDESERIALIZEINSTANCEFROMJSON */
355 static DECLCALLBACK(int) deserializeInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT
356 {
357 *a_ppInstance = new (std::nothrow) RTCRestStringMap<ValueType>();
358 if (*a_ppInstance)
359 return (*a_ppInstance)->deserializeFromJson(a_rCursor);
360 return a_rCursor.m_pPrimary->addError(a_rCursor, VERR_NO_MEMORY, "Out of memory");
361 }
362
363 /**
364 * Inserts the given object into the map.
365 *
366 * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
367 * VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
368 * @param a_pszKey The key.
369 * @param a_pValue The value to insert. Ownership is transferred to the map on success.
370 * @param a_fReplace Whether to replace existing key-value pair with matching key.
371 */
372 inline int put(const char *a_pszKey, ValueType *a_pValue, bool a_fReplace = false) RT_NOEXCEPT
373 {
374 return putWorker(a_pszKey, a_pValue, a_fReplace);
375 }
376
377 /**
378 * Inserts the given object into the map.
379 *
380 * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
381 * VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
382 * @param a_rStrKey The key.
383 * @param a_pValue The value to insert. Ownership is transferred to the map on success.
384 * @param a_fReplace Whether to replace existing key-value pair with matching key.
385 */
386 inline int put(RTCString const &a_rStrKey, ValueType *a_pValue, bool a_fReplace = false) RT_NOEXCEPT
387 {
388 return putWorker(a_rStrKey.c_str(), a_pValue, a_fReplace, a_rStrKey.length());
389 }
390
391 /**
392 * Inserts a copy of the given object into the map.
393 *
394 * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
395 * VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
396 * @param a_pszKey The key.
397 * @param a_rValue The value to insert a copy of.
398 * @param a_fReplace Whether to replace existing key-value pair with matching key.
399 */
400 inline int putCopy(const char *a_pszKey, const ValueType &a_rValue, bool a_fReplace = false) RT_NOEXCEPT
401 {
402 return putCopyWorker(a_pszKey, a_rValue, a_fReplace);
403 }
404
405 /**
406 * Inserts a copy of the given object into the map.
407 *
408 * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
409 * VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
410 * @param a_rStrKey The key.
411 * @param a_rValue The value to insert a copy of.
412 * @param a_fReplace Whether to replace existing key-value pair with matching key.
413 */
414 inline int putCopy(RTCString const &a_rStrKey, const ValueType &a_rValue, bool a_fReplace = false) RT_NOEXCEPT
415 {
416 return putCopyWorker(a_rStrKey.c_str(), a_rValue, a_fReplace, a_rStrKey.length());
417 }
418
419 /**
420 * Gets the value corresponding to the given key.
421 *
422 * @returns Pointer to the value object if found, NULL if key not in the map.
423 * @param a_pszKey The key which value to look up.
424 */
425 inline ValueType *get(const char *a_pszKey) RT_NOEXCEPT
426 {
427 return (ValueType *)getWorker(a_pszKey);
428 }
429
430 /**
431 * Gets the value corresponding to the given key.
432 *
433 * @returns Pointer to the value object if found, NULL if key not in the map.
434 * @param a_rStrKey The key which value to look up.
435 */
436 inline ValueType *get(RTCString const &a_rStrKey) RT_NOEXCEPT
437 {
438 return (ValueType *)getWorker(a_rStrKey.c_str());
439 }
440
441 /**
442 * Gets the const value corresponding to the given key.
443 *
444 * @returns Pointer to the value object if found, NULL if key not in the map.
445 * @param a_pszKey The key which value to look up.
446 */
447 inline ValueType const *get(const char *a_pszKey) const RT_NOEXCEPT
448 {
449 return (ValueType const *)getWorker(a_pszKey);
450 }
451
452 /**
453 * Gets the const value corresponding to the given key.
454 *
455 * @returns Pointer to the value object if found, NULL if key not in the map.
456 * @param a_rStrKey The key which value to look up.
457 */
458 inline ValueType const *get(RTCString const &a_rStrKey) const RT_NOEXCEPT
459 {
460 return (ValueType const *)getWorker(a_rStrKey.c_str());
461 }
462
463 /** @todo enumerator*/
464
465protected:
466 virtual RTCRestStringMapBase *createClone(void) const RT_NOEXCEPT RT_OVERRIDE
467 {
468 return new (std::nothrow) RTCRestStringMap();
469 }
470
471 virtual RTCRestObjectBase *createValue(void) RT_NOEXCEPT RT_OVERRIDE
472 {
473 return new (std::nothrow) ValueType();
474 }
475
476 virtual int deserializeValueInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT RT_OVERRIDE
477 {
478 return ValueType::deserializeInstanceFromJson(a_rCursor, a_ppInstance);
479 }
480};
481
482
483/** @} */
484
485#endif
486
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