BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdljsn_writeoptions.h
Go to the documentation of this file.
1/// @file bdljsn_writeoptions.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdljsn_writeoptions.h -*-C++-*-
8#ifndef INCLUDED_BDLJSN_WRITEOPTIONS
9#define INCLUDED_BDLJSN_WRITEOPTIONS
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdljsn_writeoptions bdljsn_writeoptions
15/// @brief Provide options for writing a JSON document.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdljsn
19/// @{
20/// @addtogroup bdljsn_writeoptions
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdljsn_writeoptions-purpose"> Purpose</a>
25/// * <a href="#bdljsn_writeoptions-classes"> Classes </a>
26/// * <a href="#bdljsn_writeoptions-description"> Description </a>
27/// * <a href="#bdljsn_writeoptions-attributes"> Attributes </a>
28/// * <a href="#bdljsn_writeoptions-usage"> Usage </a>
29/// * <a href="#bdljsn_writeoptions-example-1-creating-and-populating-an-options-object"> Example 1: Creating and Populating an Options Object </a>
30///
31/// # Purpose {#bdljsn_writeoptions-purpose}
32/// Provide options for writing a JSON document.
33///
34/// # Classes {#bdljsn_writeoptions-classes}
35///
36/// - bdljsn::WriteOptions: options for writing a JSON document
37///
38/// @see bdljsn_jsonutil, bdljsn_json
39///
40/// # Description {#bdljsn_writeoptions-description}
41/// This component provides a single, simply constrained
42/// (value-semantic) attribute class, `bdljsn::WriteOptions`, that is used to
43/// specify options for writing a JSON document (see @ref bdljsn_jsonutil ).
44///
45/// ## Attributes {#bdljsn_writeoptions-attributes}
46///
47///
48/// @code
49/// Name Type Default Simple Constraints
50/// ------------------ ----------- ------- ------------------
51/// initialIndentLevel int 0 >= 0
52/// sortMembers bool false none
53/// spacesPerLevel int 4 >= 0
54/// style WriteStyle e_COMPACT none
55/// @endcode
56/// * `initialIndentLevel`: initial indent level for the top-most element. If
57/// `style` is `e_COMPACT`, or `spacesPerLevel` is 0, this option is ignored.
58/// * `sortMembers`: indicates whether the members of a object will be sorted
59/// in lexicographical order based on the member name.
60/// * `spacesPerLevel`: spaces per indent level. If this option is 0, no
61/// indentation is used. If `style` is `e_COMPACT` or `e_ONELINE`, this
62/// option is ignored.
63/// * `style`: the style used to encode the JSON data.
64///
65/// ## Usage {#bdljsn_writeoptions-usage}
66///
67///
68/// This section illustrates intended use of this component.
69///
70/// ### Example 1: Creating and Populating an Options Object {#bdljsn_writeoptions-example-1-creating-and-populating-an-options-object}
71///
72///
73/// This component is designed to be used at a higher level to set the options
74/// for writing `bdljsn::Json` objects in JSON format. This example shows how
75/// to create and populate an options object.
76///
77/// First, we default-construct a `bdljsn::WriteOptions` object:
78/// @code
79/// const int INITIAL_INDENT_LEVEL = 1;
80/// const int SPACES_PER_LEVEL = 4;
81///
82/// bdljsn::WriteOptions options;
83/// assert(0 == options.initialIndentLevel());
84/// assert(4 == options.spacesPerLevel());
85/// assert(false == options.sortMembers());
86/// assert(bdljsn::WriteStyle::e_COMPACT == options.style());
87/// @endcode
88/// Finally, we populate that object using a pre-defined initial indent level
89/// and spaces per level:
90/// @code
91/// options.setInitialIndentLevel(INITIAL_INDENT_LEVEL);
92/// assert(INITIAL_INDENT_LEVEL == options.initialIndentLevel());
93///
94/// options.setSpacesPerLevel(SPACES_PER_LEVEL);
95/// assert(SPACES_PER_LEVEL == options.spacesPerLevel());
96/// @endcode
97/// @}
98/** @} */
99/** @} */
100
101/** @addtogroup bdl
102 * @{
103 */
104/** @addtogroup bdljsn
105 * @{
106 */
107/** @addtogroup bdljsn_writeoptions
108 * @{
109 */
110
111#include <bdlscm_version.h>
112
113#include <bdljsn_writestyle.h>
114
115#include <bsl_iosfwd.h>
116
117#include <bsls_assert.h>
118
119
120
121namespace bdljsn {
122
123 // ==================
124 // class WriteOptions
125 // ==================
126
127/// This simply constrained (value-semantic) attribute class specifies
128/// options for writing a JSON document. See the {Attributes} section under
129/// {DESCRIPTION} in the component-level documentation for information on
130/// the class attributes. Note that the class invariants are identically
131/// the constraints on the individual attributes.
132///
133/// See @ref bdljsn_writeoptions
135
136 // INSTANCE DATA
137
138 // initial indentation level for the topmost element
139 int d_initialIndentLevel;
140
141 // whether to sort members of an object by member name
142 bool d_sortMembers;
143
144 // spaces per additional level of indentation
145 int d_spacesPerLevel;
146
147 // write style used for formatting JSON text
149
150 public:
151 // CONSTANTS
153
155
157
158 static const bdljsn::WriteStyle::Enum
160
161 public:
162 // CREATORS
163
164 /// Create an object of type `WriteOptions` having the (default)
165 /// attribute values:
166 /// @code
167 /// initialIndentLevel() == 0
168 /// sortMembers() == false
169 /// spacesPerLevel() == 4
170 /// style() == e_COMPACT
171 /// @endcode
173
174 /// Create an object of type `WriteOptions` having the value of the
175 /// specified `original` object.
176 WriteOptions(const WriteOptions& original);
177
178 /// Destroy this object.
180
181 // MANIPULATORS
182
183 /// Assign to this object the value of the specified `rhs` object and
184 /// return a non-`const` reference to this object.
186
187 /// Reset this object to the default value (i.e., its value upon default
188 /// construction) and return a non-`const` reference to this object.
190
191 /// Set the `initialIndentLevel` attribute of this object to the
192 /// specified `value` and return a non-`const` reference to this object.
193 /// The behavior is undefined unless `0 <= value`.
195
196 /// Set the `sortMembers` attribute of this object to the specified
197 /// `value` and return a non-`const` reference to this object.
198 WriteOptions& setSortMembers(bool value);
199
200 /// Set the `spacesPerLevel` attribute of this object to the specified
201 /// `value` and return a non-`const` reference to this object. The
202 /// behavior is undefined unless `0 <= value`.
204
205 /// Set the `style` attribute of this object to the specified `value`.
206 /// and return a non-`const` reference to this object.
208
209 // ACCESSORS
210
211 /// Return the `initialIndentLevel` attribute of this object.
212 int initialIndentLevel() const;
213
214 /// Return the `sortMembers` attribute of this object.
215 bool sortMembers() const;
216
217 /// Return the `spacesPerLevel` attribute of this object.
218 int spacesPerLevel() const;
219
220 /// Return the `style` attribute of this object.
222
223 // Aspects
224
225 /// Format this object to the specified output `stream` at the
226 /// optionally specified indentation `level` and return a reference to
227 /// the modifiable `stream`. If `level` is specified, optionally
228 /// specify `spacesPerLevel`, the number of spaces per indentation level
229 /// for this and all of its nested objects. Each line is indented by
230 /// the absolute value of `level * spacesPerLevel`. If `level` is
231 /// negative, suppress indentation of the first line. If
232 /// `spacesPerLevel` is negative, suppress line breaks and format the
233 /// entire output on one line. If `stream` is initially invalid, this
234 /// operation has no effect. Note that a trailing newline is provided
235 /// in multiline mode only. Also note that the format is not fully
236 /// specified, and can change without notice.
237 bsl::ostream& print(bsl::ostream& stream,
238 int level = 0,
239 int spacesPerLevel = 4) const;
240};
241
242// FREE OPERATORS
243
244/// Return `true` if the specified `lhs` and `rhs` attribute objects have
245/// the same value, and `false` otherwise. Two attribute objects have the
246/// same value if each respective attribute has the same value.
247inline
248bool operator==(const WriteOptions& lhs, const WriteOptions& rhs);
249
250/// Return `true` if the specified `lhs` and `rhs` attribute objects do not
251/// have the same value, and `false` otherwise. Two attribute objects do
252/// not have the same value if one or more respective attributes differ in
253/// values.
254inline
255bool operator!=(const WriteOptions& lhs, const WriteOptions& rhs);
256
257/// Format the specified `rhs` to the specified output `stream` and return a
258/// non-`const` reference `stream`.
259inline
260bsl::ostream& operator<<(bsl::ostream& stream, const WriteOptions& rhs);
261
262// ============================================================================
263// INLINE DEFINITIONS
264// ============================================================================
265
266 // ------------------
267 // class WriteOptions
268 // ------------------
269
270// CREATORS
271inline
273: d_initialIndentLevel(original.d_initialIndentLevel)
274, d_sortMembers (original.d_sortMembers)
275, d_spacesPerLevel (original.d_spacesPerLevel)
276, d_style (original.d_style)
277{
278}
279
280inline
282{
283 BSLS_ASSERT(0 <= d_initialIndentLevel);
284 BSLS_ASSERT(0 <= d_spacesPerLevel);
285}
286
287// MANIPULATORS
288inline
290{
291 d_initialIndentLevel = rhs.d_initialIndentLevel;
292 d_sortMembers = rhs.d_sortMembers;
293 d_spacesPerLevel = rhs.d_spacesPerLevel;
294 d_style = rhs.d_style;
295
296 return *this;
297}
298
299inline
301{
302 BSLS_ASSERT(0 <= value);
303
304 d_initialIndentLevel = value;
305 return *this;
306}
307
308inline
310{
311 d_sortMembers = value;
312 return *this;
313}
314
315inline
317{
318 BSLS_ASSERT(0 <= value);
319
320 d_spacesPerLevel = value;
321 return *this;
322}
323
324inline
326{
327 d_style = value;
328 return *this;
329}
330
331// ACCESSORS
332inline
334{
335 return d_initialIndentLevel;
336}
337
338inline
340{
341 return d_sortMembers;
342}
343
344inline
346{
347 return d_spacesPerLevel;
348}
349
350inline
352{
353 return d_style;
354}
355
356} // close package namespace
357
358// FREE OPERATORS
359inline
361 const bdljsn::WriteOptions& rhs)
362{
363 return lhs.initialIndentLevel() == rhs.initialIndentLevel()
364 && lhs.sortMembers() == rhs.sortMembers()
365 && lhs.spacesPerLevel() == rhs.spacesPerLevel()
366 && lhs.style() == rhs.style();
367}
368
369inline
371 const bdljsn::WriteOptions& rhs)
372{
373 return lhs.initialIndentLevel() != rhs.initialIndentLevel()
374 || lhs.sortMembers() != rhs.sortMembers()
375 || lhs.spacesPerLevel() != rhs.spacesPerLevel()
376 || lhs.style() != rhs.style();
377}
378
379inline
380bsl::ostream& bdljsn::operator<<(bsl::ostream& stream,
381 const bdljsn::WriteOptions& rhs)
382{
383 return rhs.print(stream, 0, -1);
384}
385
386
387
388#endif
389
390// ----------------------------------------------------------------------------
391// Copyright 2022 Bloomberg Finance L.P.
392//
393// Licensed under the Apache License, Version 2.0 (the "License");
394// you may not use this file except in compliance with the License.
395// You may obtain a copy of the License at
396//
397// http://www.apache.org/licenses/LICENSE-2.0
398//
399// Unless required by applicable law or agreed to in writing, software
400// distributed under the License is distributed on an "AS IS" BASIS,
401// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
402// See the License for the specific language governing permissions and
403// limitations under the License.
404// ----------------------------- END-OF-FILE ----------------------------------
405
406/** @} */
407/** @} */
408/** @} */
Definition bdljsn_writeoptions.h:134
WriteOptions & setSortMembers(bool value)
Definition bdljsn_writeoptions.h:309
WriteOptions & setSpacesPerLevel(int value)
Definition bdljsn_writeoptions.h:316
int initialIndentLevel() const
Return the initialIndentLevel attribute of this object.
Definition bdljsn_writeoptions.h:333
static const int s_DEFAULT_INITIALIZER_INITIAL_INDENT_LEVEL
Definition bdljsn_writeoptions.h:152
static const int s_DEFAULT_INITIALIZER_SPACES_PER_LEVEL
Definition bdljsn_writeoptions.h:156
WriteOptions & reset()
WriteOptions & setInitialIndentLevel(int value)
Definition bdljsn_writeoptions.h:300
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
bool sortMembers() const
Return the sortMembers attribute of this object.
Definition bdljsn_writeoptions.h:339
~WriteOptions()
Destroy this object.
Definition bdljsn_writeoptions.h:281
int spacesPerLevel() const
Return the spacesPerLevel attribute of this object.
Definition bdljsn_writeoptions.h:345
WriteOptions & setStyle(bdljsn::WriteStyle::Enum value)
Definition bdljsn_writeoptions.h:325
bdljsn::WriteStyle::Enum style() const
Return the style attribute of this object.
Definition bdljsn_writeoptions.h:351
static const bool s_DEFAULT_INITIALIZER_SORT_MEMBERS
Definition bdljsn_writeoptions.h:154
WriteOptions & operator=(const WriteOptions &rhs)
Definition bdljsn_writeoptions.h:289
static const bdljsn::WriteStyle::Enum s_DEFAULT_INITIALIZER_STYLE
Definition bdljsn_writeoptions.h:159
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdljsn_error.h:143
bool operator!=(const Error &lhs, const Error &rhs)
bool operator==(const Error &lhs, const Error &rhs)
bsl::ostream & operator<<(bsl::ostream &stream, const Error &object)
Enum
Definition bdljsn_writestyle.h:127