VirtualBox

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

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