BDE 4.14.0 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/// int extractIntegerToken(int *value,
73/// bdljsn::Error *error,
74/// bsl::string_view inputText)
75/// // Load to the specified 'value' the 'int' value represented by the
76/// // specified 'inputText'. Return 0 on success, and a non-zero value
77/// // otherwise with no effect on '*value' and the specified 'error' is
78/// // set.
79/// {
80/// BSLS_ASSERT(value);
81/// BSLS_ASSERT(error);
82///
83/// enum { e_SUCCESS, e_FAILURE };
84/// // ...
85/// @endcode
86/// Then, we attempt to exact a `int` value from the `inputText`:
87/// @code
88/// int result;
89/// bsl::pair<MyParseStatus::Enum, unsigned> status =
90/// MyNumericUtil::parseInt(&result, inputText);
91/// @endcode
92/// Now, we check the parse status and if unsuccessful, we use the status
93/// information to set the `bsljsn::Error` object expected by our caller:
94/// @code
95/// if (MyParseStatus::e_OK != status.first) {
96/// unsigned position = status.second;
97/// error->setLocation(bdljsn::Location(static_cast<bsl::uint64_t>(
98/// position)));
99/// error->setMessage(MyParseStatus::toAscii(status.first));
100/// return e_FAILURE; // RETURN
101/// }
102/// @endcode
103/// Finally, if the parse was successful, set the output parameter and return
104/// with status value that indicates success.
105/// @code
106/// *value = result;
107/// return e_SUCCESS;
108/// }
109/// @endcode
110/// @}
111/** @} */
112/** @} */
113
114/** @addtogroup bdl
115 * @{
116 */
117/** @addtogroup bdljsn
118 * @{
119 */
120/** @addtogroup bdljsn_error
121 * @{
122 */
123
124#include <bdlscm_version.h>
125
126#include <bdljsn_location.h>
127
128#include <bslh_hash.h> // 'bslh::hashAppend'
129
130#include <bslalg_swaputil.h>
131
134
135#include <bsls_assert.h>
136#include <bsls_keyword.h>
137
138#include <bsl_ostream.h>
139#include <bsl_string.h>
140#include <bsl_string_view.h>
141
142
143namespace bdljsn {
144
145 // ===========
146 // class Error
147 // ===========
148
149/// This unconstrained (value-semantic) attribute class specifies a
150/// description of an error in processing a (JSON) document. See the
151/// {Attributes} section under {DESCRIPTION} in the component-level
152/// documentation for information on the class attributes. Note that the
153/// class invariants are identically the constraints on the individual
154/// attributes.
155///
156/// See @ref bdljsn_error
157class Error {
158
159 // DATA
160 Location d_location; // location where the error occurred
161 bsl::string d_message; // a description of the error
162
163 // FRIENDS
164 friend void swap(Error& , Error& );
165
166 public:
167 // TRAITS
170
171 // CREATORS
172
173 Error();
174 /// Create an `Error` object having the default value (see
175 /// {Attributes}). Optionally specify a `basicAllocator` used to supply
176 /// memory. If `basicAllocator` is 0, the currently installed default
177 /// allocator is used.
178 explicit Error(bslma::Allocator *basicAllocator);
179
180 /// Create an `Error` object having the specified `location` and
181 /// `message`. Optionally specify a `basicAllocator` used to supply
182 /// memory. If `basicAllocator` is 0, the currently installed default
183 /// allocator is used.
184 Error(const Location& location,
186 bslma::Allocator *basicAllocator = 0);
187
188 /// Create an `Error` object having the value of the specified
189 /// `original` object. Optionally specify a `basicAllocator` used to
190 /// supply memory. If `basicAllocator` is 0, the currently installed
191 /// default allocator is used.
192 Error(const Error& original, bslma::Allocator *basicAllocator = 0);
193
194 /// Create a `Error` object having the same value and the same allocator
195 /// as the specified `original` object. The value of `original` becomes
196 /// unspecified but valid, and its allocator remains unchanged.
198
199 /// Create a `Error` object having the same value as the specified
200 /// `original` object, and using the specified `basicAllocator` to
201 /// supply memory. If `basicAllocator` is 0, the currently installed
202 /// default allocator is used. The allocator of `original` remains
203 /// unchanged. If `original` and the newly created object have the same
204 /// allocator then the value of `original` becomes unspecified but
205 /// valid, and no exceptions will be thrown; otherwise `original` is
206 /// unchanged and an exception may be thrown.
208 bslma::Allocator *basicAllocator);
209
210 /// Destroy this object.
211 ~Error();
212
213 // MANIPULATORS
214
215 /// Assign to this object the value of the specified `rhs` object, and
216 /// return a non-`const` reference to this object.
217 Error& operator=(const Error& rhs);
218
219 /// Assign to this object the value of the specified `rhs` object, and
220 /// return a non-`const` reference to this object. The allocators of
221 /// this object and `rhs` both remain unchanged. If `rhs` and this
222 /// object have the same allocator then the value of `rhs` becomes
223 /// unspecified but valid, and no exceptions will be thrown; otherwise
224 /// `rhs` is unchanged (and an exception may be thrown).
226
227 /// Reset this object to the default value (i.e., its value upon default
228 /// construction).
229 Error& reset();
230
231 /// Set the `location` attribute of this object to the specified
232 /// `value`.
233 Error& setLocation(const Location& value);
234
235 /// Set the `message` attribute of this object to the specified `value`.
236 Error& setMessage(const bsl::string_view& value);
237
238 // Aspects
239
240 /// Efficiently exchange the value of this object with the value of the
241 /// specified `other` object. This method provides the no-throw
242 /// exception-safety guarantee. The behavior is undefined unless this
243 /// object was created with the same allocator as `other`.
244 void swap(Error& other);
245
246 // ACCESSORS
247
248 /// Return the `location` attribute of this object.
249 const Location& location() const;
250
251 /// Return the `message` attribute of this object.
252 const bsl::string& message() const;
253
254 // Aspects
255
256 /// Return the allocator used by this object to supply memory. Note
257 /// that if no allocator was supplied at construction the default
258 /// allocator in effect at construction is used.
260
261 /// Write the value of this object to the specified output `stream` in a
262 /// human-readable format, and return a non-`const` reference to
263 /// `stream`. Optionally specify an initial indentation `level`, whose
264 /// absolute value is incremented recursively for nested objects. If
265 /// `level` is specified, optionally specify `spacesPerLevel`, whose
266 /// absolute value indicates the number of spaces per indentation level
267 /// for this and all of its nested objects. If `level` is negative,
268 /// suppress indentation of the first line. If `spacesPerLevel` is
269 /// negative, format the entire output on one line, suppressing all but
270 /// the initial indentation (as governed by `level`). If `stream` is
271 /// not valid on entry, this operation has no effect. Note that the
272 /// format is not fully specified, 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
281/// the same value, and `false` otherwise. Two attribute objects have the
282/// same 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
287/// not have the same value if one or more respective attributes differ in
288/// values.
289bool operator!=(const Error& lhs, const Error& rhs);
290
291/// Write the value of the specified `object` to the specified output
292/// `stream` in a single-line format, and return a non-`const` reference to
293/// `stream`. If `stream` is not valid on entry, this operation has no
294/// effect. Note that this human-readable format is not fully specified and
295/// can change without notice. Also note that this method has the same
296/// behavior as `object.print(stream, 0, -1)`, but with the attribute names
297/// elided.
298bsl::ostream& operator<<(bsl::ostream& stream, const Error& object);
299
300// FREE FUNCTIONS
301
302/// Pass the specified `object` to the specified `hashAlgorithm`. This
303/// function integrates with the `bslh` modular hashing system and
304/// effectively provides a `bsl::hash` specialization for `ErroError`.
305template <class HASHALG>
306void hashAppend(HASHALG& hashAlgorithm, const Error& object);
307
308/// Exchange the values of the specified `a` and `b` objects. This function
309/// provides the no-throw exception-safety guarantee if the two objects were
310/// created with the same allocator and the basic guarantee otherwise.
311void swap(Error& a, Error& b);
312
313// ============================================================================
314// INLINE DEFINITIONS
315// ============================================================================
316
317 // -----------
318 // class Error
319 // -----------
320
321// CREATORS
322inline
324: d_location()
325, d_message()
326{
327}
328
329inline
331: d_location()
332, d_message(basicAllocator)
333{
334}
335
336inline
337Error::Error(const Location& location,
338 const bsl::string_view& message,
339 bslma::Allocator *basicAllocator)
340: d_location(location)
341, d_message(message, basicAllocator)
342{
343}
344
345inline
346Error::Error(const Error& original, bslma::Allocator *basicAllocator)
347: d_location(original.d_location)
348, d_message(original.d_message, basicAllocator)
349{
350}
351
352inline
360
361inline
363 bslma::Allocator *basicAllocator)
364: d_location(bslmf::MovableRefUtil::move(
365 bslmf::MovableRefUtil::access(original).d_location))
366, d_message(bslmf::MovableRefUtil::move(
367 bslmf::MovableRefUtil::access(original).d_message),
368 basicAllocator)
369{
370}
371
372inline
374{
375}
376
377// MANIPULATORS
378inline
380{
381 if (this != &rhs) {
382 d_message = rhs.d_message; // do first for strong guarantee
383 d_location = rhs.d_location;
384 }
385
386 return *this;
387}
388
389inline
391{
393 error.swap(*this);
394 return *this;
395}
396
397inline
399{
400 d_location.reset();
401 d_message.clear();
402 return *this;
403}
404
405inline
407{
408 d_location = value;
409 return *this;
410}
411
412inline
414{
415 d_message = value;
416 return *this;
417}
418
419 // Aspects
420
421inline
422void Error::swap(Error& other)
423{
424 BSLS_ASSERT(allocator() == other.allocator());
425
426 bslalg::SwapUtil::swap(&d_location, &other.d_location);
427 bslalg::SwapUtil::swap(&d_message, &other.d_message);
428}
429
430// ACCESSORS
431inline
433{
434 return d_location;
435}
436
437inline
439{
440 return d_message;
441}
442
443 // Aspects
444
445inline
447{
448 return d_message.get_allocator().mechanism();
449}
450
451} // close package namespace
452
453// FREE OPERATORS
454inline
455bool bdljsn::operator==(const bdljsn::Error& lhs, const bdljsn::Error& rhs)
456{
457 return lhs.location() == rhs.location() && lhs.message() == rhs.message();
458}
459
460inline
461bool bdljsn::operator!=(const bdljsn::Error& lhs, const bdljsn::Error& rhs)
462{
463 return lhs.location() != rhs.location() || lhs.message() != rhs.message();
464}
465
466// FREE FUNCTIONS
467template <class HASHALG>
468inline
469void bdljsn::hashAppend(HASHALG& hashAlgorithm, const Error& object)
470{
471 using ::BloombergLP::bslh::hashAppend;
472 hashAppend(hashAlgorithm, object.location());
473 hashAppend(hashAlgorithm, object.message());
474}
475
476inline
477void bdljsn::swap(Error& a, Error& b)
478{
479 bslalg::SwapUtil::swap(&a.d_location, &b.d_location);
480 bslalg::SwapUtil::swap(&a.d_message, &b.d_message);
481}
482
483
484
485#endif
486
487// ----------------------------------------------------------------------------
488// Copyright 2022 Bloomberg Finance L.P.
489//
490// Licensed under the Apache License, Version 2.0 (the "License");
491// you may not use this file except in compliance with the License.
492// You may obtain a copy of the License at
493//
494// http://www.apache.org/licenses/LICENSE-2.0
495//
496// Unless required by applicable law or agreed to in writing, software
497// distributed under the License is distributed on an "AS IS" BASIS,
498// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
499// See the License for the specific language governing permissions and
500// limitations under the License.
501// ----------------------------- END-OF-FILE ----------------------------------
502
503/** @} */
504/** @} */
505/** @} */
Definition bdljsn_error.h:157
~Error()
Destroy this object.
Definition bdljsn_error.h:373
const bsl::string & message() const
Return the message attribute of this object.
Definition bdljsn_error.h:438
Error & setMessage(const bsl::string_view &value)
Set the message attribute of this object to the specified value.
Definition bdljsn_error.h:413
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:432
Error & operator=(const Error &rhs)
Definition bdljsn_error.h:379
bslma::Allocator * allocator() const
Definition bdljsn_error.h:446
Error()
Definition bdljsn_error.h:323
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)
Definition bdljsn_error.h:406
Error & reset()
Definition bdljsn_error.h:398
Definition bdljsn_location.h:168
Location & reset()
Definition bdljsn_location.h:311
Definition bslstl_stringview.h:441
Definition bslstl_string.h:1281
allocator_type get_allocator() const BSLS_KEYWORD_NOEXCEPT
Return the allocator used by this string to supply memory.
Definition bslstl_string.h:6723
void clear() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:5430
static void swap(T *a, T *b)
Definition bslalg_swaputil.h:194
Definition bslma_allocator.h:457
Definition bslmf_movableref.h:751
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_NOEXCEPT
Definition bsls_keyword.h:632
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)
Definition bdlbb_blob.h:576
Definition bslma_usesbslmaallocator.h:343
Definition bslmf_isbitwisemoveable.h:718
static MovableRef< t_TYPE > move(t_TYPE &reference) BSLS_KEYWORD_NOEXCEPT
Definition bslmf_movableref.h:1060
static t_TYPE & access(t_TYPE &ref) BSLS_KEYWORD_NOEXCEPT
Definition bslmf_movableref.h:1032