VirtualBox

source: vbox/trunk/include/iprt/cpp/reststringmap.h@ 74387

Last change on this file since 74387 was 74387, checked in by vboxsync, 7 years ago

IPRT/rest: Early support for polymorphic data objects in the data model. bugref:9167

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.0 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.215389.xyz. 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();
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_OVERRIDE;
56 virtual int resetToDefault() RT_OVERRIDE;
57 virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_OVERRIDE;
58 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE;
59 // later?
60 //virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = kCollectionFormat_Unspecified) const 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_OVERRIDE;
63 virtual kTypeClass typeClass(void) const RT_OVERRIDE;
64 virtual const char *typeName(void) const RT_OVERRIDE;
65
66 /**
67 * Clear the content of the map.
68 */
69 void clear();
70
71 /**
72 * Checks if the map is empty.
73 */
74 inline bool isEmpty() const { return m_cEntries == 0; }
75
76 /**
77 * Gets the number of entries in the map.
78 */
79 size_t size() const;
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;
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;
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);
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);
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);
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);
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();
162 protected:
163 ConstIterator(MapEntry *a_pEntry) : m_pCur(a_pEntry) { }
164 public:
165 ConstIterator(ConstIterator const &a_rThat) : m_pCur(a_rThat.m_pCur) { }
166
167 /** Gets the key string. */
168 inline RTCString const &getKey() { return m_pCur->strKey; }
169 /** Gets poitner to the value object. */
170 inline RTCRestObjectBase const *getValue() { return m_pCur->pValue; }
171
172 /** Advance to the next map entry. */
173 inline ConstIterator &operator++()
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--()
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) { return m_pCur == a_rThat.m_pCur; }
188 /** Compare not equal. */
189 inline bool operator!=(ConstIterator const &a_rThat) { 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
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
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
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 = 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) = 0;
231
232 /**
233 * Worker for the copy constructor and the assignment operator.
234 *
235 * This will use createEntryCopy to do the copying.
236 *
237 * @returns VINF_SUCCESS on success, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
238 * @param a_rThat The map to copy. Caller makes 100% sure the it has
239 * the same type as the destination.
240 * @param a_fThrow Whether to throw error.
241 */
242 int copyMapWorker(RTCRestStringMapBase const &a_rThat, bool a_fThrow);
243
244 /**
245 * Worker for performing inserts.
246 *
247 * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
248 * VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
249 * @param a_pszKey The key.
250 * @param a_pValue The value to insert. Ownership is transferred to the map on success.
251 * @param a_fReplace Whether to replace existing key-value pair with matching key.
252 * @param a_cchKey The key length, the whole string by default.
253 */
254 int putWorker(const char *a_pszKey, RTCRestObjectBase *a_pValue, bool a_fReplace, size_t a_cchKey = RTSTR_MAX);
255
256 /**
257 * Worker for performing inserts.
258 *
259 * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
260 * VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
261 * @param a_pszKey The key.
262 * @param a_rValue The value to copy into the map.
263 * @param a_fReplace Whether to replace existing key-value pair with matching key.
264 * @param a_cchKey The key length, the whole string by default.
265 */
266 int putCopyWorker(const char *a_pszKey, RTCRestObjectBase const &a_rValue, bool a_fReplace, size_t a_cchKey = RTSTR_MAX);
267
268 /**
269 * Worker for getting the value corresponding to the given key.
270 *
271 * @returns Pointer to the value object if found, NULL if key not in the map.
272 * @param a_pszKey The key which value to look up.
273 */
274 RTCRestObjectBase *getWorker(const char *a_pszKey);
275
276 /**
277 * Worker for getting the value corresponding to the given key, const variant.
278 *
279 * @returns Pointer to the value object if found, NULL if key not in the map.
280 * @param a_pszKey The key which value to look up.
281 */
282 RTCRestObjectBase const *getWorker(const char *a_pszKey) const;
283
284private:
285 static DECLCALLBACK(int) stringSpaceDestructorCallback(PRTSTRSPACECORE pStr, void *pvUser);
286};
287
288
289/**
290 * Limited map class.
291 */
292template<class ValueType> class RTCRestStringMap : public RTCRestStringMapBase
293{
294public:
295 /** Default constructor, creates emtpy map. */
296 RTCRestStringMap()
297 : RTCRestStringMapBase()
298 {}
299
300 /** Copy constructor. */
301 RTCRestStringMap(RTCRestStringMap const &a_rThat)
302 : RTCRestStringMapBase()
303 {
304 copyMapWorker(a_rThat, true /*a_fThrow*/);
305 }
306
307 /** Destructor. */
308 virtual ~RTCRestStringMap()
309 {
310 /* nothing to do here. */
311 }
312
313 /** Copy assignment operator. */
314 RTCRestStringMap &operator=(RTCRestStringMap const &a_rThat)
315 {
316 copyMapWorker(a_rThat, true /*a_fThrow*/);
317 return *this;
318 }
319
320 /** Safe copy assignment method. */
321 int assignCopy(RTCRestStringMap const &a_rThat)
322 {
323 return copyMapWorker(a_rThat, false /*a_fThrow*/);
324 }
325
326 /** Make a clone of this object. */
327 inline RTCRestStringMap *clone() const
328 {
329 return (RTCRestStringMap *)baseClone();
330 }
331
332 /** Factory method. */
333 static DECLCALLBACK(RTCRestObjectBase *) createInstance(void)
334 {
335 return new (std::nothrow) RTCRestStringMap<ValueType>();
336 }
337
338 /** Factory method for values. */
339 static DECLCALLBACK(RTCRestObjectBase *) createValueInstance(void)
340 {
341 return new (std::nothrow) ValueType();
342 }
343
344 /**
345 * Inserts the given object into the map.
346 *
347 * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
348 * VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
349 * @param a_pszKey The key.
350 * @param a_pValue The value to insert. Ownership is transferred to the map on success.
351 * @param a_fReplace Whether to replace existing key-value pair with matching key.
352 */
353 inline int put(const char *a_pszKey, ValueType *a_pValue, bool a_fReplace = false)
354 {
355 return putWorker(a_pszKey, a_pValue, a_fReplace);
356 }
357
358 /**
359 * Inserts the given object into the map.
360 *
361 * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
362 * VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
363 * @param a_rStrKey The key.
364 * @param a_pValue The value to insert. Ownership is transferred to the map on success.
365 * @param a_fReplace Whether to replace existing key-value pair with matching key.
366 */
367 inline int put(RTCString const &a_rStrKey, ValueType *a_pValue, bool a_fReplace = false)
368 {
369 return putWorker(a_rStrKey.c_str(), a_pValue, a_fReplace, a_rStrKey.length());
370 }
371
372 /**
373 * Inserts a copy of the given object into the map.
374 *
375 * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
376 * VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
377 * @param a_pszKey The key.
378 * @param a_rValue The value to insert a copy of.
379 * @param a_fReplace Whether to replace existing key-value pair with matching key.
380 */
381 inline int putCopy(const char *a_pszKey, const ValueType &a_rValue, bool a_fReplace = false)
382 {
383 return putCopyWorker(a_pszKey, a_rValue, a_fReplace);
384 }
385
386 /**
387 * Inserts a copy of the given object into the map.
388 *
389 * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
390 * VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
391 * @param a_rStrKey The key.
392 * @param a_rValue The value to insert a copy of.
393 * @param a_fReplace Whether to replace existing key-value pair with matching key.
394 */
395 inline int putCopy(RTCString const &a_rStrKey, const ValueType &a_rValue, bool a_fReplace = false)
396 {
397 return putCopyWorker(a_rStrKey.c_str(), a_rValue, a_fReplace, a_rStrKey.length());
398 }
399
400 /**
401 * Gets the value corresponding to the given key.
402 *
403 * @returns Pointer to the value object if found, NULL if key not in the map.
404 * @param a_pszKey The key which value to look up.
405 */
406 inline ValueType *get(const char *a_pszKey)
407 {
408 return (ValueType *)getWorker(a_pszKey);
409 }
410
411 /**
412 * Gets the value corresponding to the given key.
413 *
414 * @returns Pointer to the value object if found, NULL if key not in the map.
415 * @param a_rStrKey The key which value to look up.
416 */
417 inline ValueType *get(RTCString const &a_rStrKey)
418 {
419 return (ValueType *)getWorker(a_rStrKey.c_str());
420 }
421
422 /**
423 * Gets the const value corresponding to the given key.
424 *
425 * @returns Pointer to the value object if found, NULL if key not in the map.
426 * @param a_pszKey The key which value to look up.
427 */
428 inline ValueType const *get(const char *a_pszKey) const
429 {
430 return (ValueType const *)getWorker(a_pszKey);
431 }
432
433 /**
434 * Gets the const value corresponding to the given key.
435 *
436 * @returns Pointer to the value object if found, NULL if key not in the map.
437 * @param a_rStrKey The key which value to look up.
438 */
439 inline ValueType const *get(RTCString const &a_rStrKey) const
440 {
441 return (ValueType const *)getWorker(a_rStrKey.c_str());
442 }
443
444 /** @todo enumerator*/
445
446protected:
447 virtual RTCRestStringMapBase *createClone(void) const RT_OVERRIDE
448 {
449 return new (std::nothrow) RTCRestStringMap();
450 }
451
452 virtual RTCRestObjectBase *createValue(void) RT_OVERRIDE
453 {
454 return new (std::nothrow) ValueType();
455 }
456};
457
458
459/** @} */
460
461#endif
462
Note: See TracBrowser for help on using the repository browser.

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