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