1 | /** @file
|
---|
2 | * IPRT - C++ Representational State Transfer (REST) Output Classes.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2008-2023 Oracle and/or its affiliates.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox base platform packages, as
|
---|
9 | * available from https://www.virtualbox.org.
|
---|
10 | *
|
---|
11 | * This program is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU General Public License
|
---|
13 | * as published by the Free Software Foundation, in version 3 of the
|
---|
14 | * License.
|
---|
15 | *
|
---|
16 | * This program is distributed in the hope that it will be useful, but
|
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | * General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
23 | *
|
---|
24 | * The contents of this file may alternatively be used under the terms
|
---|
25 | * of the Common Development and Distribution License Version 1.0
|
---|
26 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
27 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
28 | * CDDL are applicable instead of those of the GPL.
|
---|
29 | *
|
---|
30 | * You may elect to license modified versions of this file under the
|
---|
31 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
32 | *
|
---|
33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef IPRT_INCLUDED_cpp_restoutput_h
|
---|
37 | #define IPRT_INCLUDED_cpp_restoutput_h
|
---|
38 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
39 | # pragma once
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #include <iprt/cdefs.h>
|
---|
43 | #include <iprt/types.h>
|
---|
44 | #include <iprt/stdarg.h>
|
---|
45 | #include <iprt/cpp/ministring.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | /** @defgroup grp_rt_cpp_restoutput C++ Representational State Transfer (REST) Output Classes.
|
---|
49 | * @ingroup grp_rt_cpp
|
---|
50 | * @{
|
---|
51 | */
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Abstract base class for serializing data objects.
|
---|
56 | */
|
---|
57 | class RT_DECL_CLASS RTCRestOutputBase
|
---|
58 | {
|
---|
59 | public:
|
---|
60 | RTCRestOutputBase() RT_NOEXCEPT;
|
---|
61 | virtual ~RTCRestOutputBase();
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Raw output function.
|
---|
65 | *
|
---|
66 | * @returns Number of bytes outputted.
|
---|
67 | * @param a_pchString The string to output (not necessarily terminated).
|
---|
68 | * @param a_cchToWrite The length of the string
|
---|
69 | */
|
---|
70 | virtual size_t output(const char *a_pchString, size_t a_cchToWrite) RT_NOEXCEPT = 0;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * RTStrPrintf like function (see @ref pg_rt_str_format).
|
---|
74 | *
|
---|
75 | * @returns Number of bytes outputted.
|
---|
76 | * @param pszFormat The format string.
|
---|
77 | * @param ... Argument specfied in @a pszFormat.
|
---|
78 | */
|
---|
79 | inline size_t printf(const char *pszFormat, ...) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(2, 3)
|
---|
80 | {
|
---|
81 | va_list va;
|
---|
82 | va_start(va, pszFormat);
|
---|
83 | size_t cchWritten = this->vprintf(pszFormat, va);
|
---|
84 | va_end(va);
|
---|
85 | return cchWritten;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * RTStrPrintfV like function (see @ref pg_rt_str_format).
|
---|
90 | *
|
---|
91 | * @returns Number of bytes outputted.
|
---|
92 | * @param pszFormat The format string.
|
---|
93 | * @param va Argument specfied in @a pszFormat.
|
---|
94 | */
|
---|
95 | size_t vprintf(const char *pszFormat, va_list va) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(2, 0);
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Begins an array.
|
---|
99 | * @returns Previous output state. Pass to endArray() when done.
|
---|
100 | */
|
---|
101 | virtual uint32_t beginArray() RT_NOEXCEPT;
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Ends an array.
|
---|
105 | * @param a_uOldState Previous output state (returned by beginArray()).
|
---|
106 | */
|
---|
107 | virtual void endArray(uint32_t a_uOldState) RT_NOEXCEPT;
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Begins an object.
|
---|
111 | * @returns Previous output state. Pass to endObject() when done.
|
---|
112 | */
|
---|
113 | virtual uint32_t beginObject() RT_NOEXCEPT;
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Ends an array.
|
---|
117 | * @param a_uOldState Previous output state (returned by beginObject()).
|
---|
118 | */
|
---|
119 | virtual void endObject(uint32_t a_uOldState) RT_NOEXCEPT;
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Outputs a value separator.
|
---|
123 | * This is called before a value, not after.
|
---|
124 | */
|
---|
125 | virtual void valueSeparator() RT_NOEXCEPT;
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Outputs a value separator, name and name separator.
|
---|
129 | */
|
---|
130 | virtual void valueSeparatorAndName(const char *a_pszName, size_t a_cchName) RT_NOEXCEPT;
|
---|
131 |
|
---|
132 | /** Outputs a null-value. */
|
---|
133 | void nullValue() RT_NOEXCEPT;
|
---|
134 |
|
---|
135 | protected:
|
---|
136 | /** The current indentation level (bits 15:0) and separator state (bit 31). */
|
---|
137 | uint32_t m_uState;
|
---|
138 |
|
---|
139 | /** @callback_method_impl{FNRTSTROUTPUT} */
|
---|
140 | static DECLCALLBACK(size_t) printfOutputCallback(void *pvArg, const char *pachChars, size_t cbChars) RT_NOEXCEPT;
|
---|
141 | };
|
---|
142 |
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Abstract base class for pretty output.
|
---|
146 | */
|
---|
147 | class RT_DECL_CLASS RTCRestOutputPrettyBase : public RTCRestOutputBase
|
---|
148 | {
|
---|
149 | public:
|
---|
150 | RTCRestOutputPrettyBase() RT_NOEXCEPT;
|
---|
151 | virtual ~RTCRestOutputPrettyBase();
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Begins an array.
|
---|
155 | * @returns Previous output state. Pass to endArray() when done.
|
---|
156 | */
|
---|
157 | virtual uint32_t beginArray() RT_NOEXCEPT RT_OVERRIDE;
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Ends an array.
|
---|
161 | * @param a_uOldState Previous output state (returned by beginArray()).
|
---|
162 | */
|
---|
163 | virtual void endArray(uint32_t a_uOldState) RT_NOEXCEPT RT_OVERRIDE;
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Begins an object.
|
---|
167 | * @returns Previous output state. Pass to endObject() when done.
|
---|
168 | */
|
---|
169 | virtual uint32_t beginObject() RT_NOEXCEPT RT_OVERRIDE;
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Ends an array.
|
---|
173 | * @param a_uOldState Previous output state (returned by beginObject()).
|
---|
174 | */
|
---|
175 | virtual void endObject(uint32_t a_uOldState) RT_NOEXCEPT RT_OVERRIDE;
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Outputs a value separator.
|
---|
179 | * This is called before a value, not after.
|
---|
180 | */
|
---|
181 | virtual void valueSeparator() RT_NOEXCEPT RT_OVERRIDE;
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Outputs a value separator, name and name separator.
|
---|
185 | */
|
---|
186 | virtual void valueSeparatorAndName(const char *a_pszName, size_t a_cchName) RT_NOEXCEPT RT_OVERRIDE;
|
---|
187 |
|
---|
188 | protected:
|
---|
189 | /** Helper for outputting the correct amount of indentation. */
|
---|
190 | void outputIndentation() RT_NOEXCEPT;
|
---|
191 | };
|
---|
192 |
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Serialize to a string object.
|
---|
196 | */
|
---|
197 | class RT_DECL_CLASS RTCRestOutputToString : public RTCRestOutputBase
|
---|
198 | {
|
---|
199 | public:
|
---|
200 | /**
|
---|
201 | * Creates an instance that appends to @a a_pDst.
|
---|
202 | * @param a_pDst Pointer to the destination string object.
|
---|
203 | * NULL is not accepted and will assert.
|
---|
204 | * @param a_fAppend Whether to append to the current string value, or
|
---|
205 | * nuke the string content before starting the output.
|
---|
206 | */
|
---|
207 | RTCRestOutputToString(RTCString *a_pDst, bool a_fAppend = false) RT_NOEXCEPT;
|
---|
208 | virtual ~RTCRestOutputToString();
|
---|
209 |
|
---|
210 | virtual size_t output(const char *a_pchString, size_t a_cchToWrite) RT_NOEXCEPT RT_OVERRIDE;
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Finalizes the output and releases the string object to the caller.
|
---|
214 | *
|
---|
215 | * @returns The released string object. NULL if we ran out of memory or if
|
---|
216 | * called already.
|
---|
217 | *
|
---|
218 | * @remark This sets m_pDst to NULL and the object cannot be use for any
|
---|
219 | * more output afterwards.
|
---|
220 | */
|
---|
221 | virtual RTCString *finalize() RT_NOEXCEPT;
|
---|
222 |
|
---|
223 | protected:
|
---|
224 | /** Pointer to the destination string. NULL after finalize(). */
|
---|
225 | RTCString *m_pDst;
|
---|
226 | /** Set if we ran out of memory and should ignore subsequent calls. */
|
---|
227 | bool m_fOutOfMemory;
|
---|
228 |
|
---|
229 | /* Make non-copyable (RTCNonCopyable causes warnings): */
|
---|
230 | RTCRestOutputToString(RTCRestOutputToString const &);
|
---|
231 | RTCRestOutputToString *operator=(RTCRestOutputToString const &);
|
---|
232 | };
|
---|
233 |
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Serialize pretty JSON to a string object.
|
---|
237 | */
|
---|
238 | class RT_DECL_CLASS RTCRestOutputPrettyToString : public RTCRestOutputPrettyBase
|
---|
239 | {
|
---|
240 | public:
|
---|
241 | /**
|
---|
242 | * Creates an instance that appends to @a a_pDst.
|
---|
243 | * @param a_pDst Pointer to the destination string object.
|
---|
244 | * NULL is not accepted and will assert.
|
---|
245 | * @param a_fAppend Whether to append to the current string value, or
|
---|
246 | * nuke the string content before starting the output.
|
---|
247 | */
|
---|
248 | RTCRestOutputPrettyToString(RTCString *a_pDst, bool a_fAppend = false) RT_NOEXCEPT;
|
---|
249 | virtual ~RTCRestOutputPrettyToString();
|
---|
250 |
|
---|
251 | virtual size_t output(const char *a_pchString, size_t a_cchToWrite) RT_NOEXCEPT RT_OVERRIDE;
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * Finalizes the output and releases the string object to the caller.
|
---|
255 | *
|
---|
256 | * @returns The released string object. NULL if we ran out of memory or if
|
---|
257 | * called already.
|
---|
258 | *
|
---|
259 | * @remark This sets m_pDst to NULL and the object cannot be use for any
|
---|
260 | * more output afterwards.
|
---|
261 | */
|
---|
262 | virtual RTCString *finalize() RT_NOEXCEPT;
|
---|
263 |
|
---|
264 | protected:
|
---|
265 | /** Pointer to the destination string. NULL after finalize(). */
|
---|
266 | RTCString *m_pDst;
|
---|
267 | /** Set if we ran out of memory and should ignore subsequent calls. */
|
---|
268 | bool m_fOutOfMemory;
|
---|
269 |
|
---|
270 | /* Make non-copyable (RTCNonCopyable causes warnings): */
|
---|
271 | RTCRestOutputPrettyToString(RTCRestOutputToString const &);
|
---|
272 | RTCRestOutputPrettyToString *operator=(RTCRestOutputToString const &);
|
---|
273 | };
|
---|
274 |
|
---|
275 |
|
---|
276 |
|
---|
277 | /** @} */
|
---|
278 |
|
---|
279 | #endif /* !IPRT_INCLUDED_cpp_restoutput_h */
|
---|
280 |
|
---|