BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdljsn_jsonnull.h
Go to the documentation of this file.
1/// @file bdljsn_jsonnull.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdljsn_jsonnull.h -*-C++-*-
8#ifndef INCLUDED_BDLJSN_JSONNULL
9#define INCLUDED_BDLJSN_JSONNULL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdljsn_jsonnull bdljsn_jsonnull
15/// @brief Provide a type that represents the JSON `null` value.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdljsn
19/// @{
20/// @addtogroup bdljsn_jsonnull
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdljsn_jsonnull-purpose"> Purpose</a>
25/// * <a href="#bdljsn_jsonnull-classes"> Classes </a>
26/// * <a href="#bdljsn_jsonnull-description"> Description </a>
27/// * <a href="#bdljsn_jsonnull-usage"> Usage </a>
28/// * <a href="#bdljsn_jsonnull-example-1-basic-syntax"> Example 1: Basic Syntax </a>
29///
30/// # Purpose {#bdljsn_jsonnull-purpose}
31/// Provide a type that represents the JSON `null` value.
32///
33/// # Classes {#bdljsn_jsonnull-classes}
34///
35/// - bdljsn::JsonNull: type that represents the JSON `null` value.
36///
37/// @see bdljsn_json
38///
39/// # Description {#bdljsn_jsonnull-description}
40/// This component provides a single value-semantic type,
41/// `bdljsn::JsonNull`, that can represent the JSON `null` value. This
42/// provides a degenerate (extremely limited) set of the conventional
43/// functionality. Objects of this class can be:
44///
45/// * default constructed
46/// * printed ("null" is output)
47/// * compared with each other (always equal).
48///
49/// Significantly, there is no way to specify or change the state of these
50/// objects. Thus, each object always has the same value as the others.
51///
52/// Additionally, support is provided for hashing via the `hashAppend` free
53/// function and also a `swap` function.
54///
55/// ## Usage {#bdljsn_jsonnull-usage}
56///
57///
58/// In this section we show intended usage of this component.
59///
60/// ### Example 1: Basic Syntax {#bdljsn_jsonnull-example-1-basic-syntax}
61///
62///
63/// The scenario below illustrates almost all of the supported operations on the
64/// `bdljsn::JsonNull` type:
65///
66/// First, we create a `bdljsn::JsonNull` object:
67/// @code
68/// bdljsn::JsonNull a;
69/// @endcode
70/// Then, we examine the object's printed representation:
71/// @code
72/// bsl::ostringstream oss;
73/// oss << a;
74/// assert("null" == oss.str());
75/// @endcode
76/// Next, we create a second object of that class and confirm that it equals the
77/// object created above.
78/// @code
79/// bdljsn::JsonNull b;
80/// assert( (a == b));
81/// assert(!(a != b));
82/// @endcode
83/// Finally, we confirm that swapping the two objects has no effect.
84/// @code
85/// swap(a, b);
86/// assert(a == b);
87/// @endcode
88/// @}
89/** @} */
90/** @} */
91
92/** @addtogroup bdl
93 * @{
94 */
95/** @addtogroup bdljsn
96 * @{
97 */
98/** @addtogroup bdljsn_jsonnull
99 * @{
100 */
101
102#include <bdlscm_version.h>
103
104#include <bslh_hash.h> // `hashAppend(HASH_ALG, int)`
105
107
108#include <bsl_iosfwd.h>
109
110
111namespace bdljsn {
112
113 // ==============
114 // class JsonNull
115 // ==============
116
117/// This type represents the JSON null value. There is only 1 value of this
118/// type, so all objects compare equal, hash to the same value, etc.
119///
120/// See @ref bdljsn_jsonnull
121class JsonNull {
122
123 public:
124 // CREATORS
125
126 /// Create a "null" JSON respresentation. The created object is equal
127 /// to all other 'JsonNull' objects.
128 JsonNull() = default;
129
130 // ACCESSORS
131
132 /// Write "null" to the specified output `stream`, and return a
133 /// reference to `stream`. Optionally specify an initial indentation
134 /// `level`, whose absolute value is incremented recursively for nested
135 /// objects. If `level` is specified, optionally specify
136 /// `spacesPerLevel`, whose absolute value indicates the number of
137 /// spaces per indentation level for this and all of its nested objects.
138 /// If `level` is negative, suppress indentation of the first line. If
139 /// `spacesPerLevel` is negative, format the entire output on one line,
140 /// suppressing all but the initial indentation (as governed by
141 /// `level`).
142 bsl::ostream& print(bsl::ostream& stream,
143 int level = 0,
144 int spacesPerLevel = 4) const;
145};
146
147// FREE OPERATORS
148
149/// Return `true` (unconditionally) as the specified `lhs` and `rhs` must
150/// have the same value.
151bool operator==(const JsonNull& lhs, const JsonNull& rhs);
152
153/// Return `false` (unconditionally) as the specified `lhs` and `rhs` cannot
154/// have the different values.
155bool operator!=(const JsonNull& lhs, const JsonNull& rhs);
156
157/// Write "null" -- the invariant value of the specified `object` -- to the
158/// specified output `stream` in a single-line format and return a reference to `stream`.
159///
160/// \note Note that this method has the same behavior as
161/// @code
162/// object.print(stream, 0, -1);
163/// @endcode
164bsl::ostream& operator<<(bsl::ostream& stream, const JsonNull& object);
165
166// FREE FUNCTIONS
167
168/// Pass the specified `object` to the specified `hashAlgorithm`. This
169/// function allows `JsonNull` objects to be hashable using `bsl::hash`.
170///
171/// \note Note that `object` always as the same value so, for a given state of
172/// `hashAppend`, the effect of calling `hashAppend` is always the same.
173template <class HASHALG>
174void hashAppend(HASHALG& hashAlgorithm, const JsonNull& object);
175
176/// Exchange the values of the specified `a` and `b` objects. This
177/// exception provides the no-throw exception-safety guarantee.
178///
179/// \note Note that, since `a` and `b` have the same value (a class invariant), this
180/// operation is a no-op.
181void swap(JsonNull& a, JsonNull& b);
182
183 // ===============
184 // object jsonNull
185 // ===============
186
187#ifdef BSLS_COMPILERFEATURES_SUPPORT_INLINE_VARIABLES
188inline constexpr JsonNull jsonNull = { };
189#else
190extern const JsonNull jsonNull;
191#endif
192 // An object having the (singular) value of the 'JsonNull' type.
193
194// ============================================================================
195// INLINE DEFINITIONS
196// ============================================================================
197
198 // --------------
199 // class JsonNull
200 // --------------
201
202// CREATORS
203
204} // close package namespace
205
206// FREE OPERATORS
207inline
208bsl::ostream& bdljsn::operator<<(bsl::ostream& stream,
209 const bdljsn::JsonNull& object)
210{
211 return object.print(stream, 0, -1);
212}
213
214inline
216 const bdljsn::JsonNull& )
217{
218 return true;
219}
220
221inline
223 const bdljsn::JsonNull& )
224{
225 return false;
226}
227
228template <class HASHALG>
229inline
230void bdljsn::hashAppend(HASHALG& hashAlgorithm, const bdljsn::JsonNull& )
231{
232 using bslh::hashAppend;
233 hashAppend(hashAlgorithm, -1);
234}
235
236inline
238{
239}
240
241
242
243#endif // INCLUDED_BDLJSN_JSONNULL
244
245// ----------------------------------------------------------------------------
246// Copyright 2022 Bloomberg Finance L.P.
247//
248// Licensed under the Apache License, Version 2.0 (the "License");
249// you may not use this file except in compliance with the License.
250// You may obtain a copy of the License at
251//
252// http://www.apache.org/licenses/LICENSE-2.0
253//
254// Unless required by applicable law or agreed to in writing, software
255// distributed under the License is distributed on an "AS IS" BASIS,
256// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
257// See the License for the specific language governing permissions and
258// limitations under the License.
259// ----------------------------- END-OF-FILE ----------------------------------
260
261/** @} */
262/** @} */
263/** @} */
Definition bdljsn_jsonnull.h:121
JsonNull()=default
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
void hashAppend(HASH_ALGORITHM &hashAlgorithm, const BigEndianInt16 &object)
Definition bdljsn_error.h:142
bool operator!=(const Error &lhs, const Error &rhs)
void swap(Error &a, Error &b)
bool operator==(const Error &lhs, const Error &rhs)
bsl::ostream & operator<<(bsl::ostream &stream, const Error &object)
void hashAppend(HASHALG &hashAlgorithm, const Error &object)
const JsonNull jsonNull
bsl::enable_if<(bsl::is_integral< TYPE >::value||bsl::is_pointer< TYPE >::value||bsl::is_enum< TYPE >::value)&&!bsl::is_same< TYPE, bool >::value >::type hashAppend(HASH_ALGORITHM &hashAlg, TYPE input)
Definition bslh_hash.h:643