CMS 3D CMS Logo

DDLVector.cc
Go to the documentation of this file.
2 
3 #include <cstddef>
4 #include <map>
5 #include <memory>
6 #include <utility>
7 
13 
14 class DDCompactView;
15 
16 namespace {
17  template <typename F>
18  void parse(char const* str, F&& f) {
19  auto ptr = str;
20 
21  while (*ptr != 0) {
22  //remove any leading spaces
23  while (std::isspace(*ptr)) {
24  ++ptr;
25  }
26  char const* strt = ptr;
27 
28  //find either the end of the char array
29  // or a comma. Spaces are allowed
30  // between characters.
31  while ((*ptr != 0) and (*ptr != ',')) {
32  ++ptr;
33  }
34  char const* end = ptr;
35  if (*ptr == ',') {
36  ++ptr;
37  }
38 
39  if (strt == end) {
40  break;
41  }
42 
43  //strip off any ending spaces
44  while (strt != end - 1 and std::isspace(*(end - 1))) {
45  --end;
46  }
47  f(strt, end);
48  }
49  }
50 } // namespace
51 
52 bool DDLVector::parse_numbers(char const* str) {
53  parse(str, [this](char const* st, char const* end) { do_makeDouble(st, end); });
54  return true;
55 }
56 
57 bool DDLVector::parse_strings(char const* str) {
58  parse(str, [this](char const* st, char const* end) { do_makeString(st, end); });
59  return true;
60 }
61 
63 
65  pVector.clear();
66  pStrVector.clear();
67  pNameSpace = nmspace;
68 }
69 
72  bool isNumVec((atts.find("type") == atts.end() || atts.find("type")->second == "numeric") ? true : false);
73  bool isStringVec((!isNumVec && atts.find("type") != atts.end() && atts.find("type")->second == "string") ? true
74  : false);
75  std::string tTextToParse = getText();
76 
77  if (tTextToParse.empty()) {
78  errorOut(" EMPTY STRING ");
79  }
80 
81  if (isNumVec) {
82  if (!parse_numbers(tTextToParse.c_str())) {
83  errorOut(tTextToParse.c_str());
84  }
85  } else if (isStringVec) {
86  if (!parse_strings(tTextToParse.c_str())) {
87  errorOut(tTextToParse.c_str());
88  }
89  } else {
90  errorOut("Unexpected std::vector type. Only \"numeric\" and \"string\" are allowed.");
91  }
92 
93  if (parent() == "Algorithm" || parent() == "SpecPar") {
94  if (isNumVec) {
95  pVecMap[atts.find("name")->second] = pVector;
96  } else if (isStringVec) {
97  pStrVecMap[atts.find("name")->second] = pStrVector;
98  }
99  size_t expNEntries = 0;
100  if (atts.find("nEntries") != atts.end()) {
101  std::string nEntries = atts.find("nEntries")->second;
102  expNEntries = size_t(myRegistry_->evaluator().eval(pNameSpace, nEntries));
103  }
104  if ((isNumVec && pVector.size() != expNEntries) || (isStringVec && pStrVector.size() != expNEntries)) {
105  std::string msg("Number of entries found in Vector text does not match number in attribute nEntries.");
106  msg += "\n\tnEntries = " + atts.find("nEntries")->second;
107  msg += "\n------------------text---------\n";
108  msg += tTextToParse;
109  msg += "\n------------------text---------\n";
110  errorOut(msg.c_str());
111  }
112  } else if (parent() == "ConstantsSection" || parent() == "DDDefinition") {
113  if (atts.find("type") == atts.end() || atts.find("type")->second == "numeric") {
114  DDVector v(getDDName(nmspace), std::make_unique<std::vector<double>>(pVector));
115  } else {
116  DDStrVector v(getDDName(nmspace), std::make_unique<std::vector<std::string>>(pStrVector));
117  }
118  }
119  clear();
120 }
121 
123 
125 
126 void DDLVector::do_makeDouble(char const* str, char const* end) {
127  std::string ts(str, end);
128  double td = myRegistry_->evaluator().eval(pNameSpace, ts);
129  pVector.emplace_back(td);
130 }
131 
132 void DDLVector::do_makeString(char const* str, char const* end) {
133  std::string ts(str, end);
134  pStrVector.emplace_back(ts);
135 }
136 
137 void DDLVector::errorOut(const char* str) const {
138  std::string e("Failed to parse the following: \n");
139  e += std::string(str);
140  e += "\n as a Vector element (comma separated list).";
141  throwError(e);
142 }
143 
146  pVecMap.clear();
147  pStrVecMap.clear();
148 }
ReadMapType< std::vector< std::string > > pStrVecMap
Definition: DDLVector.h:44
vector< string > parse(string line, const string &delimiter)
DDLElementRegistry * myRegistry_
Definition: DDXMLElement.h:173
std::vector< double > pVector
Definition: DDLVector.h:41
void preProcessElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv) override
Called by loadAttributes AFTER attributes are loaded.
Definition: DDLVector.cc:64
DDLVector(DDLElementRegistry *myreg)
Definition: DDLVector.cc:62
ReadMapType< std::vector< std::string > > & getMapOfStrVectors()
Definition: DDLVector.cc:124
a named constant corresponding to the DDL-XML tag <Constant> and <ConstantsStrVector> ...
Definition: DDStrVector.h:18
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
void do_makeDouble(char const *str, char const *end)
Definition: DDLVector.cc:126
std::map< std::string, std::string > DDXMLAttribute
Definition: DDXMLElement.h:45
ReadMapType< std::vector< double > > & getMapOfVectors()
Definition: DDLVector.cc:122
void do_makeString(char const *str, char const *end)
Definition: DDLVector.cc:132
virtual std::vector< DDXMLAttribute >::const_iterator end(void)
bool parse_strings(char const *str)
Definition: DDLVector.cc:57
std::string pNameSpace
Definition: DDLVector.h:45
a std::map<std::string,YourType> that offers a const operator[key]; if key is not stored in the std::...
Definition: DDReadMapType.h:14
ClhepEvaluator & evaluator()
ReadMapType< std::vector< double > > pVecMap
Definition: DDLVector.h:43
virtual const DDXMLAttribute & getAttributeSet(size_t aIndex=0) const
Get a "row" of attributes, i.e. one attribute set.
Definition: DDXMLElement.cc:54
void clearall()
Definition: DDLVector.cc:144
double f[11][100]
a named constant corresponding to the DDL-XML tag <Constant> and <ConstantsVector> ...
Definition: DDVector.h:18
bool parse_numbers(char const *str)
Definition: DDLVector.cc:52
std::vector< std::string > pStrVector
Definition: DDLVector.h:42
void errorOut(const char *str) const
Definition: DDLVector.cc:137
This is a base class for processing XML elements in the DDD.
Definition: DDXMLElement.h:48
tuple msg
Definition: mps_check.py:286
const std::string & parent(void) const
access to parent element name
double eval(const std::string &ns, const std::string &expr)
The main class for processing parsed elements.
virtual void clear(void)
clear this element&#39;s contents.
Definition: DDXMLElement.cc:40
void throwError(const std::string &keyMessage) const
format std::string for throw an error.
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
void processElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv) override
Processing the element.
Definition: DDLVector.cc:70
#define str(s)
virtual const DDName getDDName(const std::string &defaultNS, const std::string &attname=std::string("name"), size_t aIndex=0)
Definition: DDXMLElement.cc:56
const std::string getText(size_t tindex=0) const
retrieve the text blob.