VirtualBox

source: vbox/trunk/include/iprt/cpp/restbase.h@ 100997

Last change on this file since 100997 was 98103, checked in by vboxsync, 23 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: 44.0 KB
Line 
1/** @file
2 * IPRT - C++ Representational State Transfer (REST) Base Classes.
3 */
4
5/*
6 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation, in version 3 of the
14 * License.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses>.
23 *
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
26 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 * in the VirtualBox distribution, in which case the provisions of the
28 * CDDL are applicable instead of those of the GPL.
29 *
30 * You may elect to license modified versions of this file under the
31 * terms and conditions of either the GPL or the CDDL or both.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef IPRT_INCLUDED_cpp_restbase_h
37#define IPRT_INCLUDED_cpp_restbase_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#include <iprt/cdefs.h>
43#include <iprt/types.h>
44#include <iprt/errcore.h> /* VERR_NO_MEMORY */
45#include <iprt/json.h>
46#include <iprt/stdarg.h>
47#include <iprt/time.h>
48#include <iprt/cpp/ministring.h>
49
50
51/** @defgroup grp_rt_cpp_restbase C++ Representational State Transfer (REST) Base Classes.
52 * @ingroup grp_rt_cpp
53 * @{
54 */
55
56/* forward decl: */
57class RTCRestOutputBase;
58class RTCRestJsonPrimaryCursor;
59
60/**
61 * JSON cursor structure.
62 *
63 * This reduces the number of parameters passed around when deserializing JSON
64 * input and also helps constructing full object name for logging and error reporting.
65 */
66struct RT_DECL_CLASS RTCRestJsonCursor
67{
68 /** Handle to the value being parsed. */
69 RTJSONVAL m_hValue;
70 /** Name of the value. */
71 const char *m_pszName;
72 /** Pointer to the parent, NULL if primary. */
73 struct RTCRestJsonCursor const *m_pParent;
74 /** Pointer to the primary cursor structure. */
75 RTCRestJsonPrimaryCursor *m_pPrimary;
76
77 RTCRestJsonCursor(struct RTCRestJsonCursor const &a_rParent) RT_NOEXCEPT
78 : m_hValue(NIL_RTJSONVAL), m_pszName(NULL), m_pParent(&a_rParent), m_pPrimary(a_rParent.m_pPrimary)
79 { }
80
81 RTCRestJsonCursor(RTJSONVAL hValue, const char *pszName, struct RTCRestJsonCursor *pParent) RT_NOEXCEPT
82 : m_hValue(hValue), m_pszName(pszName), m_pParent(pParent), m_pPrimary(pParent->m_pPrimary)
83 { }
84
85 RTCRestJsonCursor(RTJSONVAL hValue, const char *pszName) RT_NOEXCEPT
86 : m_hValue(hValue), m_pszName(pszName), m_pParent(NULL), m_pPrimary(NULL)
87 { }
88
89 ~RTCRestJsonCursor()
90 {
91 if (m_hValue != NIL_RTJSONVAL)
92 {
93 RTJsonValueRelease(m_hValue);
94 m_hValue = NIL_RTJSONVAL;
95 }
96 }
97};
98
99
100/**
101 * The primary JSON cursor class.
102 */
103class RT_DECL_CLASS RTCRestJsonPrimaryCursor
104{
105public:
106 /** The cursor for the first level. */
107 RTCRestJsonCursor m_Cursor;
108 /** Error info keeper. */
109 PRTERRINFO m_pErrInfo;
110
111 /** Creates a primary json cursor with optiona error info. */
112 RTCRestJsonPrimaryCursor(RTJSONVAL hValue, const char *pszName, PRTERRINFO pErrInfo = NULL) RT_NOEXCEPT
113 : m_Cursor(hValue, pszName)
114 , m_pErrInfo(pErrInfo)
115 {
116 m_Cursor.m_pPrimary = this;
117 }
118
119 virtual ~RTCRestJsonPrimaryCursor()
120 { }
121
122 /**
123 * Add an error message.
124 *
125 * @returns a_rc
126 * @param a_rCursor The cursor reporting the error.
127 * @param a_rc The status code.
128 * @param a_pszFormat Format string.
129 * @param ... Format string arguments.
130 */
131 virtual int addError(RTCRestJsonCursor const &a_rCursor, int a_rc, const char *a_pszFormat, ...) RT_NOEXCEPT;
132
133 /**
134 * Reports that the current field is not known.
135 *
136 * @returns Status to propagate.
137 * @param a_rCursor The cursor for the field.
138 */
139 virtual int unknownField(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT;
140
141 /**
142 * Copies the full path into pszDst.
143 *
144 * @returns pszDst
145 * @param a_rCursor The cursor to start walking at.
146 * @param a_pszDst Where to put the path.
147 * @param a_cbDst Size of the destination buffer.
148 */
149 virtual char *getPath(RTCRestJsonCursor const &a_rCursor, char *a_pszDst, size_t a_cbDst) const RT_NOEXCEPT;
150};
151
152
153/**
154 * Abstract base class for REST primitive types and data objects (via
155 * RTCRestDataObject).
156 *
157 * The only information this keeps is the null indicator.
158 */
159class RT_DECL_CLASS RTCRestObjectBase
160{
161public:
162 RTCRestObjectBase() RT_NOEXCEPT;
163 RTCRestObjectBase(RTCRestObjectBase const &a_rThat) RT_NOEXCEPT;
164 virtual ~RTCRestObjectBase();
165
166 /** Copy assignment operator. */
167 RTCRestObjectBase &operator=(RTCRestObjectBase const &a_rThat) RT_NOEXCEPT;
168
169 /**
170 * Create a copy of this object.
171 *
172 * @returns Pointer to copy.
173 */
174 virtual RTCRestObjectBase *baseClone() const RT_NOEXCEPT = 0;
175
176 /**
177 * Tests if the object is @a null.
178 * @returns true if null, false if not.
179 */
180 inline bool isNull(void) const RT_NOEXCEPT { return m_fNullIndicator; };
181
182 /**
183 * Sets the object to @a null and fills it with defaults.
184 * @returns IPRT status code (from resetToDefault).
185 */
186 virtual int setNull(void) RT_NOEXCEPT;
187
188 /**
189 * Sets the object to not-null state (i.e. undoes setNull()).
190 * @remarks Only really important for strings.
191 */
192 virtual void setNotNull(void) RT_NOEXCEPT;
193
194 /**
195 * Resets the object to all default values.
196 * @returns IPRT status code.
197 */
198 virtual int resetToDefault() RT_NOEXCEPT = 0;
199
200 /**
201 * Serialize the object as JSON.
202 *
203 * @returns a_rDst
204 * @param a_rDst The destination for the serialization.
205 */
206 virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT = 0;
207
208 /**
209 * Deserialize object from the given JSON iterator.
210 *
211 * @returns IPRT status code.
212 * @param a_rCursor The JSON cursor.
213 */
214 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT = 0;
215
216 /**
217 * Polymorphic JSON deserialization helper that instantiate the matching class using
218 * the discriminator field.
219 *
220 * @returns IPRT status code.
221 * @param a_rCursor The JSON cursor.
222 * @param a_ppInstance Where to return the deserialized instance.
223 * May return an object on failure.
224 */
225 typedef DECLCALLBACKTYPE(int, FNDESERIALIZEINSTANCEFROMJSON,(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance));
226 /** Pointer to a FNDESERIALIZEINSTANCEFROMJSON function. */
227 typedef FNDESERIALIZEINSTANCEFROMJSON *PFNDESERIALIZEINSTANCEFROMJSON;
228
229 /**
230 * Flags for toString().
231 *
232 * The kCollectionFormat_xxx bunch controls multiple values in arrays
233 * are formatted. They are ignored by everyone else.
234 *
235 * @note When adding collection format types, make sure to also
236 * update RTCRestArrayBase::toString().
237 * @note Bit 24 is reserved (for kHdrField_MapCollection).
238 */
239 enum
240 {
241 kCollectionFormat_Unspecified = 0, /**< Not specified. */
242 kCollectionFormat_csv, /**< Comma-separated list. */
243 kCollectionFormat_ssv, /**< Space-separated list. */
244 kCollectionFormat_tsv, /**< Tab-separated list. */
245 kCollectionFormat_pipes, /**< Pipe-separated list. */
246 kCollectionFormat_multi, /**< Special collection type that must be handled by caller of toString. */
247 kCollectionFormat_Mask = 7, /**< Collection type mask. */
248
249 kToString_Append = 8 /**< Append to the string/object (rather than assigning). */
250 };
251
252 /**
253 * String conversion.
254 *
255 * The default implementation of is a wrapper around serializeAsJson().
256 *
257 * @returns IPRT status code.
258 * @param a_pDst Pointer to the destionation string.
259 * @param a_fFlags kCollectionFormat_xxx.
260 */
261 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = kCollectionFormat_Unspecified) const RT_NOEXCEPT;
262
263 /**
264 * String convertsion, naive variant.
265 *
266 * @returns String represenation.
267 */
268 RTCString toString() const;
269
270 /**
271 * Convert from (header) string value.
272 *
273 * The default implementation of is a wrapper around deserializeFromJson().
274 *
275 * @returns IPRT status code.
276 * @param a_rValue The string value string to parse.
277 * @param a_pszName Field name or similar.
278 * @param a_pErrInfo Where to return additional error info. Optional.
279 * @param a_fFlags kCollectionFormat_xxx.
280 */
281 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
282 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_NOEXCEPT;
283
284 /** Type classification */
285 typedef enum kTypeClass
286 {
287 kTypeClass_Invalid = 0,
288 kTypeClass_Bool, /**< Primitive: bool. */
289 kTypeClass_Int64, /**< Primitive: int64_t. */
290 kTypeClass_Int32, /**< Primitive: int32_t. */
291 kTypeClass_Int16, /**< Primitive: int16_t. */
292 kTypeClass_Double, /**< Primitive: double. */
293 kTypeClass_String, /**< Primitive: string. */
294 kTypeClass_Date, /**< Date. */
295 kTypeClass_Uuid, /**< UUID. */
296 kTypeClass_Binary, /**< Binary blob. */
297 kTypeClass_DataObject, /**< Data object child (RTCRestDataObject). */
298 kTypeClass_AnyObject, /**< Any kind of object (RTCRestAnyObject). */
299 kTypeClass_Array, /**< Array (containing any kind of object). */
300 kTypeClass_StringMap, /**< String map (containing any kind of object). */
301 kTypeClass_StringEnum /**< String enum. */
302 } kTypeClass;
303
304 /**
305 * Returns the object type class.
306 */
307 virtual kTypeClass typeClass(void) const RT_NOEXCEPT = 0;
308
309 /**
310 * Returns the object type name.
311 */
312 virtual const char *typeName(void) const RT_NOEXCEPT = 0;
313
314protected:
315 /** Null indicator.
316 * @remarks The null values could be mapped onto C/C++ NULL pointer values,
317 * with the consequence that all data members in objects and such would
318 * have had to been allocated individually, even simple @a bool members.
319 * Given that we're overly paranoid about heap allocations (std::bad_alloc),
320 * it's more fitting to use a null indicator for us.
321 */
322 bool m_fNullIndicator;
323};
324
325
326/**
327 * Class wrapping 'bool'.
328 */
329class RT_DECL_CLASS RTCRestBool : public RTCRestObjectBase
330{
331public:
332 /** Default constructor. */
333 RTCRestBool() RT_NOEXCEPT;
334 /** Copy constructor. */
335 RTCRestBool(RTCRestBool const &a_rThat) RT_NOEXCEPT;
336 /** From value constructor. */
337 RTCRestBool(bool fValue) RT_NOEXCEPT;
338 /** Destructor. */
339 virtual ~RTCRestBool();
340 /** Copy assignment operator. */
341 RTCRestBool &operator=(RTCRestBool const &a_rThat) RT_NOEXCEPT;
342 /** Safe copy assignment method. */
343 int assignCopy(RTCRestBool const &a_rThat) RT_NOEXCEPT;
344 /** Assign value and clear null indicator. */
345 void assignValue(bool a_fValue) RT_NOEXCEPT;
346 /** Make a clone of this object. */
347 inline RTCRestBool *clone() const RT_NOEXCEPT { return (RTCRestBool *)baseClone(); }
348
349 /* Overridden methods: */
350 virtual RTCRestObjectBase *baseClone() const RT_NOEXCEPT RT_OVERRIDE;
351 virtual int resetToDefault() RT_NOEXCEPT RT_OVERRIDE;
352 virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT RT_OVERRIDE;
353 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT RT_OVERRIDE;
354 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_NOEXCEPT RT_OVERRIDE;
355 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
356 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_NOEXCEPT RT_OVERRIDE;
357 virtual kTypeClass typeClass(void) const RT_NOEXCEPT RT_OVERRIDE;
358 virtual const char *typeName(void) const RT_NOEXCEPT RT_OVERRIDE;
359
360 /** Factory method. */
361 static DECLCALLBACK(RTCRestObjectBase *) createInstance(void) RT_NOEXCEPT;
362 /** @copydoc RTCRestObjectBase::FNDESERIALIZEINSTANCEFROMJSON */
363 static DECLCALLBACK(int) deserializeInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT;
364
365public:
366 /** The value. */
367 bool m_fValue;
368};
369
370
371/**
372 * Class wrapping 'int64_t'.
373 */
374class RT_DECL_CLASS RTCRestInt64 : public RTCRestObjectBase
375{
376public:
377 /** Default constructor. */
378 RTCRestInt64() RT_NOEXCEPT;
379 /** Copy constructor. */
380 RTCRestInt64(RTCRestInt64 const &a_rThat) RT_NOEXCEPT;
381 /** From value constructor. */
382 RTCRestInt64(int64_t a_iValue) RT_NOEXCEPT;
383 /** Destructor. */
384 virtual ~RTCRestInt64();
385 /** Copy assignment operator. */
386 RTCRestInt64 &operator=(RTCRestInt64 const &a_rThat) RT_NOEXCEPT;
387 /** Safe copy assignment method. */
388 int assignCopy(RTCRestInt64 const &a_rThat) RT_NOEXCEPT;
389 /** Assign value and clear null indicator. */
390 void assignValue(int64_t a_iValue) RT_NOEXCEPT;
391 /** Make a clone of this object. */
392 inline RTCRestInt64 *clone() const RT_NOEXCEPT { return (RTCRestInt64 *)baseClone(); }
393
394 /* Overridden methods: */
395 virtual RTCRestObjectBase *baseClone() const RT_NOEXCEPT RT_OVERRIDE;
396 virtual int resetToDefault() RT_NOEXCEPT RT_OVERRIDE;
397 virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT RT_OVERRIDE;
398 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT RT_OVERRIDE;
399 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_NOEXCEPT RT_OVERRIDE;
400 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
401 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_NOEXCEPT RT_OVERRIDE;
402 virtual kTypeClass typeClass(void) const RT_NOEXCEPT RT_OVERRIDE;
403 virtual const char *typeName(void) const RT_NOEXCEPT RT_OVERRIDE;
404
405 /** Factory method. */
406 static DECLCALLBACK(RTCRestObjectBase *) createInstance(void) RT_NOEXCEPT;
407 /** @copydoc RTCRestObjectBase::FNDESERIALIZEINSTANCEFROMJSON */
408 static DECLCALLBACK(int) deserializeInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT;
409
410public:
411 /** The value. */
412 int64_t m_iValue;
413};
414
415
416/**
417 * Class wrapping 'int32_t'.
418 */
419class RT_DECL_CLASS RTCRestInt32 : public RTCRestObjectBase
420{
421public:
422 /** Default constructor. */
423 RTCRestInt32() RT_NOEXCEPT;
424 /** Copy constructor. */
425 RTCRestInt32(RTCRestInt32 const &a_rThat) RT_NOEXCEPT;
426 /** From value constructor. */
427 RTCRestInt32(int32_t iValue) RT_NOEXCEPT;
428 /** Destructor. */
429 virtual ~RTCRestInt32() RT_NOEXCEPT;
430 /** Copy assignment operator. */
431 RTCRestInt32 &operator=(RTCRestInt32 const &a_rThat) RT_NOEXCEPT;
432 /** Safe copy assignment method. */
433 int assignCopy(RTCRestInt32 const &a_rThat) RT_NOEXCEPT;
434 /** Assign value and clear null indicator. */
435 void assignValue(int32_t a_iValue) RT_NOEXCEPT;
436 /** Make a clone of this object. */
437 inline RTCRestInt32 *clone() const { return (RTCRestInt32 *)baseClone(); }
438
439 /* Overridden methods: */
440 virtual RTCRestObjectBase *baseClone() const RT_NOEXCEPT RT_OVERRIDE;
441 virtual int resetToDefault() RT_NOEXCEPT RT_OVERRIDE;
442 virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT RT_OVERRIDE;
443 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT RT_OVERRIDE;
444 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_NOEXCEPT RT_OVERRIDE;
445 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
446 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_NOEXCEPT RT_OVERRIDE;
447 virtual kTypeClass typeClass(void) const RT_NOEXCEPT RT_OVERRIDE;
448 virtual const char *typeName(void) const RT_NOEXCEPT RT_OVERRIDE;
449
450 /** Factory method. */
451 static DECLCALLBACK(RTCRestObjectBase *) createInstance(void) RT_NOEXCEPT;
452 /** @copydoc RTCRestObjectBase::FNDESERIALIZEINSTANCEFROMJSON */
453 static DECLCALLBACK(int) deserializeInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT;
454
455public:
456 /** The value. */
457 int32_t m_iValue;
458};
459
460
461/**
462 * Class wrapping 'int16_t'.
463 */
464class RT_DECL_CLASS RTCRestInt16 : public RTCRestObjectBase
465{
466public:
467 /** Default constructor. */
468 RTCRestInt16() RT_NOEXCEPT;
469 /** Copy constructor. */
470 RTCRestInt16(RTCRestInt16 const &a_rThat) RT_NOEXCEPT;
471 /** From value constructor. */
472 RTCRestInt16(int16_t iValue) RT_NOEXCEPT;
473 /** Destructor. */
474 virtual ~RTCRestInt16();
475 /** Copy assignment operator. */
476 RTCRestInt16 &operator=(RTCRestInt16 const &a_rThat) RT_NOEXCEPT;
477 /** Safe copy assignment method. */
478 int assignCopy(RTCRestInt16 const &a_rThat) RT_NOEXCEPT;
479 /** Assign value and clear null indicator. */
480 void assignValue(int16_t a_iValue) RT_NOEXCEPT;
481 /** Make a clone of this object. */
482 inline RTCRestInt16 *clone() const RT_NOEXCEPT { return (RTCRestInt16 *)baseClone(); }
483
484 /* Overridden methods: */
485 virtual RTCRestObjectBase *baseClone() const RT_NOEXCEPT RT_OVERRIDE;
486 virtual int resetToDefault() RT_NOEXCEPT RT_OVERRIDE;
487 virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT RT_OVERRIDE;
488 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT RT_OVERRIDE;
489 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_NOEXCEPT RT_OVERRIDE;
490 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
491 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_NOEXCEPT RT_OVERRIDE;
492 virtual kTypeClass typeClass(void) const RT_NOEXCEPT RT_OVERRIDE;
493 virtual const char *typeName(void) const RT_NOEXCEPT RT_OVERRIDE;
494
495 /** Factory method. */
496 static DECLCALLBACK(RTCRestObjectBase *) createInstance(void) RT_NOEXCEPT;
497 /** @copydoc RTCRestObjectBase::FNDESERIALIZEINSTANCEFROMJSON */
498 static DECLCALLBACK(int) deserializeInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT;
499
500public:
501 /** The value. */
502 int16_t m_iValue;
503};
504
505
506/**
507 * Class wrapping 'double'.
508 */
509class RT_DECL_CLASS RTCRestDouble : public RTCRestObjectBase
510{
511public:
512 /** Default constructor. */
513 RTCRestDouble() RT_NOEXCEPT;
514 /** Copy constructor. */
515 RTCRestDouble(RTCRestDouble const &a_rThat) RT_NOEXCEPT;
516 /** From value constructor. */
517 RTCRestDouble(double rdValue) RT_NOEXCEPT;
518 /** Destructor. */
519 virtual ~RTCRestDouble();
520 /** Copy assignment operator. */
521 RTCRestDouble &operator=(RTCRestDouble const &a_rThat) RT_NOEXCEPT;
522 /** Safe copy assignment method. */
523 int assignCopy(RTCRestDouble const &a_rThat) RT_NOEXCEPT;
524 /** Assign value and clear null indicator. */
525 void assignValue(double a_rdValue) RT_NOEXCEPT;
526 /** Make a clone of this object. */
527 inline RTCRestDouble *clone() const RT_NOEXCEPT { return (RTCRestDouble *)baseClone(); }
528
529 /* Overridden methods: */
530 virtual RTCRestObjectBase *baseClone() const RT_NOEXCEPT RT_OVERRIDE;
531 virtual int resetToDefault() RT_NOEXCEPT RT_OVERRIDE;
532 virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT RT_OVERRIDE;
533 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT RT_OVERRIDE;
534 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_NOEXCEPT RT_OVERRIDE;
535 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
536 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_NOEXCEPT RT_OVERRIDE;
537 virtual kTypeClass typeClass(void) const RT_NOEXCEPT RT_OVERRIDE;
538 virtual const char *typeName(void) const RT_NOEXCEPT RT_OVERRIDE;
539
540 /** Factory method. */
541 static DECLCALLBACK(RTCRestObjectBase *) createInstance(void) RT_NOEXCEPT;
542 /** @copydoc RTCRestObjectBase::FNDESERIALIZEINSTANCEFROMJSON */
543 static DECLCALLBACK(int) deserializeInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT;
544
545public:
546 /** The value. */
547 double m_rdValue;
548};
549
550
551/**
552 * Class wrapping 'RTCString'.
553 */
554class RT_DECL_CLASS RTCRestString : public RTCRestObjectBase, public RTCString
555{
556public:
557 /** Default constructor. */
558 RTCRestString() RT_NOEXCEPT;
559 /** Destructor. */
560 virtual ~RTCRestString();
561
562 /** Copy constructor. */
563 RTCRestString(RTCRestString const &a_rThat);
564 /** From value constructor. */
565 RTCRestString(RTCString const &a_rThat);
566 /** From value constructor. */
567 RTCRestString(const char *a_pszSrc);
568 /** Safe copy assignment method. */
569 int assignCopy(RTCRestString const &a_rThat) RT_NOEXCEPT;
570 /** Safe copy assignment method. */
571 int assignCopy(RTCString const &a_rThat) RT_NOEXCEPT;
572 /** Safe copy assignment method. */
573 int assignCopy(const char *a_pszThat) RT_NOEXCEPT;
574 /** Make a clone of this object. */
575 inline RTCRestString *clone() const RT_NOEXCEPT { return (RTCRestString *)baseClone(); }
576
577 /* Overridden methods: */
578 virtual RTCRestObjectBase *baseClone() const RT_NOEXCEPT RT_OVERRIDE;
579 virtual int setNull(void) RT_NOEXCEPT RT_OVERRIDE; /* (ambigious, so overrider it to make sure.) */
580 virtual int resetToDefault() RT_NOEXCEPT RT_OVERRIDE;
581 virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT RT_OVERRIDE;
582 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT RT_OVERRIDE;
583 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = kCollectionFormat_Unspecified) const RT_NOEXCEPT RT_OVERRIDE;
584 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
585 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_NOEXCEPT RT_OVERRIDE;
586 virtual kTypeClass typeClass(void) const RT_NOEXCEPT RT_OVERRIDE;
587 virtual const char *typeName(void) const RT_NOEXCEPT RT_OVERRIDE;
588
589 /** Factory method. */
590 static DECLCALLBACK(RTCRestObjectBase *) createInstance(void) RT_NOEXCEPT;
591 /** @copydoc RTCRestObjectBase::FNDESERIALIZEINSTANCEFROMJSON */
592 static DECLCALLBACK(int) deserializeInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT;
593
594 /** @name RTCString assignment methods we need to replace to manage the null indicator
595 * @{ */
596 int assignNoThrow(const RTCString &a_rSrc) RT_NOEXCEPT;
597 int assignNoThrow(const char *a_pszSrc) RT_NOEXCEPT;
598 int assignNoThrow(const RTCString &a_rSrc, size_t a_offSrc, size_t a_cchSrc = npos) RT_NOEXCEPT;
599 int assignNoThrow(const char *a_pszSrc, size_t a_cchSrc) RT_NOEXCEPT;
600 int assignNoThrow(size_t a_cTimes, char a_ch) RT_NOEXCEPT;
601 int printfNoThrow(const char *pszFormat, ...) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 2);
602 int printfVNoThrow(const char *pszFormat, va_list va) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 0);
603 RTCRestString &operator=(const char *a_pcsz);
604 RTCRestString &operator=(const RTCString &a_rThat);
605 RTCRestString &operator=(const RTCRestString &a_rThat);
606 RTCRestString &assign(const RTCString &a_rSrc);
607 RTCRestString &assign(const char *a_pszSrc);
608 RTCRestString &assign(const RTCString &a_rSrc, size_t a_offSrc, size_t a_cchSrc = npos);
609 RTCRestString &assign(const char *a_pszSrc, size_t a_cchSrc);
610 RTCRestString &assign(size_t a_cTimes, char a_ch);
611 RTCRestString &printf(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
612 RTCRestString &printfV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
613 /** @} */
614};
615
616
617/**
618 * Date class.
619 *
620 * There are numerous ways of formatting a timestamp and the specifications
621 * we're currently working with doesn't have a way of telling it seems.
622 * Thus, decoding need to have fail safes built in so the user can give hints.
623 * The formatting likewise needs to be told which format to use by the user.
624 *
625 * Two side-effects of the format stuff is that the default constructor creates
626 * an object that is null, and resetToDefault will do the same bug leave the
627 * format as a hint.
628 */
629class RT_DECL_CLASS RTCRestDate : public RTCRestObjectBase
630{
631public:
632 /** Default constructor.
633 * @note The result is a null-object. */
634 RTCRestDate() RT_NOEXCEPT;
635 /** Copy constructor. */
636 RTCRestDate(RTCRestDate const &a_rThat);
637 /** Destructor. */
638 virtual ~RTCRestDate();
639 /** Copy assignment operator. */
640 RTCRestDate &operator=(RTCRestDate const &a_rThat);
641 /** Safe copy assignment method. */
642 int assignCopy(RTCRestDate const &a_rThat) RT_NOEXCEPT;
643 /** Make a clone of this object. */
644 inline RTCRestDate *clone() const RT_NOEXCEPT{ return (RTCRestDate *)baseClone(); }
645
646 /* Overridden methods: */
647 virtual RTCRestObjectBase *baseClone() const RT_NOEXCEPT RT_OVERRIDE;
648 virtual int resetToDefault() RT_NOEXCEPT RT_OVERRIDE;
649 virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT RT_OVERRIDE;
650 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT RT_OVERRIDE;
651 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_NOEXCEPT RT_OVERRIDE;
652 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
653 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_NOEXCEPT RT_OVERRIDE;
654 virtual kTypeClass typeClass(void) const RT_NOEXCEPT RT_OVERRIDE;
655 virtual const char *typeName(void) const RT_NOEXCEPT RT_OVERRIDE;
656
657 /** Factory method. */
658 static DECLCALLBACK(RTCRestObjectBase *) createInstance(void) RT_NOEXCEPT;
659 /** @copydoc RTCRestObjectBase::FNDESERIALIZEINSTANCEFROMJSON */
660 static DECLCALLBACK(int) deserializeInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT;
661
662 /** Date formats. */
663 typedef enum
664 {
665 kFormat_Invalid = 0,
666 kFormat_Rfc2822, /**< Format it according to RFC-2822. */
667 kFormat_Rfc7131, /**< Format it according to RFC-7131 (HTTP). */
668 kFormat_Rfc3339, /**< Format it according to RFC-3339 (ISO-8601) (no fraction). */
669 kFormat_Rfc3339_Fraction_2, /**< Format it according to RFC-3339 (ISO-8601) with two digit fraction (hundreths). */
670 kFormat_Rfc3339_Fraction_3, /**< Format it according to RFC-3339 (ISO-8601) with three digit fraction (milliseconds). */
671 kFormat_Rfc3339_Fraction_6, /**< Format it according to RFC-3339 (ISO-8601) with six digit fraction (microseconds). */
672 kFormat_Rfc3339_Fraction_9, /**< Format it according to RFC-3339 (ISO-8601) with nine digit fraction (nanoseconds). */
673 kFormat_End
674 } kFormat;
675
676 /**
677 * Assigns the value, formats it as a string and clears the null indicator.
678 *
679 * @returns VINF_SUCCESS, VERR_NO_STR_MEMORY or VERR_INVALID_PARAMETER.
680 * @param a_pTimeSpec The time spec to set.
681 * @param a_enmFormat The date format to use when formatting it.
682 */
683 int assignValue(PCRTTIMESPEC a_pTimeSpec, kFormat a_enmFormat) RT_NOEXCEPT;
684 int assignValueRfc2822(PCRTTIMESPEC a_pTimeSpec) RT_NOEXCEPT; /**< Convenience method for email/whatnot. */
685 int assignValueRfc7131(PCRTTIMESPEC a_pTimeSpec) RT_NOEXCEPT; /**< Convenience method for HTTP date. */
686 int assignValueRfc3339(PCRTTIMESPEC a_pTimeSpec) RT_NOEXCEPT; /**< Convenience method for ISO-8601 timstamp. */
687
688 /**
689 * Assigns the current UTC time and clears the null indicator .
690 *
691 * @returns VINF_SUCCESS, VERR_NO_STR_MEMORY or VERR_INVALID_PARAMETER.
692 * @returns VINF_SUCCESS or VERR_NO_STR_MEMORY.
693 * @param a_enmFormat The date format to use when formatting it.
694 */
695 int assignNow(kFormat a_enmFormat) RT_NOEXCEPT;
696 int assignNowRfc2822() RT_NOEXCEPT; /**< Convenience method for email/whatnot. */
697 int assignNowRfc7131() RT_NOEXCEPT; /**< Convenience method for HTTP date. */
698 int assignNowRfc3339() RT_NOEXCEPT; /**< Convenience method for ISO-8601 timstamp. */
699
700 /**
701 * Sets the format to help deal with decoding issues.
702 *
703 * This can also be used to change the date format for an okay timespec.
704 * @returns IPRT status code.
705 * @param a_enmFormat The date format to try/set.
706 */
707 int setFormat(kFormat a_enmFormat) RT_NOEXCEPT;
708
709 /** Check if the value is okay (m_TimeSpec & m_Exploded). */
710 inline bool isOkay() const RT_NOEXCEPT { return m_fTimeSpecOkay; }
711 /** Get the timespec value. */
712 inline RTTIMESPEC const &getTimeSpec() const RT_NOEXCEPT { return m_TimeSpec; }
713 /** Get the exploded time. */
714 inline RTTIME const &getExploded() const RT_NOEXCEPT { return m_Exploded; }
715 /** Gets the format. */
716 inline kFormat getFormat() const RT_NOEXCEPT { return m_enmFormat; }
717 /** Get the formatted/raw string value. */
718 inline RTCString const &getString() const RT_NOEXCEPT { return m_strFormatted; }
719
720 /** Get nanoseconds since unix epoch. */
721 inline int64_t getEpochNano() const RT_NOEXCEPT { return RTTimeSpecGetNano(&m_TimeSpec); }
722 /** Get seconds since unix epoch. */
723 inline int64_t getEpochSeconds() const RT_NOEXCEPT { return RTTimeSpecGetSeconds(&m_TimeSpec); }
724 /** Checks if UTC time. */
725 inline bool isUtc() const RT_NOEXCEPT { return (m_Exploded.fFlags & RTTIME_FLAGS_TYPE_MASK) != RTTIME_FLAGS_TYPE_LOCAL; }
726 /** Checks if local time. */
727 inline bool isLocal() const RT_NOEXCEPT { return (m_Exploded.fFlags & RTTIME_FLAGS_TYPE_MASK) == RTTIME_FLAGS_TYPE_LOCAL; }
728
729protected:
730 /** The value. */
731 RTTIMESPEC m_TimeSpec;
732 /** The exploded time value. */
733 RTTIME m_Exploded;
734 /** Set if m_TimeSpec is okay, consult m_strFormatted if not. */
735 bool m_fTimeSpecOkay;
736 /** The format / format hint. */
737 kFormat m_enmFormat;
738 /** The formatted date string.
739 * This will be the raw input string for a deserialized value, where as for
740 * a value set by the user it will be the formatted value. */
741 RTCString m_strFormatted;
742
743 /**
744 * Explodes and formats the m_TimeSpec value.
745 *
746 * Sets m_Exploded, m_strFormatted, m_fTimeSpecOkay, and m_enmFormat, clears m_fNullIndicator.
747 *
748 * @returns VINF_SUCCESS or VERR_NO_STR_MEMORY.
749 * @param a_enmFormat The format to use.
750 */
751 int explodeAndFormat(kFormat a_enmFormat) RT_NOEXCEPT;
752
753 /**
754 * Formats the m_Exploded value.
755 *
756 * Sets m_strFormatted, m_fTimeSpecOkay, and m_enmFormat, clears m_fNullIndicator.
757 *
758 * @returns VINF_SUCCESS or VERR_NO_STR_MEMORY.
759 * @param a_enmFormat The format to use.
760 */
761 int format(kFormat a_enmFormat) RT_NOEXCEPT;
762
763 /**
764 * Internal worker that attempts to decode m_strFormatted.
765 *
766 * Sets m_fTimeSpecOkay.
767 *
768 * @returns IPRT status code.
769 * @param enmFormat Specific format to try, kFormat_Invalid (default) to try guess it.
770 */
771 int decodeFormattedString(kFormat enmFormat = kFormat_Invalid) RT_NOEXCEPT;
772};
773
774
775/** We should provide a proper UUID class eventually. Currently it is not used. */
776typedef RTCRestString RTCRestUuid;
777
778
779/**
780 * String enum base class.
781 */
782class RT_DECL_CLASS RTCRestStringEnumBase : public RTCRestObjectBase
783{
784public:
785 /** Enum map entry. */
786 typedef struct ENUMMAPENTRY
787 {
788 const char *pszName;
789 uint32_t cchName;
790 int32_t iValue;
791 } ENUMMAPENTRY;
792
793 /** Default constructor. */
794 RTCRestStringEnumBase() RT_NOEXCEPT;
795 /** Destructor. */
796 virtual ~RTCRestStringEnumBase();
797
798 /** Copy constructor. */
799 RTCRestStringEnumBase(RTCRestStringEnumBase const &a_rThat);
800 /** Copy assignment operator. */
801 RTCRestStringEnumBase &operator=(RTCRestStringEnumBase const &a_rThat);
802
803 /** Safe copy assignment method. */
804 int assignCopy(RTCRestStringEnumBase const &a_rThat) RT_NOEXCEPT;
805 /** Safe copy assignment method. */
806 inline int assignCopy(RTCString const &a_rThat) RT_NOEXCEPT { return setByString(a_rThat); }
807 /** Safe copy assignment method. */
808 inline int assignCopy(const char *a_pszThat) RT_NOEXCEPT { return setByString(a_pszThat); }
809
810 /* Overridden methods: */
811 virtual int resetToDefault() RT_NOEXCEPT RT_OVERRIDE;
812 virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT RT_OVERRIDE;
813 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT RT_OVERRIDE;
814 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = kCollectionFormat_Unspecified) const RT_NOEXCEPT RT_OVERRIDE;
815 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
816 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_NOEXCEPT RT_OVERRIDE;
817 virtual kTypeClass typeClass(void) const RT_NOEXCEPT RT_OVERRIDE;
818
819 /**
820 * Sets the value given a C-string value.
821 *
822 * @retval VINF_SUCCESS on success.
823 * @retval VWRN_NOT_FOUND if not mappable to enum value.
824 * @retval VERR_NO_STR_MEMORY if not mappable and we're out of memory.
825 * @param a_pszValue The string value.
826 * @param a_cchValue The string value length. Optional.
827 */
828 int setByString(const char *a_pszValue, size_t a_cchValue = RTSTR_MAX) RT_NOEXCEPT;
829
830 /**
831 * Sets the value given a string value.
832 *
833 * @retval VINF_SUCCESS on success.
834 * @retval VWRN_NOT_FOUND if not mappable to enum value.
835 * @retval VERR_NO_STR_MEMORY if not mappable and we're out of memory.
836 * @param a_rValue The string value.
837 */
838 int setByString(RTCString const &a_rValue) RT_NOEXCEPT;
839
840 /**
841 * Gets the string value.
842 */
843 const char *getString() const RT_NOEXCEPT;
844
845 /** Maps the given string value to an enum. */
846 int stringToEnum(const char *a_pszValue, size_t a_cchValue = RTSTR_MAX) RT_NOEXCEPT;
847 /** Maps the given string value to an enum. */
848 int stringToEnum(RTCString const &a_rStrValue) RT_NOEXCEPT;
849 /** Maps the given string value to an enum. */
850 const char *enumToString(int a_iEnumValue, size_t *a_pcchString) RT_NOEXCEPT;
851
852
853protected:
854 /** The enum value. */
855 int m_iEnumValue;
856 /** The string value if not a match. */
857 RTCString m_strValue;
858
859 /**
860 * Worker for setting the object to the given enum value.
861 *
862 * @retval true on success.
863 * @retval false if a_iEnumValue can't be translated.
864 * @param a_iEnumValue The enum value to set.
865 */
866 bool setWorker(int a_iEnumValue) RT_NOEXCEPT;
867
868 /** Helper for implementing RTCRestObjectBase::clone(). */
869 RTCRestObjectBase *cloneWorker(RTCRestStringEnumBase *a_pDst) const RT_NOEXCEPT;
870
871 /**
872 * Gets the mapping table.
873 *
874 * @returns Pointer to the translation table.
875 * @param pcEntries Where to return the translation table size.
876 */
877 virtual ENUMMAPENTRY const *getMappingTable(size_t *pcEntries) const RT_NOEXCEPT = 0;
878};
879
880
881/**
882 * String enum template class.
883 *
884 * Takes the enum type as argument.
885 */
886template <typename EnumType>
887class RTCRestStringEnum : public RTCRestStringEnumBase
888{
889public:
890 typedef EnumType Type; /**< The enum type. */
891
892 /** Default constructor */
893 RTCRestStringEnum() RT_NOEXCEPT : RTCRestStringEnumBase() { }
894 /** Constructor with initial enum value. */
895 RTCRestStringEnum(Type a_enmValue) RT_NOEXCEPT : RTCRestStringEnumBase() { set(a_enmValue); }
896 /** Constructor with string default. */
897 RTCRestStringEnum(const char *a_pszDefault) : RTCRestStringEnumBase() { setByString(a_pszDefault); }
898 /** Copy constructor */
899 RTCRestStringEnum(RTCRestStringEnum const &a_rThat) : RTCRestStringEnumBase(a_rThat) { }
900 /** Make a clone of this object. */
901 inline RTCRestStringEnum *clone() const RT_NOEXCEPT { return (RTCRestStringEnum *)baseClone(); }
902
903 virtual RTCRestObjectBase *baseClone() const RT_NOEXCEPT RT_OVERRIDE
904 {
905 return cloneWorker(new (std::nothrow) RTCRestStringEnum());
906 }
907
908 /** Copy assignment operator. */
909 RTCRestStringEnum &operator=(RTCRestStringEnum const &a_rThat) RT_NOEXCEPT
910 {
911 RTCRestStringEnumBase::operator=(a_rThat);
912 return *this;
913 }
914
915 /**
916 * Gets the enum value.
917 * @returns enum value.
918 * @retval kXxxxInvalid means there was no mapping for the string, or that
919 * no value has been assigned yet.
920 */
921 Type get() const RT_NOEXCEPT { return (Type)m_iEnumValue; }
922
923 /**
924 * Sets the object value to @a a_enmType
925 *
926 * @returns true if a_enmType is valid, false if not.
927 * @param a_enmType The new value.
928 */
929 bool set(Type a_enmType) RT_NOEXCEPT { return setWorker((int)a_enmType); }
930
931 virtual const char *typeName(void) const RT_NOEXCEPT RT_OVERRIDE { return "RTCRestStringEnum<EnumType>"; }
932
933 /** Factory method. */
934 static DECLCALLBACK(RTCRestObjectBase *) createInstance(void) RT_NOEXCEPT
935 {
936 return new (std::nothrow) RTCRestStringEnum();
937 }
938
939 /** @copydoc RTCRestObjectBase::FNDESERIALIZEINSTANCEFROMJSON */
940 static DECLCALLBACK(int) deserializeInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT
941 {
942 *a_ppInstance = new (std::nothrow) RTCRestStringEnum();
943 if (*a_ppInstance)
944 return (*a_ppInstance)->deserializeFromJson(a_rCursor);
945 return a_rCursor.m_pPrimary->addError(a_rCursor, VERR_NO_MEMORY, "Out of memory");
946 }
947
948protected:
949 /** Enum mapping table. */
950 static const ENUMMAPENTRY s_aMappingTable[];
951 /** Enum mapping table size. */
952 static const size_t s_cMappingTable;
953
954 virtual ENUMMAPENTRY const *getMappingTable(size_t *pcEntries) const RT_NOEXCEPT RT_OVERRIDE
955 {
956 *pcEntries = s_cMappingTable;
957 return s_aMappingTable;
958 }
959};
960
961
962/**
963 * Class for handling binary blobs (strings).
964 *
965 * There are specializations of this class for body parameters and responses,
966 * see RTCRestBinaryParameter and RTCRestBinaryResponse.
967 */
968class RT_DECL_CLASS RTCRestBinary : public RTCRestObjectBase
969{
970public:
971 /** Default constructor. */
972 RTCRestBinary() RT_NOEXCEPT;
973 /** Destructor. */
974 virtual ~RTCRestBinary();
975
976 /** Safe copy assignment method. */
977 virtual int assignCopy(RTCRestBinary const &a_rThat) RT_NOEXCEPT;
978 /** Safe buffer copy method. */
979 virtual int assignCopy(void const *a_pvData, size_t a_cbData) RT_NOEXCEPT;
980
981 /** Use the specified data buffer directly. */
982 virtual int assignReadOnly(void const *a_pvData, size_t a_cbData) RT_NOEXCEPT;
983 /** Use the specified data buffer directly. */
984 virtual int assignWriteable(void *a_pvBuf, size_t a_cbBuf) RT_NOEXCEPT;
985 /** Frees the data held by the object and resets it default state. */
986 virtual void freeData() RT_NOEXCEPT;
987
988 /** Returns a pointer to the data blob. */
989 inline const uint8_t *getPtr() const RT_NOEXCEPT { return m_pbData; }
990 /** Gets the size of the data. */
991 inline size_t getSize() const RT_NOEXCEPT { return m_cbData; }
992
993 /** Make a clone of this object. */
994 inline RTCRestBinary *clone() const RT_NOEXCEPT { return (RTCRestBinary *)baseClone(); }
995
996 /* Overridden methods: */
997 virtual RTCRestObjectBase *baseClone() const RT_NOEXCEPT RT_OVERRIDE;
998 virtual int setNull(void) RT_NOEXCEPT RT_OVERRIDE;
999 virtual int resetToDefault(void) RT_NOEXCEPT RT_OVERRIDE;
1000 virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT RT_OVERRIDE;
1001 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT RT_OVERRIDE;
1002 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = kCollectionFormat_Unspecified) const RT_NOEXCEPT RT_OVERRIDE;
1003 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
1004 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_NOEXCEPT RT_OVERRIDE;
1005 virtual kTypeClass typeClass(void) const RT_NOEXCEPT RT_OVERRIDE;
1006 virtual const char *typeName(void) const RT_NOEXCEPT RT_OVERRIDE;
1007
1008 /** Factory method. */
1009 static DECLCALLBACK(RTCRestObjectBase *) createInstance(void) RT_NOEXCEPT;
1010 /** @copydoc RTCRestObjectBase::FNDESERIALIZEINSTANCEFROMJSON */
1011 static DECLCALLBACK(int) deserializeInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance) RT_NOEXCEPT;
1012
1013protected:
1014 /** Pointer to data blob. */
1015 uint8_t *m_pbData;
1016 /** Amount of valid data in the blob. */
1017 size_t m_cbData;
1018 /** Number of bytes allocated for the m_pbData buffer. */
1019 size_t m_cbAllocated;
1020 /** Set if the data is freeable, only ever clear if user data. */
1021 bool m_fFreeable;
1022 /** Set if the data blob is readonly user provided data. */
1023 bool m_fReadOnly;
1024
1025private:
1026 /* No copy constructor or copy assignment: */
1027 RTCRestBinary(RTCRestBinary const &a_rThat);
1028 RTCRestBinary &operator=(RTCRestBinary const &a_rThat);
1029};
1030
1031
1032/**
1033 * Abstract base class for REST data model classes.
1034 */
1035class RT_DECL_CLASS RTCRestDataObject : public RTCRestObjectBase
1036{
1037public:
1038 RTCRestDataObject() RT_NOEXCEPT;
1039 RTCRestDataObject(RTCRestDataObject const &a_rThat) RT_NOEXCEPT;
1040 virtual ~RTCRestDataObject();
1041
1042 /* Overridden methods:*/
1043 virtual int resetToDefault() RT_NOEXCEPT RT_OVERRIDE;
1044 virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT RT_OVERRIDE;
1045 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT RT_OVERRIDE;
1046 virtual kTypeClass typeClass(void) const RT_NOEXCEPT RT_OVERRIDE;
1047
1048 /**
1049 * Serialize the object members as JSON.
1050 *
1051 * @returns a_rDst
1052 * @param a_rDst The destination for the serialization.
1053 */
1054 virtual RTCRestOutputBase &serializeMembersAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT;
1055
1056 /**
1057 * Deserialize object from the given JSON iterator.
1058 *
1059 * @returns IPRT status code.
1060 * @retval VERR_NOT_FOUND if field is unknown. Top level caller will do
1061 * invoke unknownField() on it.
1062 *
1063 * @param a_rCursor The JSON cursor with the current member.
1064 * @param a_cchName The length of a_rCursor.m_pszName.
1065 */
1066 virtual int deserializeMemberFromJson(RTCRestJsonCursor const &a_rCursor, size_t a_cchName) RT_NOEXCEPT;
1067
1068protected:
1069 /** The is-set bits for all the fields. */
1070 uint64_t m_fIsSet;
1071
1072 /** Copy assignment operator. */
1073 RTCRestDataObject &operator=(RTCRestDataObject const &a_rThat) RT_NOEXCEPT;
1074
1075 /** Safe copy assignment method. */
1076 virtual int assignCopy(RTCRestDataObject const &a_rThat) RT_NOEXCEPT;
1077};
1078
1079
1080/**
1081 * Abstract base class for polymorphic REST data model classes.
1082 */
1083class RT_DECL_CLASS RTCRestPolyDataObject : public RTCRestDataObject
1084{
1085public:
1086 RTCRestPolyDataObject() RT_NOEXCEPT;
1087 RTCRestPolyDataObject(RTCRestPolyDataObject const &a_rThat) RT_NOEXCEPT;
1088 virtual ~RTCRestPolyDataObject();
1089
1090 /* Overridden methods:*/
1091 virtual int resetToDefault() RT_NOEXCEPT RT_OVERRIDE;
1092
1093 /** Checks if the instance is of a child class (@c true) or of the parent (@c false). */
1094 virtual bool isChild() const RT_NOEXCEPT;
1095
1096protected:
1097
1098 /** Copy assignment operator. */
1099 RTCRestPolyDataObject &operator=(RTCRestPolyDataObject const &a_rThat) RT_NOEXCEPT;
1100};
1101
1102
1103/** @} */
1104
1105#endif /* !IPRT_INCLUDED_cpp_restbase_h */
1106
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