summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/common/rest/RTCRestStringMapBase.cpp
blob: 35ec98050e0b9a3da706e8cc6a0266e7b28450e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/* $Id: RTCRestStringMapBase.cpp $ */
/** @file
 * IPRT - C++ REST, RTCRestStringMapBase implementation.
 */

/*
 * Copyright (C) 2018-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP RTLOGGROUP_REST
#include <iprt/cpp/reststringmap.h>

#include <iprt/err.h>
#include <iprt/string.h>
#include <iprt/cpp/restoutput.h>


/**
 * Default destructor.
 */
RTCRestStringMapBase::RTCRestStringMapBase() RT_NOEXCEPT
    : RTCRestObjectBase()
    , m_Map(NULL)
    , m_cEntries(0)
{
    RTListInit(&m_ListHead);
}


#if 0 /* trigger link error for now. */
/** Copy constructor. */
RTCRestStringMapBase::RTCRestStringMapBase(RTCRestStringMapBase const &a_rThat);
#endif


/**
 * Destructor.
 */
RTCRestStringMapBase::~RTCRestStringMapBase()
{
    clear();
}



#if 0 /* trigger link error for now. */
/** Copy assignment operator. */
RTCRestStringMapBase &RTCRestStringMapBase::operator=(RTCRestStringMapBase const &a_rThat);
#endif


/*********************************************************************************************************************************
*   Overridden base object methods                                                                                               *
*********************************************************************************************************************************/

RTCRestObjectBase *RTCRestStringMapBase::baseClone() const RT_NOEXCEPT
{
    RTCRestStringMapBase *pClone = createClone();
    if (pClone)
    {
        int rc = pClone->copyMapWorkerNoThrow(*this);
        if (RT_SUCCESS(rc))
            return pClone;
        delete pClone;
    }
    return NULL;
}


int RTCRestStringMapBase::resetToDefault() RT_NOEXCEPT
{
    /* Default is an empty map. */
    clear();
    m_fNullIndicator = false;
    return VINF_SUCCESS;
}


RTCRestOutputBase &RTCRestStringMapBase::serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT
{
    if (!m_fNullIndicator)
    {
        uint32_t const uOldState = a_rDst.beginObject();
        MapEntry const *pCur;
        RTListForEachCpp(&m_ListHead, pCur, MapEntry, ListEntry)
        {
            a_rDst.valueSeparatorAndName(pCur->strKey.c_str(), pCur->strKey.length());
            pCur->pValue->serializeAsJson(a_rDst);
        }
        a_rDst.endObject(uOldState);
    }
    else
        a_rDst.nullValue();
    return a_rDst;
}


int RTCRestStringMapBase::deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT
{
    if (RTJsonValueGetType(a_rCursor.m_hValue) == RTJSONVALTYPE_NULL)
    {
        setNull();
        return VINF_SUCCESS;
    }

    /*
     * Make sure the object starts out with an empty map.
     */
    if (m_cEntries > 0)
        clear();
    m_fNullIndicator = false;

    /*
     * Iterate the object values.
     */
    RTJSONIT hIterator;
    int rcRet = RTJsonIteratorBeginObject(a_rCursor.m_hValue, &hIterator);
    if (RT_SUCCESS(rcRet))
    {
        for (;;)
        {
            /* Set up the sub-cursor. */
            RTCRestJsonCursor SubCursor(a_rCursor);
            int rc = RTJsonIteratorQueryValue(hIterator, &SubCursor.m_hValue, &SubCursor.m_pszName);
            if (RT_SUCCESS(rc))
            {
                /* Call the static deserializeInstanceFromJson method of the value class.  */
                RTCRestObjectBase *pObj = NULL;
                rc = deserializeValueInstanceFromJson(SubCursor, &pObj);
                if (RT_SUCCESS(rc))
                    Assert(pObj);
                else if (RT_SUCCESS(rcRet))
                    rcRet = rc;
                if (pObj)
                {
                    /* Insert the value. */
                    rc = putWorker(SubCursor.m_pszName, pObj, true /*a_fReplace*/);
                    if (rc == VINF_SUCCESS)
                    { /* likely */ }
                    else if (RT_SUCCESS(rc))
                    {
                        a_rCursor.m_pPrimary->addError(a_rCursor, rc, "warning %Rrc inserting '%s' into map",
                                                       rc, SubCursor.m_pszName);
                        if (rcRet == VINF_SUCCESS)
                            rcRet = rc;
                    }
                    else
                    {
                        rcRet = a_rCursor.m_pPrimary->addError(a_rCursor, rc, "Failed to insert '%s' into map: %Rrc",
                                                               SubCursor.m_pszName, rc);
                        delete pObj;
                    }
                }
            }
            else
                rcRet = a_rCursor.m_pPrimary->addError(a_rCursor, rc, "RTJsonIteratorQueryValue failed: %Rrc", rc);

            /*
             * Advance.
             */
            rc = RTJsonIteratorNext(hIterator);
            if (RT_SUCCESS(rc))
            { /* likely */ }
            else if (rc == VERR_JSON_ITERATOR_END)
                break;
            else
            {
                rcRet = a_rCursor.m_pPrimary->addError(a_rCursor, rc, "RTJsonIteratorNext failed: %Rrc", rc);
                break;
            }
        }

        RTJsonIteratorFree(hIterator);
    }
    else if (rcRet == VERR_JSON_IS_EMPTY)
        rcRet = VINF_SUCCESS;
    else if (   rcRet == VERR_JSON_VALUE_INVALID_TYPE
             && RTJsonValueGetType(a_rCursor.m_hValue) == RTJSONVALTYPE_NULL)
    {
        m_fNullIndicator = true;
        rcRet = VINF_SUCCESS;
    }
    else
        rcRet = a_rCursor.m_pPrimary->addError(a_rCursor, rcRet, "RTJsonIteratorBegin failed: %Rrc (type %s)",
                                               rcRet, RTJsonValueTypeName(RTJsonValueGetType(a_rCursor.m_hValue)));
    return rcRet;
}

