VirtualBox

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

Last change on this file since 93176 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

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