summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/common/rest/RTCRestAnyObject.cpp
blob: cc202015318ea3257043e81af4eb8cdb0e5dcef8 (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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/* $Id: RTCRestAnyObject.cpp $ */
/** @file
 * IPRT - C++ REST, RTCRestAnyObject 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/restanyobject.h>

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



/**
 * Default constructor.
 */
RTCRestAnyObject::RTCRestAnyObject() RT_NOEXCEPT
    : RTCRestObjectBase()
    , m_pData(NULL)
{
    m_fNullIndicator = true;
}


/**
 * Destructor.
 */
RTCRestAnyObject::~RTCRestAnyObject()
{
    if (m_pData)
    {
        delete m_pData;
        m_pData = NULL;
    }
}


/**
 * Copy constructor.
 */
RTCRestAnyObject::RTCRestAnyObject(RTCRestAnyObject const &a_rThat)
    : RTCRestObjectBase()
    , m_pData(NULL)
{
    int rc = assignCopy(a_rThat);
    if (RT_FAILURE(rc))
        throw std::bad_alloc();
}


/**
 * Copy assignment operator.
 */
RTCRestAnyObject &RTCRestAnyObject::operator=(RTCRestAnyObject const &a_rThat)
{
    int rc = assignCopy(a_rThat);
    if (RT_FAILURE(rc))
        throw std::bad_alloc();
    return *this;
}


/**
 * Safe copy assignment method.
 */
int RTCRestAnyObject::assignCopy(RTCRestAnyObject const &a_rThat) RT_NOEXCEPT
{
    setNull();
    if (   !a_rThat.m_fNullIndicator
        && a_rThat.m_pData != NULL)
    {
        kTypeClass enmType = a_rThat.m_pData->typeClass();
        switch (enmType)
        {
            case kTypeClass_Bool:       return assignCopy(*(RTCRestBool const *)a_rThat.m_pData);
            case kTypeClass_Int64:      return assignCopy(*(RTCRestInt64 const *)a_rThat.m_pData);
            case kTypeClass_Int32:      return assignCopy(*(RTCRestInt32 const *)a_rThat.m_pData);
            case kTypeClass_Int16:      return assignCopy(*(RTCRestInt16 const *)a_rThat.m_pData);
            case kTypeClass_Double:     return assignCopy(*(RTCRestDouble const *)a_rThat.m_pData);
            case kTypeClass_String:     return assignCopy(*(RTCRestString const *)a_rThat.m_pData);
            case kTypeClass_Array:      return assignCopy(*(RTCRestArray<RTCRestAnyObject> const *)a_rThat.m_pData);
            case kTypeClass_StringMap:  return assignCopy(*(RTCRestStringMap<RTCRestAnyObject> const *)a_rThat.m_pData);

            /* Currently unused of invalid: */
            case kTypeClass_Date:
            case kTypeClass_Uuid:
            case kTypeClass_Binary:
            case kTypeClass_StringEnum:
            case kTypeClass_AnyObject:
            case kTypeClass_DataObject:
            case kTypeClass_Invalid:
                AssertFailedReturn(VERR_REST_INTERNAL_ERROR_7);
        }
    }
    return VINF_SUCCESS;
}


/**
 * Safe copy assignment method, boolean variant.
 */
int RTCRestAnyObject::assignCopy(RTCRestBool const &a_rThat) RT_NOEXCEPT
{
    setNull();
    RTCRestBool *pData = new (std::nothrow) RTCRestBool();
    if (pData)
    {
        m_pData = pData;
        m_fNullIndicator = false;
        return pData->assignCopy(a_rThat);
    }
    return VERR_NO_MEMORY;
}


/**
 * Safe copy assignment method, int64_t variant.
 */
int RTCRestAnyObject::assignCopy(RTCRestInt64 const &a_rThat) RT_NOEXCEPT
{
    setNull();
    RTCRestInt64 *pData = new (std::nothrow) RTCRestInt64();
    if (pData)
    {
        m_pData = pData;
        m_fNullIndicator = false;
        return pData->assignCopy(a_rThat);
    }
    return VERR_NO_MEMORY;
}


/**
 * Safe copy assignment method, int32_t variant.
 */
int RTCRestAnyObject::assignCopy(RTCRestInt32 const &a_rThat) RT_NOEXCEPT
{
    setNull();
    RTCRestInt32 *pData = new (std::nothrow) RTCRestInt32();
    if (pData)
    {
        m_pData = pData;
        m_fNullIndicator = false;
        return pData->assignCopy(a_rThat);
    }
    return VERR_NO_MEMORY;
}


/**
 * Safe copy assignment method, int16_t variant.
 */
int RTCRestAnyObject::assignCopy(RTCRestInt16 const &a_rThat) RT_NOEXCEPT
{
    setNull();
    RTCRestInt16 *pData = new (std::nothrow) RTCRestInt16();
    if (pData)
    {
        m_pData = pData;
        m_fNullIndicator = false;
        return pData->assignCopy(a_rThat);
    }
    return VERR_NO_MEMORY;
}


/**
 * Safe copy assignment method, double variant.
 */
int RTCRestAnyObject::assignCopy(RTCRestDouble const &a_rThat) RT_NOEXCEPT
{
    setNull();
    RTCRestDouble *pData = new (std::nothrow) RTCRestDouble();
    if (pData)
    {
        m_pData = pData;
        m_fNullIndicator = false;
        return pData->assignCopy(a_rThat);
    }
    return VERR_NO_MEMORY;
}


/**
 * Safe copy assignment method, string variant.
 */
int RTCRestAnyObject::assignCopy(RTCRestString const &a_rThat) RT_NOEXCEPT
{
    setNull();
    RTCRestString *pData = new (std::nothrow) RTCRestString();
    if (pData)
    {
        m_pData = pData;
        m_fNullIndicator = false;
        return pData->assignCopy(a_rThat);
    }
    return VERR_NO_MEMORY;
}


/**
 * Safe copy assignment method, array variant.
 */
int RTCRestAnyObject::assignCopy(RTCRestArray<RTCRestAnyObject> const &a_rThat) RT_NOEXCEPT
{
    setNull();
    RTCRestArray<RTCRestAnyObject> *pData = new (std::nothrow) RTCRestArray<RTCRestAnyObject>();
    if (pData)
    {
        m_pData = pData;
        m_fNullIndicator = false;
        return pData->assignCopy(a_rThat);
    }
    return VERR_NO_MEMORY;
}


/**
 * Safe copy assignment method, string map variant.
 */
int RTCRestAnyObject::assignCopy(RTCRestStringMap<RTCRestAnyObject> const &a_rThat) RT_NOEXCEPT
{
    setNull();
    RTCRestStringMap<RTCRestAnyObject> *pData = new (std::nothrow) RTCRestStringMap<RTCRestAnyObject>();
    if (pData)
    {
        m_pData = pData;
        m_fNullIndicator = false;
        return pData->assignCopy(a_rThat);
    }
    return VERR_NO_MEMORY;
}


/**
 * Safe value assignment method, boolean variant.
 */
int RTCRestAnyObject::assignValue(bool a_fValue) RT_NOEXCEPT
{
    setNull();
    RTCRestBool *pData = new (std::nothrow) RTCRestBool();
    if (pData)
    {
        m_pData = pData;
        pData->assignValue(a_fValue);
        m_fNullIndicator = false;
        return VINF_SUCCESS;
    }
    return VERR_NO_MEMORY;
}


/**
 * Safe value assignment method, int64_t variant.
 */
int RTCRestAnyObject::assignValue(int64_t a_iValue) RT_NOEXCEPT
{
    setNull();
    RTCRestInt64 *pData = new (std::nothrow) RTCRestInt64();
    if (pData)
    {
        m_pData = pData;
        pData->assignValue(a_iValue);
        m_fNullIndicator = false;
        return VINF_SUCCESS;
    }
    return VERR_NO_MEMORY;
}


/**
 * Safe value assignment method, int32_t variant.
 */
int RTCRestAnyObject::assignValue(int32_t a_iValue) RT_NOEXCEPT
{
    setNull();
    RTCRestInt32 *pData = new (std::nothrow) RTCRestInt32();
    if (pData)
    {
        m_pData = pData;
        pData->assignValue(a_iValue);
        m_fNullIndicator = false;
        return VINF_SUCCESS;
    }
    return VERR_NO_MEMORY;
}


/**
 * Safe value assignment method, int16_t variant.
 */
int RTCRestAnyObject::assignValue(int16_t a_iValue) RT_NOEXCEPT
{
    setNull();
    RTCRestInt16 *pData = new (std::nothrow) RTCRestInt16();
    if (pData)
    {
        m_pData = pData;
        pData->assignValue(a_iValue);
        m_fNullIndicator = false;
        return VINF_SUCCESS;
    }
    return VERR_NO_MEMORY;
}


/**
 * Safe value assignment method, double variant.
 */
int RTCRestAnyObject::assignValue(double a_iValue) RT_NOEXCEPT
{
    setNull();
    RTCRestDouble *pData = new (std::nothrow) RTCRestDouble();
    if (pData)
    {
        m_pData = pData;
        pData->assignValue(a_iValue);
        m_fNullIndicator = false;
        return VINF_SUCCESS;
    }
    return VERR_NO_MEMORY;
}


/**
 * Safe value assignment method, string variant.
 */
int RTCRestAnyObject::assignValue(RTCString const &a_rValue) RT_NOEXCEPT
{
    setNull();
    RTCRestString *pData = new (std::nothrow) RTCRestString();
    if (pData)
    {
        m_pData = pData;
        m_fNullIndicator = false;
        return pData->assignNoThrow(a_rValue);
    }
    return VERR_NO_MEMORY;
}


/**
 * Safe value assignment method, C-string variant.
 */
int RTCRestAnyObject::assignValue(const char *a_pszValue) RT_NOEXCEPT
{
    setNull();
    RTCRestString *pData = new (std::nothrow) RTCRestString();
    if (pData)
    {
        m_pData = pData;
        m_fNullIndicator = false;
        return pData->assignNoThrow(a_pszValue);
    }
    return VERR_NO_MEMORY;
}


RTCRestObjectBase *RTCRestAnyObject::baseClone() const RT_NOEXCEPT
{
    RTCRestAnyObject *pClone = new (std::nothrow) RTCRestAnyObject();
    if (pClone)
    {
        int rc = pClone->assignCopy(*this);
        if (RT_SUCCESS(rc))
            return pClone;
        delete pClone;
    }
    return NULL;
}


int RTCRestAnyObject::setNull(void) RT_NOEXCEPT
{
    if (m_pData)
    {
        delete m_pData;
        m_pData = NULL;
    }
    return RTCRestObjectBase::setNull();
}


int RTCRestAnyObject::resetToDefault() RT_NOEXCEPT
{
    if (m_pData)
        return m_pData->resetToDefault();
    return VINF_SUCCESS;
}


RTCRestOutputBase &RTCRestAnyObject::serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT
{
    if (m_pData)
        return m_pData->serializeAsJson(a_rDst);
    a_rDst.nullValue();
    return a_rDst;
}


int RTCRestAnyObject::deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT
{
    setNull();

    RTJSONVALTYPE enmType = RTJsonValueGetType(a_rCursor.m_hValue);
    switch (enmType)
    {
        case RTJSONVALTYPE_OBJECT:
        {
            RTCRestStringMap<RTCRestAnyObject> *pMap = new (std::nothrow) RTCRestStringMap<RTCRestAnyObject>();
            if (pMap)
            {
                m_pData = pMap;
                m_fNullIndicator = false;
                return pMap->deserializeFromJson(a_rCursor);
            }
            break;
        }

        case RTJSONVALTYPE_ARRAY:
        {
            RTCRestArray<RTCRestAnyObject> *pArray = new (std::nothrow) RTCRestArray<RTCRestAnyObject>();
            if (pArray)
            {
                m_pData = pArray;
                m_fNullIndicator = false;
                return pArray->deserializeFromJson(a_rCursor);
            }
            break;
        }

        case RTJSONVALTYPE_STRING:
        {
            RTCRestString *pString = new (std::nothrow) RTCRestString();
            if (pString)
            {
                m_pData = pString;
                m_fNullIndicator = false;
                return pString->deserializeFromJson(a_rCursor);
            }
            break;
        }

        case RTJSONVALTYPE_INTEGER:
        {
            RTCRestInt64 *pInt64 = new (std::nothrow) RTCRestInt64();
            if (pInt64)
            {
                m_pData = pInt64;
                m_fNullIndicator = false;
                return pInt64->deserializeFromJson(a_rCursor);
            }
            break;
        }

        case RTJSONVALTYPE_NUMBER:
        {
            RTCRestDouble *pDouble = new (std::nothrow) RTCRestDouble();
            if (pDouble)
            {
                m_pData = pDouble;
                m_fNullIndicator = false;
                return pDouble->deserializeFromJson(a_rCursor);
            }
            break;
        }

        case RTJSONVALTYPE_NULL:
            return VINF_SUCCESS;

        case RTJSONVALTYPE_TRUE:
        case RTJSONVALTYPE_FALSE:
        {
            RTCRestBool *pBool = new (std::nothrow) RTCRestBool();
            if (pBool)
            {
                m_pData = pBool;
                m_fNullIndicator = false;
                pBool->assignValue(enmType == RTJSONVALTYPE_TRUE);
                return VINF_SUCCESS;
            }
            break;
        }

        /* no break. */
        case RTJSONVALTYPE_INVALID:
        case RTJSONVALTYPE_32BIT_HACK:
            break;
    }
    return a_rCursor.m_pPrimary->addError(a_rCursor, VERR_WRONG_TYPE, "RTCRestAnyObject found %d (%s)",
                                          enmType, RTJsonValueTypeName(enmType));
}


int RTCRestAnyObject::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const RT_NOEXCEPT
{
    if (m_pData)
        return m_pData->toString(a_pDst, a_fFlags);
    if (a_fFlags & kToString_Append)
        return a_pDst->appendNoThrow(RT_STR_TUPLE("null"));
    return a_pDst->assignNoThrow(RT_STR_TUPLE("null"));
}


int RTCRestAnyObject::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/,
                                 uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) RT_NOEXCEPT
{
    return RTCRestObjectBase::fromString(a_rValue, a_pszName, a_pErrInfo, a_fFlags);
}


RTCRestObjectBase::kTypeClass RTCRestAnyObject::typeClass(void) const RT_NOEXCEPT
{
    return kTypeClass_AnyObject;
}


const char *RTCRestAnyObject::typeName(void) const RT_NOEXCEPT
{
    if (m_pData)
    {
        kTypeClass enmType = m_pData->typeClass();
        switch (enmType)
        {
            case kTypeClass_Bool:       return "RTCRestAnyObject[Bool]";
            case kTypeClass_Int64:      return "RTCRestAnyObject[Int64]";
            case kTypeClass_Int32:      return "RTCRestAnyObject[Int32]";
            case kTypeClass_Int16:      return "RTCRestAnyObject[Int16]";
            case kTypeClass_Double:     return "RTCRestAnyObject[Double]";
            case kTypeClass_String:     return "RTCRestAnyObject[String]";
            case kTypeClass_Array:      return "RTCRestAnyObject[Array]";
            case kTypeClass_StringMap:  return "RTCRestAnyObject[StringMap]";

            /* Currently unused of invalid: */
            case kTypeClass_Date:
            case kTypeClass_Uuid:
            case kTypeClass_Binary:
            case kTypeClass_StringEnum:
            case kTypeClass_DataObject:
            case kTypeClass_AnyObject:
            case kTypeClass_Invalid:
                AssertFailed();
        }
    }
    return "RTCRestAnyObject";
}


/**
 * Factory method.
 */
/*static*/ DECLCALLBACK(RTCRestObjectBase *) RTCRestAnyObject::createInstance(void) RT_NOEXCEPT
{
    return new (std::nothrow) RTCRestAnyObject();
}


/**
 * @copydoc RTCRestObjectBase::FNDESERIALIZEINSTANCEFROMJSON
 */
/*static*/ DECLCALLBACK(int)
RTCRestAnyObject::deserializeInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance)
{
    RTCRestObjectBase *pObj = createInstance();
    *a_ppInstance = pObj;
    if (pObj)
        return pObj->deserializeFromJson(a_rCursor);
    return a_rCursor.m_pPrimary->addError(a_rCursor, VERR_NO_MEMORY, "Out of memory");
}