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 
20  auto ptr = str;
21 
22  while( *ptr != 0) {
23  //remove any leading spaces
24  while( std::isspace(*ptr) ) {
25  ++ptr;
26  }
27  char const* strt = ptr;
28 
29  //find either the end of the char array
30  // or a comma. Spaces are allowed
31  // between characters.
32  while( (*ptr != 0) and
33  (*ptr !=',')) {++ptr;}
34  char const* end = ptr;
35  if(*ptr == ',') {++ptr;}
36 
37  if(strt == end) {
38  break;
39  }
40 
41  //strip off any ending spaces
42  while(strt != end-1 and
43  std::isspace(*(end-1)) ) {
44  --end;
45  }
46  f(strt,end);
47  }
48 }
49 }
50 
51 
52 bool
53 DDLVector::parse_numbers(char const* str)
54 {
55  parse(str, [this](char const* st, char const* end) {
56  do_makeDouble(st, end);
57  });
58  return true;
59 }
60 
61 bool
62 DDLVector::parse_strings(char const* str)
63 {
64  parse(str,[this](char const* st, char const* end) {
65  do_makeString(st,end);
66  });
67  return true;
68 }
69 
71  : DDXMLElement( myreg )
72 {}
73 
74 void
76 {
77  pVector.clear();
78  pStrVector.clear();
79  pNameSpace = nmspace;
80 }
81 
82 void
84 {
86  bool isNumVec((atts.find("type") == atts.end()
87  || atts.find("type")->second == "numeric")
88  ? true : false);
89  bool isStringVec((!isNumVec && atts.find("type") != atts.end()
90  && atts.find("type")->second == "string")
91  ? true : false);
92  std::string tTextToParse = getText();
93 
94  if (tTextToParse.empty()) {
95  errorOut(" EMPTY STRING ");
96  }
97 
98  if (isNumVec) {
99  if (!parse_numbers(tTextToParse.c_str())) {
100  errorOut(tTextToParse.c_str());
101  }
102  }
103  else if (isStringVec) {
104  if (!parse_strings(tTextToParse.c_str())) {
105  errorOut(tTextToParse.c_str());
106  }
107  }
108  else {
109  errorOut("Unexpected std::vector type. Only \"numeric\" and \"string\" are allowed.");
110  }
111 
112  if (parent() == "Algorithm" || parent() == "SpecPar")
113  {
114  if (isNumVec) {
115  pVecMap[atts.find("name")->second] = pVector;
116  }
117  else if (isStringVec) {
118  pStrVecMap[atts.find("name")->second] = pStrVector;
119  }
120  size_t expNEntries = 0;
121  if (atts.find("nEntries") != atts.end()) {
122  std::string nEntries = atts.find("nEntries")->second;
123  expNEntries = size_t (myRegistry_->evaluator().eval(pNameSpace, nEntries));
124  }
125  if ( (isNumVec && pVector.size() != expNEntries)
126  || (isStringVec && pStrVector.size() != expNEntries) )
127  {
128  std::string msg ("Number of entries found in Vector text does not match number in attribute nEntries.");
129  msg += "\n\tnEntries = " + atts.find("nEntries")->second;
130  msg += "\n------------------text---------\n";
131  msg += tTextToParse;
132  msg += "\n------------------text---------\n";
133  errorOut(msg.c_str());
134  }
135  }
136  else if (parent() == "ConstantsSection" || parent() == "DDDefinition")
137  {
138  if (atts.find("type") == atts.end() || atts.find("type")->second == "numeric") {
139  DDVector v( getDDName(nmspace), std::make_unique<std::vector<double>>( pVector ));
140  }
141  else {
142  DDStrVector v( getDDName(nmspace), std::make_unique<std::vector<std::string>>( pStrVector ));
143  }
144  }
145  clear();
146 }
147 
150 {
151  return pVecMap;
152 }
153 
156 {
157  return pStrVecMap;
158 }
159 
160 void
161 DDLVector::do_makeDouble( char const* str, char const* end )
162 {
163  std::string ts(str, end);
164  double td = myRegistry_->evaluator().eval(pNameSpace, ts);
165  pVector.emplace_back(td);
166 }
167 
168 void
169 DDLVector::do_makeString( char const* str, char const* end )
170 {
171  std::string ts(str, end);
172  pStrVector.emplace_back(ts);
173 }
174 
175 void
176 DDLVector::errorOut( const char* str ) const
177 {
178  std::string e("Failed to parse the following: \n");
179  e+= std::string(str);
180  e+="\n as a Vector element (comma separated list).";
181  throwError (e);
182 }
183 
184 void
186 {
188  pVecMap.clear();
189  pStrVecMap.clear();
190 }
DDLElementRegistry * myRegistry_
Definition: DDXMLElement.h:172
std::vector< double > pVector
Definition: DDLVector.h:44
void preProcessElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv) override
Called by loadAttributes AFTER attributes are loaded.
Definition: DDLVector.cc:75
DDLVector(DDLElementRegistry *myreg)
Definition: DDLVector.cc:70
ReadMapType< std::vector< std::string > > & getMapOfStrVectors()
Definition: DDLVector.cc:155
virtual const DDXMLAttribute & getAttributeSet(size_t aIndex=0) const
Get a "row" of attributes, i.e. one attribute set.
Definition: DDXMLElement.cc:72
void throwError(const std::string &keyMessage) const
format std::string for throw an error.
const std::string & parent(void) const
access to parent element name
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:80
void do_makeDouble(char const *str, char const *end)
Definition: DDLVector.cc:161
std::map< std::string, std::string > DDXMLAttribute
Definition: DDXMLElement.h:45
ReadMapType< std::vector< double > > & getMapOfVectors()
Definition: DDLVector.cc:149
void do_makeString(char const *str, char const *end)
Definition: DDLVector.cc:169
bool parse_strings(char const *str)
Definition: DDLVector.cc:62
void errorOut(const char *str) const
Definition: DDLVector.cc:176
std::string pNameSpace
Definition: DDLVector.h:48
a std::map<std::string,YourType> that offers a const operator[key]; if key is not stored in the std::...
Definition: DDReadMapType.h:13
ClhepEvaluator & evaluator()
void clearall()
Definition: DDLVector.cc:185
double f[11][100]
#define end
Definition: vmac.h:39
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:53
std::vector< std::string > pStrVector
Definition: DDLVector.h:45
ReadMapType< std::vector< double > > pVecMap
Definition: DDLVector.h:46
def parse(path, config)
Definition: dumpparser.py:13
This is a base class for processing XML elements in the DDD.
Definition: DDXMLElement.h:48
tuple msg
Definition: mps_check.py:278
ReadMapType< std::vector< std::string > > pStrVecMap
Definition: DDLVector.h:47
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:54
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
void processElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv) override
Processing the element.
Definition: DDLVector.cc:83
#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:79
const std::string getText(size_t tindex=0) const
retrieve the text blob.