BDE 4.14.0 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
156/// descriptive error message. Note that this class holds a reference to
157/// the error message and does not make a copy of it.
158///
159/// See @ref bdld_datumerror
161
162 private:
163 // DATA
164 int d_code; // error code
165 bslstl::StringRef d_message; // error message
166
167 public:
168 // TRAITS
171
172 // CREATORS
173
174 /// Create a `DatumError` object having the default error code of 0 and
175 /// an empty error message.
176 DatumError();
177
178 /// Create a `DatumError` object having the specified error `code` value
179 /// and and empty error message.
180 explicit DatumError(int code);
181
182 /// Create a `DatumError` object having the specified error `code` value
183 /// and the specified error `message` value. Note that the `message` is
184 /// held by the reference and not copied.
186
187 DatumError(const DatumError& origin) = default;
188 ~DatumError() = default;
189
190 // MANIPULATORS
191
192 DatumError& operator=(const DatumError& rhs) = default;
193
194 // ACCESSORS
195
196 /// Return the error code.
197 int code() const;
198
199 /// Return a reference to the non-modifyable error message. The
200 /// returned reference remains valid as long as the underlying message
201 /// is not modified or destroyed -- irrespective of the state (or
202 /// existence) of this object. The behavior is undefined unless the
203 /// underlying error message has been modified or destroyed since this
204 /// object was created.
206
207 /// Write the value of this object to the specified output `stream` in a
208 /// human-readable format, and return a reference to the modifyable
209 /// `stream`. Optionally specify an initial indentation `level`, whose
210 /// absolute value is incremented recursively for nested objects. If
211 /// `level` is specified, optionally specify `spacesPerLevel`, whose
212 /// absolute value indicates the number of spaces per indentation level
213 /// for this and all of its nested objects. If `level` is negative,
214 /// suppress indentation of the first line. If `spacesPerLevel` is
215 /// negative, suppress all but the initial indentation (as governed by
216 /// `level`). For readability the entire output is formatted on one
217 /// line, regardless of the `spacesPerLevel` value. If `stream` is not
218 /// valid on entry, this operation has no effect. Note that this
219 /// human-readable format is not fully specified, and can change without
220 /// notice.
221 bsl::ostream& print(bsl::ostream& stream,
222 int level = 0,
223 int spacesPerLevel = 4) const;
224};
225
226// FREE OPERATORS
227
228/// Return `true` if the specified `lhs` and `rhs` have the same value and
229/// `false` otherwise. Two `DatumError` objects have the same value if they
230/// have same error code and message values.
231bool operator==(const DatumError& lhs, const DatumError& rhs);
232
233/// Return `true` if the specified `lhs` and `rhs` have different values and
234/// `false` otherwise. Two `DatumError` objects have different values if
235/// they have different error code or message values.
236bool operator!=(const DatumError& lhs, const DatumError& rhs);
237
238/// Return `true` if value of the specified `lhs` is less than value of the
239/// specified `rhs` and `false` otherwise. Value of `lhs` is less than
240/// value of `rhs` if error code value of `lhs` is less than error code
241/// value of `rhs`, or they have the same error code value and error message
242/// value of `lhs` is less than error message value of `rhs`.
243bool operator<(const DatumError& lhs, const DatumError& rhs);
244
245/// Return `true` if value of the specified `lhs` is less than or equal to
246/// value of the specified `rhs` and `false` otherwise. Value of `lhs` is
247/// less than or equal to value of `rhs` if error code value of `lhs` is
248/// less than or equal to error code value of `rhs`, or they have the same
249/// error code value and error message value of `lhs` is less than or equal
250/// to error message value of `rhs`.
251bool operator<=(const DatumError& lhs, const DatumError& rhs);
252
253/// Return `true` if value of the specified `lhs` is greater than value of
254/// the specified `rhs` and `false` otherwise. Value of `lhs` is greater
255/// than value of `rhs` if error code value of `lhs` is greater than error
256/// code value of `rhs`, or they have the same error code value and error
257/// message value of `lhs` is greater than error message value of `rhs`.
258bool operator>(const DatumError& lhs, const DatumError& rhs);
259
260/// Return `true` if value of the specified `lhs` is greater than or equal
261/// to value of the specified `rhs` and `false` otherwise. Value of `lhs`
262/// is greater than or equal to value of `rhs` if error code value of `lhs`
263/// is greater than or equal to error code value of `rhs`, or they have the
264/// same error code value and error message value of `lhs` is greater than
265/// or equal to error message value of `rhs`.
266bool operator>=(const DatumError& lhs, const DatumError& rhs);
267
268/// Write the value of the specified `rhs` object to the specified output
269/// `stream` in a single-line format, and return a reference to the
270/// modifyable `stream`. If `stream` is not valid on entry, this operation
271/// has no effect. Note that this human-readable format is not fully
272/// specified, can change without notice, and is logically equivalent to:
273/// @code
274/// print(stream, 0, -1);
275/// @endcode
276/// Single line output format for the `DatumError` object is shown below:
277/// @code
278/// error(n)
279/// error(n,'msg')
280/// @endcode
281/// (where n is the integer error code value and `msg` is the error message
282/// value in single quotes). Note that the first version will be output if
283/// there is no error message string.
284bsl::ostream& operator<<(bsl::ostream& stream, const DatumError& rhs);
285
286// ============================================================================
287// INLINE DEFINITIONS
288// ============================================================================
289
290 // ----------------
291 // class DatumError
292 // ----------------
293// CREATORS
294inline
296: d_code(0)
297{
298}
299
300inline
302: d_code(code)
303{
304}
305
306inline
308: d_code(code)
309, d_message(message)
310{
311}
312
313// ACCESSORS
314inline
316{
317 return d_code;
318}
319
320inline
322{
323 return d_message;
324}
325
326} // close package namespace
327
328// FREE OPERATORS
329inline
330bool bdld::operator==(const DatumError& lhs, const DatumError& rhs)
331{
332 return (lhs.code() == rhs.code() && lhs.message() == rhs.message());
333}
334
335inline
336bool bdld::operator!=(const DatumError& lhs, const DatumError& rhs)
337{
338 return !(lhs == rhs);
339}
340
341inline
342bool bdld::operator<(const DatumError& lhs, const DatumError& rhs)
343{
344 return (lhs.code() < rhs.code() ||
345 (lhs.code() == rhs.code() && lhs.message() < rhs.message()));
346}
347
348inline
349bool bdld::operator<=(const DatumError& lhs, const DatumError& rhs)
350{
351 return (lhs == rhs || lhs < rhs);
352}
353
354inline
355bool bdld::operator>(const DatumError& lhs, const DatumError& rhs)
356{
357 return !(lhs <= rhs);
358}
359
360inline
361bool bdld::operator>=(const DatumError& lhs, const DatumError& rhs)
362{
363 return !(lhs < rhs);
364}
365
366inline
367bsl::ostream& bdld::operator<<(bsl::ostream& stream, const DatumError& rhs)
368{
369 return rhs.print(stream, 0, -1);
370}
371
372
373
374#endif
375
376// ----------------------------------------------------------------------------
377// Copyright 2015 Bloomberg Finance L.P.
378//
379// Licensed under the Apache License, Version 2.0 (the "License");
380// you may not use this file except in compliance with the License.
381// You may obtain a copy of the License at
382//
383// http://www.apache.org/licenses/LICENSE-2.0
384//
385// Unless required by applicable law or agreed to in writing, software
386// distributed under the License is distributed on an "AS IS" BASIS,
387// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
388// See the License for the specific language governing permissions and
389// limitations under the License.
390// ----------------------------- END-OF-FILE ----------------------------------
391
392
393/** @} */
394/** @} */
395/** @} */
Definition bdld_datumerror.h:160
DatumError & operator=(const DatumError &rhs)=default
BSLMF_NESTED_TRAIT_DECLARATION(DatumError, bdlb::HasPrintMethod)
~DatumError()=default
bslstl::StringRef message() const
Definition bdld_datumerror.h:321
DatumError()
Definition bdld_datumerror.h:295
BSLMF_NESTED_TRAIT_DECLARATION(DatumError, bsl::is_trivially_copyable)
int code() const
Return the error code.
Definition bdld_datumerror.h:315
DatumError(const DatumError &origin)=default
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
Definition bslstl_stringref.h:372
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdld_datum.h:730
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)
Definition bdlb_printmethods.h:306
Definition bslmf_istriviallycopyable.h:329