// later?
//    virtual int RTCRestStringMapBase::toString(RTCString *a_pDst, uint32_t a_fFlags = kCollectionFormat_Unspecified) const ;
//    virtual int RTCRestStringMapBase::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
//                           uint32_t a_fFlags = kCollectionFormat_Unspecified) ;
//


RTCRestObjectBase::kTypeClass RTCRestStringMapBase::typeClass(void) const RT_NOEXCEPT
{
    return kTypeClass_StringMap;
}


const char *RTCRestStringMapBase::typeName(void) const RT_NOEXCEPT
{
    return "RTCRestStringMap<ValueType>";
}


/*********************************************************************************************************************************
*   Generic map methods                                                                                                          *
*********************************************************************************************************************************/

/**
 * @callback_method_impl{FNRTSTRSPACECALLBACK}
 */
/*static*/ DECLCALLBACK(int) RTCRestStringMapBase::stringSpaceDestructorCallback(PRTSTRSPACECORE pStr, void *pvUser) RT_NOEXCEPT
{
    MapEntry *pNode = (MapEntry *)pStr;
    if (pNode->pValue)
    {
        delete pNode->pValue;
        pNode->pValue = NULL;
    }
    pNode->strKey.setNull();
    delete pNode;

    RT_NOREF(pvUser);
    return VINF_SUCCESS;
}


void RTCRestStringMapBase::clear() RT_NOEXCEPT
{
    RTStrSpaceDestroy(&m_Map, stringSpaceDestructorCallback, NULL);
    RTListInit(&m_ListHead);
    m_cEntries = 0;
    m_fNullIndicator = false;
}


size_t RTCRestStringMapBase::size() const RT_NOEXCEPT
{
    return m_cEntries;
}


bool RTCRestStringMapBase::containsKey(const char *a_pszKey) const RT_NOEXCEPT
{
    if (isNull())
        return false;

    return RTStrSpaceGet((PRTSTRSPACE)&m_Map, a_pszKey) != NULL;
}


bool RTCRestStringMapBase::containsKey(RTCString const &a_rStrKey) const RT_NOEXCEPT
{
    return containsKey(a_rStrKey.c_str());
}


bool RTCRestStringMapBase::remove(const char *a_pszKey) RT_NOEXCEPT
{
    if (isNull())
        return false;

    MapEntry *pRemoved = (MapEntry *)RTStrSpaceRemove(&m_Map, a_pszKey);
    if (pRemoved)
    {
        m_cEntries--;
        RTListNodeRemove(&pRemoved->ListEntry);
        stringSpaceDestructorCallback(&pRemoved->Core, NULL);
        return true;
    }
    return false;
}


bool RTCRestStringMapBase::remove(RTCString const &a_rStrKey) RT_NOEXCEPT
{
    return remove(a_rStrKey.c_str());
}


int RTCRestStringMapBase::putNewValue(RTCRestObjectBase **a_ppValue, const char *a_pszKey, size_t a_cchKey /*= RTSTR_MAX*/,
                                      bool a_fReplace /*= false*/) RT_NOEXCEPT
{
    RTCRestObjectBase *pValue = createValue();
    if (pValue)
    {
        int rc = putWorker(a_pszKey, pValue, a_fReplace, a_cchKey);
        if (RT_SUCCESS(rc))
            *a_ppValue = pValue;
        else
        {
            delete pValue;
            *a_ppValue = NULL;
        }
        return rc;
    }
    *a_ppValue = NULL;
    return VERR_NO_MEMORY;
}


