Quick Links: |
Provide a value-semantic type for Globally Unique Identifiers. More...
Namespaces | |
namespace | bdlb |
bdlb::Guid | value-semantic type to represent Globally Unique Identifiers |
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. 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; }
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; }
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; }