Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bdld_datumerror
[Package bdld]

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

Namespaces

namespace  bdld

Detailed Description

Outline
Purpose:
Provide a type for an error code with an optional error message.
Classes:
bdld::DatumError type for an error code with an optional error message
See also:
Component bdld_datum, Component 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:
  bdld::DatumError verifyNewPassword(const char *password)
      // Verify if specified 'password' meets basic security requirements.
  {
      bool uppercasePresence     = false;
      bool lowercasePresence     = false;
      bool numeralPresence       = false;
      bool specialSymbolPresence = false;
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:
      bdld::DatumError result;

      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;
  }
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());
Finally, we can print the result to the output stream:
  ostringstream out;
  error.print(out);