Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bdlb_guid
[Package bdlb]

Provide a value-semantic type for Globally Unique Identifiers. More...

Namespaces

namespace  bdlb

Detailed Description

Outline
Purpose:
Provide a value-semantic type for Globally Unique Identifiers.
Classes:
bdlb::Guid value-semantic type to represent Globally Unique Identifiers
See also:
Component bdlb_guidutil
Description:
This component provides a value-semantic type for Globally Unique Identifiers (GUIDs), bdlb::Guid, with format as described by RFC 4122 (http://www.ietf.org/rfc/rfc4122.txt). All equality and comparison methods are defined for these GUIDs. Note that this component does not provide the facilities to generate GUIDs, and thus makes no guarantees of uniqueness or randomness.
Usage:
Suppose we are building a utility to create globally unique names which may be based on a common base name, such as a code-generator.
First, let us define the core types needed, the first of which is a utility to allocate GUIDs.
  struct MyGuidGeneratorUtil {
      // This struct provides a namespace for methods to generate GUIDs.

      // CLASS METHODS
      static int generate(bdlb::Guid *guid);
          // Generate a version 1 GUID, placing the value into the
          // specified 'guid' pointer.  Return 0 on success, and non-zero
          // otherwise.
  };

  // CLASS METHODS
  inline
  int my_GuidGeneratorUtil::generate(bdlb::Guid *guid)
  {
      // For brevity, we use a static sequence of pre-generated GUIDs.

      static unsigned char GUIDS[][bdlb::Guid::k_GUID_NUM_BYTES] = {
          { 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0xba, 0xbe,
            0x91, 0x91, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66 },

          { 0x5c, 0x9d, 0x4e, 0x51, 0x0d, 0xf1, 0x11, 0xe4,
            0x91, 0x91, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66 },

          { 0x5c, 0x9d, 0x4e, 0x52, 0x0d, 0xf1, 0x11, 0xe4,
            0x91, 0x91, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66 },

          { 0x5c, 0x9d, 0x4e, 0x53, 0x0d, 0xf1, 0x11, 0xe4,
            0x91, 0x91, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66 },

          { 0x5c, 0x9d, 0x4e, 0x54, 0x0d, 0xf1, 0x11, 0xe4,
            0x91, 0x91, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66 },

          { 0x5c, 0x9d, 0x4e, 0x55, 0x0d, 0xf1, 0x11, 0xe4,
            0x91, 0x91, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66 },

          { 0x5c, 0x9d, 0x4e, 0x56, 0x0d, 0xf1, 0x11, 0xe4,
            0x91, 0x91, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66 },
      };

      const bsl::size_t NUM_GUIDS = sizeof GUIDS / sizeof *GUIDS;

      static bsl::size_t nextGuidIdx = 0;

      int rval = -1;
      if (nextGuidIdx++ < NUM_GUIDS) {
          *guid = bdlb::Guid(GUIDS[nextGuidIdx]);
          rval = 0;
      }
      return rval;
 }
Next, we create a utility to create unique strings.
  struct UniqueStringGenerator {
      // This struct provides methods to create globally unique strings.

      static int uniqueStringFromBase(bsl::string        *unique,
                                      const bsl::string&  base);
            // Create a globally unique string from the specified non-unique
            // 'base' string, placing the result into the specified 'unique'
            // string pointer.

  };

  int
  UniqueStringGenerator::uniqueStringFromBase(bsl::string        *unique,
                                              const bsl::string&  base,)
  {
      bdlb::Guid guid;

      int rval = my_GuidGeneratorUtil::generate(&guid);
      if (rval == 0) {
      {
          ostringstream convert;
          convert << base << "-" << guid;
          *unique = convert.str();
      }
      return rval;
  }
Finally, we implement a program to generate unique names for a code auto-generator.
  bsl::string baseFileName = "foo.cpp";
  bsl::string uniqueFileName;
  bsl::string previousFileName;

  const bsl::size_t NUM_FILES = 5;
  for (bsl::size_t i = 0; i < NUM_FILES; ++i) {
      UniqueStringGenerator::uniqueStringFromBase(&uniqueFileName,
                                                  baseFileName);
      assert(previousFileName != uniqueFileName);
       previousFileName = uniqueFileName;
  }