VirtualBox

source: vbox/trunk/include/iprt/cpp/restoutput.h@ 106571

Last change on this file since 106571 was 106498, checked in by vboxsync, 3 months ago

iprt/rest: Shut up some complains about default copy assignment operator. jiraref:VBP-1171

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.5 KB
Line 
1/** @file
2 * IPRT - C++ Representational State Transfer (REST) Output Classes.
3 */
4
5/*
6 * Copyright (C) 2008-2024 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 */
57class RT_DECL_CLASS RTCRestOutputBase
58{
59public:
60 RTCRestOutputBase() RT_NOEXCEPT;
61 virtual ~RTCRestOutputBase();
62
63 RTCRestOutputBase(const RTCRestOutputBase &a_rThat) RT_NOEXCEPT;
64 RTCRestOutputBase &operator=(const RTCRestOutputBase &a_rThat) RT_NOEXCEPT;
65
66 /**
67 * Raw output function.
68 *
69 * @returns Number of bytes outputted.
70 * @param a_pchString The string to output (not necessarily terminated).
71 * @param a_cchToWrite The length of the string
72 */
73 virtual size_t output(const char *a_pchString, size_t a_cchToWrite) RT_NOEXCEPT = 0;
74
75 /**
76 * RTStrPrintf like function (see @ref pg_rt_str_format).
77 *
78 * @returns Number of bytes outputted.
79 * @param pszFormat The format string.
80 * @param ... Argument specfied in @a pszFormat.
81 */
82 inline size_t printf(const char *pszFormat, ...) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(2, 3)
83 {
84 va_list va;
85 va_start(va, pszFormat);
86 size_t cchWritten = this->vprintf(pszFormat, va);
87 va_end(va);
88 return cchWritten;
89 }
90
91 /**
92 * RTStrPrintfV like function (see @ref pg_rt_str_format).
93 *
94 * @returns Number of bytes outputted.
95 * @param pszFormat The format string.
96 * @param va Argument specfied in @a pszFormat.
97 */
98 size_t vprintf(const char *pszFormat, va_list va) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(2, 0);
99
100 /**
101 * Begins an array.
102 * @returns Previous output state. Pass to endArray() when done.
103 */
104 virtual uint32_t beginArray() RT_NOEXCEPT;
105
106 /**
107 * Ends an array.
108 * @param a_uOldState Previous output state (returned by beginArray()).
109 */
110 virtual void endArray(uint32_t a_uOldState) RT_NOEXCEPT;
111
112 /**
113 * Begins an object.
114 * @returns Previous output state. Pass to endObject() when done.
115 */
116 virtual uint32_t beginObject() RT_NOEXCEPT;
117
118 /**
119 * Ends an array.
120 * @param a_uOldState Previous output state (returned by beginObject()).
121 */
122 virtual void endObject(uint32_t a_uOldState) RT_NOEXCEPT;
123
124 /**
125 * Outputs a value separator.
126 * This is called before a value, not after.
127 */
128 virtual void valueSeparator() RT_NOEXCEPT;
129
130 /**
131 * Outputs a value separator, name and name separator.
132 */
133 virtual void valueSeparatorAndName(const char *a_pszName, size_t a_cchName) RT_NOEXCEPT;
134
135 /** Outputs a null-value. */
136 void nullValue() RT_NOEXCEPT;
137
138protected:
139 /** The current indentation level (bits 15:0) and separator state (bit 31). */
140 uint32_t m_uState;
141
142 /** @callback_method_impl{FNRTSTROUTPUT} */
143 static DECLCALLBACK(size_t) printfOutputCallback(void *pvArg, const char *pachChars, size_t cbChars) RT_NOEXCEPT;
144};
145
146
147/**
148 * Abstract base class for pretty output.
149 */
150class RT_DECL_CLASS RTCRestOutputPrettyBase : public RTCRestOutputBase
151{
152public:
153 RTCRestOutputPrettyBase() RT_NOEXCEPT;
154 virtual ~RTCRestOutputPrettyBase();
155
156 RTCRestOutputPrettyBase(const RTCRestOutputPrettyBase &a_rThat) RT_NOEXCEPT;
157 RTCRestOutputPrettyBase &operator=(const RTCRestOutputPrettyBase &a_rThat) RT_NOEXCEPT;
158
159 /**
160 * Begins an array.
161 * @returns Previous output state. Pass to endArray() when done.
162 */
163 virtual uint32_t beginArray() RT_NOEXCEPT RT_OVERRIDE;
164
165 /**
166 * Ends an array.
167 * @param a_uOldState Previous output state (returned by beginArray()).
168 */
169 virtual void endArray(uint32_t a_uOldState) RT_NOEXCEPT RT_OVERRIDE;
170
171 /**
172 * Begins an object.
173 * @returns Previous output state. Pass to endObject() when done.
174 */
175 virtual uint32_t beginObject() RT_NOEXCEPT RT_OVERRIDE;
176
177 /**
178 * Ends an array.
179 * @param a_uOldState Previous output state (returned by beginObject()).
180 */
181 virtual void endObject(uint32_t a_uOldState) RT_NOEXCEPT RT_OVERRIDE;
182
183 /**
184 * Outputs a value separator.
185 * This is called before a value, not after.
186 */
187 virtual void valueSeparator() RT_NOEXCEPT RT_OVERRIDE;
188
189 /**
190 * Outputs a value separator, name and name separator.
191 */
192 virtual void valueSeparatorAndName(const char *a_pszName, size_t a_cchName) RT_NOEXCEPT RT_OVERRIDE;
193
194protected:
195 /** Helper for outputting the correct amount of indentation. */
196 void outputIndentation() RT_NOEXCEPT;
197};
198
199
200/**
201 * Serialize to a string object.
202 */
203class RT_DECL_CLASS RTCRestOutputToString : public RTCRestOutputBase
204{
205public:
206 /**
207 * Creates an instance that appends to @a a_pDst.
208 * @param a_pDst Pointer to the destination string object.
209 * NULL is not accepted and will assert.
210 * @param a_fAppend Whether to append to the current string value, or
211 * nuke the string content before starting the output.
212 */
213 RTCRestOutputToString(RTCString *a_pDst, bool a_fAppend = false) RT_NOEXCEPT;
214 virtual ~RTCRestOutputToString();
215
216 virtual size_t output(const char *a_pchString, size_t a_cchToWrite) RT_NOEXCEPT RT_OVERRIDE;
217
218 /**
219 * Finalizes the output and releases the string object to the caller.
220 *
221 * @returns The released string object. NULL if we ran out of memory or if
222 * called already.
223 *
224 * @remark This sets m_pDst to NULL and the object cannot be use for any
225 * more output afterwards.
226 */
227 virtual RTCString *finalize() RT_NOEXCEPT;
228
229protected:
230 /** Pointer to the destination string. NULL after finalize(). */
231 RTCString *m_pDst;
232 /** Set if we ran out of memory and should ignore subsequent calls. */
233 bool m_fOutOfMemory;
234
235 /* Make non-copyable (RTCNonCopyable causes warnings): */
236#if RT_CPLUSPLUS_PREREQ(201100)
237 RTCRestOutputToString(RTCRestOutputToString const &) = delete;
238 RTCRestOutputToString *operator=(RTCRestOutputToString const &) = delete;
239#else
240 RTCRestOutputToString(RTCRestOutputToString const &);
241 RTCRestOutputToString *operator=(RTCRestOutputToString const &);
242#endif
243};
244
245
246/**
247 * Serialize pretty JSON to a string object.
248 */
249class RT_DECL_CLASS RTCRestOutputPrettyToString : public RTCRestOutputPrettyBase
250{
251public:
252 /**
253 * Creates an instance that appends to @a a_pDst.
254 * @param a_pDst Pointer to the destination string object.
255 * NULL is not accepted and will assert.
256 * @param a_fAppend Whether to append to the current string value, or
257 * nuke the string content before starting the output.
258 */
259 RTCRestOutputPrettyToString(RTCString *a_pDst, bool a_fAppend = false) RT_NOEXCEPT;
260 virtual ~RTCRestOutputPrettyToString();
261
262 virtual size_t output(const char *a_pchString, size_t a_cchToWrite) RT_NOEXCEPT RT_OVERRIDE;
263
264 /**
265 * Finalizes the output and releases the string object to the caller.
266 *
267 * @returns The released string object. NULL if we ran out of memory or if
268 * called already.
269 *
270 * @remark This sets m_pDst to NULL and the object cannot be use for any
271 * more output afterwards.
272 */
273 virtual RTCString *finalize() RT_NOEXCEPT;
274
275protected:
276 /** Pointer to the destination string. NULL after finalize(). */
277 RTCString *m_pDst;
278 /** Set if we ran out of memory and should ignore subsequent calls. */
279 bool m_fOutOfMemory;
280
281 /* Make non-copyable (RTCNonCopyable causes warnings): */
282#if RT_CPLUSPLUS_PREREQ(201100)
283 RTCRestOutputPrettyToString(RTCRestOutputToString const &) = delete;
284 RTCRestOutputPrettyToString *operator=(RTCRestOutputToString const &) = delete;
285#else
286 RTCRestOutputPrettyToString(RTCRestOutputToString const &);
287 RTCRestOutputPrettyToString *operator=(RTCRestOutputToString const &);
288#endif
289};
290
291
292
293/** @} */
294
295#endif /* !IPRT_INCLUDED_cpp_restoutput_h */
296
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette