VirtualBox

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

Last change on this file since 76558 was 76557, checked in by vboxsync, 6 years ago

include/iprt: Use IPRT_INCLUDED_ rather than _iprt_ as header guard prefix, letting scm enforce this (thereby avoiding copy&paste errors like rsa.h).

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