VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/rest/RTCRestAnyObject.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.8 KB
Line 
1/* $Id: RTCRestAnyObject.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - C++ REST, RTCRestAnyObject implementation.
4 */
5
6/*
7 * Copyright (C) 2018-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_REST
42#include <iprt/cpp/restanyobject.h>
43
44#include <iprt/assert.h>
45#include <iprt/err.h>
46#include <iprt/cpp/restoutput.h>
47
48
49
50/**
51 * Default constructor.
52 */
53RTCRestAnyObject::RTCRestAnyObject() RT_NOEXCEPT
54 : RTCRestObjectBase()
55 , m_pData(NULL)
56{
57 m_fNullIndicator = true;
58}
59
60
61/**
62 * Destructor.
63 */
64RTCRestAnyObject::~RTCRestAnyObject()
65{
66 if (m_pData)
67 {
68 delete m_pData;
69 m_pData = NULL;
70 }
71}
72
73
74/**
75 * Copy constructor.
76 */
77RTCRestAnyObject::RTCRestAnyObject(RTCRestAnyObject const &a_rThat)
78 : RTCRestObjectBase()
79 , m_pData(NULL)
80{
81 int rc = assignCopy(a_rThat);
82 if (RT_FAILURE(rc))
83 throw std::bad_alloc();
84}
85
86
87/**
88 * Copy assignment operator.
89 */
90RTCRestAnyObject &RTCRestAnyObject::operator=(RTCRestAnyObject const &a_rThat)
91{
92 int rc = assignCopy(a_rThat);
93 if (RT_FAILURE(rc))
94 throw std::bad_alloc();
95 return *this;
96}
97
98
99/**
100 * Safe copy assignment method.
101 */
102int RTCRestAnyObject::assignCopy(RTCRestAnyObject const &a_rThat) RT_NOEXCEPT
103{
104 setNull();
105 if ( !a_rThat.m_fNullIndicator
106 && a_rThat.m_pData != NULL)
107 {
108 kTypeClass enmType = a_rThat.m_pData->typeClass();
109 switch (enmType)
110 {
111 case kTypeClass_Bool: return assignCopy(*(RTCRestBool const *)a_rThat.m_pData);
112 case kTypeClass_Int64: return assignCopy(*(RTCRestInt64 const *)a_rThat.m_pData);
113 case kTypeClass_Int32: return assignCopy(*(RTCRestInt32 const *)a_rThat.m_pData);
114 case kTypeClass_Int16: return assignCopy(*(RTCRestInt16 const *)a_rThat.m_pData);
115 case kTypeClass_Double: return assignCopy(*(RTCRestDouble const *)a_rThat.m_pData);
116 case kTypeClass_String: return assignCopy(*(RTCRestString const *)a_rThat.m_pData);
117 case kTypeClass_Array: return assignCopy(*(RTCRestArray<RTCRestAnyObject> const *)a_rThat.m_pData);
118 case kTypeClass_StringMap: return assignCopy(*(RTCRestStringMap<RTCRestAnyObject> const *)a_rThat.m_pData);
119
120 /* Currently unused of invalid: */
121 case kTypeClass_Date:
122 case kTypeClass_Uuid:
123 case kTypeClass_Binary:
124 case kTypeClass_StringEnum:
125 case kTypeClass_AnyObject:
126 case kTypeClass_DataObject:
127 case kTypeClass_Invalid:
128 AssertFailedReturn(VERR_REST_INTERNAL_ERROR_7);
129 }
130 }
131 return VINF_SUCCESS;
132}
133
134
135/**
136 * Safe copy assignment method, boolean variant.
137 */
138int RTCRestAnyObject::assignCopy(RTCRestBool const &a_rThat) RT_NOEXCEPT
139{
140 setNull();
141 RTCRestBool *pData = new (std::nothrow) RTCRestBool();
142 if (pData)
143 {
144 m_pData = pData;
145 m_fNullIndicator = false;
146 return pData->assignCopy(a_rThat);
147 }
148 return VERR_NO_MEMORY;
149}
150
151
152/**
153 * Safe copy assignment method, int64_t variant.
154 */
155int RTCRestAnyObject::assignCopy(RTCRestInt64 const &a_rThat) RT_NOEXCEPT
156{
157 setNull();
158 RTCRestInt64 *pData = new (std::nothrow) RTCRestInt64();
159 if (pData)
160 {
161 m_pData = pData;
162 m_fNullIndicator = false;
163 return pData->assignCopy(a_rThat);
164 }
165 return VERR_NO_MEMORY;
166}
167
168
169/**
170 * Safe copy assignment method, int32_t variant.
171 */
172int RTCRestAnyObject::assignCopy(RTCRestInt32 const &a_rThat) RT_NOEXCEPT
173{
174 setNull();
175 RTCRestInt32 *pData = new (std::nothrow) RTCRestInt32();
176 if (pData)
177 {
178 m_pData = pData;
179 m_fNullIndicator = false;
180 return pData->assignCopy(a_rThat);
181 }
182 return VERR_NO_MEMORY;
183}
184
185
186/**
187 * Safe copy assignment method, int16_t variant.
188 */
189int RTCRestAnyObject::assignCopy(RTCRestInt16 const &a_rThat) RT_NOEXCEPT
190{
191 setNull();
192 RTCRestInt16 *pData = new (std::nothrow) RTCRestInt16();
193 if (pData)
194 {
195 m_pData = pData;
196 m_fNullIndicator = false;
197 return pData->assignCopy(a_rThat);
198 }
199 return VERR_NO_MEMORY;
200}
201
202
203/**
204 * Safe copy assignment method, double variant.
205 */
206int RTCRestAnyObject::assignCopy(RTCRestDouble const &a_rThat) RT_NOEXCEPT
207{
208 setNull();
209 RTCRestDouble *pData = new (std::nothrow) RTCRestDouble();
210 if (pData)
211 {
212 m_pData = pData;
213 m_fNullIndicator = false;
214 return pData->assignCopy(a_rThat);
215 }
216 return VERR_NO_MEMORY;
217}
218
219
220/**
221 * Safe copy assignment method, string variant.
222 */
223int RTCRestAnyObject::assignCopy(RTCRestString const &a_rThat) RT_NOEXCEPT
224{
225 setNull();
226 RTCRestString *pData = new (std::nothrow) RTCRestString();
227 if (pData)
228 {
229 m_pData = pData;
230 m_fNullIndicator = false;
231 return pData->assignCopy(a_rThat);
232 }
233 return VERR_NO_MEMORY;
234}
235
236
237/**
238 * Safe copy assignment method, array variant.
239 */
240int RTCRestAnyObject::assignCopy(RTCRestArray<RTCRestAnyObject> const &a_rThat) RT_NOEXCEPT
241{
242 setNull();
243 RTCRestArray<RTCRestAnyObject> *pData = new (std::nothrow) RTCRestArray<RTCRestAnyObject>();
244 if (pData)
245 {
246 m_pData = pData;
247 m_fNullIndicator = false;
248 return pData->assignCopy(a_rThat);
249 }
250 return VERR_NO_MEMORY;
251}
252
253
254/**
255 * Safe copy assignment method, string map variant.
256 */
257int RTCRestAnyObject::assignCopy(RTCRestStringMap<RTCRestAnyObject> const &a_rThat) RT_NOEXCEPT
258{
259 setNull();
260 RTCRestStringMap<RTCRestAnyObject> *pData = new (std::nothrow) RTCRestStringMap<RTCRestAnyObject>();
261 if (pData)
262 {
263 m_pData = pData;
264 m_fNullIndicator = false;
265 return pData->assignCopy(a_rThat);
266 }
267 return VERR_NO_MEMORY;
268}
269
270
271/**
272 * Safe value assignment method, boolean variant.
273 */
274int RTCRestAnyObject::assignValue(bool a_fValue) RT_NOEXCEPT
275{
276 setNull();
277 RTCRestBool *pData = new (std::nothrow) RTCRestBool();
278 if (pData)
279 {
280 m_pData = pData;
281 pData->assignValue(a_fValue);
282 m_fNullIndicator = false;
283 return VINF_SUCCESS;
284 }
285 return VERR_NO_MEMORY;
286}
287
288
289/**
290 * Safe value assignment method, int64_t variant.
291 */
292int RTCRestAnyObject::assignValue(int64_t a_iValue) RT_NOEXCEPT
293{
294 setNull();
295 RTCRestInt64 *pData = new (std::nothrow) RTCRestInt64();
296 if (pData)
297 {
298 m_pData = pData;
299 pData->assignValue(a_iValue);
300 m_fNullIndicator = false;
301 return VINF_SUCCESS;
302 }
303 return VERR_NO_MEMORY;
304}
305
306
307/**
308 * Safe value assignment method, int32_t variant.
309 */
310int RTCRestAnyObject::assignValue(int32_t a_iValue) RT_NOEXCEPT
311{
312 setNull();
313 RTCRestInt32 *pData = new (std::nothrow) RTCRestInt32();
314 if (pData)
315 {
316 m_pData = pData;
317 pData->assignValue(a_iValue);
318 m_fNullIndicator = false;
319 return VINF_SUCCESS;
320 }
321 return VERR_NO_MEMORY;
322}
323
324
325/**
326 * Safe value assignment method, int16_t variant.
327 */
328int RTCRestAnyObject::assignValue(int16_t a_iValue) RT_NOEXCEPT
329{
330 setNull();
331 RTCRestInt16 *pData = new (std::nothrow) RTCRestInt16();
332 if (pData)
333 {
334 m_pData = pData;
335 pData->assignValue(a_iValue);
336 m_fNullIndicator = false;
337 return VINF_SUCCESS;
338 }
339 return VERR_NO_MEMORY;
340}
341
342
343/**
344 * Safe value assignment method, double variant.
345 */
346int RTCRestAnyObject::assignValue(double a_iValue) RT_NOEXCEPT
347{
348 setNull();
349 RTCRestDouble *pData = new (std::nothrow) RTCRestDouble();
350 if (pData)
351 {
352 m_pData = pData;
353 pData->assignValue(a_iValue);
354 m_fNullIndicator = false;
355 return VINF_SUCCESS;
356 }
357 return VERR_NO_MEMORY;
358}
359
360
361/**
362 * Safe value assignment method, string variant.
363 */
364int RTCRestAnyObject::assignValue(RTCString const &a_rValue) RT_NOEXCEPT
365{
366 setNull();
367 RTCRestString *pData = new (std::nothrow) RTCRestString();
368 if (pData)
369 {
370 m_pData = pData;
371 m_fNullIndicator = false;
372 return pData->assignNoThrow(a_rValue);
373 }
374 return VERR_NO_MEMORY;
375}
376
377
378/**
379 * Safe value assignment method, C-string variant.
380 */
381int RTCRestAnyObject::assignValue(const char *a_pszValue) RT_NOEXCEPT
382{
383 setNull();
384 RTCRestString *pData = new (std::nothrow) RTCRestString();
385 if (pData)
386 {
387 m_pData = pData;
388 m_fNullIndicator = false;
389 return pData->assignNoThrow(a_pszValue);
390 }
391 return VERR_NO_MEMORY;
392}
393
394
395RTCRestObjectBase *RTCRestAnyObject::baseClone() const RT_NOEXCEPT
396{
397 RTCRestAnyObject *pClone = new (std::nothrow) RTCRestAnyObject();
398 if (pClone)
399 {
400 int rc = pClone->assignCopy(*this);
401 if (RT_SUCCESS(rc))
402 return pClone;
403 delete pClone;
404 }
405 return NULL;
406}
407
408
409int RTCRestAnyObject::setNull(void) RT_NOEXCEPT
410{
411 if (m_pData)
412 {
413 delete m_pData;
414 m_pData = NULL;
415 }
416 return RTCRestObjectBase::setNull();
417}
418
419
420int RTCRestAnyObject::resetToDefault() RT_NOEXCEPT
421{
422 if (m_pData)
423 return m_pData->resetToDefault();
424 return VINF_SUCCESS;
425}
426
427
428RTCRestOutputBase &RTCRestAnyObject::serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT
429{
430 if (m_pData)
431 return m_pData->serializeAsJson(a_rDst);
432 a_rDst.nullValue();
433 return a_rDst;
434}
435
436
437int RTCRestAnyObject::deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT
438{
439 setNull();
440
441 RTJSONVALTYPE enmType = RTJsonValueGetType(a_rCursor.m_hValue);
442 switch (enmType)
443 {
444 case RTJSONVALTYPE_OBJECT:
445 {
446 RTCRestStringMap<RTCRestAnyObject> *pMap = new (std::nothrow) RTCRestStringMap<RTCRestAnyObject>();
447 if (pMap)
448 {
449 m_pData = pMap;
450 m_fNullIndicator = false;
451 return pMap->deserializeFromJson(a_rCursor);
452 }
453 break;
454 }
455
456 case RTJSONVALTYPE_ARRAY:
457 {
458 RTCRestArray<RTCRestAnyObject> *pArray = new (std::nothrow) RTCRestArray<RTCRestAnyObject>();
459 if (pArray)
460 {
461 m_pData = pArray;
462 m_fNullIndicator = false;
463 return pArray->deserializeFromJson(a_rCursor);
464 }
465 break;
466 }
467
468 case RTJSONVALTYPE_STRING:
469 {
470 RTCRestString *pString = new (std::nothrow) RTCRestString();
471 if (pString)
472 {
473 m_pData = pString;
474 m_fNullIndicator = false;
475 return pString->deserializeFromJson(a_rCursor);
476 }
477 break;
478 }
479
480 case RTJSONVALTYPE_INTEGER:
481 {
482 RTCRestInt64 *pInt64 = new (std::nothrow) RTCRestInt64();
483 if (pInt64)
484 {
485 m_pData = pInt64;
486 m_fNullIndicator = false;
487 return pInt64->deserializeFromJson(a_rCursor);
488 }
489 break;
490 }
491
492 case RTJSONVALTYPE_NUMBER:
493 {
494 RTCRestDouble *pDouble = new (std::nothrow) RTCRestDouble();
495 if (pDouble)
496 {
497 m_pData = pDouble;
498 m_fNullIndicator = false;
499 return pDouble->deserializeFromJson(a_rCursor);
500 }
501 break;
502 }
503
504 case RTJSONVALTYPE_NULL:
505 return VINF_SUCCESS;
506
507 case RTJSONVALTYPE_TRUE:
508 case RTJSONVALTYPE_FALSE:
509 {
510 RTCRestBool *pBool = new (std::nothrow) RTCRestBool();
511 if (pBool)
512 {
513 m_pData = pBool;
514 m_fNullIndicator = false;
515 pBool->assignValue(enmType == RTJSONVALTYPE_TRUE);
516 return VINF_SUCCESS;
517 }
518 break;
519 }
520
521 /* no break. */
522 case RTJSONVALTYPE_INVALID:
523 case RTJSONVALTYPE_32BIT_HACK:
524 break;
525 }
526 return a_rCursor.m_pPrimary->addError(a_rCursor, VERR_WRONG_TYPE, "RTCRestAnyObject found %d (%s)",
527 enmType, RTJsonValueTypeName(enmType));
528}
529
530
531int RTCRestAnyObject::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const RT_NOEXCEPT
532{
533 if (m_pData)
534 return m_pData->toString(a_pDst, a_fFlags);
535 if (a_fFlags & kToString_Append)
536 return a_pDst->appendNoThrow(RT_STR_TUPLE("null"));
537 return a_pDst->assignNoThrow(RT_STR_TUPLE("null"));
538}
539
540
541int RTCRestAnyObject::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/,
542 uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) RT_NOEXCEPT
543{
544 return RTCRestObjectBase::fromString(a_rValue, a_pszName, a_pErrInfo, a_fFlags);
545}
546
547
548RTCRestObjectBase::kTypeClass RTCRestAnyObject::typeClass(void) const RT_NOEXCEPT
549{
550 return kTypeClass_AnyObject;
551}
552
553
554const char *RTCRestAnyObject::typeName(void) const RT_NOEXCEPT
555{
556 if (m_pData)
557 {
558 kTypeClass enmType = m_pData->typeClass();
559 switch (enmType)
560 {
561 case kTypeClass_Bool: return "RTCRestAnyObject[Bool]";
562 case kTypeClass_Int64: return "RTCRestAnyObject[Int64]";
563 case kTypeClass_Int32: return "RTCRestAnyObject[Int32]";
564 case kTypeClass_Int16: return "RTCRestAnyObject[Int16]";
565 case kTypeClass_Double: return "RTCRestAnyObject[Double]";
566 case kTypeClass_String: return "RTCRestAnyObject[String]";
567 case kTypeClass_Array: return "RTCRestAnyObject[Array]";
568 case kTypeClass_StringMap: return "RTCRestAnyObject[StringMap]";
569
570 /* Currently unused of invalid: */
571 case kTypeClass_Date:
572 case kTypeClass_Uuid:
573 case kTypeClass_Binary:
574 case kTypeClass_StringEnum:
575 case kTypeClass_DataObject:
576 case kTypeClass_AnyObject:
577 case kTypeClass_Invalid:
578 AssertFailed();
579 }
580 }
581 return "RTCRestAnyObject";
582}
583
584
585/**
586 * Factory method.
587 */
588/*static*/ DECLCALLBACK(RTCRestObjectBase *) RTCRestAnyObject::createInstance(void) RT_NOEXCEPT
589{
590 return new (std::nothrow) RTCRestAnyObject();
591}
592
593
594/**
595 * @copydoc RTCRestObjectBase::FNDESERIALIZEINSTANCEFROMJSON
596 */
597/*static*/ DECLCALLBACK(int)
598RTCRestAnyObject::deserializeInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance)
599{
600 RTCRestObjectBase *pObj = createInstance();
601 *a_ppInstance = pObj;
602 if (pObj)
603 return pObj->deserializeFromJson(a_rCursor);
604 return a_rCursor.m_pPrimary->addError(a_rCursor, VERR_NO_MEMORY, "Out of memory");
605}
606
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use