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