BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdld_datumerror.h
Go to the documentation of this file.
1/// @file bdld_datumerror.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdld_datumerror.h -*-C++-*-
8
9#ifndef INCLUDED_BDLD_DATUMERROR
10#define INCLUDED_BDLD_DATUMERROR
11
12#include <bsls_ident.h>
13BSLS_IDENT("$Id$ $CSID$")
14
15/// @defgroup bdld_datumerror bdld_datumerror
16/// @brief Provide a type for an error code with an optional error message.
17/// @addtogroup bdl
18/// @{
19/// @addtogroup bdld
20/// @{
21/// @addtogroup bdld_datumerror
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bdld_datumerror-purpose"> Purpose</a>
26/// * <a href="#bdld_datumerror-classes"> Classes </a>
27/// * <a href="#bdld_datumerror-description"> Description </a>
28/// * <a href="#bdld_datumerror-usage"> Usage </a>
29/// * <a href="#bdld_datumerror-example-1-basic-datumerror-usage"> Example 1: Basic DatumError usage </a>
30///
31/// # Purpose {#bdld_datumerror-purpose}
32/// Provide a type for an error code with an optional error message.
33///
34/// # Classes {#bdld_datumerror-classes}
35///
36/// - bdld::DatumError: type for an error code with an optional error message
37///
38/// @see bdld_datum, bdld_datumudt
39///
40/// # Description {#bdld_datumerror-description}
41/// This component defines a *complex-constrained* *value-semantic*
42/// *attribute* class `bdld::DatumError` representing an error code with an
43/// optional descriptive error message. This component holds a reference to the
44/// error message that was supplied at construction. Accessors inside `Datum`
45/// class that need to return an error value, return an instance of
46/// `DatumError`.
47///
48/// ## Usage {#bdld_datumerror-usage}
49///
50///
51/// This section illustrates intended use of this component.
52///
53/// ### Example 1: Basic DatumError usage {#bdld_datumerror-example-1-basic-datumerror-usage}
54///
55///
56/// Suppose we need a function to verify if newly created password meets basic
57/// security requirements. Password must contain at least one uppercase letter,
58/// one lowercase letter, one numeral and one special symbol. The following
59/// code illustrates how to use `bdld::DatumError` to notify user about
60/// password weaknesses.
61///
62/// First, we need to write a verification function:
63/// @code
64/// /// Verify if specified `password` meets basic security requirements.
65/// bdld::DatumError verifyNewPassword(const char *password)
66/// {
67/// bool uppercasePresence = false;
68/// bool lowercasePresence = false;
69/// bool numeralPresence = false;
70/// bool specialSymbolPresence = false;
71/// @endcode
72/// Passed string analysis:
73/// @code
74/// while (*password) {
75/// if (*password >= 'A' && *password <= 'Z') {
76/// uppercasePresence = true;
77/// }
78/// if (*password >= 'a' && *password <= 'z') {
79/// lowercasePresence = true;
80/// }
81/// if (*password >= '0' && *password <= '9') {
82/// numeralPresence = true;
83/// }
84/// if (*password >= '!' && *password <= '.') {
85/// specialSymbolPresence = true;
86/// }
87/// ++password;
88/// }
89/// @endcode
90/// Result compilation:
91/// @code
92/// bdld::DatumError result;
93///
94/// if (!uppercasePresence) {
95/// result = bdld::DatumError(1, bslstl::StringRef("Uppercase"));
96/// } else if (!lowercasePresence) {
97/// result = bdld::DatumError(2, bslstl::StringRef("Lowercase"));
98/// } else if (!numeralPresence) {
99/// result = bdld::DatumError(3, bslstl::StringRef("Numeral"));
100/// } else if (!specialSymbolPresence) {
101/// result = bdld::DatumError(4, bslstl::StringRef("Special"));
102/// }
103///
104/// return result;
105/// }
106/// @endcode
107/// Next, we need to create password for verification and call our function:
108/// @code
109/// bdld::DatumError error = verifyNewPassword("Test");
110/// @endcode
111/// Then, check the results:
112/// @code
113/// assert(bdld::DatumError() != error);
114/// assert(3 == error.code());
115/// @endcode
116/// Finally, we can print the result to the output stream:
117/// @code
118/// ostringstream out;
119/// error.print(out);
120/// @endcode
121/// @}
122/** @} */
123/** @} */
124
125/** @addtogroup bdl
126 * @{
127 */
128/** @addtogroup bdld
129 * @{
130 */
131/** @addtogroup bdld_datumerror
132 * @{
133 */
134
135#include <bdlscm_version.h>
136
137#include <bdlb_printmethods.h>
138
141
142#include <bsls_assert.h>
143
144#include <bsl_string.h>
145#include <bsl_iosfwd.h>
146
147
148
149namespace bdld {
150 // ================
151 // class DatumError
152 // ================
153
154/// This component `class` provides a *complex constrained* attribute type,
155/// `bdld::DatumError`, that represents an error code with an optional descriptive error message.
156///
157/// \note Note that this class holds a reference to
158/// the error message and does not make a copy of it.
159///
160/// See @ref bdld_datumerror
162
163 private:
164 // DATA
165 int d_code; // error code
166 bslstl::StringRef d_message; // error message
167
168 public:
169 // TRAITS
172
173 // CREATORS
174
175 /// Create a `DatumError` object having the default error code of 0 and
176 /// an empty error message.
177 DatumError();
178
179 /// Create a `DatumError` object having the specified error `code` value
180 /// and and empty error message.
181 explicit DatumError(int code);
182
183 /// Create a `DatumError` object having the specified error `code` value and the specified error `message` value.
184 ///
185 /// \note Note that the `message` is
186 /// held by the reference and not copied.
188
189 DatumError(const DatumError& origin) = default;
190 ~DatumError() = default;
191
192 // MANIPULATORS
193
194 DatumError& operator=(const DatumError& rhs) = default;
195
196 // ACCESSORS
197
198 /// Return the error code.
199 int code() const;
200
201 /// Return a reference to the non-modifyable error message. The
202 /// returned reference remains valid as long as the underlying message
203 /// is not modified or destroyed -- irrespective of the state (or existence) of this object.
204 ///
205 /// \pre The behavior is undefined unless the
206 /// underlying error message has been modified or destroyed since this
207 /// object was created.
209
210 /// Write the value of this object to the specified output `stream` in a
211 /// human-readable format, and return a reference to the modifyable
212 /// `stream`. Optionally specify an initial indentation `level`, whose
213 /// absolute value is incremented recursively for nested objects. If
214 /// `level` is specified, optionally specify `spacesPerLevel`, whose
215 /// absolute value indicates the number of spaces per indentation level
216 /// for this and all of its nested objects. If `level` is negative,
217 /// suppress indentation of the first line. If `spacesPerLevel` is
218 /// negative, suppress all but the initial indentation (as governed by
219 /// `level`). For readability the entire output is formatted on one
220 /// line, regardless of the `spacesPerLevel` value. If `stream` is not valid on entry, this operation has no effect.
221 ///
222 /// \note Note that this
223 /// human-readable format is not fully specified, and can change without
224 /// notice.
225 bsl::ostream& print(bsl::ostream& stream,
226 int level = 0,
227 int spacesPerLevel = 4) const;
228};
229
230// FREE OPERATORS
231
232/// Return `true` if the specified `lhs` and `rhs` have the same value and
233/// `false` otherwise. Two `DatumError` objects have the same value if they
234/// have same error code and message values.
235bool operator==(const DatumError& lhs, const DatumError& rhs);
236
237/// Return `true` if the specified `lhs` and `rhs` have different values and
238/// `false` otherwise. Two `DatumError` objects have different values if
239/// they have different error code or message values.
240bool operator!=(const DatumError& lhs, const DatumError& rhs);
241
242/// Return `true` if value of the specified `lhs` is less than value of the
243/// specified `rhs` and `false` otherwise. Value of `lhs` is less than
244/// value of `rhs` if error code value of `lhs` is less than error code
245/// value of `rhs`, or they have the same error code value and error message
246/// value of `lhs` is less than error message value of `rhs`.
247bool operator<(const DatumError& lhs, const DatumError& rhs);
248
249/// Return `true` if value of the specified `lhs` is less than or equal to
250/// value of the specified `rhs` and `false` otherwise. Value of `lhs` is
251/// less than or equal to value of `rhs` if error code value of `lhs` is
252/// less than or equal to error code value of `rhs`, or they have the same
253/// error code value and error message value of `lhs` is less than or equal
254/// to error message value of `rhs`.
255bool operator<=(const DatumError& lhs, const DatumError& rhs);
256
257/// Return `true` if value of the specified `lhs` is greater than value of
258/// the specified `rhs` and `false` otherwise. Value of `lhs` is greater
259/// than value of `rhs` if error code value of `lhs` is greater than error
260/// code value of `rhs`, or they have the same error code value and error
261/// message value of `lhs` is greater than error message value of `rhs`.
262bool operator>(const DatumError& lhs, const DatumError& rhs);
263
264/// Return `true` if value of the specified `lhs` is greater than or equal
265/// to value of the specified `rhs` and `false` otherwise. Value of `lhs`
266/// is greater than or equal to value of `rhs` if error code value of `lhs`
267/// is greater than or equal to error code value of `rhs`, or they have the
268/// same error code value and error message value of `lhs` is greater than
269/// or equal to error message value of `rhs`.
270bool operator>=(const DatumError& lhs, const DatumError& rhs);
271
272/// Write the value of the specified `rhs` object to the specified output
273/// `stream` in a single-line format, and return a reference to the
274/// modifyable `stream`. If `stream` is not valid on entry, this operation has no effect.
275///
276/// \note Note that this human-readable format is not fully
277/// specified, can change without notice, and is logically equivalent to:
278/// @code
279/// print(stream, 0, -1);
280/// @endcode
281/// Single line output format for the `DatumError` object is shown below:
282/// @code
283/// error(n)
284/// error(n,'msg')
285/// @endcode
286/// (where n is the integer error code value and `msg` is the error message value in single quotes).
287///
288/// \note Note that the first version will be output if
289/// there is no error message string.
290bsl::ostream& operator<<(bsl::ostream& stream, const DatumError& rhs);
291
292// ============================================================================
293// INLINE DEFINITIONS
294// ============================================================================
295
296 // ----------------
297 // class DatumError
298 // ----------------
299// CREATORS
300inline
302: d_code(0)
303{
304}
305
306inline
308: d_code(code)
309{
310}
311
312inline
314: d_code(code)
315, d_message(message)
316{
317}
318
319// ACCESSORS
320inline
322{
323 return d_code;
324}
325
326inline
328{
329 return d_message;
330}
331
332} // close package namespace
333
334// FREE OPERATORS
335inline
336bool bdld::operator==(const DatumError& lhs, const DatumError& rhs)
337{
338 return (lhs.code() == rhs.code() && lhs.message() == rhs.message());
339}
340
341inline
342bool bdld::operator!=(const DatumError& lhs, const DatumError& rhs)
343{
344 return !(lhs == rhs);
345}
346
347inline
348bool bdld::operator<(const DatumError& lhs, const DatumError& rhs)
349{
350 return (lhs.code() < rhs.code() ||
351 (lhs.code() == rhs.code() && lhs.message() < rhs.message()));
352}
353
354inline
355bool bdld::operator<=(const DatumError& lhs, const DatumError& rhs)
356{
357 return (lhs == rhs || lhs < rhs);
358}
359
360inline
361bool bdld::operator>(const DatumError& lhs, const DatumError& rhs)
362{
363 return !(lhs <= rhs);
364}
365
366inline
367bool bdld::operator>=(const DatumError& lhs, const DatumError& rhs)
368{
369 return !(lhs < rhs);
370}
371
372inline
373bsl::ostream& bdld::operator<<(bsl::ostream& stream, const DatumError& rhs)
374{
375 return rhs.print(stream, 0, -1);
376}
377
378
379
380#endif
381
382// ----------------------------------------------------------------------------
383// Copyright 2015 Bloomberg Finance L.P.
384//
385// Licensed under the Apache License, Version 2.0 (the "License");
386// you may not use this file except in compliance with the License.
387// You may obtain a copy of the License at
388//
389// http://www.apache.org/licenses/LICENSE-2.0
390//
391// Unless required by applicable law or agreed to in writing, software
392// distributed under the License is distributed on an "AS IS" BASIS,
393// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
394// See the License for the specific language governing permissions and
395// limitations under the License.
396// ----------------------------- END-OF-FILE ----------------------------------
397
398
399/** @} */
400/** @} */
401/** @} */
Definition bdld_datumerror.h:161
DatumError & operator=(const DatumError &rhs)=default
BSLMF_NESTED_TRAIT_DECLARATION(DatumError, bdlb::HasPrintMethod)
~DatumError()=default
bslstl::StringRef message() const
Definition bdld_datumerror.h:327
DatumError()
Definition bdld_datumerror.h:301
BSLMF_NESTED_TRAIT_DECLARATION(DatumError, bsl::is_trivially_copyable)
int code() const
Return the error code.
Definition bdld_datumerror.h:321
DatumError(const DatumError &origin)=default
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
Definition bslstl_stringref.h:374
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdld_datum.h:740
bsl::ostream & operator<<(bsl::ostream &stream, const Datum &rhs)
bool operator<=(const DatumBinaryRef &lhs, const DatumBinaryRef &rhs)
bool operator<(const DatumBinaryRef &lhs, const DatumBinaryRef &rhs)
bool operator>=(const DatumBinaryRef &lhs, const DatumBinaryRef &rhs)
bool operator==(const Datum &lhs, const Datum &rhs)
bool operator>(const DatumBinaryRef &lhs, const DatumBinaryRef &rhs)
bool operator!=(const Datum &lhs, const Datum &rhs)
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917
Definition bdlb_printmethods.h:306
Definition bslmf_istriviallycopyable.h:324