int RTCRestStringMapBase::putNewValue(RTCRestObjectBase **a_ppValue, RTCString const &a_rStrKey, bool a_fReplace /*= false*/) RT_NOEXCEPT
{
    return putNewValue(a_ppValue, a_rStrKey.c_str(), a_rStrKey.length(), a_fReplace);
}


/*********************************************************************************************************************************
*   Protected methods                                                                                                            *
*********************************************************************************************************************************/

int RTCRestStringMapBase::copyMapWorkerNoThrow(RTCRestStringMapBase const &a_rThat) RT_NOEXCEPT
{
    Assert(this != &a_rThat);
    clear();
    m_fNullIndicator = a_rThat.m_fNullIndicator;

    if (!a_rThat.m_fNullIndicator)
    {
        MapEntry const *pCur;
        RTListForEachCpp(&a_rThat.m_ListHead, pCur, MapEntry, ListEntry)
        {
            int rc = putCopyWorker(pCur->strKey.c_str(), *pCur->pValue, true /*a_fReplace*/);
            if (RT_SUCCESS(rc))
            { /* likely */ }
            else
                return rc;
        }
    }

    return VINF_SUCCESS;
}


void RTCRestStringMapBase::copyMapWorkerMayThrow(RTCRestStringMapBase const &a_rThat)
{
    int rc = copyMapWorkerNoThrow(a_rThat);
    if (RT_SUCCESS(rc))
        return;
    throw std::bad_alloc();
}


int RTCRestStringMapBase::putWorker(const char *a_pszKey, RTCRestObjectBase *a_pValue, bool a_fReplace,
                                    size_t a_cchKey /*= RTSTR_MAX*/) RT_NOEXCEPT
{
    int rc;
    MapEntry *pEntry =  new (std::nothrow) MapEntry;
    if (pEntry)
    {
        rc = pEntry->strKey.assignNoThrow(a_pszKey, a_cchKey);
        if (RT_SUCCESS(rc))
        {
            pEntry->Core.pszString = pEntry->strKey.c_str();
            pEntry->Core.cchString = pEntry->strKey.length();
            pEntry->pValue = a_pValue;
            if (RTStrSpaceInsert(&m_Map, &pEntry->Core))
            {
                RTListAppend(&m_ListHead, &pEntry->ListEntry);
                m_cEntries++;
                m_fNullIndicator = false;
                return VINF_SUCCESS;
            }

            Assert(!m_fNullIndicator);
            if (!a_fReplace)
                rc = VERR_ALREADY_EXISTS;
            else
            {
                /* Just replace the pValue in the existing entry. */
                MapEntry *pCollision = (MapEntry *)RTStrSpaceGet(&m_Map, a_pszKey);
                if (pCollision)
                {
                    if (pCollision->pValue)
                        delete pCollision->pValue;
                    pCollision->pValue = a_pValue;
                    pEntry->pValue = NULL; /* paranoia */
                    rc = VWRN_ALREADY_EXISTS;
                }
                else
                    rc = VERR_INTERNAL_ERROR;
            }
        }
        delete pEntry;
    }
    else
        rc = VERR_NO_MEMORY;
    return rc;
}


int RTCRestStringMapBase::putCopyWorker(const char *a_pszKey, RTCRestObjectBase const &a_rValue, bool a_fReplace,
                                        size_t a_cchKey /*= RTSTR_MAX*/) RT_NOEXCEPT
{
    int rc;
    RTCRestObjectBase *pValueCopy = a_rValue.baseClone();
    if (pValueCopy)
    {
        rc = putWorker(a_pszKey, pValueCopy, a_fReplace, a_cchKey);
        if (RT_SUCCESS(rc))
        { /* likely */ }
        else
            delete pValueCopy;
    }
    else
        rc = VERR_NO_MEMORY;
    return rc;
}


RTCRestObjectBase *RTCRestStringMapBase::getWorker(const char *a_pszKey) RT_NOEXCEPT
{
    if (isNull())
        return NULL;

    MapEntry *pHit = (MapEntry *)RTStrSpaceGet(&m_Map, a_pszKey);
    if (pHit)
        return pHit->pValue;
    return NULL;
}


RTCRestObjectBase const *RTCRestStringMapBase::getWorker(const char *a_pszKey) const RT_NOEXCEPT
{
    if (isNull())
        return NULL;

    MapEntry const *pHit = (MapEntry const *)RTStrSpaceGet((PRTSTRSPACE)&m_Map, a_pszKey);
    if (pHit)
        return pHit->pValue;
    return NULL;
}