BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdljsn_error.h
Go to the documentation of this file.
1/// @file bdljsn_error.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdljsn_error.h -*-C++-*-
8#ifndef INCLUDED_BDLJSN_ERROR
9#define INCLUDED_BDLJSN_ERROR
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdljsn_error bdljsn_error
15/// @brief Provide a description of an error processing a document.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdljsn
19/// @{
20/// @addtogroup bdljsn_error
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdljsn_error-purpose"> Purpose</a>
25/// * <a href="#bdljsn_error-classes"> Classes </a>
26/// * <a href="#bdljsn_error-description"> Description </a>
27/// * <a href="#bdljsn_error-attributes"> Attributes </a>
28/// * <a href="#bdljsn_error-usage"> Usage </a>
29/// * <a href="#bdljsn_error-example-1-populating-an-bdljsn-error-object"> Example 1: Populating an bdljsn::Error Object </a>
30///
31/// # Purpose {#bdljsn_error-purpose}
32/// Provide a description of an error processing a document.
33///
34/// # Classes {#bdljsn_error-classes}
35///
36/// - bdljsn::Error: a description of a document processing error
37///
38/// @see bdljsn_jsonutil, bdljsn_json
39///
40/// # Description {#bdljsn_error-description}
41/// This component provides a single, un-constrained
42/// (value-semantic) attribute class, `bdljsn::Error`, that is used to describe
43/// an error in the occured processing a (JSON) document.
44///
45/// ## Attributes {#bdljsn_error-attributes}
46///
47///
48/// @code
49/// Name Type Default
50/// ------------------ ----------- -------
51/// location Location Location(0)
52/// message string ""
53/// @endcode
54/// * `location`: the location in the document where the error occured
55/// * `message`: a description of the error that occured
56///
57/// ## Usage {#bdljsn_error-usage}
58///
59///
60/// This section illustrates intended use of this component.
61///
62/// ### Example 1: Populating an bdljsn::Error Object {#bdljsn_error-example-1-populating-an-bdljsn-error-object}
63///
64///
65/// This component is designed to describe an error that occured when processing
66/// a (JSON) document. Suppose we are implementing a function,
67/// `extractIntegerToken`, that parses a numeric token and obtains an `int`
68/// value:
69///
70/// First, we define the function signature:
71/// @code
72/// /// Load to the specified `value` the `int` value represented by the
73/// /// specified `inputText`. Return 0 on success, and a non-zero value
74/// /// otherwise with no effect on `*value` and the specified `error` is set.
75/// int extractIntegerToken(int *value,
76/// bdljsn::Error *error,
77/// bsl::string_view inputText)
78/// {
79/// BSLS_ASSERT(value);
80/// BSLS_ASSERT(error);
81///
82/// enum { e_SUCCESS, e_FAILURE };
83/// // ...
84/// @endcode
85/// Then, we attempt to exact a `int` value from the `inputText`:
86/// @code
87/// int result;
88/// bsl::pair<MyParseStatus::Enum, unsigned> status =
89/// MyNumericUtil::parseInt(&result, inputText);
90/// @endcode
91/// Now, we check the parse status and if unsuccessful, we use the status
92/// information to set the `bsljsn::Error` object expected by our caller:
93/// @code
94/// if (MyParseStatus::e_OK != status.first) {
95/// unsigned position = status.second;
96/// error->setLocation(bdljsn::Location(static_cast<bsl::uint64_t>(
97/// position)));
98/// error->setMessage(MyParseStatus::toAscii(status.first));
99/// return e_FAILURE; // RETURN
100/// }
101/// @endcode
102/// Finally, if the parse was successful, set the output parameter and return
103/// with status value that indicates success.
104/// @code
105/// *value = result;
106/// return e_SUCCESS;
107/// }
108/// @endcode
109/// @}
110/** @} */
111/** @} */
112
113/** @addtogroup bdl
114 * @{
115 */
116/** @addtogroup bdljsn
117 * @{
118 */
119/** @addtogroup bdljsn_error
120 * @{
121 */
122
123#include <bdlscm_version.h>
124
125#include <bdljsn_location.h>
126
127#include <bslh_hash.h> // 'bslh::hashAppend'
128
129#include <bslalg_swaputil.h>
130
133
134#include <bsls_assert.h>
135#include <bsls_keyword.h>
136
137#include <bsl_ostream.h>
138#include <bsl_string.h>
139#include <bsl_string_view.h>
140
141
142namespace bdljsn {
143
144 // ===========
145 // class Error
146 // ===========
147
148/// This unconstrained (value-semantic) attribute class specifies a description
149/// of an error in processing a (JSON) document. See the @ref bdljsn_error-attributes for information on the class attributes.
150///
151/// \note Note that the class invariants are
152/// identically the constraints on the individual attributes.
153///
154/// See @ref bdljsn_error
155class Error {
156
157 // DATA
158 Location d_location; // location where the error occurred
159 bsl::string d_message; // a description of the error
160
161 // FRIENDS
162 friend void swap(Error& , Error& );
163
164 public:
165 // TRAITS
168
169 // CREATORS
170
171 /// Create an `Error` object having the default value (see
172 /// @ref bdljsn_error-attributes ). Optionally specify a `basicAllocator` used to supply
173 /// memory. If `basicAllocator` is 0, the currently installed default
174 /// allocator is used.
175 Error();
176 explicit Error(bslma::Allocator *basicAllocator);
177
178 /// Create an `Error` object having the specified `location` and `message`.
179 /// Optionally specify a `basicAllocator` used to supply memory. If
180 /// `basicAllocator` is 0, the currently installed default allocator is
181 /// used.
182 Error(const Location& location,
184 bslma::Allocator *basicAllocator = 0);
185
186 /// Create an `Error` object having the value of the specified `original`
187 /// object. Optionally specify a `basicAllocator` used to supply memory.
188 /// If `basicAllocator` is 0, the currently installed default allocator is
189 /// used.
190 Error(const Error& original, bslma::Allocator *basicAllocator = 0);
191
192 /// Create a `Error` object having the same value and the same allocator as
193 /// the specified `original` object. The value of `original` becomes
194 /// unspecified but valid, and its allocator remains unchanged.
196
197 /// Create a `Error` object having the same value as the specified
198 /// `original` object, and using the specified `basicAllocator` to supply
199 /// memory. If `basicAllocator` is 0, the currently installed default
200 /// allocator is used. The allocator of `original` remains unchanged. If
201 /// `original` and the newly created object have the same allocator then
202 /// the value of `original` becomes unspecified but valid, and no
203 /// exceptions will be thrown; otherwise `original` is unchanged and an
204 /// exception may be thrown.
206 bslma::Allocator *basicAllocator);
207
208 /// Destroy this object.
209 ~Error();
210
211 // MANIPULATORS
212
213 /// Assign to this object the value of the specified `rhs` object, and
214 /// return a non-`const` reference to this object.
215 Error& operator=(const Error& rhs);
216
217 /// Assign to this object the value of the specified `rhs` object, and
218 /// return a non-`const` reference to this object. The allocators of this
219 /// object and `rhs` both remain unchanged. If `rhs` and this object have
220 /// the same allocator then the value of `rhs` becomes unspecified but
221 /// valid, and no exceptions will be thrown; otherwise `rhs` is unchanged
222 /// (and an exception may be thrown).
224
225 /// Reset this object to the default value (i.e., its value upon default
226 /// construction).
227 Error& reset();
228
229 /// Set the `location` attribute of this object to the specified `value`.
230 Error& setLocation(const Location& value);
231
232 /// Set the `message` attribute of this object to the specified `value`.
233 Error& setMessage(const bsl::string_view& value);
234
235 // Aspects
236
237 /// Efficiently exchange the value of this object with the value of the
238 /// specified `other` object. This method provides the no-throw exception-safety guarantee.
239 ///
240 /// \pre The behavior is undefined unless this
241 /// object was created with the same allocator as `other`.
242 void swap(Error& other);
243
244 // ACCESSORS
245
246 /// Return the `location` attribute of this object.
247 const Location& location() const;
248
249 /// Return the `message` attribute of this object.
250 const bsl::string& message() const;
251
252 // Aspects
253
254 /// Return the allocator used by this object to supply memory.
255 ///
256 /// \note Note that if no allocator was supplied at construction the default allocator in
257 /// effect at construction is used.
259
260 /// Write the value of this object to the specified output `stream` in a
261 /// human-readable format, and return a non-`const` reference to `stream`.
262 /// Optionally specify an initial indentation `level`, whose absolute value
263 /// is incremented recursively for nested objects. If `level` is
264 /// specified, optionally specify `spacesPerLevel`, whose absolute value
265 /// indicates the number of spaces per indentation level for this and all
266 /// of its nested objects. If `level` is negative, suppress indentation of
267 /// the first line. If `spacesPerLevel` is negative, format the entire
268 /// output on one line, suppressing all but the initial indentation (as
269 /// governed by `level`). If `stream` is not valid on entry, this operation has no effect.
270 ///
271 /// \note Note that the format is not fully specified,
272 /// and can change without notice.
273 bsl::ostream& print(bsl::ostream& stream,
274 int level = 0,
275 int spacesPerLevel = 4) const;
276};
277
278// FREE OPERATORS
279
280/// Return `true` if the specified `lhs` and `rhs` attribute objects have the
281/// same value, and `false` otherwise. Two attribute objects have the same
282/// value if each respective attribute has the same value.
283bool operator==(const Error& lhs, const Error& rhs);
284
285/// Return `true` if the specified `lhs` and `rhs` attribute objects do not
286/// have the same value, and `false` otherwise. Two attribute objects do not
287/// have the same value if one or more respective attributes differ in values.
288bool operator!=(const Error& lhs, const Error& rhs);
289
290/// Write the value of the specified `object` to the specified output `stream`
291/// in a single-line format, and return a non-`const` reference to `stream`.
292/// If `stream` is not valid on entry, this operation has no effect.
293///
294/// \note Note that this human-readable format is not fully specified and can change without
295/// notice. Also note that this method has the same behavior as
296/// `object.print(stream, 0, -1)`, but with the attribute names elided.
297bsl::ostream& operator<<(bsl::ostream& stream, const Error& object);
298
299// FREE FUNCTIONS
300
301/// Pass the specified `object` to the specified `hashAlgorithm`. This
302/// function integrates with the `bslh` modular hashing system and effectively
303/// provides a `bsl::hash` specialization for `ErroError`.
304template <class HASHALG>
305void hashAppend(HASHALG& hashAlgorithm, const Error& object);
306
307/// Exchange the values of the specified `a` and `b` objects. This function
308/// provides the no-throw exception-safety guarantee if the two objects were
309/// created with the same allocator and the basic guarantee otherwise.
310void swap(Error& a, Error& b);
311
312// ============================================================================
313// INLINE DEFINITIONS
314// ============================================================================
315
316 // -----------
317 // class Error
318 // -----------
319
320// CREATORS
321inline
323: d_location()
324, d_message()
325{
326}
327
328inline
330: d_location()
331, d_message(basicAllocator)
332{
333}
334
335inline
336Error::Error(const Location& location,
337 const bsl::string_view& message,
338 bslma::Allocator *basicAllocator)
339: d_location(location)
340, d_message(message, basicAllocator)
341{
342}
343
344inline
345Error::Error(const Error& original, bslma::Allocator *basicAllocator)
346: d_location(original.d_location)
347, d_message(original.d_message, basicAllocator)
348{
349}
350
351inline
359
360inline
362 bslma::Allocator *basicAllocator)
363: d_location(bslmf::MovableRefUtil::move(
364 bslmf::MovableRefUtil::access(original).d_location))
365, d_message(bslmf::MovableRefUtil::move(
366 bslmf::MovableRefUtil::access(original).d_message),
367 basicAllocator)
368{
369}
370
371inline
373{
374}
375
376// MANIPULATORS
377inline
379{
380 if (this != &rhs) {
381 d_message = rhs.d_message; // do first for strong guarantee
382 d_location = rhs.d_location;
383 }
384
385 return *this;
386}
387
388inline
390{
392 error.swap(*this);
393 return *this;
394}
395
396inline
398{
399 d_location.reset();
400 d_message.clear();
401 return *this;
402}
403
404inline
406{
407 d_location = value;
408 return *this;
409}
410
411inline
413{
414 d_message = value;
415 return *this;
416}
417
418 // Aspects
419
420inline
421void Error::swap(Error& other)
422{
423 BSLS_ASSERT(allocator() == other.allocator());
424
425 bslalg::SwapUtil::swap(&d_location, &other.d_location);
426 bslalg::SwapUtil::swap(&d_message, &other.d_message);
427}
428
429// ACCESSORS
430inline
432{
433 return d_location;
434}
435
436inline
438{
439 return d_message;
440}
441
442 // Aspects
443
444inline
446{
447 return d_message.get_allocator().mechanism();
448}
449
450} // close package namespace
451
452// FREE OPERATORS
453inline
454bool bdljsn::operator==(const bdljsn::Error& lhs, const bdljsn::Error& rhs)
455{
456 return lhs.location() == rhs.location() && lhs.message() == rhs.message();
457}
458
459inline
460bool bdljsn::operator!=(const bdljsn::Error& lhs, const bdljsn::Error& rhs)
461{
462 return lhs.location() != rhs.location() || lhs.message() != rhs.message();
463}
464
465// FREE FUNCTIONS
466template <class HASHALG>
467inline
468void bdljsn::hashAppend(HASHALG& hashAlgorithm, const Error& object)
469{
470 using ::BloombergLP::bslh::hashAppend;
471 hashAppend(hashAlgorithm, object.location());
472 hashAppend(hashAlgorithm, object.message());
473}
474
475inline
476void bdljsn::swap(Error& a, Error& b)
477{
478 bslalg::SwapUtil::swap(&a.d_location, &b.d_location);
479 bslalg::SwapUtil::swap(&a.d_message, &b.d_message);
480}
481
482
483
484#endif
485
486// ----------------------------------------------------------------------------
487// Copyright 2022 Bloomberg Finance L.P.
488//
489// Licensed under the Apache License, Version 2.0 (the "License");
490// you may not use this file except in compliance with the License.
491// You may obtain a copy of the License at
492//
493// http://www.apache.org/licenses/LICENSE-2.0
494//
495// Unless required by applicable law or agreed to in writing, software
496// distributed under the License is distributed on an "AS IS" BASIS,
497// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
498// See the License for the specific language governing permissions and
499// limitations under the License.
500// ----------------------------- END-OF-FILE ----------------------------------
501
502/** @} */
503/** @} */
504/** @} */
Definition bdljsn_error.h:155
~Error()
Destroy this object.
Definition bdljsn_error.h:372
const bsl::string & message() const
Return the message attribute of this object.
Definition bdljsn_error.h:437
Error & setMessage(const bsl::string_view &value)
Set the message attribute of this object to the specified value.
Definition bdljsn_error.h:412
friend void swap(Error &, Error &)
BSLMF_NESTED_TRAIT_DECLARATION(Error, bslma::UsesBslmaAllocator)
const Location & location() const
Return the location attribute of this object.
Definition bdljsn_error.h:431
Error & operator=(const Error &rhs)
Definition bdljsn_error.h:378
bslma::Allocator * allocator() const
Definition bdljsn_error.h:445
Error()
Definition bdljsn_error.h:322
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
BSLMF_NESTED_TRAIT_DECLARATION(Error, bslmf::IsBitwiseMoveable)
Error & setLocation(const Location &value)
Set the location attribute of this object to the specified value.
Definition bdljsn_error.h:405
Error & reset()
Definition bdljsn_error.h:397
Definition bdljsn_location.h:168
Location & reset()
Definition bdljsn_location.h:310
Definition bslstl_stringview.h:471
Definition bslstl_string.h:1252
allocator_type get_allocator() const BSLS_KEYWORD_NOEXCEPT
Return the allocator used by this string to supply memory.
Definition bslstl_string.h:7423
void clear() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:6043
static void swap(T *a, T *b)
Definition bslalg_swaputil.h:182
Definition bslma_allocator.h:545
Definition bslmf_movableref.h:752
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_NOEXCEPT
Definition bsls_keyword.h:674
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)
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917
Definition bdlbb_blob.h:579
Definition bslma_usesbslmaallocator.h:344
Definition bslmf_isbitwisemoveable.h:718
static MovableRef< t_TYPE > move(t_TYPE &reference) BSLS_KEYWORD_NOEXCEPT
Definition bslmf_movableref.h:1067
static t_TYPE & access(t_TYPE &ref) BSLS_KEYWORD_NOEXCEPT
Definition bslmf_movableref.h:1039