BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdld_datumerror

Detailed Description

Outline

Purpose

Provide a type for an error code with an optional error message.

Classes

See also
bdld_datum, bdld_datumudt

Description

This component defines a complex-constrained value-semantic attribute class bdld::DatumError representing an error code with an optional descriptive error message. This component holds a reference to the error message that was supplied at construction. Accessors inside Datum class that need to return an error value, return an instance of DatumError.

Usage

This section illustrates intended use of this component.

Example 1: Basic DatumError usage

Suppose we need a function to verify if newly created password meets basic security requirements. Password must contain at least one uppercase letter, one lowercase letter, one numeral and one special symbol. The following code illustrates how to use bdld::DatumError to notify user about password weaknesses.

First, we need to write a verification function:

/// Verify if specified `password` meets basic security requirements.
bdld::DatumError verifyNewPassword(const char *password)
{
bool uppercasePresence = false;
bool lowercasePresence = false;
bool numeralPresence = false;
bool specialSymbolPresence = false;
Definition bdld_datumerror.h:160

Passed string analysis:

while (*password) {
if (*password >= 'A' && *password <= 'Z') {
uppercasePresence = true;
}
if (*password >= 'a' && *password <= 'z') {
lowercasePresence = true;
}
if (*password >= '0' && *password <= '9') {
numeralPresence = true;
}
if (*password >= '!' && *password <= '.') {
specialSymbolPresence = true;
}
++password;
}

Result compilation:

if (!uppercasePresence) {
result = bdld::DatumError(1, bslstl::StringRef("Uppercase"));
} else if (!lowercasePresence) {
result = bdld::DatumError(2, bslstl::StringRef("Lowercase"));
} else if (!numeralPresence) {
result = bdld::DatumError(3, bslstl::StringRef("Numeral"));
} else if (!specialSymbolPresence) {
result = bdld::DatumError(4, bslstl::StringRef("Special"));
}
return result;
}
Definition bslstl_stringref.h:372

Next, we need to create password for verification and call our function:

bdld::DatumError error = verifyNewPassword("Test");

Then, check the results:

assert(bdld::DatumError() != error);
assert(3 == error.code());
int code() const
Return the error code.
Definition bdld_datumerror.h:315

Finally, we can print the result to the output stream:

ostringstream out;
error.print(out);
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const