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 #include "boost/spirit/include/classic.hpp"
14 
15 class DDCompactView;
16 
17 namespace boost { namespace spirit { namespace classic { } } } using namespace boost::spirit::classic;
18 
19 using namespace boost::spirit;
20 
22 {
23 public:
24  void operator() (char const* str, char const* end) const
25  {
26  ddlVector_->do_makeDouble(str, end);
27  }
28 
30  ddlVector_ = std::static_pointer_cast<DDLVector>(DDLGlobalRegistry::instance().getElement("Vector"));
31  }
32 private:
33  std::shared_ptr<DDLVector> ddlVector_;
34 };
35 
37 {
38 public:
39  void operator() (char const* str, char const* end) const
40  {
41  ddlVector_->do_makeString(str, end);
42  }
43 
45  ddlVector_ = std::static_pointer_cast<DDLVector>(DDLGlobalRegistry::instance().getElement("Vector"));
46  }
47 private:
48  std::shared_ptr<DDLVector> ddlVector_;
49 };
50 
51 bool
52 DDLVector::parse_numbers(char const* str) const
53 {
54  static VectorMakeDouble makeDouble;
55  return parse(str,
56  ((+(anychar_p - ','))[makeDouble]
57  >> *(',' >> (+(anychar_p - ','))[makeDouble]))
58  >> end_p
59  , space_p).full;
60 }
61 
62 bool
63 DDLVector::parse_strings(char const* str) const
64 {
65  static VectorMakeString makeString;
66  return parse(str,
67  ((+(anychar_p - ','))[makeString]
68  >> *(',' >> (+(anychar_p - ','))[makeString]))
69  >> end_p
70  , space_p).full;
71 }
72 
74  : DDXMLElement( myreg )
75 {}
76 
77 void
79 {
80  pVector.clear();
81  pStrVector.clear();
82  pNameSpace = nmspace;
83 }
84 
85 void
87 {
89  bool isNumVec((atts.find("type") == atts.end()
90  || atts.find("type")->second == "numeric")
91  ? true : false);
92  bool isStringVec((!isNumVec && atts.find("type") != atts.end()
93  && atts.find("type")->second == "string")
94  ? true : false);
95  std::string tTextToParse = getText();
96 
97  if (tTextToParse.empty()) {
98  errorOut(" EMPTY STRING ");
99  }
100 
101  if (isNumVec) {
102  if (!parse_numbers(tTextToParse.c_str())) {
103  errorOut(tTextToParse.c_str());
104  }
105  }
106  else if (isStringVec) {
107  if (!parse_strings(tTextToParse.c_str())) {
108  errorOut(tTextToParse.c_str());
109  }
110  }
111  else {
112  errorOut("Unexpected std::vector type. Only \"numeric\" and \"string\" are allowed.");
113  }
114 
115  if (parent() == "Algorithm" || parent() == "SpecPar")
116  {
117  if (isNumVec) {
118  pVecMap[atts.find("name")->second] = pVector;
119  }
120  else if (isStringVec) {
121  pStrVecMap[atts.find("name")->second] = pStrVector;
122  }
123  size_t expNEntries = 0;
124  if (atts.find("nEntries") != atts.end()) {
125  std::string nEntries = atts.find("nEntries")->second;
126  expNEntries = size_t (myRegistry_->evaluator().eval(pNameSpace, nEntries));
127  }
128  if ( (isNumVec && pVector.size() != expNEntries)
129  || (isStringVec && pStrVector.size() != expNEntries) )
130  {
131  std::string msg ("Number of entries found in Vector text does not match number in attribute nEntries.");
132  msg += "\n\tnEntries = " + atts.find("nEntries")->second;
133  msg += "\n------------------text---------\n";
134  msg += tTextToParse;
135  msg += "\n------------------text---------\n";
136  errorOut(msg.c_str());
137  }
138  }
139  else if (parent() == "ConstantsSection" || parent() == "DDDefinition")
140  {
141  if (atts.find("type") == atts.end() || atts.find("type")->second == "numeric") {
142  DDVector v(getDDName(nmspace), new std::vector<double>(pVector));
143  }
144  else {
145  DDStrVector v(getDDName(nmspace), new std::vector<std::string>(pStrVector));
146  }
147  }
148  clear();
149 }
150 
153 {
154  return pVecMap;
155 }
156 
159 {
160  return pStrVecMap;
161 }
162 
163 void
164 DDLVector::do_makeDouble( char const* str, char const* end )
165 {
166  std::string ts(str, end);
167  double td = myRegistry_->evaluator().eval(pNameSpace, ts);
168  pVector.emplace_back(td);
169 }
170 
171 void
172 DDLVector::do_makeString( char const* str, char const* end )
173 {
174  std::string ts(str, end);
175  pStrVector.emplace_back(ts);
176 }
177 
178 void
179 DDLVector::errorOut( const char* str ) const
180 {
181  std::string e("Failed to parse the following: \n");
182  e+= std::string(str);
183  e+="\n as a Vector element (comma separated list).";
184  throwError (e);
185 }
186 
187 void
189 {
191  pVecMap.clear();
192  pStrVecMap.clear();
193 }
DDLElementRegistry * myRegistry_
Definition: DDXMLElement.h:172
Definition: CLHEP.h:16
std::vector< double > pVector
Definition: DDLVector.h:49
void preProcessElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv) override
Called by loadAttributes AFTER attributes are loaded.
Definition: DDLVector.cc:78
DDLVector handles Rotation and ReflectionRotation elements.
Definition: DDLVector.h:31
DDLVector(DDLElementRegistry *myreg)
Definition: DDLVector.cc:73
ReadMapType< std::vector< std::string > > & getMapOfStrVectors()
Definition: DDLVector.cc:158
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:17
type of data representation of DDCompactView
Definition: DDCompactView.h:90
void do_makeDouble(char const *str, char const *end)
Definition: DDLVector.cc:164
std::map< std::string, std::string > DDXMLAttribute
Definition: DDXMLElement.h:45
ReadMapType< std::vector< double > > & getMapOfVectors()
Definition: DDLVector.cc:152
void do_makeString(char const *str, char const *end)
Definition: DDLVector.cc:172
virtual std::vector< DDXMLAttribute >::const_iterator end(void)
void errorOut(const char *str) const
Definition: DDLVector.cc:179
std::string pNameSpace
Definition: DDLVector.h:53
static value_type & instance()
bool parse_strings(char const *str) const
Definition: DDLVector.cc:63
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:188
#define end
Definition: vmac.h:37
a named constant corresponding to the DDL-XML tag <Constant> and <ConstantsVector> ...
Definition: DDVector.h:17
std::shared_ptr< DDLVector > ddlVector_
Definition: DDLVector.cc:33
std::vector< std::string > pStrVector
Definition: DDLVector.h:50
ReadMapType< std::vector< double > > pVecMap
Definition: DDLVector.h:51
def parse(path, config)
Definition: dumpparser.py:13
bool parse_numbers(char const *str) const
Definition: DDLVector.cc:52
This is a base class for processing XML elements in the DDD.
Definition: DDXMLElement.h:48
tuple msg
Definition: mps_check.py:277
ReadMapType< std::vector< std::string > > pStrVecMap
Definition: DDLVector.h:52
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
void processElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv) override
Processing the element.
Definition: DDLVector.cc:86
std::shared_ptr< DDLVector > ddlVector_
Definition: DDLVector.cc:48
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.