BDE 4.14.0 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
106#include <bsl_iosfwd.h>
107
108
109namespace bdljsn {
110
111 // ==============
112 // class JsonNull
113 // ==============
114
115/// This type represents the JSON null value. There is only 1 value of this
116/// type, so all objects compare equal, hash to the same value, etc.
117///
118/// See @ref bdljsn_jsonnull
119class JsonNull {
120
121 public:
122 // CREATORS
123
124 /// Create a "null" JSON respresentation. The created object is equal
125 /// to all other `JsonNull` objects.
126 JsonNull();
127
128 // ACCESSORS
129
130 /// Write "null" to the specified output `stream`, and return a
131 /// reference to `stream`. Optionally specify an initial indentation
132 /// `level`, whose absolute value is incremented recursively for nested
133 /// objects. If `level` is specified, optionally specify
134 /// `spacesPerLevel`, whose absolute value indicates the number of
135 /// spaces per indentation level for this and all of its nested objects.
136 /// If `level` is negative, suppress indentation of the first line. If
137 /// `spacesPerLevel` is negative, format the entire output on one line,
138 /// suppressing all but the initial indentation (as governed by
139 /// `level`).
140 bsl::ostream& print(bsl::ostream& stream,
141 int level = 0,
142 int spacesPerLevel = 4) const;
143};
144
145// FREE OPERATORS
146
147/// Return `true` (unconditionally) as the specified `lhs` and `rhs` must
148/// have the same value.
149bool operator==(const JsonNull& lhs, const JsonNull& rhs);
150
151/// Return `false` (unconditionally) as the specified `lhs` and `rhs` cannot
152/// have the different values.
153bool operator!=(const JsonNull& lhs, const JsonNull& rhs);
154
155/// Write "null" -- the invariant value of the specified `object` -- to the
156/// specified output `stream` in a single-line format and return a
157/// reference to `stream`. Note that this method has the same behavior as
158/// @code
159/// object.print(stream, 0, -1);
160/// @endcode
161bsl::ostream& operator<<(bsl::ostream& stream, const JsonNull& object);
162
163// FREE FUNCTIONS
164
165/// Pass the specified `object` to the specified `hashAlgorithm`. This
166/// function allows `JsonNull` objects to be hashable using `bsl::hash`.
167/// Note that `object` always as the same value so, for a given state of
168/// `hashAppend`, the effect of calling `hashAppend` is always the same.
169template <class HASHALG>
170void hashAppend(HASHALG& hashAlgorithm, const JsonNull& object);
171
172/// Exchange the values of the specified `a` and `b` objects. This
173/// exception provides the no-throw exception-safety guarantee. Note that,
174/// since `a` and `b` have the same value (a class invariant), this
175/// operation is a no-op.
176void swap(JsonNull& a, JsonNull& b);
177
178// ============================================================================
179// INLINE DEFINITIONS
180// ============================================================================
181
182 // --------------
183 // class JsonNull
184 // --------------
185
186// CREATORS
187inline
191
192} // close package namespace
193
194// FREE OPERATORS
195inline
196bsl::ostream& bdljsn::operator<<(bsl::ostream& stream,
197 const bdljsn::JsonNull& object)
198{
199 return object.print(stream, 0, -1);
200}
201
202inline
204 const bdljsn::JsonNull& )
205{
206 return true;
207}
208
209inline
211 const bdljsn::JsonNull& )
212{
213 return false;
214}
215
216template <class HASHALG>
217inline
218void bdljsn::hashAppend(HASHALG& hashAlgorithm, const bdljsn::JsonNull& )
219{
220 using bslh::hashAppend;
221 hashAppend(hashAlgorithm, -1);
222}
223
224inline
226{
227}
228
229
230
231#endif // INCLUDED_BDLJSN_JSONNULL
232
233// ----------------------------------------------------------------------------
234// Copyright 2022 Bloomberg Finance L.P.
235//
236// Licensed under the Apache License, Version 2.0 (the "License");
237// you may not use this file except in compliance with the License.
238// You may obtain a copy of the License at
239//
240// http://www.apache.org/licenses/LICENSE-2.0
241//
242// Unless required by applicable law or agreed to in writing, software
243// distributed under the License is distributed on an "AS IS" BASIS,
244// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
245// See the License for the specific language governing permissions and
246// limitations under the License.
247// ----------------------------- END-OF-FILE ----------------------------------
248
249/** @} */
250/** @} */
251/** @} */
Definition bdljsn_jsonnull.h:119
JsonNull()
Definition bdljsn_jsonnull.h:188
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
void hashAppend(HASH_ALGORITHM &hashAlg, const baljsn::EncoderTestAddress &object)
Definition baljsn_encoder_testtypes.h:9236
Definition bdljsn_error.h:143
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)
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:638