VirtualBox

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

Last change on this file since 74883 was 74425, checked in by vboxsync, 6 years ago

IPRT/rest: Missed RT_NOEXCEPT in two place. Went wild adding RT_NOEXCEPT everywhere possible. bugref:9167

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