LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
StringIDs.cc
Go to the documentation of this file.
1 // Implementation of StringIDs
2 
4 
5 #ifndef __GCCXML__
6 #include "cetlib_except/exception.h"
7 #endif
8 
9 // C'tor
11 
12 // Root need not know about the below
13 #ifndef __GCCXML__
14 
15 // Initialize
16 void
18 {
19 
20  // If the string is not empty, then create the map
21  if (!stringVec_.empty()) {
22  unsigned int count = 0;
23  for (auto entry : stringVec_) {
24  stringToIdMap_.insert(std::pair<std::string, unsigned int>(entry, count));
25  }
26  }
27 }
28 
29 // Get the ID for a string. If it is not already in the vector, then add it
30 unsigned int
31 artg4tk::StringIDs::idGivenString(const std::string& s)
32 {
33  // Do we already have s?
34  unsigned int val = 0;
35  bool found = false;
36 
37  // If the string vector is not empty, then we have to look in the map
38  if (!stringVec_.empty()) {
39 
40  // If the map is empty, then it hasn't been initialized. Throw.
41  if (stringToIdMap_.empty()) {
42  throw cet::exception("STRINGIDS") << "You forgot to initialize the StringIds object"
43  << "\n";
44  }
45 
46  // Look in the map for the ID number
47  auto mapIter = stringToIdMap_.find(s);
48  if (mapIter != stringToIdMap_.end()) {
49  // Found it
50  val = mapIter->second;
51  found = true;
52  }
53  }
54 
55  if (!found) {
56  // String not found, add it
57  val = stringVec_.size();
58  stringVec_.push_back(s);
59  stringToIdMap_[s] = val;
60  }
61 
62  return val;
63 }
64 
65 // Reset - put in a new string vector (presumedly because you've read one in)
66 void
68 {
69 
70  // Replace contents
71  stringVec_ = desired.stringVec_;
72 
73  // Re-initialize
74  initialize();
75 }
76 
77 #endif // __GCCXML__
void reset(StringIDs const &desired)
Definition: StringIDs.cc:67
std::vector< std::string > stringVec_
Definition: StringIDs.hh:65
std::map< std::string, unsigned int > stringToIdMap_
Definition: StringIDs.hh:69
unsigned int idGivenString(const std::string &s)
Definition: StringIDs.cc:31
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33