Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:14

0001 //      ====================================================================
0002 //
0003 //      Guid.cpp
0004 //      --------------------------------------------------------------------
0005 //
0006 //      Package   : Persistent Guid to identify objects in the persistent
0007 //              world.
0008 //
0009 //      Author    : Markus Frank
0010 //
0011 //      ====================================================================
0012 #include "FWCore/Utilities/interface/Guid.h"
0013 #include <cstring>
0014 #include <cassert>
0015 
0016 namespace edm {
0017   /// Initialize a new Guid
0018   void Guid::init(bool usetime) {
0019     if (usetime) {
0020       ::uuid_generate_time(data_);
0021     } else {
0022       // uuid_generate() defaults to uuid_generate_random() if /dev/urandom
0023       // is available; if /dev/urandom is not available, then it is better
0024       // to let uuid_generate() choose the best fallback rather than forcing
0025       // use of an inferior source of randomness
0026       ::uuid_generate(data_);
0027     }
0028   }
0029 
0030   std::string const Guid::toBinary() const { return std::string(reinterpret_cast<const char*>(data_), sizeof(data_)); }
0031 
0032   Guid const& Guid::fromBinary(std::string const& source) {
0033     assert(source.size() == sizeof(data_));
0034     std::memcpy(data_, source.data(), sizeof(data_));
0035     return *this;
0036   }
0037 
0038   std::string const Guid::toString() const {
0039     char out[UUID_STR_LEN];
0040     ::uuid_unparse(data_, out);
0041     return std::string(out);
0042   }
0043 
0044   // fromString is used only in a unit test, so performance is not critical.
0045   Guid const& Guid::fromString(std::string const& source) {
0046     auto err = ::uuid_parse(source.c_str(), data_);
0047     assert(err == 0);
0048     return *this;
0049   }
0050 
0051   bool Guid::isValidString(std::string const& source) {
0052     uuid_t tmp;
0053     auto err = ::uuid_parse(source.c_str(), tmp);
0054     return err == 0;
0055   }
0056 
0057   bool Guid::operator<(Guid const& g) const { return ::uuid_compare(data_, g.data_) < 0; }
0058 }  // namespace edm