CMS 3D CMS Logo

Guid.cc
Go to the documentation of this file.
1 // ====================================================================
2 //
3 // Guid.cpp
4 // --------------------------------------------------------------------
5 //
6 // Package : Persistent Guid to identify objects in the persistent
7 // world.
8 //
9 // Author : Markus Frank
10 //
11 // ====================================================================
12 #include "Guid.h"
13 #include <cassert>
14 #include <cstdio>
15 #include <cstring>
16 #include <cstdlib>
17 #include <string>
18 #include "uuid/uuid.h"
19 
20 namespace edm {
21  static char const* fmt_Guid = "%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX";
22 
23  static int const bufSize = 128;
24 
26  void Guid::init() {
27  uuid_t me_;
28  ::uuid_generate_random(me_);
29  unsigned int* d1 = reinterpret_cast<unsigned int*>(me_);
30  unsigned short* d2 = reinterpret_cast<unsigned short*>(me_ + 4);
31  unsigned short* d3 = reinterpret_cast<unsigned short*>(me_ + 6);
32  Data1 = *d1;
33  Data2 = *d2;
34  Data3 = *d3;
35  for (int i = 0; i < 8; ++i) {
36  Data4[i] = me_[i + 8];
37  }
38  }
39 
40  std::string const Guid::toString() const {
41  char text[bufSize];
42  ::snprintf(text,
43  sizeof(text),
44  fmt_Guid,
45  Data1,
46  Data2,
47  Data3,
48  Data4[0],
49  Data4[1],
50  Data4[2],
51  Data4[3],
52  Data4[4],
53  Data4[5],
54  Data4[6],
55  Data4[7]);
56  return text;
57  }
58 
59  // fromString is used only in a unit test, so performance is not critical.
61  char const dash = '-';
62  size_t const iSize = 8;
63  size_t const sSize = 4;
64  size_t const cSize = 2;
65  size_t offset = 0;
66  Data1 = strtol(source.substr(offset, iSize).c_str(), nullptr, 16);
67  offset += iSize;
68  assert(dash == source[offset++]);
69  Data2 = strtol(source.substr(offset, sSize).c_str(), nullptr, 16);
70  offset += sSize;
71  assert(dash == source[offset++]);
72  Data3 = strtol(source.substr(offset, sSize).c_str(), nullptr, 16);
73  offset += sSize;
74  assert(dash == source[offset++]);
75  Data4[0] = strtol(source.substr(offset, cSize).c_str(), nullptr, 16);
76  offset += cSize;
77  Data4[1] = strtol(source.substr(offset, cSize).c_str(), nullptr, 16);
78  offset += cSize;
79  assert(dash == source[offset++]);
80  Data4[2] = strtol(source.substr(offset, cSize).c_str(), nullptr, 16);
81  offset += cSize;
82  Data4[3] = strtol(source.substr(offset, cSize).c_str(), nullptr, 16);
83  offset += cSize;
84  Data4[4] = strtol(source.substr(offset, cSize).c_str(), nullptr, 16);
85  offset += cSize;
86  Data4[5] = strtol(source.substr(offset, cSize).c_str(), nullptr, 16);
87  offset += cSize;
88  Data4[6] = strtol(source.substr(offset, cSize).c_str(), nullptr, 16);
89  offset += cSize;
90  Data4[7] = strtol(source.substr(offset, cSize).c_str(), nullptr, 16);
91  offset += cSize;
92  assert(source.size() == offset);
93  return *this;
94  }
95 
96  bool Guid::operator<(Guid const& g) const { return ::memcmp(&g.Data1, &Data1, 16) < 0; }
97 } // namespace edm
static char const * fmt_Guid
Definition: Guid.cc:21
unsigned short Data2
Definition: Guid.h:26
void init()
initialize a new Guid
Definition: Guid.cc:26
static std::string const source("source")
static int const bufSize
Definition: Guid.cc:23
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
unsigned int Data1
Definition: Guid.h:25
std::string const toString() const
Automatic conversion from string reprentation.
Definition: Guid.cc:40
Definition: Guid.h:23
unsigned short Data3
Definition: Guid.h:27
unsigned char Data4[8]
Definition: Guid.h:28
HLT enums.
bool operator<(Guid const &g) const
Smaller operator.
Definition: Guid.cc:96
Guid const & fromString(std::string const &s)
Automatic conversion to string representation.
Definition: Guid.cc:60